1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 #  include "config.h"
28 #endif
29 
30 #include "byte-swap.h"
31 
32 #include "ov-flt-re-diag.h"
33 #include "ov-base-diag.cc"
34 #include "ov-float.h"
35 #include "ov-flt-re-mat.h"
36 #include "ls-utils.h"
37 
38 
39 template class octave_base_diag<FloatDiagMatrix, FloatMatrix>;
40 
41 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_float_diag_matrix,
42                                      "float diagonal matrix", "single");
43 
44 static octave_base_value *
default_numeric_conversion_function(const octave_base_value & a)45 default_numeric_conversion_function (const octave_base_value& a)
46 {
47   const octave_float_diag_matrix& v
48     = dynamic_cast<const octave_float_diag_matrix&> (a);
49 
50   return new octave_float_matrix (v.float_matrix_value ());
51 }
52 
53 octave_base_value::type_conv_info
numeric_conversion_function(void) const54 octave_float_diag_matrix::numeric_conversion_function (void) const
55 {
56   return octave_base_value::type_conv_info (default_numeric_conversion_function,
57                                             octave_float_matrix::static_type_id ());
58 }
59 
60 octave_base_value *
try_narrowing_conversion(void)61 octave_float_diag_matrix::try_narrowing_conversion (void)
62 {
63   octave_base_value *retval = nullptr;
64 
65   if (matrix.nelem () == 1)
66     retval = new octave_float_scalar (matrix (0, 0));
67 
68   return retval;
69 }
70 
71 DiagMatrix
diag_matrix_value(bool) const72 octave_float_diag_matrix::diag_matrix_value (bool) const
73 {
74   return DiagMatrix (matrix);
75 }
76 
77 FloatDiagMatrix
float_diag_matrix_value(bool) const78 octave_float_diag_matrix::float_diag_matrix_value (bool) const
79 {
80   return matrix;
81 }
82 
83 ComplexDiagMatrix
complex_diag_matrix_value(bool) const84 octave_float_diag_matrix::complex_diag_matrix_value (bool) const
85 {
86   return ComplexDiagMatrix (matrix);
87 }
88 
89 FloatComplexDiagMatrix
float_complex_diag_matrix_value(bool) const90 octave_float_diag_matrix::float_complex_diag_matrix_value (bool) const
91 {
92   return FloatComplexDiagMatrix (matrix);
93 }
94 
95 octave_value
as_double(void) const96 octave_float_diag_matrix::as_double (void) const
97 {
98   return DiagMatrix (matrix);
99 }
100 
101 octave_value
as_single(void) const102 octave_float_diag_matrix::as_single (void) const
103 {
104   return matrix;
105 }
106 
107 octave_value
as_int8(void) const108 octave_float_diag_matrix::as_int8 (void) const
109 {
110   return int8_array_value ();
111 }
112 
113 octave_value
as_int16(void) const114 octave_float_diag_matrix::as_int16 (void) const
115 {
116   return int16_array_value ();
117 }
118 
119 octave_value
as_int32(void) const120 octave_float_diag_matrix::as_int32 (void) const
121 {
122   return int32_array_value ();
123 }
124 
125 octave_value
as_int64(void) const126 octave_float_diag_matrix::as_int64 (void) const
127 {
128   return int64_array_value ();
129 }
130 
131 octave_value
as_uint8(void) const132 octave_float_diag_matrix::as_uint8 (void) const
133 {
134   return uint8_array_value ();
135 }
136 
137 octave_value
as_uint16(void) const138 octave_float_diag_matrix::as_uint16 (void) const
139 {
140   return uint16_array_value ();
141 }
142 
143 octave_value
as_uint32(void) const144 octave_float_diag_matrix::as_uint32 (void) const
145 {
146   return uint32_array_value ();
147 }
148 
149 octave_value
as_uint64(void) const150 octave_float_diag_matrix::as_uint64 (void) const
151 {
152   return uint64_array_value ();
153 }
154 
155 octave_value
map(unary_mapper_t umap) const156 octave_float_diag_matrix::map (unary_mapper_t umap) const
157 {
158   switch (umap)
159     {
160     case umap_abs:
161       return matrix.abs ();
162     case umap_real:
163     case umap_conj:
164       return matrix;
165     case umap_imag:
166       return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
167     case umap_sqrt:
168       {
169         FloatComplexColumnVector tmp = matrix.extract_diag ().map<FloatComplex>
170                                        (octave::math::rc_sqrt);
171         FloatComplexDiagMatrix retval (tmp);
172         retval.resize (matrix.rows (), matrix.columns ());
173         return retval;
174       }
175     default:
176       return to_dense ().map (umap);
177     }
178 }
179 
180 bool
save_binary(std::ostream & os,bool)181 octave_float_diag_matrix::save_binary (std::ostream& os,
182                                        bool /* save_as_floats*/)
183 {
184 
185   int32_t r = matrix.rows ();
186   int32_t c = matrix.cols ();
187   os.write (reinterpret_cast<char *> (&r), 4);
188   os.write (reinterpret_cast<char *> (&c), 4);
189 
190   FloatMatrix m = FloatMatrix (matrix.extract_diag ());
191   save_type st = LS_FLOAT;
192   if (matrix.length () > 8192) // FIXME: make this configurable.
193     {
194       float max_val, min_val;
195       if (m.all_integers (max_val, min_val))
196         st = get_save_type (max_val, min_val);
197     }
198 
199   const float *mtmp = m.data ();
200   write_floats (os, mtmp, st, m.numel ());
201 
202   return true;
203 }
204 
205 bool
load_binary(std::istream & is,bool swap,octave::mach_info::float_format fmt)206 octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
207                                        octave::mach_info::float_format fmt)
208 {
209   int32_t r, c;
210   char tmp;
211   if (! (is.read (reinterpret_cast<char *> (&r), 4)
212          && is.read (reinterpret_cast<char *> (&c), 4)
213          && is.read (reinterpret_cast<char *> (&tmp), 1)))
214     return false;
215   if (swap)
216     {
217       swap_bytes<4> (&r);
218       swap_bytes<4> (&c);
219     }
220 
221   FloatDiagMatrix m (r, c);
222   float *re = m.fortran_vec ();
223   octave_idx_type len = m.length ();
224   read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
225 
226   if (! is)
227     return false;
228 
229   matrix = m;
230 
231   return true;
232 }
233 
234 bool
chk_valid_scalar(const octave_value & val,float & x) const235 octave_float_diag_matrix::chk_valid_scalar (const octave_value& val,
236                                             float& x) const
237 {
238   bool retval = val.is_real_scalar ();
239   if (retval)
240     x = val.float_value ();
241   return retval;
242 }
243