GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-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-re-diag.h"
33 #include "ov-flt-re-diag.h"
34 #include "ov-base-diag.cc"
35 #include "ov-scalar.h"
36 #include "ov-re-mat.h"
37 #include "ls-utils.h"
38 
39 
41 
43  "double");
44 
45 static octave_base_value *
46 default_numeric_conversion_function (const octave_base_value& a)
47 {
48  const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
49 
50  return new octave_matrix (v.matrix_value ());
51 }
52 
55 {
56  return octave_base_value::type_conv_info (default_numeric_conversion_function,
58 }
59 
60 static octave_base_value *
61 default_numeric_demotion_function (const octave_base_value& a)
62 {
63  const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
64 
66 }
67 
70 {
72  (default_numeric_demotion_function,
74 }
75 
78 {
79  octave_base_value *retval = nullptr;
80 
81  if (m_matrix.nelem () == 1)
82  retval = new octave_scalar (m_matrix (0, 0));
83 
84  return retval;
85 }
86 
89  bool resize_ok)
90 {
91  octave_value retval;
92 
93  // This hack is to allow constructing permutation matrices using
94  // eye(n)(p,:), eye(n)(:,q) && eye(n)(p,q) where p & q are permutation
95  // vectors.
96  if (! resize_ok && idx.length () == 2 && m_matrix.is_multiple_of_identity (1))
97  {
98  int k = 0; // index we're accessing when index_vector throws
99  try
100  {
101  octave::idx_vector idx0 = idx(0).index_vector ();
102  k = 1;
103  octave::idx_vector idx1 = idx(1).index_vector ();
104 
105  bool left = idx0.is_permutation (m_matrix.rows ());
106  bool right = idx1.is_permutation (m_matrix.cols ());
107 
108  if (left && right)
109  {
110  if (idx0.is_colon ()) left = false;
111  if (idx1.is_colon ()) right = false;
112  if (left && right)
113  retval = PermMatrix (idx0, false) * PermMatrix (idx1, true);
114  else if (left)
115  retval = PermMatrix (idx0, false);
116  else if (right)
117  retval = PermMatrix (idx1, true);
118  else
119  {
120  retval = this;
121  this->m_count++;
122  }
123  }
124  }
125  catch (octave::index_exception& ie)
126  {
127  // Rethrow to allow more info to be reported later.
128  ie.set_pos_if_unset (2, k+1);
129  throw;
130  }
131  }
132 
133  if (retval.is_undefined ())
134  retval = octave_base_diag<DiagMatrix, Matrix>::do_index_op (idx, resize_ok);
135 
136  return retval;
137 }
138 
141 {
142  return m_matrix;
143 }
144 
147 {
148  return FloatDiagMatrix (m_matrix);
149 }
150 
153 {
154  return ComplexDiagMatrix (m_matrix);
155 }
156 
159 {
161 }
162 
165 {
166  return m_matrix;
167 }
168 
171 {
172  return FloatDiagMatrix (m_matrix);
173 }
174 
177 {
178  return int8_array_value ();
179 }
180 
183 {
184  return int16_array_value ();
185 }
186 
189 {
190  return int32_array_value ();
191 }
192 
195 {
196  return int64_array_value ();
197 }
198 
201 {
202  return uint8_array_value ();
203 }
204 
207 {
208  return uint16_array_value ();
209 }
210 
213 {
214  return uint32_array_value ();
215 }
216 
219 {
220  return uint64_array_value ();
221 }
222 
225 {
226  switch (umap)
227  {
228  case umap_abs:
229  return m_matrix.abs ();
230  case umap_real:
231  case umap_conj:
232  return m_matrix;
233  case umap_imag:
234  return DiagMatrix (m_matrix.rows (), m_matrix.cols (), 0.0);
235  case umap_sqrt:
236  {
239  ComplexDiagMatrix retval (tmp);
240  retval.resize (m_matrix.rows (), m_matrix.columns ());
241  return retval;
242  }
243  default:
244  return to_dense ().map (umap);
245  }
246 }
247 
248 bool
249 octave_diag_matrix::save_binary (std::ostream& os, bool save_as_floats)
250 {
251 
252  int32_t r = m_matrix.rows ();
253  int32_t c = m_matrix.cols ();
254  os.write (reinterpret_cast<char *> (&r), 4);
255  os.write (reinterpret_cast<char *> (&c), 4);
256 
258  save_type st = LS_DOUBLE;
259  if (save_as_floats)
260  {
261  if (m.too_large_for_float ())
262  {
263  warning ("save: some values too large to save as floats --");
264  warning ("save: saving as doubles instead");
265  }
266  else
267  st = LS_FLOAT;
268  }
269  else if (m_matrix.length () > 8192) // FIXME: make this configurable.
270  {
271  double max_val, min_val;
272  if (m.all_integers (max_val, min_val))
273  st = octave::get_save_type (max_val, min_val);
274  }
275 
276  const double *mtmp = m.data ();
277  write_doubles (os, mtmp, st, m.numel ());
278 
279  return true;
280 }
281 
282 bool
283 octave_diag_matrix::load_binary (std::istream& is, bool swap,
285 {
286  int32_t r, c;
287  char tmp;
288  if (! (is.read (reinterpret_cast<char *> (&r), 4)
289  && is.read (reinterpret_cast<char *> (&c), 4)
290  && is.read (reinterpret_cast<char *> (&tmp), 1)))
291  return false;
292  if (swap)
293  {
294  swap_bytes<4> (&r);
295  swap_bytes<4> (&c);
296  }
297 
298  DiagMatrix m (r, c);
299  double *re = m.fortran_vec ();
300  octave_idx_type len = m.length ();
301  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
302 
303  if (! is)
304  return false;
305 
306  m_matrix = m;
307 
308  return true;
309 }
310 
311 bool
312 octave_diag_matrix::chk_valid_scalar (const octave_value& val,
313  double& x) const
314 {
315  bool retval = val.is_real_scalar ();
316  if (retval)
317  x = val.double_value ();
318  return retval;
319 }
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
DiagMatrix abs() const
Definition: dDiagMatrix.cc:128
ColumnVector extract_diag(octave_idx_type k=0) const
Definition: dDiagMatrix.h:107
bool is_multiple_of_identity(T val) const
Definition: MDiagArray2.cc:36
Definition: dMatrix.h:42
uint64NDArray uint64_array_value() const
Definition: ov-base-diag.h:214
int16NDArray int16_array_value() const
Definition: ov-base-diag.h:196
Matrix matrix_value(bool=false) const
int32NDArray int32_array_value() const
Definition: ov-base-diag.h:199
uint8NDArray uint8_array_value() const
Definition: ov-base-diag.h:205
uint16NDArray uint16_array_value() const
Definition: ov-base-diag.h:208
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
uint32NDArray uint32_array_value() const
Definition: ov-base-diag.h:211
int64NDArray int64_array_value() const
Definition: ov-base-diag.h:202
octave::refcount< octave_idx_type > m_count
Definition: ov-base.h:933
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:158
octave_value as_uint32() const
Definition: ov-re-diag.cc:212
octave_value as_int16() const
Definition: ov-re-diag.cc:182
octave_value as_int64() const
Definition: ov-re-diag.cc:194
type_conv_info numeric_conversion_function() const
Definition: ov-re-diag.cc:54
octave_value as_uint64() const
Definition: ov-re-diag.cc:218
octave_value as_int8() const
Definition: ov-re-diag.cc:176
octave_value map(unary_mapper_t umap) const
Definition: ov-re-diag.cc:224
octave_value as_int32() const
Definition: ov-re-diag.cc:188
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:140
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-re-diag.cc:88
octave_value as_single() const
Definition: ov-re-diag.cc:170
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:152
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:146
octave_value as_double() const
Definition: ov-re-diag.cc:164
octave_base_value * try_narrowing_conversion()
Definition: ov-re-diag.cc:77
type_conv_info numeric_demotion_function() const
Definition: ov-re-diag.cc:69
octave_value as_uint16() const
Definition: ov-re-diag.cc:206
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-re-diag.cc:249
octave_value as_uint8() const
Definition: ov-re-diag.cc:200
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-re-diag.cc:283
static int static_type_id()
Definition: ov-re-mat.h:249
octave_idx_type length() const
Definition: ovl.h:113
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:1513
double double_value(bool frc_str_conv=false) const
Definition: ov.h:841
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:776
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:892
save_type
Definition: data-conv.h:87
@ LS_DOUBLE
Definition: data-conv.h:95
@ LS_FLOAT
Definition: data-conv.h:94
void warning(const char *fmt,...)
Definition: error.cc:1063
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
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< double > Complex
Definition: oct-cmplx.h:33
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235
F77_RET_T len
Definition: xerbla.cc:61