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-flt-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-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 
38 
40  "float complex diagonal matrix", "single");
41 
42 static octave_base_value *
44 {
46 
47  return new octave_float_complex_matrix (v.float_complex_matrix_value ());
48 }
49 
52 {
55 }
56 
59 {
60  octave_base_value *retval = 0;
61 
62  if (matrix.nelem () == 1)
63  {
64  retval = new octave_float_complex (matrix (0, 0));
66  if (rv2)
67  {
68  delete retval;
69  retval = rv2;
70  }
71  }
72  else if (matrix.all_elements_are_real ())
73  {
74  return new octave_float_diag_matrix (::real (matrix));
75  }
76 
77  return retval;
78 }
79 
82 {
83  DiagMatrix retval;
84 
85  if (! force_conversion)
86  gripe_implicit_conversion ("Octave:imag-to-real",
87  type_name (), "real matrix");
88 
89  retval = ::real (matrix);
90 
91  return retval;
92 }
93 
96 {
97  DiagMatrix retval;
98 
99  if (! force_conversion)
100  gripe_implicit_conversion ("Octave:imag-to-real",
101  type_name (), "real matrix");
102 
103  retval = ::real (matrix);
104 
105  return retval;
106 }
107 
110 {
111  return ComplexDiagMatrix (matrix);
112 }
113 
116 {
117  return matrix;
118 }
119 
122 {
123  switch (umap)
124  {
125  case umap_abs:
126  return matrix.abs ();
127  case umap_real:
129  case umap_conj:
131  case umap_imag:
133  case umap_sqrt:
134  {
136  (std::sqrt);
137  FloatComplexDiagMatrix retval (tmp);
138  retval.resize (matrix.rows (), matrix.columns ());
139  return retval;
140  }
141  default:
142  return to_dense ().map (umap);
143  }
144 }
145 
146 
147 bool
149  bool& /* save_as_floats */)
150 {
151 
152  int32_t r = matrix.rows ();
153  int32_t 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 }
save_type
Definition: data-conv.h:83
void gripe_implicit_conversion(const char *id, const char *from, const char *to)
Definition: gripes.cc:180
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:943
const T * fortran_vec(void) const
Definition: DiagArray2.h:182
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
DiagMatrix diag_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
OCTAVE_EMPTY_CPP_ARG std::string type_name(void) const
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
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
bool all_integers(float &max_val, float &min_val) const
Definition: fCNDArray.cc:533
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
type_conv_info numeric_conversion_function(void) const
bool chk_valid_scalar(const octave_value &, FloatComplex &) const
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:244
virtual octave_base_value * try_narrowing_conversion(void)
Definition: ov-base.h:240
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
bool all_elements_are_real(void) const
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
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
Definition: Array.h:659
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition: ov.h:788
const T * data(void) const
Definition: Array.h:479
int error_state
Definition: error.cc:101
bool save_binary(std::ostream &os, bool &save_as_floats)
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: fCDiagMatrix.h:136
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
octave_idx_type cols(void) const
Definition: DiagArray2.h:87
octave_idx_type columns(void) const
Definition: DiagArray2.h:88
FloatDiagMatrix abs(void) const
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:162
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
static int static_type_id(void)
octave_base_value * try_narrowing_conversion(void)
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:156
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, oct_mach_info::float_format fmt)
Definition: data-conv.cc:837
octave_idx_type length(void) const
Definition: DiagArray2.h:92
F77_RET_T const double * x