GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-flt-cx-diag.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2024 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-flt-cx-diag.h"
33 #include "ov-base-diag.cc"
34 #include "ov-flt-re-diag.h"
35 #include "ov-flt-complex.h"
36 #include "ov-flt-cx-mat.h"
37 #include "ls-utils.h"
38 
39 
41 
43  "float complex diagonal matrix", "single");
44 
45 static octave_base_value *
46 default_numeric_conversion_function (const octave_base_value& a)
47 {
49  = dynamic_cast<const octave_float_complex_diag_matrix&> (a);
50 
52 }
53 
56 {
57  return octave_base_value::type_conv_info (default_numeric_conversion_function,
59 }
60 
63 {
64  octave_base_value *retval = nullptr;
65 
66  if (m_matrix.nelem () == 1)
67  {
68  retval = new octave_float_complex (m_matrix (0, 0));
70  if (rv2)
71  {
72  delete retval;
73  retval = rv2;
74  }
75  }
76  else if (m_matrix.all_elements_are_real ())
77  {
78  return new octave_float_diag_matrix (::real (m_matrix));
79  }
80 
81  return retval;
82 }
83 
86 {
87  DiagMatrix retval;
88 
89  if (! force_conversion)
90  warn_implicit_conversion ("Octave:imag-to-real",
91  type_name (), "real matrix");
92 
93  retval = ::real (m_matrix);
94 
95  return retval;
96 }
97 
100 {
101  DiagMatrix retval;
102 
103  if (! force_conversion)
104  warn_implicit_conversion ("Octave:imag-to-real",
105  type_name (), "real matrix");
106 
107  retval = ::real (m_matrix);
108 
109  return retval;
110 }
111 
114 {
115  return ComplexDiagMatrix (m_matrix);
116 }
117 
120 {
121  return m_matrix;
122 }
123 
126 {
127  return ComplexDiagMatrix (m_matrix);
128 }
129 
132 {
133  return m_matrix;
134 }
135 
138 {
139  switch (umap)
140  {
141  case umap_abs:
142  return m_matrix.abs ();
143  case umap_real:
145  case umap_conj:
147  case umap_imag:
149  case umap_sqrt:
150  {
152  FloatComplexDiagMatrix retval (tmp);
153  retval.resize (m_matrix.rows (), m_matrix.columns ());
154  return retval;
155  }
156  default:
157  return to_dense ().map (umap);
158  }
159 }
160 
161 bool
163  bool /* save_as_floats */)
164 {
165 
166  int32_t r = m_matrix.rows ();
167  int32_t c = m_matrix.cols ();
168  os.write (reinterpret_cast<char *> (&r), 4);
169  os.write (reinterpret_cast<char *> (&c), 4);
170 
172  save_type st = LS_FLOAT;
173  if (m_matrix.length () > 4096) // FIXME: make this configurable.
174  {
175  float max_val, min_val;
176  if (m.all_integers (max_val, min_val))
177  st = octave::get_save_type (max_val, min_val);
178  }
179 
180  const FloatComplex *mtmp = m.data ();
181  write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
182 
183  return true;
184 }
185 
186 bool
189 {
190  int32_t r, c;
191  char tmp;
192  if (! (is.read (reinterpret_cast<char *> (&r), 4)
193  && is.read (reinterpret_cast<char *> (&c), 4)
194  && is.read (reinterpret_cast<char *> (&tmp), 1)))
195  return false;
196  if (swap)
197  {
198  swap_bytes<4> (&r);
199  swap_bytes<4> (&c);
200  }
201 
203  FloatComplex *re = m.fortran_vec ();
204  octave_idx_type len = m.length ();
205  read_floats (is, reinterpret_cast<float *> (re),
206  static_cast<save_type> (tmp), 2 * len, swap, fmt);
207 
208  if (! is)
209  return false;
210 
211  m_matrix = m;
212 
213  return true;
214 }
215 
216 bool
217 octave_float_complex_diag_matrix::chk_valid_scalar (const octave_value& val,
218  FloatComplex& x) const
219 {
220  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
221  if (retval)
222  x = val.float_complex_value ();
223  return retval;
224 }
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
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:859
octave_idx_type nelem() const
Definition: DiagArray2.h:96
octave_idx_type rows() const
Definition: DiagArray2.h:89
octave_idx_type length() const
Definition: DiagArray2.h:95
octave_idx_type columns() const
Definition: DiagArray2.h:91
octave_idx_type cols() const
Definition: DiagArray2.h:90
FloatDiagMatrix abs() const
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: fCDiagMatrix.h:144
bool all_elements_are_real() const
FloatComplexMatrix float_complex_matrix_value(bool=false) const
virtual octave_base_value * try_narrowing_conversion()
Definition: ov-base.h:323
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
DiagMatrix diag_matrix_value(bool=false) const
bool save_binary(std::ostream &os, bool save_as_floats)
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_base_value * try_narrowing_conversion()
type_conv_info numeric_conversion_function() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
bool is_real_scalar() const
Definition: ov.h:610
bool is_complex_scalar() const
Definition: ov.h:616
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition: ov.h:868
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:137
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:143
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:942
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:836
save_type
Definition: data-conv.h:87
@ LS_FLOAT
Definition: data-conv.h:94
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:344
F77_RET_T const F77_DBLE * x
save_type get_save_type(double, double)
Definition: ls-utils.cc:40
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
T * r
Definition: mx-inlines.cc:781
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235
F77_RET_T len
Definition: xerbla.cc:61