GNU Octave  3.8.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-2013 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 
40 
42  "complex diagonal matrix", "double");
43 
44 static octave_base_value *
46 {
48 
49  return new octave_complex_matrix (v.complex_matrix_value ());
50 }
51 
54 {
58 }
59 
60 static octave_base_value *
62 {
64 
66  (v.float_complex_diag_matrix_value ());
67 }
68 
71 {
72  return
75 }
76 
79 {
80  octave_base_value *retval = 0;
81 
82  if (matrix.nelem () == 1)
83  {
84  retval = new octave_complex (matrix (0, 0));
86  if (rv2)
87  {
88  delete retval;
89  retval = rv2;
90  }
91  }
92  else if (matrix.all_elements_are_real ())
93  {
94  return new octave_diag_matrix (::real (matrix));
95  }
96 
97  return retval;
98 }
99 
102 {
103  DiagMatrix retval;
104 
105  if (! force_conversion)
106  gripe_implicit_conversion ("Octave:imag-to-real",
107  type_name (), "real matrix");
108 
109  retval = ::real (matrix);
110 
111  return retval;
112 }
113 
116 {
117  DiagMatrix retval;
118 
119  if (! force_conversion)
120  gripe_implicit_conversion ("Octave:imag-to-real",
121  type_name (), "real matrix");
122 
123  retval = ::real (matrix);
124 
125  return retval;
126 }
127 
130 {
131  return matrix;
132 }
133 
136 {
138 }
139 
142 {
143  switch (umap)
144  {
145  case umap_abs:
146  return matrix.abs ();
147  case umap_real:
149  case umap_conj:
151  case umap_imag:
153  case umap_sqrt:
154  {
155  ComplexColumnVector tmp =
156  matrix.extract_diag ().map<Complex> (std::sqrt);
157  ComplexDiagMatrix retval (tmp);
158  retval.resize (matrix.rows (), matrix.columns ());
159  return retval;
160  }
161  default:
162  return to_dense ().map (umap);
163  }
164 }
165 
166 bool
167 octave_complex_diag_matrix::save_binary (std::ostream& os, bool& save_as_floats)
168 {
169 
170  int32_t r = matrix.rows (), 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 */