GNU Octave  6.2.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-2021 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 *
47 {
49  = dynamic_cast<const octave_float_complex_diag_matrix&> (a);
50 
52 }
53 
56 {
59 }
60 
63 {
64  octave_base_value *retval = nullptr;
65 
66  if (matrix.nelem () == 1)
67  {
68  retval = new octave_float_complex (matrix (0, 0));
69  octave_base_value *rv2 = retval->try_narrowing_conversion ();
70  if (rv2)
71  {
72  delete retval;
73  retval = rv2;
74  }
75  }
76  else if (matrix.all_elements_are_real ())
77  {
78  return new octave_float_diag_matrix (::real (matrix));
79  }
80 
81  return retval;
82 }
83 
86 {
88 
89  if (! force_conversion)
90  warn_implicit_conversion ("Octave:imag-to-real",
91  type_name (), "real matrix");
92 
93  retval = ::real (matrix);
94 
95  return retval;
96 }
97 
100 {
102 
103  if (! force_conversion)
104  warn_implicit_conversion ("Octave:imag-to-real",
105  type_name (), "real matrix");
106 
107  retval = ::real (matrix);
108 
109  return retval;
110 }
111 
114 {
115  return ComplexDiagMatrix (matrix);
116 }
117 
120 {
121  return matrix;
122 }
123 
126 {
127  return ComplexDiagMatrix (matrix);
128 }
129 
132 {
133  return matrix;
134 }
135 
138 {
139  switch (umap)
140  {
141  case umap_abs:
142  return matrix.abs ();
143  case umap_real:
145  case umap_conj:
147  case umap_imag:
149  case umap_sqrt:
150  {
152  (std::sqrt);
155  return retval;
156  }
157  default:
158  return to_dense ().map (umap);
159  }
160 }
161 
162 bool
164  bool /* save_as_floats */)
165 {
166 
167  int32_t r = matrix.rows ();
168  int32_t c = matrix.cols ();
169  os.write (reinterpret_cast<char *> (&r), 4);
170  os.write (reinterpret_cast<char *> (&c), 4);
171 
173  save_type st = LS_FLOAT;
174  if (matrix.length () > 4096) // FIXME: make this configurable.
175  {
176  float max_val, min_val;
177  if (m.all_integers (max_val, min_val))
178  st = get_save_type (max_val, min_val);
179  }
180 
181  const FloatComplex *mtmp = m.data ();
182  write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
183 
184  return true;
185 }
186 
187 bool
190 {
191  int32_t r, c;
192  char tmp;
193  if (! (is.read (reinterpret_cast<char *> (&r), 4)
194  && is.read (reinterpret_cast<char *> (&c), 4)
195  && is.read (reinterpret_cast<char *> (&tmp), 1)))
196  return false;
197  if (swap)
198  {
199  swap_bytes<4> (&r);
200  swap_bytes<4> (&c);
201  }
202 
204  FloatComplex *re = m.fortran_vec ();
205  octave_idx_type len = m.length ();
206  read_floats (is, reinterpret_cast<float *> (re),
207  static_cast<save_type> (tmp), 2 * len, swap, fmt);
208 
209  if (! is)
210  return false;
211 
212  matrix = m;
213 
214  return true;
215 }
216 
217 bool
219  FloatComplex& x) const
220 {
221  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
222  if (retval)
223  x = val.float_complex_value ();
224  return retval;
225 }
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:63
Array< U > map(F fcn) const
Apply function fcn to each element of the Array<T>.
Definition: Array.h:759
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
octave_idx_type nelem(void) const
Definition: DiagArray2.h:95
octave_idx_type length(void) const
Definition: DiagArray2.h:94
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
octave_idx_type columns(void) const
Definition: DiagArray2.h:90
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: fCDiagMatrix.h:142
bool all_elements_are_real(void) const
FloatDiagMatrix abs(void) const
FloatComplexMatrix float_complex_matrix_value(bool=false) const
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
octave_value as_double(void) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
type_conv_info numeric_conversion_function(void) const
octave_value map(unary_mapper_t umap) const
DiagMatrix diag_matrix_value(bool=false) const
std::string type_name(void) 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(void)
bool chk_valid_scalar(const octave_value &, FloatComplex &) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_value as_single(void) const
static int static_type_id(void)
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition: ov.h:821
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1436
bool is_real_scalar(void) const
Definition: ov.h:566
bool is_complex_scalar(void) const
Definition: ov.h:572
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:941
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:835
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:38
T octave_idx_type m
Definition: mx-inlines.cc:773
T * r
Definition: mx-inlines.cc:773
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
F77_RET_T len
Definition: xerbla.cc:61