GNU Octave  6.2.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-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-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 *
46 {
48  = dynamic_cast<const octave_float_diag_matrix&> (a);
49 
50  return new octave_float_matrix (v.float_matrix_value ());
51 }
52 
55 {
58 }
59 
62 {
63  octave_base_value *retval = nullptr;
64 
65  if (matrix.nelem () == 1)
66  retval = new octave_float_scalar (matrix (0, 0));
67 
68  return retval;
69 }
70 
73 {
74  return DiagMatrix (matrix);
75 }
76 
79 {
80  return matrix;
81 }
82 
85 {
86  return ComplexDiagMatrix (matrix);
87 }
88 
91 {
93 }
94 
97 {
98  return DiagMatrix (matrix);
99 }
100 
103 {
104  return 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 matrix.abs ();
162  case umap_real:
163  case umap_conj:
164  return matrix;
165  case umap_imag:
166  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
167  case umap_sqrt:
168  {
173  return retval;
174  }
175  default:
176  return to_dense ().map (umap);
177  }
178 }
179 
180 bool
182  bool /* save_as_floats*/)
183 {
184 
185  int32_t r = matrix.rows ();
186  int32_t c = matrix.cols ();
187  os.write (reinterpret_cast<char *> (&r), 4);
188  os.write (reinterpret_cast<char *> (&c), 4);
189 
191  save_type st = LS_FLOAT;
192  if (matrix.length () > 8192) // FIXME: make this configurable.
193  {
194  float max_val, min_val;
195  if (m.all_integers (max_val, min_val))
196  st = get_save_type (max_val, min_val);
197  }
198 
199  const float *mtmp = m.data ();
200  write_floats (os, mtmp, st, m.numel ());
201 
202  return true;
203 }
204 
205 bool
206 octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
208 {
209  int32_t r, c;
210  char tmp;
211  if (! (is.read (reinterpret_cast<char *> (&r), 4)
212  && is.read (reinterpret_cast<char *> (&c), 4)
213  && is.read (reinterpret_cast<char *> (&tmp), 1)))
214  return false;
215  if (swap)
216  {
217  swap_bytes<4> (&r);
218  swap_bytes<4> (&c);
219  }
220 
221  FloatDiagMatrix m (r, c);
222  float *re = m.fortran_vec ();
223  octave_idx_type len = m.length ();
224  read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
225 
226  if (! is)
227  return false;
228 
229  matrix = m;
230 
231  return true;
232 }
233 
234 bool
236  float& x) const
237 {
238  bool retval = val.is_real_scalar ();
239  if (retval)
240  x = val.float_value ();
241  return retval;
242 }
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
FloatColumnVector extract_diag(octave_idx_type k=0) const
Definition: fDiagMatrix.h:106
FloatDiagMatrix abs(void) const
Definition: fDiagMatrix.cc:128
FloatMatrix float_matrix_value(bool=false) const
type_conv_info numeric_conversion_function(void) const
octave_value as_uint64(void) const
octave_value as_int16(void) const
octave_value as_single(void) const
DiagMatrix diag_matrix_value(bool=false) const
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_value as_uint16(void) const
octave_value as_int64(void) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
octave_value as_int32(void) const
octave_value map(unary_mapper_t umap) const
bool chk_valid_scalar(const octave_value &, float &) const
octave_value as_uint32(void) const
octave_value as_uint8(void) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_base_value * try_narrowing_conversion(void)
octave_value as_int8(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
octave_value as_double(void) const
static int static_type_id(void)
float float_value(bool frc_str_conv=false) const
Definition: ov.h:797
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
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
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
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:324
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