GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-complex.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 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-hdf5.h"
36 #include "oct-stream.h"
37 #include "ops.h"
38 #include "ov-complex.h"
39 #include "ov-flt-complex.h"
40 #include "ov-base.h"
41 #include "ov-base-scalar.h"
42 #include "ov-base-scalar.cc"
43 #include "ov-cx-mat.h"
44 #include "ov-scalar.h"
45 #include "gripes.h"
46 #include "pr-output.h"
47 #include "ops.h"
48 
49 #include "ls-oct-ascii.h"
50 #include "ls-hdf5.h"
51 
52 template class octave_base_scalar<Complex>;
53 
54 
56  "complex scalar", "double");
57 
58 static octave_base_value *
60 {
62 
63  return new octave_float_complex (v.float_complex_value ());
64 }
65 
68 {
69  return
72 }
73 
76 {
77  octave_base_value *retval = 0;
78 
79  double im = std::imag (scalar);
80 
81  if (im == 0.0)
82  retval = new octave_scalar (std::real (scalar));
83 
84  return retval;
85 }
86 
88 octave_complex::do_index_op (const octave_value_list& idx, bool resize_ok)
89 {
90  // FIXME: this doesn't solve the problem of
91  //
92  // a = i; a([1,1], [1,1], [1,1])
93  //
94  // and similar constructions. Hmm...
95 
96  // FIXME: using this constructor avoids narrowing the
97  // 1x1 matrix back to a scalar value. Need a better solution
98  // to this problem.
99 
101 
102  return tmp.do_index_op (idx, resize_ok);
103 }
104 
105 double
106 octave_complex::double_value (bool force_conversion) const
107 {
108  double retval;
109 
110  if (! force_conversion)
111  gripe_implicit_conversion ("Octave:imag-to-real",
112  "complex scalar", "real scalar");
113 
114  retval = std::real (scalar);
115 
116  return retval;
117 }
118 
119 float
120 octave_complex::float_value (bool force_conversion) const
121 {
122  float retval;
123 
124  if (! force_conversion)
125  gripe_implicit_conversion ("Octave:imag-to-real",
126  "complex scalar", "real scalar");
127 
128  retval = std::real (scalar);
129 
130  return retval;
131 }
132 
133 Matrix
134 octave_complex::matrix_value (bool force_conversion) const
135 {
136  Matrix retval;
137 
138  if (! force_conversion)
139  gripe_implicit_conversion ("Octave:imag-to-real",
140  "complex scalar", "real matrix");
141 
142  retval = Matrix (1, 1, std::real (scalar));
143 
144  return retval;
145 }
146 
148 octave_complex::float_matrix_value (bool force_conversion) const
149 {
150  FloatMatrix retval;
151 
152  if (! force_conversion)
153  gripe_implicit_conversion ("Octave:imag-to-real",
154  "complex scalar", "real matrix");
155 
156  retval = FloatMatrix (1, 1, std::real (scalar));
157 
158  return retval;
159 }
160 
161 NDArray
162 octave_complex::array_value (bool force_conversion) const
163 {
164  NDArray retval;
165 
166  if (! force_conversion)
167  gripe_implicit_conversion ("Octave:imag-to-real",
168  "complex scalar", "real matrix");
169 
170  retval = NDArray (dim_vector (1, 1), std::real (scalar));
171 
172  return retval;
173 }
174 
176 octave_complex::float_array_value (bool force_conversion) const
177 {
178  FloatNDArray retval;
179 
180  if (! force_conversion)
181  gripe_implicit_conversion ("Octave:imag-to-real",
182  "complex scalar", "real matrix");
183 
184  retval = FloatNDArray (dim_vector (1, 1), std::real (scalar));
185 
186  return retval;
187 }
188 
189 Complex
191 {
192  return scalar;
193 }
194 
197 {
198  return static_cast<FloatComplex> (scalar);
199 }
200 
203 {
204  return ComplexMatrix (1, 1, scalar);
205 }
206 
209 {
210  return FloatComplexMatrix (1, 1, static_cast<FloatComplex> (scalar));
211 }
212 
214 octave_complex::complex_array_value (bool /* force_conversion */) const
215 {
216  return ComplexNDArray (dim_vector (1, 1), scalar);
217 }
218 
220 octave_complex::float_complex_array_value (bool /* force_conversion */) const
221 {
222  return FloatComplexNDArray (dim_vector (1, 1),
223  static_cast<FloatComplex> (scalar));
224 }
225 
227 octave_complex::resize (const dim_vector& dv, bool fill) const
228 {
229  if (fill)
230  {
231  ComplexNDArray retval (dv, Complex (0));
232 
233  if (dv.numel ())
234  retval(0) = scalar;
235 
236  return retval;
237  }
238  else
239  {
240  ComplexNDArray retval (dv);
241 
242  if (dv.numel ())
243  retval(0) = scalar;
244 
245  return retval;
246  }
247 }
248 
251 {
252  return ComplexDiagMatrix (Array<Complex> (dim_vector (1, 1), scalar), m, n);
253 }
254 
255 bool
256 octave_complex::save_ascii (std::ostream& os)
257 {
258  Complex c = complex_value ();
259 
260  octave_write_complex (os, c);
261 
262  os << "\n";
263 
264  return true;
265 }
266 
267 bool
268 octave_complex::load_ascii (std::istream& is)
269 {
270  scalar = octave_read_value<Complex> (is);
271 
272  if (!is)
273  {
274  error ("load: failed to load complex scalar constant");
275  return false;
276  }
277 
278  return true;
279 }
280 
281 
282 bool
283 octave_complex::save_binary (std::ostream& os, bool& /* save_as_floats */)
284 {
285  char tmp = static_cast<char> (LS_DOUBLE);
286  os.write (reinterpret_cast<char *> (&tmp), 1);
287  Complex ctmp = complex_value ();
288  os.write (reinterpret_cast<char *> (&ctmp), 16);
289 
290  return true;
291 }
292 
293 bool
294 octave_complex::load_binary (std::istream& is, bool swap,
296 {
297  char tmp;
298  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
299  return false;
300 
301  Complex ctmp;
302  read_doubles (is, reinterpret_cast<double *> (&ctmp),
303  static_cast<save_type> (tmp), 2, swap, fmt);
304  if (error_state || ! is)
305  return false;
306 
307  scalar = ctmp;
308  return true;
309 }
310 
311 bool
312 octave_complex::save_hdf5 (octave_hdf5_id loc_id, const char *name,
313  bool /* save_as_floats */)
314 {
315  bool retval = false;
316 
317 #if defined (HAVE_HDF5)
318 
319  hsize_t dimens[3];
320  hid_t space_hid, type_hid, data_hid;
321  space_hid = type_hid = data_hid = -1;
322 
323  space_hid = H5Screate_simple (0, dimens, 0);
324  if (space_hid < 0)
325  return false;
326 
327  type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
328  if (type_hid < 0)
329  {
330  H5Sclose (space_hid);
331  return false;
332  }
333 #if HAVE_HDF5_18
334  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid,
335  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
336 #else
337  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT);
338 #endif
339  if (data_hid < 0)
340  {
341  H5Sclose (space_hid);
342  H5Tclose (type_hid);
343  return false;
344  }
345 
346  Complex tmp = complex_value ();
347  retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
348  &tmp) >= 0;
349 
350  H5Dclose (data_hid);
351  H5Tclose (type_hid);
352  H5Sclose (space_hid);
353 
354 #else
355  gripe_save ("hdf5");
356 #endif
357 
358  return retval;
359 }
360 
361 bool
362 octave_complex::load_hdf5 (octave_hdf5_id loc_id, const char *name)
363 {
364  bool retval = false;
365 
366 #if defined (HAVE_HDF5)
367 
368 #if HAVE_HDF5_18
369  hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
370 #else
371  hid_t data_hid = H5Dopen (loc_id, name);
372 #endif
373  hid_t type_hid = H5Dget_type (data_hid);
374 
375  hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
376 
377  if (! hdf5_types_compatible (type_hid, complex_type))
378  {
379  H5Tclose (complex_type);
380  H5Dclose (data_hid);
381  return false;
382  }
383 
384  hid_t space_id = H5Dget_space (data_hid);
385  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
386 
387  if (rank != 0)
388  {
389  H5Tclose (complex_type);
390  H5Sclose (space_id);
391  H5Dclose (data_hid);
392  return false;
393  }
394 
395  // complex scalar:
396  Complex ctmp;
397  if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
398  &ctmp) >= 0)
399  {
400  retval = true;
401  scalar = ctmp;
402  }
403 
404  H5Tclose (complex_type);
405  H5Sclose (space_id);
406  H5Dclose (data_hid);
407 
408 #else
409  gripe_load ("hdf5");
410 #endif
411 
412  return retval;
413 }
414 
415 mxArray *
417 {
418  mxArray *retval = new mxArray (mxDOUBLE_CLASS, 1, 1, mxCOMPLEX);
419 
420  double *pr = static_cast<double *> (retval->get_data ());
421  double *pi = static_cast<double *> (retval->get_imag_data ());
422 
423  pr[0] = std::real (scalar);
424  pi[0] = std::imag (scalar);
425 
426  return retval;
427 }
428 
431 {
432  switch (umap)
433  {
434 #define SCALAR_MAPPER(UMAP, FCN) \
435  case umap_ ## UMAP: \
436  return octave_value (FCN (scalar))
437 
439  SCALAR_MAPPER (acos, ::acos);
441  SCALAR_MAPPER (angle, std::arg);
443  SCALAR_MAPPER (asin, ::asin);
445  SCALAR_MAPPER (atan, ::atan);
447  SCALAR_MAPPER (erf, ::erf);
448  SCALAR_MAPPER (erfc, ::erfc);
450  SCALAR_MAPPER (erfi, ::erfi);
452  SCALAR_MAPPER (ceil, ::ceil);
454  SCALAR_MAPPER (cos, std::cos);
455  SCALAR_MAPPER (cosh, std::cosh);
456  SCALAR_MAPPER (exp, std::exp);
458  SCALAR_MAPPER (fix, ::fix);
461  SCALAR_MAPPER (log, std::log);
462  SCALAR_MAPPER (log2, xlog2);
463  SCALAR_MAPPER (log10, std::log10);
466  SCALAR_MAPPER (round, xround);
467  SCALAR_MAPPER (roundb, xroundb);
469  SCALAR_MAPPER (sin, std::sin);
470  SCALAR_MAPPER (sinh, std::sinh);
471  SCALAR_MAPPER (sqrt, std::sqrt);
472  SCALAR_MAPPER (tan, std::tan);
473  SCALAR_MAPPER (tanh, std::tanh);
474  SCALAR_MAPPER (finite, xfinite);
475  SCALAR_MAPPER (isinf, xisinf);
476  SCALAR_MAPPER (isna, octave_is_NA);
477  SCALAR_MAPPER (isnan, xisnan);
478 
479  default:
480  return octave_base_value::map (umap);
481  }
482 }
void gripe_implicit_conversion(const char *id, const char *from, const char *to)
Definition: gripes.cc:180
bool hdf5_types_compatible(hid_t t1, hid_t t2)
Definition: ls-hdf5.cc:107
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-complex.cc:283
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-complex.cc:88
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-complex.cc:312
double xround(double x)
Definition: lo-mappers.cc:63
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-complex.cc:250
bool xisnan(double x)
Definition: lo-mappers.cc:144
double log1p(double x)
Definition: lo-specfun.cc:620
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-complex.cc:59
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void gripe_load(const char *type) const
Definition: ov-base.cc:1258
static int static_type_id(void)
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-complex.cc:362
bool save_ascii(std::ostream &os)
Definition: ov-complex.cc:256
function atanh(X)
Definition: atanh.f:2
void octave_write_complex(std::ostream &os, const Complex &c)
Definition: lo-utils.cc:399
void error(const char *fmt,...)
Definition: error.cc:476
mxArray * as_mxArray(void) const
Definition: ov-complex.cc:416
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-complex.cc:220
void * get_data(void) const
Definition: mxarray.h:433
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-complex.cc:294
bool xisinf(double x)
Definition: lo-mappers.cc:160
double double_value(bool=false) const
Definition: ov-complex.cc:106
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void gripe_save(const char *type) const
Definition: ov-base.cc:1267
double expm1(double x)
Definition: lo-specfun.cc:510
type_conv_info numeric_demotion_function(void) const
Definition: ov-complex.cc:67
#define SCALAR_MAPPER(UMAP, FCN)
double xlog2(double x)
Definition: lo-mappers.cc:93
std::complex< double > erf(std::complex< double > z, double relerr=0)
FloatNDArray float_array_value(bool=false) const
Definition: ov-complex.cc:176
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-complex.cc:227
hid_t hdf5_make_complex_type(hid_t num_type)
Definition: ls-hdf5.cc:228
double signum(double x)
Definition: lo-mappers.cc:80
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:244
#define CAST_CONV_ARG(t)
Definition: ops.h:83
function asinh(X)
Definition: asinh.f:2
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-complex.cc:214
octave_base_value * try_narrowing_conversion(void)
Definition: ov-complex.cc:75
Matrix matrix_value(bool=false) const
Definition: ov-complex.cc:134
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:275
float float_value(bool=false) const
Definition: ov-complex.cc:120
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-complex.cc:148
int error_state
Definition: error.cc:101
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
double xroundb(double x)
Definition: lo-mappers.cc:69
Definition: dMatrix.h:35
octave_value map(unary_mapper_t umap) const
Definition: ov-complex.cc:430
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, oct_mach_info::float_format fmt)
Definition: data-conv.cc:778
void mxArray
Definition: mex.h:53
function acosh(X)
Definition: acosh.f:2
double arg(double x)
Definition: lo-mappers.h:37
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-complex.cc:202
bool xfinite(double x)
Definition: lo-mappers.cc:152
void * get_imag_data(void) const
Definition: mxarray.h:435
NDArray array_value(bool=false) const
Definition: ov-complex.cc:162
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-complex.cc:208
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1276
double fix(double x)
Definition: lo-mappers.h:39
FloatComplex float_complex_value(bool=false) const
Definition: ov-complex.cc:196
float dawson(float x)
Definition: lo-specfun.cc:349
Complex asin(const Complex &x)
Definition: lo-mappers.cc:204
bool load_ascii(std::istream &is)
Definition: ov-complex.cc:268
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:162
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
std::complex< double > Complex
Definition: oct-cmplx.h:29
Complex acos(const Complex &x)
Definition: lo-mappers.cc:177
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:156
T abs(T x)
Definition: pr-output.cc:3062
Complex atan(const Complex &x)
Definition: lo-mappers.cc:231
Complex complex_value(bool=false) const
Definition: ov-complex.cc:190
bool octave_is_NA(double x)
Definition: lo-mappers.cc:167
std::complex< double > erfc(std::complex< double > z, double relerr=0)
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:438