2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
2901
|
29 |
2376
|
30 #include "lo-ieee.h" |
|
31 |
|
32 #include "oct-obj.h" |
4944
|
33 #include "oct-stream.h" |
2410
|
34 #include "ops.h" |
2376
|
35 #include "ov-complex.h" |
3223
|
36 #include "ov-base.h" |
|
37 #include "ov-base-scalar.h" |
|
38 #include "ov-base-scalar.cc" |
2423
|
39 #include "ov-cx-mat.h" |
2410
|
40 #include "ov-scalar.h" |
2376
|
41 #include "gripes.h" |
|
42 #include "pr-output.h" |
|
43 |
4687
|
44 #include "ls-oct-ascii.h" |
|
45 #include "ls-hdf5.h" |
|
46 |
3223
|
47 template class octave_base_scalar<Complex>; |
|
48 |
3219
|
49 DEFINE_OCTAVE_ALLOCATOR (octave_complex); |
2376
|
50 |
4612
|
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex, |
|
52 "complex scalar", "double"); |
2376
|
53 |
5759
|
54 octave_base_value * |
2410
|
55 octave_complex::try_narrowing_conversion (void) |
|
56 { |
5759
|
57 octave_base_value *retval = 0; |
2410
|
58 |
5450
|
59 double im = std::imag (scalar); |
|
60 |
|
61 if (im == 0.0 && ! lo_ieee_signbit (im)) |
3775
|
62 retval = new octave_scalar (real (scalar)); |
2410
|
63 |
|
64 return retval; |
|
65 } |
|
66 |
2376
|
67 octave_value |
5885
|
68 octave_complex::do_index_op (const octave_value_list& idx, bool resize_ok) |
2376
|
69 { |
|
70 octave_value retval; |
|
71 |
3933
|
72 if (idx.valid_scalar_indices ()) |
2376
|
73 retval = scalar; |
|
74 else |
|
75 { |
5775
|
76 // FIXME -- this doesn't solve the problem of |
2376
|
77 // |
|
78 // a = i; a([1,1], [1,1], [1,1]) |
|
79 // |
|
80 // and similar constructions. Hmm... |
|
81 |
5775
|
82 // FIXME -- using this constructor avoids narrowing the |
2423
|
83 // 1x1 matrix back to a scalar value. Need a better solution |
|
84 // to this problem. |
|
85 |
|
86 octave_value tmp (new octave_complex_matrix (complex_matrix_value ())); |
2376
|
87 |
3933
|
88 retval = tmp.do_index_op (idx, resize_ok); |
2376
|
89 } |
|
90 |
|
91 return retval; |
|
92 } |
|
93 |
|
94 double |
|
95 octave_complex::double_value (bool force_conversion) const |
|
96 { |
4102
|
97 double retval = lo_ieee_nan_value (); |
2376
|
98 |
5781
|
99 if (! force_conversion) |
|
100 gripe_implicit_conversion ("Octave:imag-to-real", |
|
101 "complex scalar", "real scalar"); |
2376
|
102 |
4451
|
103 retval = std::real (scalar); |
2376
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 Matrix |
|
109 octave_complex::matrix_value (bool force_conversion) const |
|
110 { |
|
111 Matrix retval; |
|
112 |
5781
|
113 if (! force_conversion) |
|
114 gripe_implicit_conversion ("Octave:imag-to-real", |
|
115 "complex scalar", "real matrix"); |
2376
|
116 |
4451
|
117 retval = Matrix (1, 1, std::real (scalar)); |
2376
|
118 |
|
119 return retval; |
|
120 } |
|
121 |
4569
|
122 NDArray |
|
123 octave_complex::array_value (bool force_conversion) const |
|
124 { |
|
125 NDArray retval; |
|
126 |
5781
|
127 if (! force_conversion) |
|
128 gripe_implicit_conversion ("Octave:imag-to-real", |
|
129 "complex scalar", "real matrix"); |
4569
|
130 |
|
131 retval = NDArray (dim_vector (1, 1), std::real (scalar)); |
|
132 |
|
133 return retval; |
|
134 } |
|
135 |
2376
|
136 Complex |
|
137 octave_complex::complex_value (bool) const |
|
138 { |
|
139 return scalar; |
|
140 } |
|
141 |
|
142 |
|
143 ComplexMatrix |
|
144 octave_complex::complex_matrix_value (bool) const |
|
145 { |
|
146 return ComplexMatrix (1, 1, scalar); |
|
147 } |
|
148 |
4569
|
149 ComplexNDArray |
4664
|
150 octave_complex::complex_array_value (bool /* force_conversion */) const |
4569
|
151 { |
|
152 return ComplexNDArray (dim_vector (1, 1), scalar); |
|
153 } |
|
154 |
5731
|
155 octave_value |
|
156 octave_complex::resize (const dim_vector& dv, bool fill) const |
|
157 { |
|
158 if (fill) |
|
159 { |
|
160 ComplexNDArray retval (dv, ComplexNDArray::resize_fill_value ()); |
|
161 |
|
162 if (dv.numel ()) |
|
163 retval(0) = scalar; |
|
164 |
|
165 return retval; |
|
166 } |
|
167 else |
|
168 { |
|
169 ComplexNDArray retval (dv); |
|
170 |
|
171 if (dv.numel ()) |
|
172 retval(0) = scalar; |
|
173 |
|
174 return retval; |
|
175 } |
|
176 } |
|
177 |
4687
|
178 bool |
|
179 octave_complex::save_ascii (std::ostream& os, bool& infnan_warned, |
|
180 bool strip_nan_and_inf) |
|
181 { |
|
182 Complex c = complex_value (); |
|
183 |
|
184 if (strip_nan_and_inf) |
|
185 { |
|
186 if (xisnan (c)) |
|
187 { |
|
188 error ("only value to plot is NaN"); |
|
189 return false; |
|
190 } |
|
191 else |
|
192 { |
|
193 double re = real (c); |
|
194 double im = imag (c); |
|
195 |
|
196 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
197 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
198 |
|
199 c = Complex (re, im); |
|
200 |
|
201 octave_write_complex (os, c); |
|
202 os << "\n"; |
|
203 } |
|
204 } |
|
205 else |
|
206 { |
|
207 if (! infnan_warned && (xisnan (c) || xisinf (c))) |
|
208 { |
|
209 warning ("save: Inf or NaN values may not be reloadable"); |
|
210 infnan_warned = true; |
|
211 } |
|
212 |
|
213 octave_write_complex (os, c); |
|
214 os << "\n"; |
|
215 } |
|
216 |
|
217 return true; |
|
218 } |
|
219 |
|
220 bool |
|
221 octave_complex::load_ascii (std::istream& is) |
|
222 { |
|
223 scalar = octave_read_complex (is); |
|
224 |
|
225 if (!is) |
|
226 { |
|
227 error ("load: failed to load complex scalar constant"); |
|
228 return false; |
|
229 } |
|
230 |
|
231 return true; |
|
232 } |
|
233 |
|
234 |
|
235 bool |
|
236 octave_complex::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
237 { |
5760
|
238 char tmp = static_cast<char> (LS_DOUBLE); |
|
239 os.write (reinterpret_cast<char *> (&tmp), 1); |
4687
|
240 Complex ctmp = complex_value (); |
5760
|
241 os.write (reinterpret_cast<char *> (&ctmp), 16); |
4687
|
242 |
|
243 return true; |
|
244 } |
|
245 |
|
246 bool |
|
247 octave_complex::load_binary (std::istream& is, bool swap, |
|
248 oct_mach_info::float_format fmt) |
|
249 { |
|
250 char tmp; |
5760
|
251 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
252 return false; |
|
253 |
|
254 Complex ctmp; |
5760
|
255 read_doubles (is, reinterpret_cast<double *> (&ctmp), |
|
256 static_cast<save_type> (tmp), 2, swap, fmt); |
4687
|
257 if (error_state || ! is) |
|
258 return false; |
|
259 |
|
260 scalar = ctmp; |
|
261 return true; |
|
262 } |
|
263 |
|
264 #if defined (HAVE_HDF5) |
4944
|
265 |
4687
|
266 bool |
|
267 octave_complex::save_hdf5 (hid_t loc_id, const char *name, |
|
268 bool /* save_as_floats */) |
|
269 { |
|
270 hsize_t dimens[3]; |
|
271 hid_t space_hid = -1, type_hid = -1, data_hid = -1; |
|
272 bool retval = true; |
|
273 |
4815
|
274 space_hid = H5Screate_simple (0, dimens, 0); |
4837
|
275 if (space_hid < 0) |
|
276 return false; |
4687
|
277 |
|
278 type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
279 if (type_hid < 0) |
|
280 { |
|
281 H5Sclose (space_hid); |
|
282 return false; |
|
283 } |
|
284 |
|
285 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
|
286 if (data_hid < 0) |
|
287 { |
|
288 H5Sclose (space_hid); |
|
289 H5Tclose (type_hid); |
|
290 return false; |
|
291 } |
|
292 |
|
293 Complex tmp = complex_value (); |
|
294 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
295 &tmp) >= 0; |
4687
|
296 |
|
297 H5Dclose (data_hid); |
|
298 H5Tclose (type_hid); |
|
299 H5Sclose (space_hid); |
4837
|
300 |
4687
|
301 return retval; |
|
302 } |
|
303 |
|
304 bool |
|
305 octave_complex::load_hdf5 (hid_t loc_id, const char *name, |
|
306 bool /* have_h5giterate_bug */) |
|
307 { |
|
308 bool retval = false; |
|
309 hid_t data_hid = H5Dopen (loc_id, name); |
|
310 hid_t type_hid = H5Dget_type (data_hid); |
|
311 |
|
312 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
313 |
|
314 if (! hdf5_types_compatible (type_hid, complex_type)) |
|
315 { |
4837
|
316 H5Tclose (complex_type); |
4687
|
317 H5Dclose (data_hid); |
|
318 return false; |
|
319 } |
|
320 |
|
321 hid_t space_id = H5Dget_space (data_hid); |
|
322 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
323 |
|
324 if (rank != 0) |
|
325 { |
4837
|
326 H5Tclose (complex_type); |
4687
|
327 H5Sclose (space_id); |
|
328 H5Dclose (data_hid); |
|
329 return false; |
|
330 } |
|
331 |
|
332 // complex scalar: |
|
333 Complex ctmp; |
|
334 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
335 &ctmp) >= 0) |
4687
|
336 { |
|
337 retval = true; |
|
338 scalar = ctmp; |
|
339 } |
|
340 |
4837
|
341 H5Tclose (complex_type); |
4687
|
342 H5Sclose (space_id); |
|
343 H5Dclose (data_hid); |
4837
|
344 |
4687
|
345 return retval; |
|
346 } |
4944
|
347 |
4687
|
348 #endif |
|
349 |
2376
|
350 /* |
|
351 ;;; Local Variables: *** |
|
352 ;;; mode: C++ *** |
|
353 ;;; End: *** |
|
354 */ |