comparison libinterp/octave-value/ov-complex.cc @ 15195:2fc554ffbc28

split libinterp from src * libinterp: New directory. Move all files from src directory here except Makefile.am, main.cc, main-cli.cc, mkoctfile.in.cc, mkoctfilr.in.sh, octave-config.in.cc, octave-config.in.sh. * libinterp/Makefile.am: New file, extracted from src/Makefile.am. * src/Makefile.am: Delete everything except targets and definitions needed to build and link main and utility programs. * Makefile.am (SUBDIRS): Include libinterp in the list. * autogen.sh: Run config-module.sh in libinterp/dldfcn directory, not src/dldfcn directory. * configure.ac (AC_CONFIG_SRCDIR): Use libinterp/octave.cc, not src/octave.cc. (DL_LDFLAGS, LIBOCTINTERP): Use libinterp, not src. (AC_CONFIG_FILES): Include libinterp/Makefile in the list. * find-docstring-files.sh: Look in libinterp, not src. * gui/src/Makefile.am (liboctgui_la_CPPFLAGS): Find header files in libinterp, not src.
author John W. Eaton <jwe@octave.org>
date Sat, 18 Aug 2012 16:23:39 -0400
parents src/octave-value/ov-complex.cc@62a35ae7d6a2
children 2fac72a256ce
comparison
equal deleted inserted replaced
15194:0f0b795044c3 15195:2fc554ffbc28
1 /*
2
3 Copyright (C) 1996-2012 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <iostream>
28
29 #include "lo-ieee.h"
30 #include "lo-specfun.h"
31 #include "lo-mappers.h"
32
33 #include "mxarray.h"
34 #include "oct-obj.h"
35 #include "oct-stream.h"
36 #include "ops.h"
37 #include "ov-complex.h"
38 #include "ov-flt-complex.h"
39 #include "ov-base.h"
40 #include "ov-base-scalar.h"
41 #include "ov-base-scalar.cc"
42 #include "ov-cx-mat.h"
43 #include "ov-scalar.h"
44 #include "gripes.h"
45 #include "pr-output.h"
46 #include "ops.h"
47
48 #include "ls-oct-ascii.h"
49 #include "ls-hdf5.h"
50
51 template class octave_base_scalar<Complex>;
52
53 DEFINE_OCTAVE_ALLOCATOR (octave_complex);
54
55 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex,
56 "complex scalar", "double");
57
58 static octave_base_value *
59 default_numeric_demotion_function (const octave_base_value& a)
60 {
61 CAST_CONV_ARG (const octave_complex&);
62
63 return new octave_float_complex (v.float_complex_value ());
64 }
65
66 octave_base_value::type_conv_info
67 octave_complex::numeric_demotion_function (void) const
68 {
69 return octave_base_value::type_conv_info (default_numeric_demotion_function,
70 octave_float_complex::static_type_id ());
71 }
72
73 octave_base_value *
74 octave_complex::try_narrowing_conversion (void)
75 {
76 octave_base_value *retval = 0;
77
78 double im = std::imag (scalar);
79
80 if (im == 0.0)
81 retval = new octave_scalar (std::real (scalar));
82
83 return retval;
84 }
85
86 octave_value
87 octave_complex::do_index_op (const octave_value_list& idx, bool resize_ok)
88 {
89 // FIXME -- this doesn't solve the problem of
90 //
91 // a = i; a([1,1], [1,1], [1,1])
92 //
93 // and similar constructions. Hmm...
94
95 // FIXME -- using this constructor avoids narrowing the
96 // 1x1 matrix back to a scalar value. Need a better solution
97 // to this problem.
98
99 octave_value tmp (new octave_complex_matrix (complex_matrix_value ()));
100
101 return tmp.do_index_op (idx, resize_ok);
102 }
103
104 double
105 octave_complex::double_value (bool force_conversion) const
106 {
107 double retval = lo_ieee_nan_value ();
108
109 if (! force_conversion)
110 gripe_implicit_conversion ("Octave:imag-to-real",
111 "complex scalar", "real scalar");
112
113 retval = std::real (scalar);
114
115 return retval;
116 }
117
118 float
119 octave_complex::float_value (bool force_conversion) const
120 {
121 float retval = lo_ieee_float_nan_value ();
122
123 if (! force_conversion)
124 gripe_implicit_conversion ("Octave:imag-to-real",
125 "complex scalar", "real scalar");
126
127 retval = std::real (scalar);
128
129 return retval;
130 }
131
132 Matrix
133 octave_complex::matrix_value (bool force_conversion) const
134 {
135 Matrix retval;
136
137 if (! force_conversion)
138 gripe_implicit_conversion ("Octave:imag-to-real",
139 "complex scalar", "real matrix");
140
141 retval = Matrix (1, 1, std::real (scalar));
142
143 return retval;
144 }
145
146 FloatMatrix
147 octave_complex::float_matrix_value (bool force_conversion) const
148 {
149 FloatMatrix retval;
150
151 if (! force_conversion)
152 gripe_implicit_conversion ("Octave:imag-to-real",
153 "complex scalar", "real matrix");
154
155 retval = FloatMatrix (1, 1, std::real (scalar));
156
157 return retval;
158 }
159
160 NDArray
161 octave_complex::array_value (bool force_conversion) const
162 {
163 NDArray retval;
164
165 if (! force_conversion)
166 gripe_implicit_conversion ("Octave:imag-to-real",
167 "complex scalar", "real matrix");
168
169 retval = NDArray (dim_vector (1, 1), std::real (scalar));
170
171 return retval;
172 }
173
174 FloatNDArray
175 octave_complex::float_array_value (bool force_conversion) const
176 {
177 FloatNDArray retval;
178
179 if (! force_conversion)
180 gripe_implicit_conversion ("Octave:imag-to-real",
181 "complex scalar", "real matrix");
182
183 retval = FloatNDArray (dim_vector (1, 1), std::real (scalar));
184
185 return retval;
186 }
187
188 Complex
189 octave_complex::complex_value (bool) const
190 {
191 return scalar;
192 }
193
194 FloatComplex
195 octave_complex::float_complex_value (bool) const
196 {
197 return static_cast<FloatComplex> (scalar);
198 }
199
200 ComplexMatrix
201 octave_complex::complex_matrix_value (bool) const
202 {
203 return ComplexMatrix (1, 1, scalar);
204 }
205
206 FloatComplexMatrix
207 octave_complex::float_complex_matrix_value (bool) const
208 {
209 return FloatComplexMatrix (1, 1, static_cast<FloatComplex> (scalar));
210 }
211
212 ComplexNDArray
213 octave_complex::complex_array_value (bool /* force_conversion */) const
214 {
215 return ComplexNDArray (dim_vector (1, 1), scalar);
216 }
217
218 FloatComplexNDArray
219 octave_complex::float_complex_array_value (bool /* force_conversion */) const
220 {
221 return FloatComplexNDArray (dim_vector (1, 1), static_cast<FloatComplex> (scalar));
222 }
223
224 octave_value
225 octave_complex::resize (const dim_vector& dv, bool fill) const
226 {
227 if (fill)
228 {
229 ComplexNDArray retval (dv, Complex (0));
230
231 if (dv.numel ())
232 retval(0) = scalar;
233
234 return retval;
235 }
236 else
237 {
238 ComplexNDArray retval (dv);
239
240 if (dv.numel ())
241 retval(0) = scalar;
242
243 return retval;
244 }
245 }
246
247 octave_value
248 octave_complex::diag (octave_idx_type m, octave_idx_type n) const
249 {
250 return ComplexDiagMatrix (Array<Complex> (dim_vector (1, 1), scalar), m, n);
251 }
252
253 bool
254 octave_complex::save_ascii (std::ostream& os)
255 {
256 Complex c = complex_value ();
257
258 octave_write_complex (os, c);
259
260 os << "\n";
261
262 return true;
263 }
264
265 bool
266 octave_complex::load_ascii (std::istream& is)
267 {
268 scalar = octave_read_value<Complex> (is);
269
270 if (!is)
271 {
272 error ("load: failed to load complex scalar constant");
273 return false;
274 }
275
276 return true;
277 }
278
279
280 bool
281 octave_complex::save_binary (std::ostream& os, bool& /* save_as_floats */)
282 {
283 char tmp = static_cast<char> (LS_DOUBLE);
284 os.write (reinterpret_cast<char *> (&tmp), 1);
285 Complex ctmp = complex_value ();
286 os.write (reinterpret_cast<char *> (&ctmp), 16);
287
288 return true;
289 }
290
291 bool
292 octave_complex::load_binary (std::istream& is, bool swap,
293 oct_mach_info::float_format fmt)
294 {
295 char tmp;
296 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
297 return false;
298
299 Complex ctmp;
300 read_doubles (is, reinterpret_cast<double *> (&ctmp),
301 static_cast<save_type> (tmp), 2, swap, fmt);
302 if (error_state || ! is)
303 return false;
304
305 scalar = ctmp;
306 return true;
307 }
308
309 #if defined (HAVE_HDF5)
310
311 bool
312 octave_complex::save_hdf5 (hid_t loc_id, const char *name,
313 bool /* save_as_floats */)
314 {
315 hsize_t dimens[3];
316 hid_t space_hid = -1, type_hid = -1, data_hid = -1;
317 bool retval = true;
318
319 space_hid = H5Screate_simple (0, dimens, 0);
320 if (space_hid < 0)
321 return false;
322
323 type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
324 if (type_hid < 0)
325 {
326 H5Sclose (space_hid);
327 return false;
328 }
329 #if HAVE_HDF5_18
330 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid,
331 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
332 #else
333 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT);
334 #endif
335 if (data_hid < 0)
336 {
337 H5Sclose (space_hid);
338 H5Tclose (type_hid);
339 return false;
340 }
341
342 Complex tmp = complex_value ();
343 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
344 &tmp) >= 0;
345
346 H5Dclose (data_hid);
347 H5Tclose (type_hid);
348 H5Sclose (space_hid);
349
350 return retval;
351 }
352
353 bool
354 octave_complex::load_hdf5 (hid_t loc_id, const char *name)
355 {
356 bool retval = false;
357 #if HAVE_HDF5_18
358 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
359 #else
360 hid_t data_hid = H5Dopen (loc_id, name);
361 #endif
362 hid_t type_hid = H5Dget_type (data_hid);
363
364 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
365
366 if (! hdf5_types_compatible (type_hid, complex_type))
367 {
368 H5Tclose (complex_type);
369 H5Dclose (data_hid);
370 return false;
371 }
372
373 hid_t space_id = H5Dget_space (data_hid);
374 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
375
376 if (rank != 0)
377 {
378 H5Tclose (complex_type);
379 H5Sclose (space_id);
380 H5Dclose (data_hid);
381 return false;
382 }
383
384 // complex scalar:
385 Complex ctmp;
386 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
387 &ctmp) >= 0)
388 {
389 retval = true;
390 scalar = ctmp;
391 }
392
393 H5Tclose (complex_type);
394 H5Sclose (space_id);
395 H5Dclose (data_hid);
396
397 return retval;
398 }
399
400 #endif
401
402 mxArray *
403 octave_complex::as_mxArray (void) const
404 {
405 mxArray *retval = new mxArray (mxDOUBLE_CLASS, 1, 1, mxCOMPLEX);
406
407 double *pr = static_cast<double *> (retval->get_data ());
408 double *pi = static_cast<double *> (retval->get_imag_data ());
409
410 pr[0] = std::real (scalar);
411 pi[0] = std::imag (scalar);
412
413 return retval;
414 }
415
416 octave_value
417 octave_complex::map (unary_mapper_t umap) const
418 {
419 switch (umap)
420 {
421 #define SCALAR_MAPPER(UMAP, FCN) \
422 case umap_ ## UMAP: \
423 return octave_value (FCN (scalar))
424
425 SCALAR_MAPPER (abs, std::abs);
426 SCALAR_MAPPER (acos, ::acos);
427 SCALAR_MAPPER (acosh, ::acosh);
428 SCALAR_MAPPER (angle, std::arg);
429 SCALAR_MAPPER (arg, std::arg);
430 SCALAR_MAPPER (asin, ::asin);
431 SCALAR_MAPPER (asinh, ::asinh);
432 SCALAR_MAPPER (atan, ::atan);
433 SCALAR_MAPPER (atanh, ::atanh);
434 SCALAR_MAPPER (ceil, ::ceil);
435 SCALAR_MAPPER (conj, std::conj);
436 SCALAR_MAPPER (cos, std::cos);
437 SCALAR_MAPPER (cosh, std::cosh);
438 SCALAR_MAPPER (exp, std::exp);
439 SCALAR_MAPPER (expm1, ::expm1);
440 SCALAR_MAPPER (fix, ::fix);
441 SCALAR_MAPPER (floor, ::floor);
442 SCALAR_MAPPER (imag, std::imag);
443 SCALAR_MAPPER (log, std::log);
444 SCALAR_MAPPER (log2, xlog2);
445 SCALAR_MAPPER (log10, std::log10);
446 SCALAR_MAPPER (log1p, ::log1p);
447 SCALAR_MAPPER (real, std::real);
448 SCALAR_MAPPER (round, xround);
449 SCALAR_MAPPER (roundb, xroundb);
450 SCALAR_MAPPER (signum, ::signum);
451 SCALAR_MAPPER (sin, std::sin);
452 SCALAR_MAPPER (sinh, std::sinh);
453 SCALAR_MAPPER (sqrt, std::sqrt);
454 SCALAR_MAPPER (tan, std::tan);
455 SCALAR_MAPPER (tanh, std::tanh);
456 SCALAR_MAPPER (finite, xfinite);
457 SCALAR_MAPPER (isinf, xisinf);
458 SCALAR_MAPPER (isna, octave_is_NA);
459 SCALAR_MAPPER (isnan, xisnan);
460
461 default:
462 return octave_base_value::map (umap);
463 }
464 }