GNU Octave  4.2.1
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-flt-complex.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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 #if defined (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 "ovl.h"
35 #include "oct-hdf5.h"
36 #include "oct-stream.h"
37 #include "ops.h"
38 #include "ov-complex.h"
39 #include "ov-base.h"
40 #include "ov-base-scalar.h"
41 #include "ov-base-scalar.cc"
42 #include "ov-flt-cx-mat.h"
43 #include "ov-float.h"
44 #include "ov-flt-complex.h"
45 #include "errwarn.h"
46 #include "pr-output.h"
47 #include "ops.h"
48 
49 #include "ls-oct-text.h"
50 #include "ls-hdf5.h"
51 
52 // Prevent implicit instantiations on some systems (Windows, others?)
53 // that can lead to duplicate definitions of static data members.
54 
55 extern template class OCTINTERP_API octave_base_scalar<float>;
56 
58 
60  "float complex scalar", "single");
61 
64 {
66 
67  float im = scalar.imag ();
68 
69  if (im == 0.0)
70  retval = new octave_float_scalar (scalar.real ());
71 
72  return retval;
73 }
74 
77 {
78  // FIXME: this doesn't solve the problem of
79  //
80  // a = i; a([1,1], [1,1], [1,1])
81  //
82  // and similar constructions. Hmm...
83 
84  // FIXME: using this constructor avoids narrowing the
85  // 1x1 matrix back to a scalar value. Need a better solution
86  // to this problem.
87 
90 
91  return tmp.do_index_op (idx, resize_ok);
92 }
93 
94 double
95 octave_float_complex::double_value (bool force_conversion) const
96 {
97  if (! force_conversion)
98  warn_implicit_conversion ("Octave:imag-to-real",
99  "complex scalar", "real scalar");
100 
101  return scalar.real ();
102 }
103 
104 float
105 octave_float_complex::float_value (bool force_conversion) const
106 {
107  if (! force_conversion)
108  warn_implicit_conversion ("Octave:imag-to-real",
109  "complex scalar", "real scalar");
110 
111  return scalar.real ();
112 }
113 
114 Matrix
115 octave_float_complex::matrix_value (bool force_conversion) const
116 {
117  Matrix retval;
118 
119  if (! force_conversion)
120  warn_implicit_conversion ("Octave:imag-to-real",
121  "complex scalar", "real matrix");
122 
123  retval = Matrix (1, 1, scalar.real ());
124 
125  return retval;
126 }
127 
129 octave_float_complex::float_matrix_value (bool force_conversion) const
130 {
132 
133  if (! force_conversion)
134  warn_implicit_conversion ("Octave:imag-to-real",
135  "complex scalar", "real matrix");
136 
137  retval = FloatMatrix (1, 1, scalar.real ());
138 
139  return retval;
140 }
141 
142 NDArray
143 octave_float_complex::array_value (bool force_conversion) const
144 {
145  NDArray retval;
146 
147  if (! force_conversion)
148  warn_implicit_conversion ("Octave:imag-to-real",
149  "complex scalar", "real matrix");
150 
151  retval = NDArray (dim_vector (1, 1), scalar.real ());
152 
153  return retval;
154 }
155 
157 octave_float_complex::float_array_value (bool force_conversion) const
158 {
160 
161  if (! force_conversion)
162  warn_implicit_conversion ("Octave:imag-to-real",
163  "complex scalar", "real matrix");
164 
165  retval = FloatNDArray (dim_vector (1, 1), scalar.real ());
166 
167  return retval;
168 }
169 
170 Complex
172 {
173  return scalar;
174 }
175 
178 {
179  return static_cast<FloatComplex> (scalar);
180 }
181 
184 {
185  return ComplexMatrix (1, 1, scalar);
186 }
187 
190 {
191  return FloatComplexMatrix (1, 1, scalar);
192 }
193 
195 octave_float_complex::complex_array_value (bool /* force_conversion */) const
196 {
197  return ComplexNDArray (dim_vector (1, 1), scalar);
198 }
199 
201 octave_float_complex::float_complex_array_value (bool /* force_conversion */) const
202 {
203  return FloatComplexNDArray (dim_vector (1, 1), scalar);
204 }
205 
207 octave_float_complex::resize (const dim_vector& dv, bool fill) const
208 {
209  if (fill)
210  {
212 
213  if (dv.numel ())
214  retval(0) = scalar;
215 
216  return retval;
217  }
218  else
219  {
221 
222  if (dv.numel ())
223  retval(0) = scalar;
224 
225  return retval;
226  }
227 }
228 
231 {
232  return Complex (scalar);
233 }
234 
237 {
238  return scalar;
239 }
240 
243 {
244  return
246  m, n);
247 }
248 
249 bool
251 {
253 
255 
256  os << "\n";
257 
258  return true;
259 }
260 
261 bool
263 {
264  scalar = octave_read_value<FloatComplex> (is);
265 
266  if (! is)
267  error ("load: failed to load complex scalar constant");
268 
269  return true;
270 }
271 
272 bool
273 octave_float_complex::save_binary (std::ostream& os, bool& /* save_as_floats */)
274 {
275  char tmp = static_cast<char> (LS_FLOAT);
276  os.write (reinterpret_cast<char *> (&tmp), 1);
278  os.write (reinterpret_cast<char *> (&ctmp), 8);
279 
280  return true;
281 }
282 
283 bool
286 {
287  char tmp;
288  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
289  return false;
290 
291  FloatComplex ctmp;
292  read_floats (is, reinterpret_cast<float *> (&ctmp),
293  static_cast<save_type> (tmp), 2, swap, fmt);
294 
295  if (! is)
296  return false;
297 
298  scalar = ctmp;
299  return true;
300 }
301 
302 bool
304  bool /* save_as_floats */)
305 {
306  bool retval = false;
307 
308 #if defined (HAVE_HDF5)
309 
310  hsize_t dimens[3];
311  hid_t space_hid, type_hid, data_hid;
312  space_hid = type_hid = data_hid = -1;
313 
314  space_hid = H5Screate_simple (0, dimens, 0);
315  if (space_hid < 0)
316  return false;
317 
318  type_hid = hdf5_make_complex_type (H5T_NATIVE_FLOAT);
319  if (type_hid < 0)
320  {
321  H5Sclose (space_hid);
322  return false;
323  }
324 #if defined (HAVE_HDF5_18)
325  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid,
327 #else
328  data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, octave_H5P_DEFAULT);
329 #endif
330  if (data_hid < 0)
331  {
332  H5Sclose (space_hid);
333  H5Tclose (type_hid);
334  return false;
335  }
336 
338  retval = H5Dwrite (data_hid, type_hid, octave_H5S_ALL, octave_H5S_ALL,
339  octave_H5P_DEFAULT, &tmp) >= 0;
340 
341  H5Dclose (data_hid);
342  H5Tclose (type_hid);
343  H5Sclose (space_hid);
344 
345 #else
346  octave_unused_parameter (loc_id);
347  octave_unused_parameter (name);
348 
349  warn_save ("hdf5");
350 #endif
351 
352  return retval;
353 }
354 
355 bool
357 {
358  bool retval = false;
359 
360 #if defined (HAVE_HDF5)
361 
362 #if defined (HAVE_HDF5_18)
363  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
364 #else
365  hid_t data_hid = H5Dopen (loc_id, name);
366 #endif
367  hid_t type_hid = H5Dget_type (data_hid);
368 
369  hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_FLOAT);
370 
371  if (! hdf5_types_compatible (type_hid, complex_type))
372  {
373  H5Tclose (complex_type);
374  H5Dclose (data_hid);
375  return false;
376  }
377 
378  hid_t space_id = H5Dget_space (data_hid);
379  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
380 
381  if (rank != 0)
382  {
383  H5Tclose (complex_type);
384  H5Sclose (space_id);
385  H5Dclose (data_hid);
386  return false;
387  }
388 
389  // complex scalar:
390  FloatComplex ctmp;
391  if (H5Dread (data_hid, complex_type, octave_H5S_ALL, octave_H5S_ALL,
392  octave_H5P_DEFAULT, &ctmp)
393  >= 0)
394  {
395  retval = true;
396  scalar = ctmp;
397  }
398 
399  H5Tclose (complex_type);
400  H5Sclose (space_id);
401  H5Dclose (data_hid);
402 
403 #else
404  octave_unused_parameter (loc_id);
405  octave_unused_parameter (name);
406 
407  warn_load ("hdf5");
408 #endif
409 
410  return retval;
411 }
412 
413 mxArray *
415 {
417 
418  float *pr = static_cast<float *> (retval->get_data ());
419  float *pi = static_cast<float *> (retval->get_imag_data ());
420 
421  pr[0] = scalar.real ();
422  pi[0] = scalar.imag ();
423 
424  return retval;
425 }
426 
429 {
430  switch (umap)
431  {
432 #define SCALAR_MAPPER(UMAP, FCN) \
433  case umap_ ## UMAP: \
434  return octave_value (FCN (scalar))
435 
439  SCALAR_MAPPER (angle, std::arg);
452  SCALAR_MAPPER (cos, std::cos);
453  SCALAR_MAPPER (cosh, std::cosh);
454  SCALAR_MAPPER (exp, std::exp);
461  SCALAR_MAPPER (log10, std::log10);
467  SCALAR_MAPPER (sin, std::sin);
468  SCALAR_MAPPER (sinh, std::sinh);
469  SCALAR_MAPPER (sqrt, std::sqrt);
470  SCALAR_MAPPER (tan, std::tan);
471  SCALAR_MAPPER (tanh, std::tanh);
476 
477  default:
478  return octave_base_value::map (umap);
479  }
480 }
octave_base_value * try_narrowing_conversion(void)
ComplexNDArray complex_array_value(bool=false) const
octave_value as_single(void) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
ComplexMatrix complex_matrix_value(bool=false) const
float float_value(bool=false) const
float erfcx(float x)
Definition: lo-specfun.cc:271
function erf(X)
Definition: erf.f:2
#define SCALAR_MAPPER(UMAP, FCN)
std::complex< double > erfi(std::complex< double > z, double relerr=0)
const octave_hdf5_id octave_H5S_ALL
bool isnan(double x)
Definition: lo-mappers.cc:347
double atanh(double x)
Definition: lo-specfun.cc:149
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the base of natural logarithms The constant ex $e satisfies the equation log(e)
double ceil(double x)
Definition: lo-mappers.h:138
function atanh(X)
Definition: atanh.f:2
void error(const char *fmt,...)
Definition: error.cc:570
void * get_data(void) const
Definition: mxarray.h:449
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
NDArray array_value(bool=false) const
Complex acos(const Complex &x)
Definition: lo-mappers.cc:90
double expm1(double x)
Definition: lo-specfun.cc:473
double asinh(double x)
Definition: lo-specfun.cc:105
double fix(double x)
Definition: lo-mappers.h:158
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:830
Complex asin(const Complex &x)
Definition: lo-mappers.cc:150
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Complex complex_value(bool=false) const
octave_value arg
Definition: pr-output.cc:3440
double acosh(double x)
Definition: lo-specfun.cc:61
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:389
double round(double x)
Definition: lo-mappers.cc:333
bool is_NA(double x)
Definition: lo-mappers.cc:54
octave_value diag(octave_idx_type m, octave_idx_type n) const
bool swap
Definition: load-save.cc:725
float erfi(float x)
Definition: lo-specfun.cc:289
void warn_load(const char *type) const
Definition: ov-base.cc:1151
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:216
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
Complex atan(const Complex &x)
Definition: lo-mappers.cc:210
Matrix matrix_value(bool=false) const
#define OCTINTERP_API
Definition: mexproto.h:69
bool load_ascii(std::istream &is)
function asinh(X)
Definition: asinh.f:2
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
bool hdf5_types_compatible(octave_hdf5_id t1, octave_hdf5_id t2)
Definition: ls-hdf5.cc:192
double signum(double x)
Definition: lo-mappers.h:259
float dawson(float x)
Definition: lo-specfun.cc:307
FloatComplexMatrix float_complex_matrix_value(bool=false) const
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
bool finite(double x)
Definition: lo-mappers.cc:367
octave_value resize(const dim_vector &dv, bool fill=false) const
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
int64_t octave_hdf5_id
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
bool save_ascii(std::ostream &os)
FloatMatrix float_matrix_value(bool=false) const
Definition: dMatrix.h:37
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
FloatComplexNDArray float_complex_array_value(bool=false) const
void warn_save(const char *type) const
Definition: ov-base.cc:1160
void mxArray
Definition: mex.h:55
bool isinf(double x)
Definition: lo-mappers.cc:387
function acosh(X)
Definition: acosh.f:2
double double_value(bool=false) const
FloatComplex float_complex_value(bool=false) const
double erf(double x)
Definition: lo-specfun.cc:193
octave_value map(unary_mapper_t umap) const
void * get_imag_data(void) const
Definition: mxarray.h:451
double log1p(double x)
Definition: lo-specfun.cc:587
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup abs(local error in x(i))<
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:332
double roundb(double x)
Definition: lo-mappers.h:189
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1175
octave_value as_double(void) const
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:142
double floor(double x)
Definition: lo-mappers.cc:330
const octave_hdf5_id octave_H5P_DEFAULT
double erfc(double x)
Definition: lo-specfun.cc:232
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
bool save_binary(std::ostream &os, bool &save_as_floats)
std::complex< double > Complex
Definition: oct-cmplx.h:31
FloatNDArray float_array_value(bool=false) const
void octave_write_float_complex(std::ostream &os, const FloatComplex &c)
Definition: lo-utils.cc:424
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:136
write the output to stdout if nargout is
Definition: load-save.cc:1576
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_hdf5_id hdf5_make_complex_type(octave_hdf5_id num_type)
Definition: ls-hdf5.cc:328
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
dim_vector dv
Definition: sub2ind.cc:263
mxArray * as_mxArray(void) const
double log2(double x)
Definition: lo-mappers.cc:233
static const double pi
Definition: lo-specfun.cc:3610
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:454