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-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-re-diag.h"
30 #include "ov-flt-re-diag.h"
31 #include "ov-base-diag.cc"
32 #include "ov-scalar.h"
33 #include "ov-re-mat.h"
34 #include "ls-utils.h"
35 
37 
39 
41  "double");
42 
43 static octave_base_value *
45 {
47 
48  return new octave_matrix (v.matrix_value ());
49 }
50 
53 {
56 }
57 
58 static octave_base_value *
60 {
62 
63  return new octave_float_diag_matrix (v.float_diag_matrix_value ());
64 }
65 
68 {
72 }
73 
76 {
77  octave_base_value *retval = 0;
78 
79  if (matrix.nelem () == 1)
80  retval = new octave_scalar (matrix (0, 0));
81 
82  return retval;
83 }
84 
87  bool resize_ok)
88 {
89  octave_value retval;
90 
91  // This hack is to allow constructing permutation matrices using
92  // eye(n)(p,:), eye(n)(:,q) && eye(n)(p,q) where p & q are permutation
93  // vectors.
94  if (! resize_ok && idx.length () == 2 && matrix.is_multiple_of_identity (1))
95  {
96  idx_vector idx0 = idx(0).index_vector ();
97  idx_vector idx1 = idx(1).index_vector ();
98 
99  if (! error_state)
100  {
101  bool left = idx0.is_permutation (matrix.rows ());
102  bool right = idx1.is_permutation (matrix.cols ());
103 
104  if (left && right)
105  {
106  if (idx0.is_colon ()) left = false;
107  if (idx1.is_colon ()) right = false;
108  if (left && right)
109  retval = PermMatrix (idx0, false) * PermMatrix (idx1, true);
110  else if (left)
111  retval = PermMatrix (idx0, false);
112  else if (right)
113  retval = PermMatrix (idx1, true);
114  else
115  {
116  retval = this;
117  this->count++;
118  }
119  }
120  }
121  }
122 
123  // if error_state is set, we've already griped.
124  if (! error_state && retval.is_undefined ())
125  retval = octave_base_diag<DiagMatrix, Matrix>::do_index_op (idx, resize_ok);
126 
127  return retval;
128 }
129 
132 {
133  return matrix;
134 }
135 
138 {
139  return FloatDiagMatrix (matrix);
140 }
141 
144 {
145  return ComplexDiagMatrix (matrix);
146 }
147 
150 {
152 }
153 
156 {
157  switch (umap)
158  {
159  case umap_abs:
160  return matrix.abs ();
161  case umap_real:
162  case umap_conj:
163  return matrix;
164  case umap_imag:
165  return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
166  case umap_sqrt:
167  {
169  ComplexDiagMatrix retval (tmp);
170  retval.resize (matrix.rows (), matrix.columns ());
171  return retval;
172  }
173  default:
174  return to_dense ().map (umap);
175  }
176 }
177 
178 bool
179 octave_diag_matrix::save_binary (std::ostream& os, bool& save_as_floats)
180 {
181 
182  int32_t r = matrix.rows (), c = matrix.cols ();
183  os.write (reinterpret_cast<char *> (&r), 4);
184  os.write (reinterpret_cast<char *> (&c), 4);
185 
186  Matrix m = Matrix (matrix.extract_diag ());
187  save_type st = LS_DOUBLE;
188  if (save_as_floats)
189  {
190  if (m.too_large_for_float ())
191  {
192  warning ("save: some values too large to save as floats --");
193  warning ("save: saving as doubles instead");
194  }
195  else
196  st = LS_FLOAT;
197  }
198  else if (matrix.length () > 8192) // FIXME: make this configurable.
199  {
200  double max_val, min_val;
201  if (m.all_integers (max_val, min_val))
202  st = get_save_type (max_val, min_val);
203  }
204 
205  const double *mtmp = m.data ();
206  write_doubles (os, mtmp, st, m.numel ());
207 
208  return true;
209 }
210 
211 bool
212 octave_diag_matrix::load_binary (std::istream& is, bool swap,
214 {
215  int32_t r, c;
216  char tmp;
217  if (! (is.read (reinterpret_cast<char *> (&r), 4)
218  && is.read (reinterpret_cast<char *> (&c), 4)
219  && is.read (reinterpret_cast<char *> (&tmp), 1)))
220  return false;
221  if (swap)
222  {
223  swap_bytes<4> (&r);
224  swap_bytes<4> (&c);
225  }
226 
227  DiagMatrix m (r, c);
228  double *re = m.fortran_vec ();
229  octave_idx_type len = m.length ();
230  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
231  if (error_state || ! is)
232  return false;
233  matrix = m;
234 
235  return true;
236 }
237 
238 bool
240  double& x) const
241 {
242  bool retval = val.is_real_scalar ();
243  if (retval)
244  x = val.double_value ();
245  return retval;
246 }