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-flt-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-flt-cx-diag.h"
30 #include "ov-base-diag.cc"
31 #include "ov-flt-re-diag.h"
32 #include "ov-flt-complex.h"
33 #include "ov-flt-cx-mat.h"
34 #include "ls-utils.h"
35 
37 
39 
41  "float complex diagonal matrix", "single");
42 
43 static octave_base_value *
45 {
47 
48  return new octave_float_complex_matrix (v.float_complex_matrix_value ());
49 }
50 
53 {
56 }
57 
60 {
61  octave_base_value *retval = 0;
62 
63  if (matrix.nelem () == 1)
64  {
65  retval = new octave_float_complex (matrix (0, 0));
67  if (rv2)
68  {
69  delete retval;
70  retval = rv2;
71  }
72  }
73  else if (matrix.all_elements_are_real ())
74  {
75  return new octave_float_diag_matrix (::real (matrix));
76  }
77 
78  return retval;
79 }
80 
83 {
84  DiagMatrix retval;
85 
86  if (! force_conversion)
87  gripe_implicit_conversion ("Octave:imag-to-real",
88  type_name (), "real matrix");
89 
90  retval = ::real (matrix);
91 
92  return retval;
93 }
94 
97 {
98  DiagMatrix retval;
99 
100  if (! force_conversion)
101  gripe_implicit_conversion ("Octave:imag-to-real",
102  type_name (), "real matrix");
103 
104  retval = ::real (matrix);
105 
106  return retval;
107 }
108 
111 {
112  return ComplexDiagMatrix (matrix);
113 }
114 
117 {
118  return matrix;
119 }
120 
123 {
124  switch (umap)
125  {
126  case umap_abs:
127  return matrix.abs ();
128  case umap_real:
130  case umap_conj:
132  case umap_imag:
134  case umap_sqrt:
135  {
137  (std::sqrt);
138  FloatComplexDiagMatrix retval (tmp);
139  retval.resize (matrix.rows (), matrix.columns ());
140  return retval;
141  }
142  default:
143  return to_dense ().map (umap);
144  }
145 }
146 
147 
148 bool
150  bool& /* save_as_floats */)
151 {
152 
153  int32_t r = matrix.rows (), c = matrix.cols ();
154  os.write (reinterpret_cast<char *> (&r), 4);
155  os.write (reinterpret_cast<char *> (&c), 4);
156 
158  save_type st = LS_FLOAT;
159  if (matrix.length () > 4096) // FIXME: make this configurable.
160  {
161  float max_val, min_val;
162  if (m.all_integers (max_val, min_val))
163  st = get_save_type (max_val, min_val);
164  }
165 
166  const FloatComplex *mtmp = m.data ();
167  write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
168 
169  return true;
170 }
171 
172 bool
175 {
176  int32_t r, c;
177  char tmp;
178  if (! (is.read (reinterpret_cast<char *> (&r), 4)
179  && is.read (reinterpret_cast<char *> (&c), 4)
180  && is.read (reinterpret_cast<char *> (&tmp), 1)))
181  return false;
182  if (swap)
183  {
184  swap_bytes<4> (&r);
185  swap_bytes<4> (&c);
186  }
187 
188  FloatComplexDiagMatrix m (r, c);
189  FloatComplex *re = m.fortran_vec ();
190  octave_idx_type len = m.length ();
191  read_floats (is, reinterpret_cast<float *> (re),
192  static_cast<save_type> (tmp), 2 * len, swap, fmt);
193  if (error_state || ! is)
194  return false;
195  matrix = m;
196 
197  return true;
198 }
199 
200 bool
202  FloatComplex& x) const
203 {
204  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
205  if (retval)
206  x = val.float_complex_value ();
207  return retval;
208 }