GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-complex.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <istream>
31 #include <ostream>
32 #include <sstream>
33 
34 #include "lo-ieee.h"
35 #include "lo-specfun.h"
36 #include "lo-mappers.h"
37 
38 #include "mxarray.h"
39 #include "ovl.h"
40 #include "oct-hdf5.h"
41 #include "oct-stream.h"
42 #include "ops.h"
43 #include "ov-complex.h"
44 #include "ov-flt-complex.h"
45 #include "ov-base.h"
46 #include "ov-base-scalar.h"
47 #include "ov-base-scalar.cc"
48 #include "ov-cx-mat.h"
49 #include "ov-scalar.h"
50 #include "errwarn.h"
51 #include "pr-output.h"
52 #include "ops.h"
53 
54 #include "ls-oct-text.h"
55 #include "ls-hdf5.h"
56 
57 // Prevent implicit instantiations on some systems (Windows, others?)
58 // that can lead to duplicate definitions of static data members.
59 
60 extern template class octave_base_scalar<double>;
61 extern template class octave_base_scalar<FloatComplex>;
62 
63 template class octave_base_scalar<Complex>;
64 
66  "complex scalar", "double");
67 
69 
70 // Complain if a complex value is used as a subscript.
71 
73 {
74 public:
75 
76  complex_index_exception (const std::string& value)
77  : index_exception (value)
78  {
79  // Virtual, but the one we want to call is defined in this class.
80  update_message ();
81  }
82 
83  ~complex_index_exception (void) = default;
84 
85  void update_message (void)
86  {
87  set_message (expression ()
88  + ": subscripts must be real (forgot to initialize i or j?)");
89  }
90 
91  // ID of error to throw.
92  const char * err_id (void) const
93  {
94  return "Octave:invalid-index";
95  }
96 };
97 
99 
100 static octave_base_value *
102 {
103  const octave_complex& v = dynamic_cast<const octave_complex&> (a);
104 
105  return new octave_float_complex (v.float_complex_value ());
106 }
107 
110 {
111  return
114 }
115 
118 {
119  octave_base_value *retval = nullptr;
120 
121  double im = scalar.imag ();
122 
123  if (im == 0.0)
124  retval = new octave_scalar (scalar.real ());
125 
126  return retval;
127 }
128 
130 octave_complex::do_index_op (const octave_value_list& idx, bool resize_ok)
131 {
132  // FIXME: this doesn't solve the problem of
133  //
134  // a = i; a([1,1], [1,1], [1,1])
135  //
136  // and similar constructions. Hmm...
137 
138  // FIXME: using this constructor avoids narrowing the
139  // 1x1 matrix back to a scalar value. Need a better solution
140  // to this problem.
141 
143 
144  return tmp.index_op (idx, resize_ok);
145 }
146 
147 // Can't make an index_vector from a complex number. Throw an error.
150 {
151  std::ostringstream buf;
152  buf << scalar.real () << std::showpos << scalar.imag () << 'i';
153  octave::complex_index_exception cie (buf.str ());
154 
155  throw cie;
156 }
157 
158 double
159 octave_complex::double_value (bool force_conversion) const
160 {
161  if (! force_conversion)
162  warn_implicit_conversion ("Octave:imag-to-real",
163  "complex scalar", "real scalar");
164 
165  return scalar.real ();
166 }
167 
168 float
169 octave_complex::float_value (bool force_conversion) const
170 {
171  if (! force_conversion)
172  warn_implicit_conversion ("Octave:imag-to-real",
173  "complex scalar", "real scalar");
174 
175  return scalar.real ();
176 }
177 
178 Matrix
179 octave_complex::matrix_value (bool force_conversion) const
180 {
181  Matrix retval;
182 
183  if (! force_conversion)
184  warn_implicit_conversion ("Octave:imag-to-real",
185  "complex scalar", "real matrix");
186 
187  retval = Matrix (1, 1, scalar.real ());
188 
189  return retval;
190 }
191 
193 octave_complex::float_matrix_value (bool force_conversion) const
194 {
195  FloatMatrix retval;
196 
197  if (! force_conversion)
198  warn_implicit_conversion ("Octave:imag-to-real",
199  "complex scalar", "real matrix");
200 
201  retval = FloatMatrix (1, 1, scalar.real ());
202 
203  return retval;
204 }
205 
206 NDArray
207 octave_complex::array_value (bool force_conversion) const
208 {
209  NDArray retval;
210 
211  if (! force_conversion)
212  warn_implicit_conversion ("Octave:imag-to-real",
213  "complex scalar", "real matrix");
214 
215  retval = NDArray (dim_vector (1, 1), scalar.real ());
216 
217  return retval;
218 }
219 
221 octave_complex::float_array_value (bool force_conversion) const
222 {
223  FloatNDArray retval;
224 
225  if (! force_conversion)
226  warn_implicit_conversion ("Octave:imag-to-real",
227  "complex scalar", "real matrix");
228 
229  retval = FloatNDArray (dim_vector (1, 1), scalar.real ());
230 
231  return retval;
232 }
233 
234 Complex
236 {
237  return scalar;
238 }
239 
242 {
243  return static_cast<FloatComplex> (scalar);
244 }
245 
248 {
249  return ComplexMatrix (1, 1, scalar);
250 }
251 
254 {
255  return FloatComplexMatrix (1, 1, static_cast<FloatComplex> (scalar));
256 }
257 
259 octave_complex::complex_array_value (bool /* force_conversion */) const
260 {
261  return ComplexNDArray (dim_vector (1, 1), scalar);
262 }
263 
265 octave_complex::float_complex_array_value (bool /* force_conversion */) const
266 {
267  return FloatComplexNDArray (dim_vector (1, 1),
268  static_cast<FloatComplex> (scalar));
269 }
270 
272 octave_complex::resize (const dim_vector& dv, bool fill) const
273 {
274  if (fill)
275  {
276  ComplexNDArray retval (dv, Complex (0));
277 
278  if (dv.numel ())
279  retval(0) = scalar;
280 
281  return retval;
282  }
283  else
284  {
285  ComplexNDArray retval (dv);
286 
287  if (dv.numel ())
288  retval(0) = scalar;
289 
290  return retval;
291  }
292 }
293 
296 {
297  return scalar;
298 }
299 
302 {
303  return FloatComplex (scalar);
304 }
305 
308 {
309  return ComplexDiagMatrix (Array<Complex> (dim_vector (1, 1), scalar), m, n);
310 }
311 
312 bool
313 octave_complex::save_ascii (std::ostream& os)
314 {
315  Complex c = complex_value ();
316 
317  octave::write_value<Complex> (os, c);
318 
319  os << "\n";
320 
321  return true;
322 }
323 
324 bool
325 octave_complex::load_ascii (std::istream& is)
326 {
327  scalar = octave::read_value<Complex> (is);
328 
329  if (! is)
330  error ("load: failed to load complex scalar constant");
331 
332  return true;
333 }
334 
335 bool
336 octave_complex::save_binary (std::ostream& os, bool /* save_as_floats */)
337 {
338  char tmp = static_cast<char> (LS_DOUBLE);
339  os.write (reinterpret_cast<char *> (&tmp), 1);
340  Complex ctmp = complex_value ();
341  os.write (reinterpret_cast<char *> (&ctmp), 16);
342 
343  return true;
344 }
345 
346 bool
347 octave_complex::load_binary (std::istream& is, bool swap,
349 {
350  char tmp;
351  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
352  return false;
353 
354  Complex ctmp;
355  read_doubles (is, reinterpret_cast<double *> (&ctmp),
356  static_cast<save_type> (tmp), 2, swap, fmt);
357 
358  if (! is)
359  return false;
360 
361  scalar = ctmp;
362  return true;
363 }
364 
365 bool
366 octave_complex::save_hdf5 (octave_hdf5_id loc_id, const char *name,
367  bool /* save_as_floats */)
368 {
369  bool retval = false;
370 
371 #if defined (HAVE_HDF5)
372 
373  hsize_t dimens[3] = {0};
374  hid_t space_hid, type_hid, data_hid;
375  space_hid = type_hid = data_hid = -1;
376 
377  space_hid = H5Screate_simple (0, dimens, nullptr);
378  if (space_hid < 0)
379  return false;
380 
381  type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
382  if (type_hid < 0)
383  {
384  H5Sclose (space_hid);
385  return false;
386  }
387 #if defined (HAVE_HDF5_18)
388  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid,
391 #else
392  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, octave_H5P_DEFAULT);
393 #endif
394  if (data_hid < 0)
395  {
396  H5Sclose (space_hid);
397  H5Tclose (type_hid);
398  return false;
399  }
400 
401  Complex tmp = complex_value ();
402  retval = H5Dwrite (data_hid, type_hid, octave_H5S_ALL, octave_H5S_ALL,
403  octave_H5P_DEFAULT, &tmp) >= 0;
404 
405  H5Dclose (data_hid);
406  H5Tclose (type_hid);
407  H5Sclose (space_hid);
408 
409 #else
410  octave_unused_parameter (loc_id);
411  octave_unused_parameter (name);
412 
413  warn_save ("hdf5");
414 #endif
415 
416  return retval;
417 }
418 
419 bool
420 octave_complex::load_hdf5 (octave_hdf5_id loc_id, const char *name)
421 {
422  bool retval = false;
423 
424 #if defined (HAVE_HDF5)
425 
426 #if defined (HAVE_HDF5_18)
427  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
428 #else
429  hid_t data_hid = H5Dopen (loc_id, name);
430 #endif
431  hid_t type_hid = H5Dget_type (data_hid);
432 
433  hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
434 
435  if (! hdf5_types_compatible (type_hid, complex_type))
436  {
437  H5Tclose (complex_type);
438  H5Dclose (data_hid);
439  return false;
440  }
441 
442  hid_t space_id = H5Dget_space (data_hid);
443  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
444 
445  if (rank != 0)
446  {
447  H5Tclose (complex_type);
448  H5Sclose (space_id);
449  H5Dclose (data_hid);
450  return false;
451  }
452 
453  // complex scalar:
454  Complex ctmp;
455  if (H5Dread (data_hid, complex_type, octave_H5S_ALL, octave_H5S_ALL,
456  octave_H5P_DEFAULT, &ctmp) >= 0)
457  {
458  retval = true;
459  scalar = ctmp;
460  }
461 
462  H5Tclose (complex_type);
463  H5Sclose (space_id);
464  H5Dclose (data_hid);
465 
466 #else
467  octave_unused_parameter (loc_id);
468  octave_unused_parameter (name);
469 
470  warn_load ("hdf5");
471 #endif
472 
473  return retval;
474 }
475 
476 mxArray *
477 octave_complex::as_mxArray (bool interleaved) const
478 {
479  mxArray *retval = new mxArray (interleaved, mxDOUBLE_CLASS, 1, 1, mxCOMPLEX);
480 
481  if (interleaved)
482  {
483  mxComplexDouble *pd
484  = reinterpret_cast<mxComplexDouble *> (retval->get_complex_doubles ());
485 
486  pd[0].real = scalar.real ();
487  pd[0].imag = scalar.imag ();
488  }
489  else
490  {
491  mxDouble *pr = static_cast<mxDouble *> (retval->get_data ());
492  mxDouble *pi = static_cast<mxDouble *> (retval->get_imag_data ());
493 
494  pr[0] = scalar.real ();
495  pi[0] = scalar.imag ();
496  }
497 
498  return retval;
499 }
500 
503 {
504  switch (umap)
505  {
506 #define SCALAR_MAPPER(UMAP, FCN) \
507  case umap_ ## UMAP: \
508  return octave_value (FCN (scalar))
509 
513  SCALAR_MAPPER (angle, std::arg);
514  SCALAR_MAPPER (arg, std::arg);
526  SCALAR_MAPPER (cos, std::cos);
527  SCALAR_MAPPER (cosh, std::cosh);
528  SCALAR_MAPPER (exp, std::exp);
533  SCALAR_MAPPER (log, std::log);
535  SCALAR_MAPPER (log10, std::log10);
541  SCALAR_MAPPER (sin, std::sin);
542  SCALAR_MAPPER (sinh, std::sinh);
543  SCALAR_MAPPER (sqrt, std::sqrt);
544  SCALAR_MAPPER (tan, std::tan);
545  SCALAR_MAPPER (tanh, std::tanh);
550 
551  // Special cases for Matlab compatibility.
552  case umap_xtolower:
553  case umap_xtoupper:
554  return scalar;
555 
556  default:
557  return octave_base_value::map (umap);
558  }
559 }
OCTAVE_END_NAMESPACE(octave)
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
Definition: dMatrix.h:42
~complex_index_exception(void)=default
complex_index_exception(const std::string &value)
Definition: ov-complex.cc:76
const char * err_id(void) const
Definition: ov-complex.cc:92
void update_message(void)
Definition: ov-complex.cc:85
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
void * get_imag_data(void) const
Definition: mxarray.h:562
void * get_data(void) const
Definition: mxarray.h:497
mxComplexDouble * get_complex_doubles(void) const
Definition: mxarray.h:529
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1181
OCTINTERP_API void warn_load(const char *type) const
Definition: ov-base.cc:1157
OCTINTERP_API void warn_save(const char *type) const
Definition: ov-base.cc:1166
OCTINTERP_API float float_value(bool=false) const
Definition: ov-complex.cc:169
octave_base_value * try_narrowing_conversion(void)
Definition: ov-complex.cc:117
OCTINTERP_API bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-complex.cc:347
OCTINTERP_API bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-complex.cc:336
OCTINTERP_API octave_value as_single(void) const
Definition: ov-complex.cc:301
OCTINTERP_API FloatComplex float_complex_value(bool=false) const
Definition: ov-complex.cc:241
OCTINTERP_API NDArray array_value(bool=false) const
Definition: ov-complex.cc:207
OCTINTERP_API FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-complex.cc:253
OCTINTERP_API octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-complex.cc:307
OCTINTERP_API bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-complex.cc:420
OCTINTERP_API mxArray * as_mxArray(bool interleaved) const
Definition: ov-complex.cc:477
OCTINTERP_API ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-complex.cc:247
OCTINTERP_API bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-complex.cc:366
OCTINTERP_API bool save_ascii(std::ostream &os)
Definition: ov-complex.cc:313
OCTINTERP_API octave_value map(unary_mapper_t umap) const
Definition: ov-complex.cc:502
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-complex.cc:130
OCTINTERP_API FloatNDArray float_array_value(bool=false) const
Definition: ov-complex.cc:221
octave::idx_vector index_vector(bool=false) const
Definition: ov-complex.cc:149
OCTINTERP_API octave_value as_double(void) const
Definition: ov-complex.cc:295
type_conv_info numeric_demotion_function(void) const
Definition: ov-complex.cc:109
OCTINTERP_API octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-complex.cc:272
OCTINTERP_API FloatMatrix float_matrix_value(bool=false) const
Definition: ov-complex.cc:193
OCTINTERP_API bool load_ascii(std::istream &is)
Definition: ov-complex.cc:325
OCTINTERP_API ComplexNDArray complex_array_value(bool=false) const
Definition: ov-complex.cc:259
OCTINTERP_API FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-complex.cc:265
OCTINTERP_API Matrix matrix_value(bool=false) const
Definition: ov-complex.cc:179
OCTINTERP_API double double_value(bool=false) const
Definition: ov-complex.cc:159
OCTINTERP_API Complex complex_value(bool=false) const
Definition: ov-complex.cc:235
static int static_type_id(void)
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:550
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:137
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:143
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:776
save_type
Definition: data-conv.h:87
@ LS_DOUBLE
Definition: data-conv.h:95
void error(const char *fmt,...)
Definition: error.cc:979
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:344
octave::idx_vector idx_vector
Definition: idx-vector.h:1039
Complex log2(const Complex &x)
Definition: lo-mappers.cc:139
Complex asin(const Complex &x)
Definition: lo-mappers.cc:107
bool isna(double x)
Definition: lo-mappers.cc:47
Complex acos(const Complex &x)
Definition: lo-mappers.cc:85
Complex atan(const Complex &x)
Definition: lo-mappers.h:71
double roundb(double x)
Definition: lo-mappers.h:147
bool isfinite(double x)
Definition: lo-mappers.h:192
bool isinf(double x)
Definition: lo-mappers.h:203
double signum(double x)
Definition: lo-mappers.h:229
double round(double x)
Definition: lo-mappers.h:136
bool isnan(bool)
Definition: lo-mappers.h:178
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
double fix(double x)
Definition: lo-mappers.h:118
double dawson(double x)
Definition: lo-specfun.cc:1466
static const double pi
Definition: lo-specfun.cc:1944
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1823
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1907
double asinh(double x)
Definition: lo-specfun.h:58
double atanh(double x)
Definition: lo-specfun.h:63
double acosh(double x)
Definition: lo-specfun.h:40
octave_hdf5_id hdf5_make_complex_type(octave_hdf5_id num_type)
Definition: ls-hdf5.cc:405
bool hdf5_types_compatible(octave_hdf5_id t1, octave_hdf5_id t2)
Definition: ls-hdf5.cc:269
float_format
Definition: mach-info.h:38
void mxArray
Definition: mex.h:58
class OCTAVE_API NDArray
Definition: mx-fwd.h:38
class OCTAVE_API ComplexDiagMatrix
Definition: mx-fwd.h:60
class OCTAVE_API Matrix
Definition: mx-fwd.h:31
class OCTAVE_API ComplexMatrix
Definition: mx-fwd.h:32
class OCTAVE_API FloatComplexMatrix
Definition: mx-fwd.h:34
class OCTAVE_API FloatMatrix
Definition: mx-fwd.h:33
class OCTAVE_API ComplexNDArray
Definition: mx-fwd.h:39
class OCTAVE_API FloatComplexNDArray
Definition: mx-fwd.h:41
class OCTAVE_API FloatNDArray
Definition: mx-fwd.h:40
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
std::complex< double > erfc(std::complex< double > z, double relerr=0)
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
std::complex< double > erfi(std::complex< double > z, double relerr=0)
std::complex< double > erf(std::complex< double > z, double relerr=0)
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
int64_t octave_hdf5_id
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:229
#define SCALAR_MAPPER(UMAP, FCN)
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-complex.cc:101
static T abs(T x)
Definition: pr-output.cc:1678