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