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 |
2410
|
54 octave_value * |
|
55 octave_complex::try_narrowing_conversion (void) |
|
56 { |
|
57 octave_value *retval = 0; |
|
58 |
|
59 if (imag (scalar) == 0.0) |
3775
|
60 retval = new octave_scalar (real (scalar)); |
2410
|
61 |
|
62 return retval; |
|
63 } |
|
64 |
2376
|
65 octave_value |
3933
|
66 octave_complex::do_index_op (const octave_value_list& idx, int resize_ok) |
2376
|
67 { |
|
68 octave_value retval; |
|
69 |
3933
|
70 if (idx.valid_scalar_indices ()) |
2376
|
71 retval = scalar; |
|
72 else |
|
73 { |
|
74 // XXX FIXME XXX -- this doesn't solve the problem of |
|
75 // |
|
76 // a = i; a([1,1], [1,1], [1,1]) |
|
77 // |
|
78 // and similar constructions. Hmm... |
|
79 |
2423
|
80 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
81 // 1x1 matrix back to a scalar value. Need a better solution |
|
82 // to this problem. |
|
83 |
|
84 octave_value tmp (new octave_complex_matrix (complex_matrix_value ())); |
2376
|
85 |
3933
|
86 retval = tmp.do_index_op (idx, resize_ok); |
2376
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
|
92 double |
|
93 octave_complex::double_value (bool force_conversion) const |
|
94 { |
4102
|
95 double retval = lo_ieee_nan_value (); |
2376
|
96 |
4451
|
97 if (! force_conversion && Vwarn_imag_to_real) |
2376
|
98 gripe_implicit_conversion ("complex scalar", "real scalar"); |
|
99 |
4451
|
100 retval = std::real (scalar); |
2376
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 Matrix |
|
106 octave_complex::matrix_value (bool force_conversion) const |
|
107 { |
|
108 Matrix retval; |
|
109 |
4451
|
110 if (! force_conversion && Vwarn_imag_to_real) |
2376
|
111 gripe_implicit_conversion ("complex scalar", "real matrix"); |
|
112 |
4451
|
113 retval = Matrix (1, 1, std::real (scalar)); |
2376
|
114 |
|
115 return retval; |
|
116 } |
|
117 |
4569
|
118 NDArray |
|
119 octave_complex::array_value (bool force_conversion) const |
|
120 { |
|
121 NDArray retval; |
|
122 |
|
123 if (! force_conversion && Vwarn_imag_to_real) |
|
124 gripe_implicit_conversion ("complex scalar", "real matrix"); |
|
125 |
|
126 retval = NDArray (dim_vector (1, 1), std::real (scalar)); |
|
127 |
|
128 return retval; |
|
129 } |
|
130 |
2376
|
131 Complex |
|
132 octave_complex::complex_value (bool) const |
|
133 { |
|
134 return scalar; |
|
135 } |
|
136 |
|
137 |
|
138 ComplexMatrix |
|
139 octave_complex::complex_matrix_value (bool) const |
|
140 { |
|
141 return ComplexMatrix (1, 1, scalar); |
|
142 } |
|
143 |
4569
|
144 ComplexNDArray |
4664
|
145 octave_complex::complex_array_value (bool /* force_conversion */) const |
4569
|
146 { |
|
147 return ComplexNDArray (dim_vector (1, 1), scalar); |
|
148 } |
|
149 |
4687
|
150 bool |
|
151 octave_complex::save_ascii (std::ostream& os, bool& infnan_warned, |
|
152 bool strip_nan_and_inf) |
|
153 { |
|
154 Complex c = complex_value (); |
|
155 |
|
156 if (strip_nan_and_inf) |
|
157 { |
|
158 if (xisnan (c)) |
|
159 { |
|
160 error ("only value to plot is NaN"); |
|
161 return false; |
|
162 } |
|
163 else |
|
164 { |
|
165 double re = real (c); |
|
166 double im = imag (c); |
|
167 |
|
168 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
169 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
170 |
|
171 c = Complex (re, im); |
|
172 |
|
173 octave_write_complex (os, c); |
|
174 os << "\n"; |
|
175 } |
|
176 } |
|
177 else |
|
178 { |
|
179 if (! infnan_warned && (xisnan (c) || xisinf (c))) |
|
180 { |
|
181 warning ("save: Inf or NaN values may not be reloadable"); |
|
182 infnan_warned = true; |
|
183 } |
|
184 |
|
185 octave_write_complex (os, c); |
|
186 os << "\n"; |
|
187 } |
|
188 |
|
189 return true; |
|
190 } |
|
191 |
|
192 bool |
|
193 octave_complex::load_ascii (std::istream& is) |
|
194 { |
|
195 scalar = octave_read_complex (is); |
|
196 |
|
197 if (!is) |
|
198 { |
|
199 error ("load: failed to load complex scalar constant"); |
|
200 return false; |
|
201 } |
|
202 |
|
203 return true; |
|
204 } |
|
205 |
|
206 |
|
207 bool |
|
208 octave_complex::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
209 { |
|
210 char tmp = (char) LS_DOUBLE; |
|
211 os.write (X_CAST (char *, &tmp), 1); |
|
212 Complex ctmp = complex_value (); |
|
213 os.write (X_CAST (char *, &ctmp), 16); |
|
214 |
|
215 return true; |
|
216 } |
|
217 |
|
218 bool |
|
219 octave_complex::load_binary (std::istream& is, bool swap, |
|
220 oct_mach_info::float_format fmt) |
|
221 { |
|
222 char tmp; |
|
223 if (! is.read (X_CAST (char *, &tmp), 1)) |
|
224 return false; |
|
225 |
|
226 Complex ctmp; |
|
227 read_doubles (is, X_CAST (double *, &ctmp), X_CAST (save_type, tmp), 2, |
|
228 swap, fmt); |
|
229 if (error_state || ! is) |
|
230 return false; |
|
231 |
|
232 scalar = ctmp; |
|
233 return true; |
|
234 } |
|
235 |
|
236 #if defined (HAVE_HDF5) |
4944
|
237 |
4687
|
238 bool |
|
239 octave_complex::save_hdf5 (hid_t loc_id, const char *name, |
|
240 bool /* save_as_floats */) |
|
241 { |
|
242 hsize_t dimens[3]; |
|
243 hid_t space_hid = -1, type_hid = -1, data_hid = -1; |
|
244 bool retval = true; |
|
245 |
4815
|
246 space_hid = H5Screate_simple (0, dimens, 0); |
4837
|
247 if (space_hid < 0) |
|
248 return false; |
4687
|
249 |
|
250 type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
251 if (type_hid < 0) |
|
252 { |
|
253 H5Sclose (space_hid); |
|
254 return false; |
|
255 } |
|
256 |
|
257 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
|
258 if (data_hid < 0) |
|
259 { |
|
260 H5Sclose (space_hid); |
|
261 H5Tclose (type_hid); |
|
262 return false; |
|
263 } |
|
264 |
|
265 Complex tmp = complex_value (); |
|
266 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
267 &tmp) >= 0; |
4687
|
268 |
|
269 H5Dclose (data_hid); |
|
270 H5Tclose (type_hid); |
|
271 H5Sclose (space_hid); |
4837
|
272 |
4687
|
273 return retval; |
|
274 } |
|
275 |
|
276 bool |
|
277 octave_complex::load_hdf5 (hid_t loc_id, const char *name, |
|
278 bool /* have_h5giterate_bug */) |
|
279 { |
|
280 bool retval = false; |
|
281 hid_t data_hid = H5Dopen (loc_id, name); |
|
282 hid_t type_hid = H5Dget_type (data_hid); |
|
283 |
|
284 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
285 |
|
286 if (! hdf5_types_compatible (type_hid, complex_type)) |
|
287 { |
4837
|
288 H5Tclose (complex_type); |
4687
|
289 H5Dclose (data_hid); |
|
290 return false; |
|
291 } |
|
292 |
|
293 hid_t space_id = H5Dget_space (data_hid); |
|
294 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
295 |
|
296 if (rank != 0) |
|
297 { |
4837
|
298 H5Tclose (complex_type); |
4687
|
299 H5Sclose (space_id); |
|
300 H5Dclose (data_hid); |
|
301 return false; |
|
302 } |
|
303 |
|
304 // complex scalar: |
|
305 Complex ctmp; |
|
306 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
307 &ctmp) >= 0) |
4687
|
308 { |
|
309 retval = true; |
|
310 scalar = ctmp; |
|
311 } |
|
312 |
4837
|
313 H5Tclose (complex_type); |
4687
|
314 H5Sclose (space_id); |
|
315 H5Dclose (data_hid); |
4837
|
316 |
4687
|
317 return retval; |
|
318 } |
4944
|
319 |
4687
|
320 #endif |
|
321 |
2376
|
322 /* |
|
323 ;;; Local Variables: *** |
|
324 ;;; mode: C++ *** |
|
325 ;;; End: *** |
|
326 */ |