GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-flt-re-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-flt-re-diag.h"
33#include "ov-float.h"
34#include "ls-utils.h"
35
37 "float diagonal matrix", "single");
38
39static octave_base_value *
40default_numeric_conversion_function (const octave_base_value& a)
41{
43 = dynamic_cast<const octave_float_diag_matrix&> (a);
44
46}
47
54
57{
58 octave_base_value *retval = nullptr;
59
60 if (m_matrix.nelem () == 1)
61 retval = new octave_float_scalar (m_matrix (0, 0));
62
63 return retval;
64}
65
71
77
83
89
95
98{
99 return m_matrix;
100}
101
104{
105 return int8_array_value ();
106}
107
113
119
125
131
137
143
149
152{
153 switch (umap)
154 {
155 case umap_abs:
156 return m_matrix.abs ();
157 case umap_real:
158 case umap_conj:
159 return m_matrix;
160 case umap_imag:
161 return DiagMatrix (m_matrix.rows (), m_matrix.cols (), 0.0);
162 case umap_sqrt:
163 {
164 FloatComplexColumnVector tmp = m_matrix.extract_diag ().map<FloatComplex> (octave::math::rc_sqrt);
165 FloatComplexDiagMatrix retval (tmp);
166 retval.resize (m_matrix.rows (), m_matrix.columns ());
167 return retval;
168 }
169 default:
170 return to_dense ().map (umap);
171 }
172}
173
174bool
176 bool /* save_as_floats*/)
177{
178
179 int32_t r = m_matrix.rows ();
180 int32_t c = m_matrix.cols ();
181 os.write (reinterpret_cast<char *> (&r), 4);
182 os.write (reinterpret_cast<char *> (&c), 4);
183
185 save_type st = LS_FLOAT;
186 if (m_matrix.length () > 8192) // FIXME: make this configurable.
187 {
188 float max_val, min_val;
189 if (m.all_integers (max_val, min_val))
190 st = octave::get_save_type (max_val, min_val);
191 }
192
193 const float *mtmp = m.data ();
194 write_floats (os, mtmp, st, m.numel ());
195
196 return true;
197}
198
199bool
200octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
201 octave::mach_info::float_format fmt)
202{
203 int32_t r, c;
204 char tmp;
205 if (! (is.read (reinterpret_cast<char *> (&r), 4)
206 && is.read (reinterpret_cast<char *> (&c), 4)
207 && is.read (reinterpret_cast<char *> (&tmp), 1)))
208 return false;
209 if (swap)
210 {
211 swap_bytes<4> (&r);
212 swap_bytes<4> (&c);
213 }
214
215 FloatDiagMatrix m (r, c);
216 float *re = m.rwdata ();
218 read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
219
220 if (! is)
221 return false;
222
223 m_matrix = m;
224
225 return true;
226}
227
228bool
229octave_float_diag_matrix::chk_valid_scalar (const octave_value& val,
230 float& x) const
231{
232 bool retval = val.is_real_scalar ();
233 if (retval)
234 x = val.float_value ();
235 return retval;
236}
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
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
FloatDiagMatrix abs() const
FloatColumnVector extract_diag(octave_idx_type k=0) const
bool all_integers(float &max_val, float &min_val) const
Definition fNDArray.cc:308
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:850
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition ov.h:1528
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition data-conv.cc:968
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:862
save_type
Definition data-conv.h:85
@ LS_FLOAT
Definition data-conv.h:92
F77_RET_T const F77_DBLE * x
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246
F77_RET_T len
Definition xerbla.cc:61