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-re-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-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 
38 
40  "float diagonal matrix", "single");
41 
42 static octave_base_value *
44 {
46 
47  return new octave_float_matrix (v.float_matrix_value ());
48 }
49 
52 {
55 }
56 
59 {
60  octave_base_value *retval = 0;
61 
62  if (matrix.nelem () == 1)
63  retval = new octave_float_scalar (matrix (0, 0));
64 
65  return retval;
66 }
67 
70 {
71  return DiagMatrix (matrix);
72 }
73 
76 {
77  return matrix;
78 }
79 
82 {
83  return ComplexDiagMatrix (matrix);
84 }
85 
88 {
90 }
91 
94 {
95  switch (umap)
96  {
97  case umap_abs:
98  return matrix.abs ();
99  case umap_real:
100  case umap_conj:
101  return matrix;
102  case umap_imag:
103  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
104  case umap_sqrt:
105  {
107  (rc_sqrt);
108  FloatComplexDiagMatrix retval (tmp);
109  retval.resize (matrix.rows (), matrix.columns ());
110  return retval;
111  }
112  default:
113  return to_dense ().map (umap);
114  }
115 }
116 
117 bool
119  bool& /* save_as_floats*/)
120 {
121 
122  int32_t r = matrix.rows (), 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 }