GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-flt-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-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 
36 
38 
40  "float complex diagonal matrix", "single");
41 
42 static octave_base_value *
44 {
46  dynamic_cast<const octave_float_complex_diag_matrix&> (a);
47 
49 }
50 
53 {
56 }
57 
60 {
61  octave_base_value *retval = nullptr;
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 {
85 
86  if (! force_conversion)
87  warn_implicit_conversion ("Octave:imag-to-real",
88  type_name (), "real matrix");
89 
90  retval = ::real (matrix);
91 
92  return retval;
93 }
94 
97 {
99 
100  if (! force_conversion)
101  warn_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  return ComplexDiagMatrix (matrix);
125 }
126 
129 {
130  return matrix;
131 }
132 
135 {
136  switch (umap)
137  {
138  case umap_abs:
139  return matrix.abs ();
140  case umap_real:
142  case umap_conj:
144  case umap_imag:
146  case umap_sqrt:
147  {
149  (std::sqrt);
152  return retval;
153  }
154  default:
155  return to_dense ().map (umap);
156  }
157 }
158 
159 bool
161  bool& /* save_as_floats */)
162 {
163 
164  int32_t r = matrix.rows ();
165  int32_t c = matrix.cols ();
166  os.write (reinterpret_cast<char *> (&r), 4);
167  os.write (reinterpret_cast<char *> (&c), 4);
168 
170  save_type st = LS_FLOAT;
171  if (matrix.length () > 4096) // FIXME: make this configurable.
172  {
173  float max_val, min_val;
174  if (m.all_integers (max_val, min_val))
175  st = get_save_type (max_val, min_val);
176  }
177 
178  const FloatComplex *mtmp = m.data ();
179  write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
180 
181  return true;
182 }
183 
184 bool
187 {
188  int32_t r, c;
189  char tmp;
190  if (! (is.read (reinterpret_cast<char *> (&r), 4)
191  && is.read (reinterpret_cast<char *> (&c), 4)
192  && is.read (reinterpret_cast<char *> (&tmp), 1)))
193  return false;
194  if (swap)
195  {
196  swap_bytes<4> (&r);
197  swap_bytes<4> (&c);
198  }
199 
200  FloatComplexDiagMatrix m (r, c);
201  FloatComplex *re = m.fortran_vec ();
202  octave_idx_type len = m.length ();
203  read_floats (is, reinterpret_cast<float *> (re),
204  static_cast<save_type> (tmp), 2 * len, swap, fmt);
205 
206  if (! is)
207  return false;
208 
209  matrix = m;
210 
211  return true;
212 }
213 
214 bool
216  FloatComplex& x) const
217 {
218  bool retval = val.is_complex_scalar () || val.is_real_scalar ();
219  if (retval)
220  x = val.float_complex_value ();
221  return retval;
222 }
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
bool all_integers(float &max_val, float &min_val) const
Definition: fCNDArray.cc:518
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1473
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:937
const T * data(void) const
Definition: Array.h:582
bool all_elements_are_real(void) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
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
const T * fortran_vec(void) const
Definition: DiagArray2.h:169
bool chk_valid_scalar(const octave_value &, FloatComplex &) const
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
octave_base_value * try_narrowing_conversion(void)
Definition: ov.h:405
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
FloatDiagMatrix abs(void) const
octave_value map(unary_mapper_t umap) const
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
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:831
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
FloatComplexMatrix float_complex_matrix_value(bool=false) const
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:215
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: fCDiagMatrix.h:142
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
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
octave_idx_type nelem(void) const
Definition: DiagArray2.h:94
std::string type_name(void) const
bool save_binary(std::ostream &os, bool &save_as_floats)
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
FloatDiagMatrix float_diag_matrix_value(bool=false) const
type_conv_info numeric_conversion_function(void) const
octave_value as_double(void) const
octave_value as_single(void) const
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:141
octave_idx_type columns(void) const
Definition: DiagArray2.h:89
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
static int static_type_id(void)
octave_base_value * try_narrowing_conversion(void)
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:135
write the output to stdout if nargout is
Definition: load-save.cc:1612
octave::stream os
Definition: file-io.cc:627
DiagMatrix diag_matrix_value(bool=false) const
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