GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-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-re-diag.h"
33#include "ov-flt-re-diag.h"
34#include "ov-scalar.h"
35#include "ls-utils.h"
36
38 "double");
39
40static octave_base_value *
41default_numeric_conversion_function (const octave_base_value& a)
42{
43 const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
44
45 return new octave_matrix (v.matrix_value ());
46}
47
50{
51 return octave_base_value::type_conv_info (default_numeric_conversion_function,
53}
54
55static octave_base_value *
56default_numeric_demotion_function (const octave_base_value& a)
57{
58 const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
59
61}
62
70
73{
74 octave_base_value *retval = nullptr;
75
76 if (m_matrix.nelem () == 1)
77 retval = new octave_scalar (m_matrix (0, 0));
78
79 return retval;
80}
81
84 bool resize_ok)
85{
86 octave_value retval;
87
88 // This hack is to allow constructing permutation matrices using
89 // eye(n)(p,:), eye(n)(:,q) && eye(n)(p,q) where p & q are permutation
90 // vectors.
91 if (! resize_ok && idx.length () == 2 && m_matrix.is_multiple_of_identity (1))
92 {
93 int k = 0; // index we're accessing when index_vector throws
94 try
95 {
96 octave::idx_vector idx0 = idx(0).index_vector ();
97 k = 1;
98 octave::idx_vector idx1 = idx(1).index_vector ();
99
100 bool left = idx0.is_permutation (m_matrix.rows ());
101 bool right = idx1.is_permutation (m_matrix.cols ());
102
103 if (left && right)
104 {
105 if (idx0.is_colon ()) left = false;
106 if (idx1.is_colon ()) right = false;
107 if (left && right)
108 retval = PermMatrix (idx0, false) * PermMatrix (idx1, true);
109 else if (left)
110 retval = PermMatrix (idx0, false);
111 else if (right)
112 retval = PermMatrix (idx1, true);
113 else
114 {
115 retval = this;
116 this->m_count++;
117 }
118 }
119 }
120 catch (octave::index_exception& ie)
121 {
122 // Rethrow to allow more info to be reported later.
123 ie.set_pos_if_unset (2, k+1);
124 throw;
125 }
126 }
127
128 if (retval.is_undefined ())
130
131 return retval;
132}
133
136{
137 return m_matrix;
138}
139
145
151
157
160{
161 return m_matrix;
162}
163
169
172{
173 return int8_array_value ();
174}
175
178{
179 return int16_array_value ();
180}
181
184{
185 return int32_array_value ();
186}
187
190{
191 return int64_array_value ();
192}
193
196{
197 return uint8_array_value ();
198}
199
202{
203 return uint16_array_value ();
204}
205
208{
209 return uint32_array_value ();
210}
211
214{
215 return uint64_array_value ();
216}
217
220{
221 switch (umap)
222 {
223 case umap_abs:
224 return m_matrix.abs ();
225 case umap_real:
226 case umap_conj:
227 return m_matrix;
228 case umap_imag:
229 return DiagMatrix (m_matrix.rows (), m_matrix.cols (), 0.0);
230 case umap_sqrt:
231 {
233 tmp = m_matrix.extract_diag ().map<Complex> (octave::math::rc_sqrt);
234 ComplexDiagMatrix retval (tmp);
235 retval.resize (m_matrix.rows (), m_matrix.columns ());
236 return retval;
237 }
238 default:
239 return to_dense ().map (umap);
240 }
241}
242
243bool
244octave_diag_matrix::save_binary (std::ostream& os, bool save_as_floats)
245{
246
247 int32_t r = m_matrix.rows ();
248 int32_t c = m_matrix.cols ();
249 os.write (reinterpret_cast<char *> (&r), 4);
250 os.write (reinterpret_cast<char *> (&c), 4);
251
253 save_type st = LS_DOUBLE;
254 if (save_as_floats)
255 {
256 if (m.too_large_for_float ())
257 {
258 warning ("save: some values too large to save as floats --");
259 warning ("save: saving as doubles instead");
260 }
261 else
262 st = LS_FLOAT;
263 }
264 else if (m_matrix.length () > 8192) // FIXME: make this configurable.
265 {
266 double max_val, min_val;
267 if (m.all_integers (max_val, min_val))
268 st = octave::get_save_type (max_val, min_val);
269 }
270
271 const double *mtmp = m.data ();
272 write_doubles (os, mtmp, st, m.numel ());
273
274 return true;
275}
276
277bool
278octave_diag_matrix::load_binary (std::istream& is, bool swap,
279 octave::mach_info::float_format fmt)
280{
281 int32_t r, c;
282 char tmp;
283 if (! (is.read (reinterpret_cast<char *> (&r), 4)
284 && is.read (reinterpret_cast<char *> (&c), 4)
285 && is.read (reinterpret_cast<char *> (&tmp), 1)))
286 return false;
287 if (swap)
288 {
289 swap_bytes<4> (&r);
290 swap_bytes<4> (&c);
291 }
292
293 DiagMatrix m (r, c);
294 double *re = m.rwdata ();
296 read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
297
298 if (! is)
299 return false;
300
301 m_matrix = m;
302
303 return true;
304}
305
306bool
307octave_diag_matrix::chk_valid_scalar (const octave_value& val,
308 double& x) const
309{
310 bool retval = val.is_real_scalar ();
311 if (retval)
312 x = val.double_value ();
313 return retval;
314}
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
DiagMatrix abs() const
ColumnVector extract_diag(octave_idx_type k=0) const
bool is_multiple_of_identity(T val) const
bool all_integers(double &max_val, double &min_val) const
Definition dNDArray.cc:351
bool too_large_for_float() const
Definition dNDArray.cc:387
uint64NDArray uint64_array_value() const
Matrix matrix_value(bool=false) const
uint16NDArray uint16_array_value() const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
uint32NDArray uint32_array_value() const
octave::refcount< octave_idx_type > m_count
Definition ov-base.h:958
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
octave_value as_uint32() const
octave_value as_int16() const
octave_value as_int64() const
type_conv_info numeric_conversion_function() const
Definition ov-re-diag.cc:49
octave_value as_uint64() const
octave_value as_int8() const
octave_value map(unary_mapper_t umap) const
octave_value as_int32() const
DiagMatrix diag_matrix_value(bool=false) const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-re-diag.cc:83
octave_value as_single() const
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_value as_double() const
octave_base_value * try_narrowing_conversion()
Definition ov-re-diag.cc:72
type_conv_info numeric_demotion_function() const
Definition ov-re-diag.cc:64
octave_value as_uint16() const
bool save_binary(std::ostream &os, bool save_as_floats)
octave_value as_uint8() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
static int static_type_id()
Definition ov-re-mat.h:249
octave_idx_type length() const
Definition ovl.h:111
bool is_undefined() const
Definition ov.h:595
bool is_real_scalar() const
Definition ov.h:610
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition ov.h:1528
double double_value(bool frc_str_conv=false) const
Definition ov.h:847
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition data-conv.cc:802
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition data-conv.cc:918
save_type
Definition data-conv.h:85
@ LS_DOUBLE
Definition data-conv.h:93
@ LS_FLOAT
Definition data-conv.h:92
void warning(const char *fmt,...)
Definition error.cc:1078
F77_RET_T const F77_DBLE * x
std::complex< double > Complex
Definition oct-cmplx.h:33
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246
F77_RET_T len
Definition xerbla.cc:61