GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-re-diag.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 {
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 ())
131  retval = octave_base_diag<DiagMatrix, Matrix>::do_index_op (idx, resize_ok);
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  {
236  retval.resize (matrix.rows (), matrix.columns ());
237  return retval;
238  }
239  default:
240  return to_dense ().map (umap);
241  }
242 }
243 
244 bool
246 {
247 
248  int32_t r = matrix.rows ();
249  int32_t c = matrix.cols ();
250  os.write (reinterpret_cast<char *> (&r), 4);
251  os.write (reinterpret_cast<char *> (&c), 4);
252 
254  save_type st = LS_DOUBLE;
255  if (save_as_floats)
256  {
257  if (m.too_large_for_float ())
258  {
259  warning ("save: some values too large to save as floats --");
260  warning ("save: saving as doubles instead");
261  }
262  else
263  st = LS_FLOAT;
264  }
265  else if (matrix.length () > 8192) // FIXME: make this configurable.
266  {
267  double max_val, min_val;
268  if (m.all_integers (max_val, min_val))
269  st = get_save_type (max_val, min_val);
270  }
271 
272  const double *mtmp = m.data ();
273  write_doubles (os, mtmp, st, m.numel ());
274 
275  return true;
276 }
277 
278 bool
281 {
282  int32_t r, c;
283  char tmp;
284  if (! (is.read (reinterpret_cast<char *> (&r), 4)
285  && is.read (reinterpret_cast<char *> (&c), 4)
286  && is.read (reinterpret_cast<char *> (&tmp), 1)))
287  return false;
288  if (swap)
289  {
290  swap_bytes<4> (&r);
291  swap_bytes<4> (&c);
292  }
293 
294  DiagMatrix m (r, c);
295  double *re = m.fortran_vec ();
296  octave_idx_type len = m.length ();
297  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
298 
299  if (! is)
300  return false;
301 
302  matrix = m;
303 
304  return true;
305 }
306 
307 bool
309  double& x) const
310 {
311  bool retval = val.is_real_scalar ();
312  if (retval)
313  x = val.double_value ();
314  return retval;
315 }
save_type
Definition: data-conv.h:85
int64NDArray int64_array_value(void) const
Definition: ov-base-diag.h:185
static int left
Definition: randmtzig.cc:185
octave_value to_dense(void) const
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:143
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-re-diag.cc:85
octave_refcount< octave_idx_type > count
Definition: ov-base.h:843
uint64NDArray uint64_array_value(void) const
Definition: ov-base-diag.h:197
octave_value as_uint32(void) const
Definition: ov-re-diag.cc:209
const T * fortran_vec(void) const
Definition: DiagArray2.h:178
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:551
int32NDArray int32_array_value(void) const
Definition: ov-base-diag.h:182
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-re-diag.cc:43
void set_pos_if_unset(octave_idx_type nd_arg, octave_idx_type dim_arg)
octave_idx_type length(void) const
Definition: ovl.h:96
octave_value as_uint64(void) const
Definition: ov-re-diag.cc:215
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
for large enough k
Definition: lu.cc:606
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-re-diag.cc:58
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
octave_value as_int64(void) const
Definition: ov-re-diag.cc:191
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1413
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:886
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
octave_value map(unary_mapper_t umap) const
Definition: ov-re-diag.cc:221
in this the arguments are accumulated from left to right
Definition: data.cc:393
ColumnVector extract_diag(octave_idx_type k=0) const
Definition: dDiagMatrix.h:104
i e
Definition: data.cc:2724
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:149
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:398
bool swap
Definition: load-save.cc:725
type_conv_info numeric_demotion_function(void) const
Definition: ov-re-diag.cc:66
bool all_integers(double &max_val, double &min_val) const
Definition: dNDArray.cc:588
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
static int static_type_id(void)
octave_idx_type nelem(void) const
Definition: DiagArray2.h:95
bool is_real_scalar(void) const
Definition: ov.h:551
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
Definition: Array.h:760
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:771
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
uint16NDArray uint16_array_value(void) const
Definition: ov-base-diag.h:191
static int static_type_id(void)
Definition: ov-re-mat.h:250
const T * data(void) const
Definition: Array.h:582
octave_value as_double(void) const
Definition: ov-re-diag.cc:161
bool save_as_floats
Definition: load-save.cc:1581
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
Definition: dMatrix.h:37
int16NDArray int16_array_value(void) const
Definition: ov-base-diag.h:179
uint8NDArray uint8_array_value(void) const
Definition: ov-base-diag.h:188
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
bool is_permutation(octave_idx_type n) const
Definition: idx-vector.cc:1135
octave_value as_int8(void) const
Definition: ov-re-diag.cc:173
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-re-diag.cc:279
void warning(const char *fmt,...)
Definition: error.cc:788
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
bool is_multiple_of_identity(T val) const
Definition: MDiagArray2.cc:34
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-re-diag.cc:245
octave_value as_int16(void) const
Definition: ov-re-diag.cc:179
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:137
bool chk_valid_scalar(const octave_value &, double &) const
Definition: ov-re-diag.cc:308
type_conv_info numeric_conversion_function(void) const
Definition: ov-re-diag.cc:51
octave_value as_uint8(void) const
Definition: ov-re-diag.cc:197
Matrix matrix_value(bool=false) const
int8NDArray int8_array_value(void) const
Definition: ov-base-diag.h:176
octave_value as_int32(void) const
Definition: ov-re-diag.cc:185
octave_idx_type columns(void) const
Definition: DiagArray2.h:90
bool is_undefined(void) const
Definition: ov.h:539
octave_value as_single(void) const
Definition: ov-re-diag.cc:167
DiagMatrix abs(void) const
Definition: dDiagMatrix.cc:127
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:155
std::complex< double > Complex
Definition: oct-cmplx.h:31
double double_value(bool frc_str_conv=false) const
Definition: ov.h:775
write the output to stdout if nargout is
Definition: load-save.cc:1576
octave_value as_uint16(void) const
Definition: ov-re-diag.cc:203
octave_idx_type length(void) const
Definition: DiagArray2.h:94
uint32NDArray uint32_array_value(void) const
Definition: ov-base-diag.h:194
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-diag.cc:74
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x
bool too_large_for_float(void) const
Definition: dNDArray.cc:624
bool is_colon(void) const
Definition: idx-vector.h:565