GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-flt-re-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-re-diag.h"
33 #include "ov-base-diag.cc"
34 #include "ov-float.h"
35 #include "ov-flt-re-mat.h"
36 #include "ls-utils.h"
37 
38 
40 
42  "float diagonal matrix", "single");
43 
44 static octave_base_value *
45 default_numeric_conversion_function (const octave_base_value& a)
46 {
48  = dynamic_cast<const octave_float_diag_matrix&> (a);
49 
50  return new octave_float_matrix (v.float_matrix_value ());
51 }
52 
55 {
56  return octave_base_value::type_conv_info (default_numeric_conversion_function,
58 }
59 
62 {
63  octave_base_value *retval = nullptr;
64 
65  if (m_matrix.nelem () == 1)
66  retval = new octave_float_scalar (m_matrix (0, 0));
67 
68  return retval;
69 }
70 
73 {
74  return DiagMatrix (m_matrix);
75 }
76 
79 {
80  return m_matrix;
81 }
82 
85 {
86  return ComplexDiagMatrix (m_matrix);
87 }
88 
91 {
93 }
94 
97 {
98  return DiagMatrix (m_matrix);
99 }
100 
103 {
104  return m_matrix;
105 }
106 
109 {
110  return int8_array_value ();
111 }
112 
115 {
116  return int16_array_value ();
117 }
118 
121 {
122  return int32_array_value ();
123 }
124 
127 {
128  return int64_array_value ();
129 }
130 
133 {
134  return uint8_array_value ();
135 }
136 
139 {
140  return uint16_array_value ();
141 }
142 
145 {
146  return uint32_array_value ();
147 }
148 
151 {
152  return uint64_array_value ();
153 }
154 
157 {
158  switch (umap)
159  {
160  case umap_abs:
161  return m_matrix.abs ();
162  case umap_real:
163  case umap_conj:
164  return m_matrix;
165  case umap_imag:
166  return DiagMatrix (m_matrix.rows (), m_matrix.cols (), 0.0);
167  case umap_sqrt:
168  {
170  FloatComplexDiagMatrix retval (tmp);
171  retval.resize (m_matrix.rows (), m_matrix.columns ());
172  return retval;
173  }
174  default:
175  return to_dense ().map (umap);
176  }
177 }
178 
179 bool
181  bool /* save_as_floats*/)
182 {
183 
184  int32_t r = m_matrix.rows ();
185  int32_t c = m_matrix.cols ();
186  os.write (reinterpret_cast<char *> (&r), 4);
187  os.write (reinterpret_cast<char *> (&c), 4);
188 
190  save_type st = LS_FLOAT;
191  if (m_matrix.length () > 8192) // FIXME: make this configurable.
192  {
193  float max_val, min_val;
194  if (m.all_integers (max_val, min_val))
195  st = octave::get_save_type (max_val, min_val);
196  }
197 
198  const float *mtmp = m.data ();
199  write_floats (os, mtmp, st, m.numel ());
200 
201  return true;
202 }
203 
204 bool
205 octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
207 {
208  int32_t r, c;
209  char tmp;
210  if (! (is.read (reinterpret_cast<char *> (&r), 4)
211  && is.read (reinterpret_cast<char *> (&c), 4)
212  && is.read (reinterpret_cast<char *> (&tmp), 1)))
213  return false;
214  if (swap)
215  {
216  swap_bytes<4> (&r);
217  swap_bytes<4> (&c);
218  }
219 
220  FloatDiagMatrix m (r, c);
221  float *re = m.fortran_vec ();
222  octave_idx_type len = m.length ();
223  read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
224 
225  if (! is)
226  return false;
227 
228  m_matrix = m;
229 
230  return true;
231 }
232 
233 bool
234 octave_float_diag_matrix::chk_valid_scalar (const octave_value& val,
235  float& x) const
236 {
237  bool retval = val.is_real_scalar ();
238  if (retval)
239  x = val.float_value ();
240  return retval;
241 }
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
Definition: fDiagMatrix.cc:128
FloatColumnVector extract_diag(octave_idx_type k=0) const
Definition: fDiagMatrix.h:110
FloatMatrix float_matrix_value(bool=false) const
octave_value as_uint8() const
octave_value as_int8() const
octave_value as_int64() const
DiagMatrix diag_matrix_value(bool=false) const
octave_value as_uint32() const
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_value as_double() const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
octave_value as_single() const
octave_value as_uint16() const
octave_value as_int16() const
octave_value as_uint64() const
type_conv_info numeric_conversion_function() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_base_value * try_narrowing_conversion()
octave_value as_int32() const
bool save_binary(std::ostream &os, bool save_as_floats)
static int static_type_id()
bool is_real_scalar() const
Definition: ov.h:610
float float_value(bool frc_str_conv=false) const
Definition: ov.h:844
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
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
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:334
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