GNU Octave  4.0.0
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-2015 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 #ifdef 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 
37 
38 
40  "double");
41 
42 static octave_base_value *
44 {
46 
47  return new octave_matrix (v.matrix_value ());
48 }
49 
52 {
55 }
56 
57 static octave_base_value *
59 {
61 
62  return new octave_float_diag_matrix (v.float_diag_matrix_value ());
63 }
64 
67 {
71 }
72 
75 {
76  octave_base_value *retval = 0;
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 {
88  octave_value retval;
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  idx_vector idx0 = idx(0).index_vector ();
96  idx_vector idx1 = idx(1).index_vector ();
97 
98  if (! error_state)
99  {
100  bool left = idx0.is_permutation (matrix.rows ());
101  bool right = idx1.is_permutation (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->count++;
117  }
118  }
119  }
120  }
121 
122  // if error_state is set, we've already griped.
123  if (! error_state && retval.is_undefined ())
124  retval = octave_base_diag<DiagMatrix, Matrix>::do_index_op (idx, resize_ok);
125 
126  return retval;
127 }
128 
131 {
132  return matrix;
133 }
134 
137 {
138  return FloatDiagMatrix (matrix);
139 }
140 
143 {
144  return ComplexDiagMatrix (matrix);
145 }
146 
149 {
151 }
152 
155 {
156  switch (umap)
157  {
158  case umap_abs:
159  return matrix.abs ();
160  case umap_real:
161  case umap_conj:
162  return matrix;
163  case umap_imag:
164  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
165  case umap_sqrt:
166  {
168  ComplexDiagMatrix retval (tmp);
169  retval.resize (matrix.rows (), matrix.columns ());
170  return retval;
171  }
172  default:
173  return to_dense ().map (umap);
174  }
175 }
176 
177 bool
178 octave_diag_matrix::save_binary (std::ostream& os, bool& save_as_floats)
179 {
180 
181  int32_t r = matrix.rows ();
182  int32_t c = matrix.cols ();
183  os.write (reinterpret_cast<char *> (&r), 4);
184  os.write (reinterpret_cast<char *> (&c), 4);
185 
186  Matrix m = Matrix (matrix.extract_diag ());
187  save_type st = LS_DOUBLE;
188  if (save_as_floats)
189  {
190  if (m.too_large_for_float ())
191  {
192  warning ("save: some values too large to save as floats --");
193  warning ("save: saving as doubles instead");
194  }
195  else
196  st = LS_FLOAT;
197  }
198  else if (matrix.length () > 8192) // FIXME: make this configurable.
199  {
200  double max_val, min_val;
201  if (m.all_integers (max_val, min_val))
202  st = get_save_type (max_val, min_val);
203  }
204 
205  const double *mtmp = m.data ();
206  write_doubles (os, mtmp, st, m.numel ());
207 
208  return true;
209 }
210 
211 bool
212 octave_diag_matrix::load_binary (std::istream& is, bool swap,
214 {
215  int32_t r, c;
216  char tmp;
217  if (! (is.read (reinterpret_cast<char *> (&r), 4)
218  && is.read (reinterpret_cast<char *> (&c), 4)
219  && is.read (reinterpret_cast<char *> (&tmp), 1)))
220  return false;
221  if (swap)
222  {
223  swap_bytes<4> (&r);
224  swap_bytes<4> (&c);
225  }
226 
227  DiagMatrix m (r, c);
228  double *re = m.fortran_vec ();
229  octave_idx_type len = m.length ();
230  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
231  if (error_state || ! is)
232  return false;
233  matrix = m;
234 
235  return true;
236 }
237 
238 bool
240  double& x) const
241 {
242  bool retval = val.is_real_scalar ();
243  if (retval)
244  x = val.double_value ();
245  return retval;
246 }
save_type
Definition: data-conv.h:83
octave_value to_dense(void) const
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:136
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:818
const T * fortran_vec(void) const
Definition: DiagArray2.h:182
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-re-diag.cc:43
octave_idx_type length(void) const
Definition: oct-obj.h:89
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
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:86
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1226
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:893
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
octave_value map(unary_mapper_t umap) const
Definition: ov-re-diag.cc:154
static int left
Definition: randmtzig.c:189
ColumnVector extract_diag(octave_idx_type k=0) const
Definition: dDiagMatrix.h:105
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:142
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:597
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:59
#define CAST_CONV_ARG(t)
Definition: ops.h:83
static int static_type_id(void)
octave_idx_type nelem(void) const
Definition: DiagArray2.h:93
bool is_real_scalar(void) const
Definition: ov.h:535
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
Definition: Array.h:659
static int static_type_id(void)
Definition: ov-re-mat.h:235
const T * data(void) const
Definition: Array.h:479
int error_state
Definition: error.cc:101
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:606
Definition: dMatrix.h:35
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, oct_mach_info::float_format fmt)
Definition: data-conv.cc:778
bool is_permutation(octave_idx_type n) const
Definition: idx-vector.cc:1145
void warning(const char *fmt,...)
Definition: error.cc:681
octave_idx_type cols(void) const
Definition: DiagArray2.h:87
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-re-diag.cc:212
bool is_multiple_of_identity(T val) const
Definition: MDiagArray2.cc:36
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-re-diag.cc:178
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:130
bool chk_valid_scalar(const octave_value &, double &) const
Definition: ov-re-diag.cc:239
type_conv_info numeric_conversion_function(void) const
Definition: ov-re-diag.cc:51
octave_idx_type columns(void) const
Definition: DiagArray2.h:88
bool is_undefined(void) const
Definition: ov.h:523
DiagMatrix abs(void) const
Definition: dDiagMatrix.cc:142
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:148
std::complex< double > Complex
Definition: oct-cmplx.h:29
double double_value(bool frc_str_conv=false) const
Definition: ov.h:759
octave_idx_type length(void) const
Definition: DiagArray2.h:92
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-diag.cc:74
bool too_large_for_float(void) const
Definition: dNDArray.cc:633
bool is_colon(void) const
Definition: idx-vector.h:575
F77_RET_T const double * x