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-re-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-re-diag.h"
30 #include "ov-base-diag.cc"
31 #include "ov-float.h"
32 #include "ov-flt-re-mat.h"
33 #include "ls-utils.h"
34 
36 
37 
39  "float diagonal matrix", "single");
40 
41 static octave_base_value *
43 {
45 
46  return new octave_float_matrix (v.float_matrix_value ());
47 }
48 
51 {
54 }
55 
58 {
59  octave_base_value *retval = 0;
60 
61  if (matrix.nelem () == 1)
62  retval = new octave_float_scalar (matrix (0, 0));
63 
64  return retval;
65 }
66 
69 {
70  return DiagMatrix (matrix);
71 }
72 
75 {
76  return matrix;
77 }
78 
81 {
82  return ComplexDiagMatrix (matrix);
83 }
84 
87 {
89 }
90 
93 {
94  switch (umap)
95  {
96  case umap_abs:
97  return matrix.abs ();
98  case umap_real:
99  case umap_conj:
100  return matrix;
101  case umap_imag:
102  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
103  case umap_sqrt:
104  {
106  (rc_sqrt);
107  FloatComplexDiagMatrix retval (tmp);
108  retval.resize (matrix.rows (), matrix.columns ());
109  return retval;
110  }
111  default:
112  return to_dense ().map (umap);
113  }
114 }
115 
116 bool
118  bool& /* save_as_floats*/)
119 {
120 
121  int32_t r = matrix.rows ();
122  int32_t c = matrix.cols ();
123  os.write (reinterpret_cast<char *> (&r), 4);
124  os.write (reinterpret_cast<char *> (&c), 4);
125 
127  save_type st = LS_FLOAT;
128  if (matrix.length () > 8192) // FIXME: make this configurable.
129  {
130  float max_val, min_val;
131  if (m.all_integers (max_val, min_val))
132  st = get_save_type (max_val, min_val);
133  }
134 
135  const float *mtmp = m.data ();
136  write_floats (os, mtmp, st, m.numel ());
137 
138  return true;
139 }
140 
141 bool
142 octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
144 {
145  int32_t r, c;
146  char tmp;
147  if (! (is.read (reinterpret_cast<char *> (&r), 4)
148  && is.read (reinterpret_cast<char *> (&c), 4)
149  && is.read (reinterpret_cast<char *> (&tmp), 1)))
150  return false;
151  if (swap)
152  {
153  swap_bytes<4> (&r);
154  swap_bytes<4> (&c);
155  }
156 
157  FloatDiagMatrix m (r, c);
158  float *re = m.fortran_vec ();
159  octave_idx_type len = m.length ();
160  read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
161  if (error_state || ! is)
162  return false;
163  matrix = m;
164 
165  return true;
166 }
167 
168 bool
170  float& x) const
171 {
172  bool retval = val.is_real_scalar ();
173  if (retval)
174  x = val.float_value ();
175  return retval;
176 }
save_type
Definition: data-conv.h:83
type_conv_info numeric_conversion_function(void) const
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:943
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
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
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
FloatDiagMatrix abs(void) const
Definition: fDiagMatrix.cc:142
float float_value(bool frc_str_conv=false) const
Definition: ov.h:762
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
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
ComplexDiagMatrix complex_diag_matrix_value(bool=false) 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
bool chk_valid_scalar(const octave_value &, float &) const
octave_value map(unary_mapper_t umap) const
const T * data(void) const
Definition: Array.h:479
int error_state
Definition: error.cc:101
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:606
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
FloatColumnVector extract_diag(octave_idx_type k=0) const
Definition: fDiagMatrix.h:106
DiagMatrix diag_matrix_value(bool=false) const
octave_idx_type cols(void) const
Definition: DiagArray2.h:87
bool save_binary(std::ostream &os, bool &save_as_floats)
octave_idx_type columns(void) const
Definition: DiagArray2.h:88
bool all_integers(float &max_val, float &min_val) const
Definition: fNDArray.cc:557
static int static_type_id(void)
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
octave_base_value * try_narrowing_conversion(void)
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