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-cx-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-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 
38 
39 
41  "complex diagonal matrix", "double");
42 
43 static octave_base_value *
45 {
47 
48  return new octave_complex_matrix (v.complex_matrix_value ());
49 }
50 
53 {
57 }
58 
59 static octave_base_value *
61 {
63 
65  (v.float_complex_diag_matrix_value ());
66 }
67 
70 {
71  return
74 }
75 
78 {
79  octave_base_value *retval = 0;
80 
81  if (matrix.nelem () == 1)
82  {
83  retval = new octave_complex (matrix (0, 0));
85  if (rv2)
86  {
87  delete retval;
88  retval = rv2;
89  }
90  }
91  else if (matrix.all_elements_are_real ())
92  {
93  return new octave_diag_matrix (::real (matrix));
94  }
95 
96  return retval;
97 }
98 
101 {
102  DiagMatrix retval;
103 
104  if (! force_conversion)
105  gripe_implicit_conversion ("Octave:imag-to-real",
106  type_name (), "real matrix");
107 
108  retval = ::real (matrix);
109 
110  return retval;
111 }
112 
115 {
116  DiagMatrix retval;
117 
118  if (! force_conversion)
119  gripe_implicit_conversion ("Octave:imag-to-real",
120  type_name (), "real matrix");
121 
122  retval = ::real (matrix);
123 
124  return retval;
125 }
126 
129 {
130  return matrix;
131 }
132 
135 {
137 }
138 
141 {
142  switch (umap)
143  {
144  case umap_abs:
145  return matrix.abs ();
146  case umap_real:
148  case umap_conj:
150  case umap_imag:
152  case umap_sqrt:
153  {
154  ComplexColumnVector tmp =
155  matrix.extract_diag ().map<Complex> (std::sqrt);
156  ComplexDiagMatrix retval (tmp);
157  retval.resize (matrix.rows (), matrix.columns ());
158  return retval;
159  }
160  default:
161  return to_dense ().map (umap);
162  }
163 }
164 
165 bool
166 octave_complex_diag_matrix::save_binary (std::ostream& os, bool& save_as_floats)
167 {
168 
169  int32_t r = matrix.rows ();
170  int32_t c = matrix.cols ();
171  os.write (reinterpret_cast<char *> (&r), 4);
172  os.write (reinterpret_cast<char *> (&c), 4);
173 
175  save_type st = LS_DOUBLE;
176  if (save_as_floats)
177  {
178  if (m.too_large_for_float ())
179  {
180  warning ("save: some values too large to save as floats --");
181  warning ("save: saving as doubles instead");
182  }
183  else
184  st = LS_FLOAT;
185  }
186  else if (matrix.length () > 4096) // FIXME: make this configurable.
187  {
188  double max_val, min_val;
189  if (m.all_integers (max_val, min_val))
190  st = get_save_type (max_val, min_val);
191  }
192 
193  const Complex *mtmp = m.data ();
194  write_doubles (os, reinterpret_cast<const double *> (mtmp), st,
195  2 * m.numel ());
196 
197  return true;
198 }
199 
200 bool
201 octave_complex_diag_matrix::load_binary (std::istream& is, bool swap,
203 {
204  int32_t r, c;
205  char tmp;
206  if (! (is.read (reinterpret_cast<char *> (&r), 4)
207  && is.read (reinterpret_cast<char *> (&c), 4)
208  && is.read (reinterpret_cast<char *> (&tmp), 1)))
209  return false;
210  if (swap)
211  {
212  swap_bytes<4> (&r);
213  swap_bytes<4> (&c);
214  }
215 
216  ComplexDiagMatrix m (r, c);
217  Complex *im = m.fortran_vec ();
218  octave_idx_type len = m.length ();
219  read_doubles (is, reinterpret_cast<double *> (im),
220  static_cast<save_type> (tmp), 2 * len, swap, fmt);
221  if (error_state || ! is)
222  return false;
223  matrix = m;
224 
225  return true;
226 }
227 
228 bool
230  Complex& x) const
231 {
232  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
233  if (retval)
234  x = val.complex_value ();
235  return retval;
236 }
237 
238 /*
239 
240 %% bug #36368
241 %!assert (diag ([1+i, 1-i])^2 , diag ([2i, -2i]), 4*eps);
242 
243 */
save_type
Definition: data-conv.h:83
bool chk_valid_scalar(const octave_value &, Complex &) const
Definition: ov-cx-diag.cc:229
void gripe_implicit_conversion(const char *id, const char *from, const char *to)
Definition: gripes.cc:180
bool all_integers(double &max_val, double &min_val) const
Definition: CNDArray.cc:536
FloatDiagMatrix float_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:114
type_conv_info numeric_conversion_function(void) const
Definition: ov-cx-diag.cc:52
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
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
ComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: CDiagMatrix.h:130
bool is_complex_scalar(void) const
Definition: ov.h:541
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
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-cx-diag.cc:166
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:100
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-cx-diag.cc:201
bool too_large_for_float(void) const
Definition: CNDArray.cc:586
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:244
virtual octave_base_value * try_narrowing_conversion(void)
Definition: ov-base.h:240
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:59
#define CAST_CONV_ARG(t)
Definition: ops.h:83
octave_idx_type nelem(void) const
Definition: DiagArray2.h:93
bool is_real_scalar(void) const
Definition: ov.h:535
type_conv_info numeric_demotion_function(void) const
Definition: ov-cx-diag.cc:69
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
Definition: Array.h:659
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-cx-diag.cc:60
const T * data(void) const
Definition: Array.h:479
int error_state
Definition: error.cc:101
octave_base_value * try_narrowing_conversion(void)
Definition: ov-cx-diag.cc:77
OCTAVE_EMPTY_CPP_ARG std::string type_name(void) const
Definition: ov-cx-diag.h:93
octave_value map(unary_mapper_t umap) const
Definition: ov-cx-diag.cc:140
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
void warning(const char *fmt,...)
Definition: error.cc:681
octave_idx_type cols(void) const
Definition: DiagArray2.h:87
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:128
Complex complex_value(bool frc_str_conv=false) const
Definition: ov.h:785
octave_idx_type columns(void) const
Definition: DiagArray2.h:88
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:134
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:162
static int static_type_id(void)
Definition: ov-cx-mat.h:180
std::complex< double > Complex
Definition: oct-cmplx.h:29
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:156
bool all_elements_are_real(void) const
Definition: CDiagMatrix.cc:407
octave_idx_type length(void) const
Definition: DiagArray2.h:92
DiagMatrix abs(void) const
Definition: CDiagMatrix.cc:238
F77_RET_T const double * x