GNU Octave  4.4.1
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-2018 Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "byte-swap.h"
28 
29 #include "ov-re-diag.h"
30 #include "ov-flt-re-diag.h"
31 #include "ov-base-diag.cc"
32 #include "ov-scalar.h"
33 #include "ov-re-mat.h"
34 #include "ls-utils.h"
35 
36 
38 
40  "double");
41 
42 static octave_base_value *
44 {
45  const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
46 
47  return new octave_matrix (v.matrix_value ());
48 }
49 
52 {
55 }
56 
57 static octave_base_value *
59 {
60  const octave_diag_matrix& v = dynamic_cast<const octave_diag_matrix&> (a);
61 
63 }
64 
67 {
71 }
72 
75 {
76  octave_base_value *retval = nullptr;
77 
78  if (matrix.nelem () == 1)
79  retval = new octave_scalar (matrix (0, 0));
80 
81  return retval;
82 }
83 
86  bool resize_ok)
87 {
89 
90  // This hack is to allow constructing permutation matrices using
91  // eye(n)(p,:), eye(n)(:,q) && eye(n)(p,q) where p & q are permutation
92  // vectors.
93  if (! resize_ok && idx.length () == 2 && matrix.is_multiple_of_identity (1))
94  {
95  int k = 0; // index we're accesing when index_vector throws
96  try
97  {
98  idx_vector idx0 = idx(0).index_vector ();
99  k = 1;
100  idx_vector idx1 = idx(1).index_vector ();
101 
102  bool left = idx0.is_permutation (matrix.rows ());
103  bool right = idx1.is_permutation (matrix.cols ());
104 
105  if (left && right)
106  {
107  if (idx0.is_colon ()) left = false;
108  if (idx1.is_colon ()) right = false;
109  if (left && right)
110  retval = PermMatrix (idx0, false) * PermMatrix (idx1, true);
111  else if (left)
112  retval = PermMatrix (idx0, false);
113  else if (right)
114  retval = PermMatrix (idx1, true);
115  else
116  {
117  retval = this;
118  this->count++;
119  }
120  }
121  }
122  catch (octave::index_exception& e)
123  {
124  // Rethrow to allow more info to be reported later.
125  e.set_pos_if_unset (2, k+1);
126  throw;
127  }
128  }
129 
130  if (retval.is_undefined ())
132 
133  return retval;
134 }
135 
138 {
139  return matrix;
140 }
141 
144 {
145  return FloatDiagMatrix (matrix);
146 }
147 
150 {
151  return ComplexDiagMatrix (matrix);
152 }
153 
156 {
158 }
159 
162 {
163  return matrix;
164 }
165 
168 {
169  return FloatDiagMatrix (matrix);
170 }
171 
174 {
175  return int8_array_value ();
176 }
177 
180 {
181  return int16_array_value ();
182 }
183 
186 {
187  return int32_array_value ();
188 }
189 
192 {
193  return int64_array_value ();
194 }
195 
198 {
199  return uint8_array_value ();
200 }
201 
204 {
205  return uint16_array_value ();
206 }
207 
210 {
211  return uint32_array_value ();
212 }
213 
216 {
217  return uint64_array_value ();
218 }
219 
222 {
223  switch (umap)
224  {
225  case umap_abs:
226  return matrix.abs ();
227  case umap_real:
228  case umap_conj:
229  return matrix;
230  case umap_imag:
231  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
232  case umap_sqrt:
233  {
238  return retval;
239  }
240  default:
241  return to_dense ().map (umap);
242  }
243 }
244 
245 bool
247 {
248 
249  int32_t r = matrix.rows ();
250  int32_t c = matrix.cols ();
251  os.write (reinterpret_cast<char *> (&r), 4);
252  os.write (reinterpret_cast<char *> (&c), 4);
253 
254  Matrix m = Matrix (matrix.extract_diag ());
255  save_type st = LS_DOUBLE;
256  if (save_as_floats)
257  {
258  if (m.too_large_for_float ())
259  {
260  warning ("save: some values too large to save as floats --");
261  warning ("save: saving as doubles instead");
262  }
263  else
264  st = LS_FLOAT;
265  }
266  else if (matrix.length () > 8192) // FIXME: make this configurable.
267  {
268  double max_val, min_val;
269  if (m.all_integers (max_val, min_val))
270  st = get_save_type (max_val, min_val);
271  }
272 
273  const double *mtmp = m.data ();
274  write_doubles (os, mtmp, st, m.numel ());
275 
276  return true;
277 }
278 
279 bool
282 {
283  int32_t r, c;
284  char tmp;
285  if (! (is.read (reinterpret_cast<char *> (&r), 4)
286  && is.read (reinterpret_cast<char *> (&c), 4)
287  && is.read (reinterpret_cast<char *> (&tmp), 1)))
288  return false;
289  if (swap)
290  {
291  swap_bytes<4> (&r);
292  swap_bytes<4> (&c);
293  }
294 
295  DiagMatrix m (r, c);
296  double *re = m.fortran_vec ();
297  octave_idx_type len = m.length ();
298  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
299 
300  if (! is)
301  return false;
302 
303  matrix = m;
304 
305  return true;
306 }
307 
308 bool
310  double& x) const
311 {
312  bool retval = val.is_real_scalar ();
313  if (retval)
314  x = val.double_value ();
315  return retval;
316 }
save_type
Definition: data-conv.h:85
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6704
Matrix matrix_value(bool=false) const
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:137
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:155
static int left
Definition: randmtzig.cc:184
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1473
ColumnVector extract_diag(octave_idx_type k=0) const
Definition: dDiagMatrix.h:104
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-re-diag.cc:85
octave_value as_uint8(void) const
Definition: ov-re-diag.cc:197
const T * data(void) const
Definition: Array.h:582
octave_value as_int64(void) const
Definition: ov-re-diag.cc:191
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:322
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-re-diag.cc:43
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
for large enough k
Definition: lu.cc:617
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-re-diag.cc:58
const T * fortran_vec(void) const
Definition: DiagArray2.h:169
octave_value as_uint32(void) const
Definition: ov-re-diag.cc:209
bool is_multiple_of_identity(T val) const
Definition: MDiagArray2.cc:34
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:887
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
uint16NDArray uint16_array_value(void) const
Definition: ov-base-diag.h:189
octave_idx_type rows(void) const
Definition: DiagArray2.h:87
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
bool too_large_for_float(void) const
Definition: dNDArray.cc:622
in this the arguments are accumulated from left to right
Definition: data.cc:390
i e
Definition: data.cc:2591
octave_value as_uint64(void) const
Definition: ov-re-diag.cc:215
int8NDArray int8_array_value(void) const
Definition: ov-base-diag.h:174
int16NDArray int16_array_value(void) const
Definition: ov-base-diag.h:177
octave_value to_dense(void) const
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
bool swap
Definition: load-save.cc:738
bool is_permutation(octave_idx_type n) const
Definition: idx-vector.cc:1139
type_conv_info numeric_demotion_function(void) const
Definition: ov-re-diag.cc:66
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:143
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
octave_idx_type length(void) const
Definition: DiagArray2.h:93
octave_idx_type cols(void) const
Definition: DiagArray2.h:88
static int static_type_id(void)
DiagMatrix abs(void) const
Definition: dDiagMatrix.cc:126
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:772
bool is_colon(void) const
Definition: idx-vector.h:575
static int static_type_id(void)
Definition: ov-re-mat.h:248
bool save_as_floats
Definition: load-save.cc:1617
bool all_integers(double &max_val, double &min_val) const
Definition: dNDArray.cc:586
octave_value as_int8(void) const
Definition: ov-re-diag.cc:173
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
octave_value as_uint16(void) const
Definition: ov-re-diag.cc:203
octave_idx_type nelem(void) const
Definition: DiagArray2.h:94
octave_value as_int16(void) const
Definition: ov-re-diag.cc:179
Definition: dMatrix.h:36
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-re-diag.cc:280
void warning(const char *fmt,...)
Definition: error.cc:801
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-re-diag.cc:246
bool is_undefined(void) const
Definition: ov.h:526
Array< U > map(F fcn) const
Apply function fcn to each element of the Array<T>.
Definition: Array.h:764
uint32NDArray uint32_array_value(void) const
Definition: ov-base-diag.h:192
octave_idx_type length(void) const
Definition: ovl.h:96
bool chk_valid_scalar(const octave_value &, double &) const
Definition: ov-re-diag.cc:309
uint8NDArray uint8_array_value(void) const
Definition: ov-base-diag.h:186
octave::refcount< octave_idx_type > count
Definition: ov-base.h:862
octave_idx_type columns(void) const
Definition: DiagArray2.h:89
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:149
uint64NDArray uint64_array_value(void) const
Definition: ov-base-diag.h:195
std::complex< double > Complex
Definition: oct-cmplx.h:31
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
write the output to stdout if nargout is
Definition: load-save.cc:1612
octave_value as_double(void) const
Definition: ov-re-diag.cc:161
type_conv_info numeric_conversion_function(void) const
Definition: ov-re-diag.cc:51
int32NDArray int32_array_value(void) const
Definition: ov-base-diag.h:180
octave_value as_single(void) const
Definition: ov-re-diag.cc:167
octave::stream os
Definition: file-io.cc:627
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-diag.cc:74
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
octave_value map(unary_mapper_t umap) const
Definition: ov-re-diag.cc:221
octave_value as_int32(void) const
Definition: ov-re-diag.cc:185
int64NDArray int64_array_value(void) const
Definition: ov-base-diag.h:183