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-cx-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-cx-diag.h"
30 #include "ov-flt-cx-diag.h"
31 #include "ov-re-diag.h"
32 #include "ov-base-diag.cc"
33 #include "ov-complex.h"
34 #include "ov-cx-mat.h"
35 #include "ls-utils.h"
36 
37 
39 
41  "complex diagonal matrix", "double");
42 
43 static octave_base_value *
45 {
47  dynamic_cast<const octave_complex_diag_matrix&> (a);
48 
50 }
51 
54 {
58 }
59 
60 static octave_base_value *
62 {
64  dynamic_cast<const octave_complex_diag_matrix&> (a);
65 
68 }
69 
72 {
73  return
76 }
77 
80 {
82 
83  if (matrix.nelem () == 1)
84  {
85  retval = new octave_complex (matrix (0, 0));
87  if (rv2)
88  {
89  delete retval;
90  retval = rv2;
91  }
92  }
93  else if (matrix.all_elements_are_real ())
94  {
95  return new octave_diag_matrix (::real (matrix));
96  }
97 
98  return retval;
99 }
100 
103 {
105 
106  if (! force_conversion)
107  warn_implicit_conversion ("Octave:imag-to-real",
108  type_name (), "real matrix");
109 
110  retval = ::real (matrix);
111 
112  return retval;
113 }
114 
117 {
119 
120  if (! force_conversion)
121  warn_implicit_conversion ("Octave:imag-to-real",
122  type_name (), "real matrix");
123 
124  retval = ::real (matrix);
125 
126  return retval;
127 }
128 
131 {
132  return matrix;
133 }
134 
137 {
139 }
140 
143 {
144  return matrix;
145 }
146 
149 {
151 }
152 
155 {
156  switch (umap)
157  {
158  case umap_abs:
159  return matrix.abs ();
160  case umap_real:
162  case umap_conj:
164  case umap_imag:
166  case umap_sqrt:
167  {
169  matrix.extract_diag ().map<Complex> (std::sqrt);
171  retval.resize (matrix.rows (), matrix.columns ());
172  return retval;
173  }
174  default:
175  return to_dense ().map (umap);
176  }
177 }
178 
179 bool
181 {
182 
183  int32_t r = matrix.rows ();
184  int32_t c = matrix.cols ();
185  os.write (reinterpret_cast<char *> (&r), 4);
186  os.write (reinterpret_cast<char *> (&c), 4);
187 
189  save_type st = LS_DOUBLE;
190  if (save_as_floats)
191  {
192  if (m.too_large_for_float ())
193  {
194  warning ("save: some values too large to save as floats --");
195  warning ("save: saving as doubles instead");
196  }
197  else
198  st = LS_FLOAT;
199  }
200  else if (matrix.length () > 4096) // FIXME: make this configurable.
201  {
202  double max_val, min_val;
203  if (m.all_integers (max_val, min_val))
204  st = get_save_type (max_val, min_val);
205  }
206 
207  const Complex *mtmp = m.data ();
208  write_doubles (os, reinterpret_cast<const double *> (mtmp), st,
209  2 * m.numel ());
210 
211  return true;
212 }
213 
214 bool
217 {
218  int32_t r, c;
219  char tmp;
220  if (! (is.read (reinterpret_cast<char *> (&r), 4)
221  && is.read (reinterpret_cast<char *> (&c), 4)
222  && is.read (reinterpret_cast<char *> (&tmp), 1)))
223  return false;
224  if (swap)
225  {
226  swap_bytes<4> (&r);
227  swap_bytes<4> (&c);
228  }
229 
230  ComplexDiagMatrix m (r, c);
231  Complex *im = m.fortran_vec ();
232  octave_idx_type len = m.length ();
233  read_doubles (is, reinterpret_cast<double *> (im),
234  static_cast<save_type> (tmp), 2 * len, swap, fmt);
235 
236  if (! is)
237  return false;
238 
239  matrix = m;
240 
241  return true;
242 }
243 
244 bool
246  Complex& x) const
247 {
248  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
249  if (retval)
250  x = val.complex_value ();
251  return retval;
252 }
253 
254 /*
255 %!assert <36368> (diag ([1+i, 1-i])^2 , diag ([2i, -2i]), 4*eps)
256 */
save_type
Definition: data-conv.h:85
bool chk_valid_scalar(const octave_value &, Complex &) const
Definition: ov-cx-diag.cc:245
bool all_integers(double &max_val, double &min_val) const
Definition: CNDArray.cc:526
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-cx-diag.cc:215
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:116
type_conv_info numeric_conversion_function(void) const
Definition: ov-cx-diag.cc:53
const T * fortran_vec(void) const
Definition: DiagArray2.h:178
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
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
ComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: CDiagMatrix.h:132
bool is_complex_scalar(void) const
Definition: ov.h:557
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
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
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-cx-diag.cc:180
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:102
bool too_large_for_float(void) const
Definition: CNDArray.cc:577
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
std::string type_name(void) const
Definition: ov-cx-diag.h:97
bool swap
Definition: load-save.cc:725
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:216
virtual octave_base_value * try_narrowing_conversion(void)
Definition: ov-base.h:258
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
octave_value as_double(void) const
Definition: ov-cx-diag.cc:142
octave_idx_type nelem(void) const
Definition: DiagArray2.h:95
bool is_real_scalar(void) const
Definition: ov.h:551
type_conv_info numeric_demotion_function(void) const
Definition: ov-cx-diag.cc:71
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
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-cx-diag.cc:61
const T * data(void) const
Definition: Array.h:582
bool save_as_floats
Definition: load-save.cc:1581
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
octave_base_value * try_narrowing_conversion(void)
Definition: ov-cx-diag.cc:79
ComplexMatrix complex_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
Definition: ov-cx-diag.cc:154
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
void warning(const char *fmt,...)
Definition: error.cc:788
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:130
Complex complex_value(bool frc_str_conv=false) const
Definition: ov.h:799
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:332
octave_idx_type columns(void) const
Definition: DiagArray2.h:90
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:136
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-cx-diag.cc:44
octave_value as_single(void) const
Definition: ov-cx-diag.cc:148
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:142
static int static_type_id(void)
Definition: ov-cx-mat.h:184
std::complex< double > Complex
Definition: oct-cmplx.h:31
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:136
write the output to stdout if nargout is
Definition: load-save.cc:1576
bool all_elements_are_real(void) const
Definition: CDiagMatrix.cc:356
octave_idx_type length(void) const
Definition: DiagArray2.h:94
DiagMatrix abs(void) const
Definition: CDiagMatrix.cc:208
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