GNU Octave 7.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-2022 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
44static octave_base_value *
46{
48 = dynamic_cast<const octave_float_diag_matrix&> (a);
49
51}
52
55{
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{
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 {
171 FloatComplexDiagMatrix retval (tmp);
172 retval.resize (m_matrix.rows (), m_matrix.columns ());
173 return retval;
174 }
175 default:
176 return to_dense ().map (umap);
177 }
178}
179
180bool
182 bool /* save_as_floats*/)
183{
184
185 int32_t r = m_matrix.rows ();
186 int32_t c = m_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 (m_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 = octave::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
205bool
206octave_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 ();
224 read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
225
226 if (! is)
227 return false;
228
229 m_matrix = m;
230
231 return true;
232}
233
234bool
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
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Array< U, A > map(F fcn) const
Apply function fcn to each element of the Array<T, Alloc>.
Definition: Array.h:799
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:616
T * fortran_vec(void)
Definition: DiagArray2.h:176
octave_idx_type nelem(void) const
Definition: DiagArray2.h:96
octave_idx_type length(void) const
Definition: DiagArray2.h:95
octave_idx_type cols(void) const
Definition: DiagArray2.h:90
octave_idx_type columns(void) const
Definition: DiagArray2.h:91
octave_idx_type rows(void) const
Definition: DiagArray2.h:89
FloatColumnVector extract_diag(octave_idx_type k=0) const
Definition: fDiagMatrix.h:110
OCTAVE_API FloatDiagMatrix abs(void) const
Definition: fDiagMatrix.cc:128
OCTAVE_API bool all_integers(float &max_val, float &min_val) const
Definition: fNDArray.cc:308
OCTINTERP_API octave_value to_dense(void) const
OCTINTERP_API 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:889
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1634
bool is_real_scalar(void) const
Definition: ov.h:655
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
F77_RET_T const F77_DBLE * x
OCTAVE_NAMESPACE_BEGIN save_type get_save_type(double, double)
Definition: ls-utils.cc:40
class OCTAVE_API ComplexDiagMatrix
Definition: mx-fwd.h:60
class OCTAVE_API DiagMatrix
Definition: mx-fwd.h:59
class OCTAVE_API FloatMatrix
Definition: mx-fwd.h:33
class OCTAVE_API FloatComplexDiagMatrix
Definition: mx-fwd.h:62
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:330
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:222
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
F77_RET_T len
Definition: xerbla.cc:61