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