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