GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-cx-diag.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2008-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include "byte-swap.h"
31
32#include "ov-cx-diag.h"
33#include "ov-flt-cx-diag.h"
34#include "ov-re-diag.h"
35#include "ov-complex.h"
36#include "ls-utils.h"
37
39 "complex diagonal matrix", "double");
40
41static octave_base_value *
42default_numeric_conversion_function (const octave_base_value& a)
43{
45 = dynamic_cast<const octave_complex_diag_matrix&> (a);
46
48}
49
57
58static octave_base_value *
59default_numeric_demotion_function (const octave_base_value& a)
60{
62 = dynamic_cast<const octave_complex_diag_matrix&> (a);
63
66}
67
75
78{
79 octave_base_value *retval = nullptr;
80
81 if (m_matrix.nelem () == 1)
82 {
83 retval = new octave_complex (m_matrix (0, 0));
85 if (rv2)
86 {
87 delete retval;
88 retval = rv2;
89 }
90 }
92 {
93 return new octave_diag_matrix (::real (m_matrix));
94 }
95
96 return retval;
97}
98
101{
102 DiagMatrix retval;
103
104 if (! force_conversion)
105 warn_implicit_conversion ("Octave:imag-to-real",
106 type_name (), "real matrix");
107
108 retval = ::real (m_matrix);
109
110 return retval;
111}
112
115{
116 DiagMatrix retval;
117
118 if (! force_conversion)
119 warn_implicit_conversion ("Octave:imag-to-real",
120 type_name (), "real matrix");
121
122 retval = ::real (m_matrix);
123
124 return retval;
125}
126
132
138
141{
142 return m_matrix;
143}
144
150
153{
154 switch (umap)
155 {
156 case umap_abs:
157 return m_matrix.abs ();
158 case umap_real:
159 return ::real (m_matrix);
160 case umap_conj:
161 return ::conj (m_matrix);
162 case umap_imag:
163 return ::imag (m_matrix);
164 case umap_sqrt:
165 {
167 = m_matrix.extract_diag ().map<Complex> (std::sqrt);
168 ComplexDiagMatrix retval (tmp);
169 retval.resize (m_matrix.rows (), m_matrix.columns ());
170 return retval;
171 }
172 default:
173 return to_dense ().map (umap);
174 }
175}
176
177bool
178octave_complex_diag_matrix::save_binary (std::ostream& os, bool save_as_floats)
179{
180
181 int32_t r = m_matrix.rows ();
182 int32_t c = m_matrix.cols ();
183 os.write (reinterpret_cast<char *> (&r), 4);
184 os.write (reinterpret_cast<char *> (&c), 4);
185
187 save_type st = LS_DOUBLE;
188 if (save_as_floats)
189 {
190 if (m.too_large_for_float ())
191 {
192 warning ("save: some values too large to save as floats --");
193 warning ("save: saving as doubles instead");
194 }
195 else
196 st = LS_FLOAT;
197 }
198 else if (m_matrix.length () > 4096) // FIXME: make this configurable.
199 {
200 double max_val, min_val;
201 if (m.all_integers (max_val, min_val))
202 st = octave::get_save_type (max_val, min_val);
203 }
204
205 const Complex *mtmp = m.data ();
206 write_doubles (os, reinterpret_cast<const double *> (mtmp), st,
207 2 * m.numel ());
208
209 return true;
210}
211
212bool
213octave_complex_diag_matrix::load_binary (std::istream& is, bool swap,
214 octave::mach_info::float_format fmt)
215{
216 int32_t r, c;
217 char tmp;
218 if (! (is.read (reinterpret_cast<char *> (&r), 4)
219 && is.read (reinterpret_cast<char *> (&c), 4)
220 && is.read (reinterpret_cast<char *> (&tmp), 1)))
221 return false;
222 if (swap)
223 {
224 swap_bytes<4> (&r);
225 swap_bytes<4> (&c);
226 }
227
228 ComplexDiagMatrix m (r, c);
229 Complex *im = m.rwdata ();
231 read_doubles (is, reinterpret_cast<double *> (im),
232 static_cast<save_type> (tmp), 2 * len, swap, fmt);
233
234 if (! is)
235 return false;
236
237 m_matrix = m;
238
239 return true;
240}
241
242bool
243octave_complex_diag_matrix::chk_valid_scalar (const octave_value& val,
244 Complex& x) const
245{
246 bool retval = val.is_complex_scalar () || val.is_real_scalar ();
247 if (retval)
248 x = val.complex_value ();
249 return retval;
250}
251
252/*
253%!assert <*36368> (diag ([1+i, 1-i])^2 , diag ([2i, -2i]), 4*eps)
254*/
void swap_bytes< 4 >(void *ptr)
Definition byte-swap.h:63
Array< U, A > map(F fcn) const
Apply function fcn to each element of the Array<T, Alloc>.
Definition Array.h:861
const T * data() const
Size of the specified dimension.
Definition Array.h:665
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
bool all_elements_are_real() const
DiagMatrix abs() const
ComplexColumnVector extract_diag(octave_idx_type k=0) const
bool all_integers(double &max_val, double &min_val) const
Definition CNDArray.cc:289
bool too_large_for_float() const
Definition CNDArray.cc:340
octave_idx_type nelem() const
Definition DiagArray2.h:93
octave_idx_type rows() const
Definition DiagArray2.h:86
octave_idx_type length() const
Definition DiagArray2.h:92
octave_idx_type columns() const
Definition DiagArray2.h:88
octave_idx_type cols() const
Definition DiagArray2.h:87
T * rwdata()
Definition DiagArray2.h:168
ComplexMatrix complex_matrix_value(bool=false) const
virtual octave_base_value * try_narrowing_conversion()
Definition ov-base.h:332
octave_value as_single() const
DiagMatrix diag_matrix_value(bool=false) const
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
octave_value as_double() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
bool save_binary(std::ostream &os, bool save_as_floats)
std::string type_name() const
Definition ov-cx-diag.h:103
type_conv_info numeric_demotion_function() const
Definition ov-cx-diag.cc:69
type_conv_info numeric_conversion_function() const
Definition ov-cx-diag.cc:51
octave_base_value * try_narrowing_conversion()
Definition ov-cx-diag.cc:77
static int static_type_id()
Definition ov-cx-mat.h:184
bool is_real_scalar() const
Definition ov.h:610
Complex complex_value(bool frc_str_conv=false) const
Definition ov.h:871
bool is_complex_scalar() const
Definition ov.h:616
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition ov.h:1528
ColumnVector real(const ComplexColumnVector &a)
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition data-conv.cc:802
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition data-conv.cc:918
save_type
Definition data-conv.h:85
@ LS_DOUBLE
Definition data-conv.h:93
@ LS_FLOAT
Definition data-conv.h:92
void warning(const char *fmt,...)
Definition error.cc:1078
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition errwarn.cc:344
F77_RET_T const F77_DBLE * x
std::complex< double > Complex
Definition oct-cmplx.h:33
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246
F77_RET_T len
Definition xerbla.cc:61