Mercurial > hg > octave-nkf
comparison src/ov-complex.cc @ 4687:e95c86d48732
[project @ 2004-01-06 21:53:34 by jwe]
author | jwe |
---|---|
date | Tue, 06 Jan 2004 21:53:34 +0000 |
parents | 19bcdc6a9bb8 |
children | 2eb844b27953 |
comparison
equal
deleted
inserted
replaced
4686:c7ae43dfdea4 | 4687:e95c86d48732 |
---|---|
41 #include "ov-cx-mat.h" | 41 #include "ov-cx-mat.h" |
42 #include "ov-scalar.h" | 42 #include "ov-scalar.h" |
43 #include "gripes.h" | 43 #include "gripes.h" |
44 #include "pr-output.h" | 44 #include "pr-output.h" |
45 | 45 |
46 #include "ls-oct-ascii.h" | |
47 #include "ls-hdf5.h" | |
48 | |
46 template class octave_base_scalar<Complex>; | 49 template class octave_base_scalar<Complex>; |
47 | 50 |
48 DEFINE_OCTAVE_ALLOCATOR (octave_complex); | 51 DEFINE_OCTAVE_ALLOCATOR (octave_complex); |
49 | 52 |
50 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex, | 53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex, |
144 octave_complex::complex_array_value (bool /* force_conversion */) const | 147 octave_complex::complex_array_value (bool /* force_conversion */) const |
145 { | 148 { |
146 return ComplexNDArray (dim_vector (1, 1), scalar); | 149 return ComplexNDArray (dim_vector (1, 1), scalar); |
147 } | 150 } |
148 | 151 |
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 | |
149 /* | 319 /* |
150 ;;; Local Variables: *** | 320 ;;; Local Variables: *** |
151 ;;; mode: C++ *** | 321 ;;; mode: C++ *** |
152 ;;; End: *** | 322 ;;; End: *** |
153 */ | 323 */ |