00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include <iostream>
00028
00029 #include "lo-ieee.h"
00030 #include "lo-specfun.h"
00031 #include "lo-mappers.h"
00032
00033 #include "oct-obj.h"
00034 #include "oct-stream.h"
00035 #include "ops.h"
00036 #include "ov-complex.h"
00037 #include "ov-base.h"
00038 #include "ov-base-scalar.h"
00039 #include "ov-base-scalar.cc"
00040 #include "ov-flt-cx-mat.h"
00041 #include "ov-float.h"
00042 #include "ov-flt-complex.h"
00043 #include "gripes.h"
00044 #include "pr-output.h"
00045 #include "ops.h"
00046
00047 #include "ls-oct-ascii.h"
00048 #include "ls-hdf5.h"
00049
00050 template class octave_base_scalar<FloatComplex>;
00051
00052 DEFINE_OCTAVE_ALLOCATOR (octave_float_complex);
00053
00054 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_float_complex,
00055 "float complex scalar", "single");
00056
00057 octave_base_value *
00058 octave_float_complex::try_narrowing_conversion (void)
00059 {
00060 octave_base_value *retval = 0;
00061
00062 float im = std::imag (scalar);
00063
00064 if (im == 0.0)
00065 retval = new octave_float_scalar (std::real (scalar));
00066
00067 return retval;
00068 }
00069
00070 octave_value
00071 octave_float_complex::do_index_op (const octave_value_list& idx, bool resize_ok)
00072 {
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 octave_value tmp (new octave_float_complex_matrix (float_complex_matrix_value ()));
00084
00085 return tmp.do_index_op (idx, resize_ok);
00086 }
00087
00088 double
00089 octave_float_complex::double_value (bool force_conversion) const
00090 {
00091 double retval = lo_ieee_nan_value ();
00092
00093 if (! force_conversion)
00094 gripe_implicit_conversion ("Octave:imag-to-real",
00095 "complex scalar", "real scalar");
00096
00097 retval = std::real (scalar);
00098
00099 return retval;
00100 }
00101
00102 float
00103 octave_float_complex::float_value (bool force_conversion) const
00104 {
00105 float retval = lo_ieee_float_nan_value ();
00106
00107 if (! force_conversion)
00108 gripe_implicit_conversion ("Octave:imag-to-real",
00109 "complex scalar", "real scalar");
00110
00111 retval = std::real (scalar);
00112
00113 return retval;
00114 }
00115
00116 Matrix
00117 octave_float_complex::matrix_value (bool force_conversion) const
00118 {
00119 Matrix retval;
00120
00121 if (! force_conversion)
00122 gripe_implicit_conversion ("Octave:imag-to-real",
00123 "complex scalar", "real matrix");
00124
00125 retval = Matrix (1, 1, std::real (scalar));
00126
00127 return retval;
00128 }
00129
00130 FloatMatrix
00131 octave_float_complex::float_matrix_value (bool force_conversion) const
00132 {
00133 FloatMatrix retval;
00134
00135 if (! force_conversion)
00136 gripe_implicit_conversion ("Octave:imag-to-real",
00137 "complex scalar", "real matrix");
00138
00139 retval = FloatMatrix (1, 1, std::real (scalar));
00140
00141 return retval;
00142 }
00143
00144 NDArray
00145 octave_float_complex::array_value (bool force_conversion) const
00146 {
00147 NDArray retval;
00148
00149 if (! force_conversion)
00150 gripe_implicit_conversion ("Octave:imag-to-real",
00151 "complex scalar", "real matrix");
00152
00153 retval = NDArray (dim_vector (1, 1), std::real (scalar));
00154
00155 return retval;
00156 }
00157
00158 FloatNDArray
00159 octave_float_complex::float_array_value (bool force_conversion) const
00160 {
00161 FloatNDArray retval;
00162
00163 if (! force_conversion)
00164 gripe_implicit_conversion ("Octave:imag-to-real",
00165 "complex scalar", "real matrix");
00166
00167 retval = FloatNDArray (dim_vector (1, 1), std::real (scalar));
00168
00169 return retval;
00170 }
00171
00172 Complex
00173 octave_float_complex::complex_value (bool) const
00174 {
00175 return scalar;
00176 }
00177
00178 FloatComplex
00179 octave_float_complex::float_complex_value (bool) const
00180 {
00181 return static_cast<FloatComplex> (scalar);
00182 }
00183
00184 ComplexMatrix
00185 octave_float_complex::complex_matrix_value (bool) const
00186 {
00187 return ComplexMatrix (1, 1, scalar);
00188 }
00189
00190 FloatComplexMatrix
00191 octave_float_complex::float_complex_matrix_value (bool) const
00192 {
00193 return FloatComplexMatrix (1, 1, scalar);
00194 }
00195
00196 ComplexNDArray
00197 octave_float_complex::complex_array_value (bool ) const
00198 {
00199 return ComplexNDArray (dim_vector (1, 1), scalar);
00200 }
00201
00202 FloatComplexNDArray
00203 octave_float_complex::float_complex_array_value (bool ) const
00204 {
00205 return FloatComplexNDArray (dim_vector (1, 1), scalar);
00206 }
00207
00208 octave_value
00209 octave_float_complex::resize (const dim_vector& dv, bool fill) const
00210 {
00211 if (fill)
00212 {
00213 FloatComplexNDArray retval (dv, FloatComplexNDArray::resize_fill_value ());
00214
00215 if (dv.numel ())
00216 retval(0) = scalar;
00217
00218 return retval;
00219 }
00220 else
00221 {
00222 FloatComplexNDArray retval (dv);
00223
00224 if (dv.numel ())
00225 retval(0) = scalar;
00226
00227 return retval;
00228 }
00229 }
00230
00231 bool
00232 octave_float_complex::save_ascii (std::ostream& os)
00233 {
00234 FloatComplex c = float_complex_value ();
00235
00236 octave_write_float_complex (os, c);
00237
00238 os << "\n";
00239
00240 return true;
00241 }
00242
00243 bool
00244 octave_float_complex::load_ascii (std::istream& is)
00245 {
00246 scalar = octave_read_value<FloatComplex> (is);
00247
00248 if (!is)
00249 {
00250 error ("load: failed to load complex scalar constant");
00251 return false;
00252 }
00253
00254 return true;
00255 }
00256
00257
00258 bool
00259 octave_float_complex::save_binary (std::ostream& os, bool& )
00260 {
00261 char tmp = static_cast<char> (LS_FLOAT);
00262 os.write (reinterpret_cast<char *> (&tmp), 1);
00263 FloatComplex ctmp = float_complex_value ();
00264 os.write (reinterpret_cast<char *> (&ctmp), 8);
00265
00266 return true;
00267 }
00268
00269 bool
00270 octave_float_complex::load_binary (std::istream& is, bool swap,
00271 oct_mach_info::float_format fmt)
00272 {
00273 char tmp;
00274 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
00275 return false;
00276
00277 FloatComplex ctmp;
00278 read_floats (is, reinterpret_cast<float *> (&ctmp),
00279 static_cast<save_type> (tmp), 2, swap, fmt);
00280 if (error_state || ! is)
00281 return false;
00282
00283 scalar = ctmp;
00284 return true;
00285 }
00286
00287 #if defined (HAVE_HDF5)
00288
00289 bool
00290 octave_float_complex::save_hdf5 (hid_t loc_id, const char *name,
00291 bool )
00292 {
00293 hsize_t dimens[3];
00294 hid_t space_hid = -1, type_hid = -1, data_hid = -1;
00295 bool retval = true;
00296
00297 space_hid = H5Screate_simple (0, dimens, 0);
00298 if (space_hid < 0)
00299 return false;
00300
00301 type_hid = hdf5_make_complex_type (H5T_NATIVE_FLOAT);
00302 if (type_hid < 0)
00303 {
00304 H5Sclose (space_hid);
00305 return false;
00306 }
00307 #if HAVE_HDF5_18
00308 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid,
00309 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00310 #else
00311 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT);
00312 #endif
00313 if (data_hid < 0)
00314 {
00315 H5Sclose (space_hid);
00316 H5Tclose (type_hid);
00317 return false;
00318 }
00319
00320 FloatComplex tmp = float_complex_value ();
00321 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
00322 &tmp) >= 0;
00323
00324 H5Dclose (data_hid);
00325 H5Tclose (type_hid);
00326 H5Sclose (space_hid);
00327
00328 return retval;
00329 }
00330
00331 bool
00332 octave_float_complex::load_hdf5 (hid_t loc_id, const char *name)
00333 {
00334 bool retval = false;
00335 #if HAVE_HDF5_18
00336 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
00337 #else
00338 hid_t data_hid = H5Dopen (loc_id, name);
00339 #endif
00340 hid_t type_hid = H5Dget_type (data_hid);
00341
00342 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_FLOAT);
00343
00344 if (! hdf5_types_compatible (type_hid, complex_type))
00345 {
00346 H5Tclose (complex_type);
00347 H5Dclose (data_hid);
00348 return false;
00349 }
00350
00351 hid_t space_id = H5Dget_space (data_hid);
00352 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
00353
00354 if (rank != 0)
00355 {
00356 H5Tclose (complex_type);
00357 H5Sclose (space_id);
00358 H5Dclose (data_hid);
00359 return false;
00360 }
00361
00362
00363 FloatComplex ctmp;
00364 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
00365 &ctmp) >= 0)
00366 {
00367 retval = true;
00368 scalar = ctmp;
00369 }
00370
00371 H5Tclose (complex_type);
00372 H5Sclose (space_id);
00373 H5Dclose (data_hid);
00374
00375 return retval;
00376 }
00377
00378 #endif
00379
00380 mxArray *
00381 octave_float_complex::as_mxArray (void) const
00382 {
00383 mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxCOMPLEX);
00384
00385 float *pr = static_cast<float *> (retval->get_data ());
00386 float *pi = static_cast<float *> (retval->get_imag_data ());
00387
00388 pr[0] = std::real (scalar);
00389 pi[0] = std::imag (scalar);
00390
00391 return retval;
00392 }
00393
00394 octave_value
00395 octave_float_complex::map (unary_mapper_t umap) const
00396 {
00397 switch (umap)
00398 {
00399 #define SCALAR_MAPPER(UMAP, FCN) \
00400 case umap_ ## UMAP: \
00401 return octave_value (FCN (scalar))
00402
00403 SCALAR_MAPPER (abs, std::abs);
00404 SCALAR_MAPPER (acos, ::acos);
00405 SCALAR_MAPPER (acosh, ::acosh);
00406 SCALAR_MAPPER (angle, std::arg);
00407 SCALAR_MAPPER (arg, std::arg);
00408 SCALAR_MAPPER (asin, ::asin);
00409 SCALAR_MAPPER (asinh, ::asinh);
00410 SCALAR_MAPPER (atan, ::atan);
00411 SCALAR_MAPPER (atanh, ::atanh);
00412 SCALAR_MAPPER (ceil, ::ceil);
00413 SCALAR_MAPPER (conj, std::conj);
00414 SCALAR_MAPPER (cos, std::cos);
00415 SCALAR_MAPPER (cosh, std::cosh);
00416 SCALAR_MAPPER (exp, std::exp);
00417 SCALAR_MAPPER (expm1, ::expm1);
00418 SCALAR_MAPPER (fix, ::fix);
00419 SCALAR_MAPPER (floor, ::floor);
00420 SCALAR_MAPPER (imag, std::imag);
00421 SCALAR_MAPPER (log, std::log);
00422 SCALAR_MAPPER (log2, xlog2);
00423 SCALAR_MAPPER (log10, std::log10);
00424 SCALAR_MAPPER (log1p, ::log1p);
00425 SCALAR_MAPPER (real, std::real);
00426 SCALAR_MAPPER (round, xround);
00427 SCALAR_MAPPER (roundb, xroundb);
00428 SCALAR_MAPPER (signum, ::signum);
00429 SCALAR_MAPPER (sin, std::sin);
00430 SCALAR_MAPPER (sinh, std::sinh);
00431 SCALAR_MAPPER (sqrt, std::sqrt);
00432 SCALAR_MAPPER (tan, std::tan);
00433 SCALAR_MAPPER (tanh, std::tanh);
00434 SCALAR_MAPPER (finite, xfinite);
00435 SCALAR_MAPPER (isinf, xisinf);
00436 SCALAR_MAPPER (isna, octave_is_NA);
00437 SCALAR_MAPPER (isnan, xisnan);
00438
00439 default:
00440 return octave_base_value::map (umap);
00441 }
00442 }