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 |
6974
|
179 octave_complex::save_ascii (std::ostream& os) |
4687
|
180 { |
|
181 Complex c = complex_value (); |
|
182 |
5958
|
183 octave_write_complex (os, c); |
4687
|
184 |
5958
|
185 os << "\n"; |
4687
|
186 |
|
187 return true; |
|
188 } |
|
189 |
|
190 bool |
|
191 octave_complex::load_ascii (std::istream& is) |
|
192 { |
|
193 scalar = octave_read_complex (is); |
|
194 |
|
195 if (!is) |
|
196 { |
|
197 error ("load: failed to load complex scalar constant"); |
|
198 return false; |
|
199 } |
|
200 |
|
201 return true; |
|
202 } |
|
203 |
|
204 |
|
205 bool |
|
206 octave_complex::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
207 { |
5760
|
208 char tmp = static_cast<char> (LS_DOUBLE); |
|
209 os.write (reinterpret_cast<char *> (&tmp), 1); |
4687
|
210 Complex ctmp = complex_value (); |
5760
|
211 os.write (reinterpret_cast<char *> (&ctmp), 16); |
4687
|
212 |
|
213 return true; |
|
214 } |
|
215 |
|
216 bool |
|
217 octave_complex::load_binary (std::istream& is, bool swap, |
|
218 oct_mach_info::float_format fmt) |
|
219 { |
|
220 char tmp; |
5760
|
221 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
222 return false; |
|
223 |
|
224 Complex ctmp; |
5760
|
225 read_doubles (is, reinterpret_cast<double *> (&ctmp), |
|
226 static_cast<save_type> (tmp), 2, swap, fmt); |
4687
|
227 if (error_state || ! is) |
|
228 return false; |
|
229 |
|
230 scalar = ctmp; |
|
231 return true; |
|
232 } |
|
233 |
|
234 #if defined (HAVE_HDF5) |
4944
|
235 |
4687
|
236 bool |
|
237 octave_complex::save_hdf5 (hid_t loc_id, const char *name, |
|
238 bool /* save_as_floats */) |
|
239 { |
|
240 hsize_t dimens[3]; |
|
241 hid_t space_hid = -1, type_hid = -1, data_hid = -1; |
|
242 bool retval = true; |
|
243 |
4815
|
244 space_hid = H5Screate_simple (0, dimens, 0); |
4837
|
245 if (space_hid < 0) |
|
246 return false; |
4687
|
247 |
|
248 type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
249 if (type_hid < 0) |
|
250 { |
|
251 H5Sclose (space_hid); |
|
252 return false; |
|
253 } |
|
254 |
|
255 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
|
256 if (data_hid < 0) |
|
257 { |
|
258 H5Sclose (space_hid); |
|
259 H5Tclose (type_hid); |
|
260 return false; |
|
261 } |
|
262 |
|
263 Complex tmp = complex_value (); |
|
264 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
265 &tmp) >= 0; |
4687
|
266 |
|
267 H5Dclose (data_hid); |
|
268 H5Tclose (type_hid); |
|
269 H5Sclose (space_hid); |
4837
|
270 |
4687
|
271 return retval; |
|
272 } |
|
273 |
|
274 bool |
|
275 octave_complex::load_hdf5 (hid_t loc_id, const char *name, |
|
276 bool /* have_h5giterate_bug */) |
|
277 { |
|
278 bool retval = false; |
|
279 hid_t data_hid = H5Dopen (loc_id, name); |
|
280 hid_t type_hid = H5Dget_type (data_hid); |
|
281 |
|
282 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
283 |
|
284 if (! hdf5_types_compatible (type_hid, complex_type)) |
|
285 { |
4837
|
286 H5Tclose (complex_type); |
4687
|
287 H5Dclose (data_hid); |
|
288 return false; |
|
289 } |
|
290 |
|
291 hid_t space_id = H5Dget_space (data_hid); |
|
292 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
293 |
|
294 if (rank != 0) |
|
295 { |
4837
|
296 H5Tclose (complex_type); |
4687
|
297 H5Sclose (space_id); |
|
298 H5Dclose (data_hid); |
|
299 return false; |
|
300 } |
|
301 |
|
302 // complex scalar: |
|
303 Complex ctmp; |
|
304 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
305 &ctmp) >= 0) |
4687
|
306 { |
|
307 retval = true; |
|
308 scalar = ctmp; |
|
309 } |
|
310 |
4837
|
311 H5Tclose (complex_type); |
4687
|
312 H5Sclose (space_id); |
|
313 H5Dclose (data_hid); |
4837
|
314 |
4687
|
315 return retval; |
|
316 } |
4944
|
317 |
4687
|
318 #endif |
|
319 |
5900
|
320 mxArray * |
|
321 octave_complex::as_mxArray (void) const |
|
322 { |
|
323 mxArray *retval = new mxArray (mxDOUBLE_CLASS, 1, 1, mxCOMPLEX); |
|
324 |
|
325 double *pr = static_cast<double *> (retval->get_data ()); |
|
326 double *pi = static_cast<double *> (retval->get_imag_data ()); |
|
327 |
|
328 pr[0] = real (scalar); |
|
329 pi[0] = imag (scalar); |
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
2376
|
334 /* |
|
335 ;;; Local Variables: *** |
|
336 ;;; mode: C++ *** |
|
337 ;;; End: *** |
|
338 */ |