GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-cx-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-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 {
81  octave_base_value *retval = nullptr;
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);
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
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
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1473
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:130
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-cx-diag.cc:215
const T * data(void) const
Definition: Array.h:582
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
ComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: CDiagMatrix.h:136
const T * fortran_vec(void) const
Definition: DiagArray2.h:169
octave_base_value * try_narrowing_conversion(void)
Definition: ov.h:405
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
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-cx-diag.cc:180
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: CNDArray.cc:575
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
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:136
bool all_elements_are_real(void) const
Definition: CDiagMatrix.cc:355
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:215
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
DiagMatrix abs(void) const
Definition: CDiagMatrix.cc:207
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:102
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
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-cx-diag.cc:61
bool save_as_floats
Definition: load-save.cc:1617
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
octave_base_value * try_narrowing_conversion(void)
Definition: ov-cx-diag.cc:79
octave_value map(unary_mapper_t umap) const
Definition: ov-cx-diag.cc:154
octave_idx_type nelem(void) const
Definition: DiagArray2.h:94
type_conv_info numeric_demotion_function(void) const
Definition: ov-cx-diag.cc:71
std::string type_name(void) const
Definition: ov-cx-diag.h:97
void warning(const char *fmt,...)
Definition: error.cc:801
bool all_integers(double &max_val, double &min_val) const
Definition: CNDArray.cc:524
Array< U > map(F fcn) const
Apply function fcn to each element of the Array<T>.
Definition: Array.h:764
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:338
type_conv_info numeric_conversion_function(void) const
Definition: ov-cx-diag.cc:53
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-cx-diag.cc:44
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:141
octave_idx_type columns(void) const
Definition: DiagArray2.h:89
static int static_type_id(void)
Definition: ov-cx-mat.h:182
std::complex< double > Complex
Definition: oct-cmplx.h:31
ComplexMatrix complex_matrix_value(bool=false) const
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
octave_value as_single(void) const
Definition: ov-cx-diag.cc:148
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:135
write the output to stdout if nargout is
Definition: load-save.cc:1612
octave_value as_double(void) const
Definition: ov-cx-diag.cc:142
octave::stream os
Definition: file-io.cc:627
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
bool chk_valid_scalar(const octave_value &, Complex &) const
Definition: ov-cx-diag.cc:245
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:116