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-scalar.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 "data-conv.h"
30 #include "mach-info.h"
31 #include "lo-specfun.h"
32 #include "lo-mappers.h"
33 
34 #include "defun.h"
35 #include "gripes.h"
36 #include "mxarray.h"
37 #include "oct-obj.h"
38 #include "oct-hdf5.h"
39 #include "oct-stream.h"
40 #include "ov-scalar.h"
41 #include "ov-float.h"
42 #include "ov-base.h"
43 #include "ov-base-scalar.h"
44 #include "ov-base-scalar.cc"
45 #include "ov-re-mat.h"
46 #include "ov-typeinfo.h"
47 #include "pr-output.h"
48 #include "xdiv.h"
49 #include "xpow.h"
50 #include "ops.h"
51 
52 #include "ls-oct-ascii.h"
53 #include "ls-hdf5.h"
54 
55 template class octave_base_scalar<double>;
56 
57 
59 
60 static octave_base_value *
62 {
64 
65  return new octave_float_scalar (v.float_value ());
66 }
67 
70 {
74 }
75 
77 octave_scalar::do_index_op (const octave_value_list& idx, bool resize_ok)
78 {
79  // FIXME: this doesn't solve the problem of
80  //
81  // a = 1; a([1,1], [1,1], [1,1])
82  //
83  // and similar constructions. Hmm...
84 
85  // FIXME: using this constructor avoids narrowing the
86  // 1x1 matrix back to a scalar value. Need a better solution
87  // to this problem.
88 
90 
91  return tmp.do_index_op (idx, resize_ok);
92 }
93 
95 octave_scalar::resize (const dim_vector& dv, bool fill) const
96 {
97  if (fill)
98  {
99  NDArray retval (dv, 0);
100 
101  if (dv.numel ())
102  retval(0) = scalar;
103 
104  return retval;
105  }
106  else
107  {
108  NDArray retval (dv);
109 
110  if (dv.numel ())
111  retval(0) = scalar;
112 
113  return retval;
114  }
115 }
116 
119 {
120  return DiagMatrix (Array<double> (dim_vector (1, 1), scalar), m, n);
121 }
122 
125 {
126  octave_value retval;
127 
128  if (xisnan (scalar))
130  else
131  {
132  int ival = NINT (scalar);
133 
134  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
135  {
136  // FIXME: is there something better we could do?
137 
138  ival = 0;
139 
140  ::warning ("range error for conversion to character value");
141  }
142 
143  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
144  }
145 
146  return retval;
147 }
148 
149 bool
150 octave_scalar::save_ascii (std::ostream& os)
151 {
152  double d = double_value ();
153 
154  octave_write_double (os, d);
155 
156  os << "\n";
157 
158  return true;
159 }
160 
161 bool
162 octave_scalar::load_ascii (std::istream& is)
163 {
164  scalar = octave_read_value<double> (is);
165  if (!is)
166  {
167  error ("load: failed to load scalar constant");
168  return false;
169  }
170 
171  return true;
172 }
173 
174 bool
175 octave_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
176 {
177  char tmp = LS_DOUBLE;
178  os.write (reinterpret_cast<char *> (&tmp), 1);
179  double dtmp = double_value ();
180  os.write (reinterpret_cast<char *> (&dtmp), 8);
181 
182  return true;
183 }
184 
185 bool
186 octave_scalar::load_binary (std::istream& is, bool swap,
188 {
189  char tmp;
190  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
191  return false;
192 
193  double dtmp;
194  read_doubles (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
195  if (error_state || ! is)
196  return false;
197 
198  scalar = dtmp;
199  return true;
200 }
201 
202 bool
203 octave_scalar::save_hdf5 (octave_hdf5_id loc_id, const char *name,
204  bool /* save_as_floats */)
205 {
206  bool retval = false;
207 
208 #if defined (HAVE_HDF5)
209 
210  hsize_t dimens[3];
211  hid_t space_hid, data_hid;
212  space_hid = data_hid = -1;
213 
214  space_hid = H5Screate_simple (0, dimens, 0);
215  if (space_hid < 0) return false;
216 
217 #if HAVE_HDF5_18
218  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
219  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
220 #else
221  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
222  H5P_DEFAULT);
223 #endif
224  if (data_hid < 0)
225  {
226  H5Sclose (space_hid);
227  return false;
228  }
229 
230  double tmp = double_value ();
231  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
232  H5P_DEFAULT, &tmp) >= 0;
233 
234  H5Dclose (data_hid);
235  H5Sclose (space_hid);
236 
237 #else
238  gripe_save ("hdf5");
239 #endif
240 
241  return retval;
242 }
243 
244 bool
245 octave_scalar::load_hdf5 (octave_hdf5_id loc_id, const char *name)
246 {
247 #if defined (HAVE_HDF5)
248 
249 #if HAVE_HDF5_18
250  hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
251 #else
252  hid_t data_hid = H5Dopen (loc_id, name);
253 #endif
254  hid_t space_id = H5Dget_space (data_hid);
255 
256  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
257 
258  if (rank != 0)
259  {
260  H5Dclose (data_hid);
261  return false;
262  }
263 
264  double dtmp;
265  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
266  H5P_DEFAULT, &dtmp) < 0)
267  {
268  H5Dclose (data_hid);
269  return false;
270  }
271 
272  scalar = dtmp;
273 
274  H5Dclose (data_hid);
275 
276  return true;
277 
278 #else
279  gripe_load ("hdf5");
280  return false;
281 #endif
282 }
283 
284 mxArray *
286 {
287  mxArray *retval = new mxArray (mxDOUBLE_CLASS, 1, 1, mxREAL);
288 
289  double *pr = static_cast<double *> (retval->get_data ());
290 
291  pr[0] = scalar;
292 
293  return retval;
294 }
295 
298 {
299  switch (umap)
300  {
301  case umap_imag:
302  return 0.0;
303 
304  case umap_real:
305  case umap_conj:
306  return scalar;
307 
308 #define SCALAR_MAPPER(UMAP, FCN) \
309  case umap_ ## UMAP: \
310  return octave_value (FCN (scalar))
311 
312  SCALAR_MAPPER (abs, ::fabs);
315  SCALAR_MAPPER (angle, ::arg);
316  SCALAR_MAPPER (arg, ::arg);
319  SCALAR_MAPPER (atan, ::atan);
321  SCALAR_MAPPER (erf, ::erf);
324  SCALAR_MAPPER (erfc, ::erfc);
326  SCALAR_MAPPER (erfi, ::erfi);
329  SCALAR_MAPPER (lgamma, rc_lgamma);
330  SCALAR_MAPPER (cbrt, ::cbrt);
331  SCALAR_MAPPER (ceil, ::ceil);
332  SCALAR_MAPPER (cos, ::cos);
333  SCALAR_MAPPER (cosh, ::cosh);
334  SCALAR_MAPPER (exp, ::exp);
336  SCALAR_MAPPER (fix, ::fix);
338  SCALAR_MAPPER (log, rc_log);
339  SCALAR_MAPPER (log2, rc_log2);
340  SCALAR_MAPPER (log10, rc_log10);
342  SCALAR_MAPPER (round, xround);
343  SCALAR_MAPPER (roundb, xroundb);
345  SCALAR_MAPPER (sin, ::sin);
346  SCALAR_MAPPER (sinh, ::sinh);
347  SCALAR_MAPPER (sqrt, rc_sqrt);
348  SCALAR_MAPPER (tan, ::tan);
349  SCALAR_MAPPER (tanh, ::tanh);
350  SCALAR_MAPPER (finite, xfinite);
351  SCALAR_MAPPER (isinf, xisinf);
352  SCALAR_MAPPER (isna, octave_is_NA);
353  SCALAR_MAPPER (isnan, xisnan);
355 
356  // Special cases for Matlab compatibility.
357  case umap_xtolower:
358  case umap_xtoupper:
359  return scalar;
360 
361  case umap_xisalnum:
362  case umap_xisalpha:
363  case umap_xisascii:
364  case umap_xiscntrl:
365  case umap_xisdigit:
366  case umap_xisgraph:
367  case umap_xislower:
368  case umap_xisprint:
369  case umap_xispunct:
370  case umap_xisspace:
371  case umap_xisupper:
372  case umap_xisxdigit:
373  case umap_xtoascii:
374  {
375  octave_value str_conv = convert_to_str (true, true);
376  return error_state ? octave_value () : str_conv.map (umap);
377  }
378 
379  default:
380  return octave_base_value::map (umap);
381  }
382 }
383 
384 bool
386 {
387 
388  // Support inline real->complex conversion.
389  if (btyp == btyp_double)
390  {
391  *(reinterpret_cast<double *>(where)) = scalar;
392  return true;
393  }
394  else if (btyp == btyp_complex)
395  {
396  *(reinterpret_cast<Complex *>(where)) = scalar;
397  return true;
398  }
399  else
400  return false;
401 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:386
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:151
bool load_ascii(std::istream &is)
Definition: ov-scalar.cc:162
Complex rc_asin(double x)
Definition: lo-mappers.cc:536
T xsignbit(T x)
Definition: lo-mappers.h:378
double xround(double x)
Definition: lo-mappers.cc:63
bool xisnan(double x)
Definition: lo-mappers.cc:144
double cbrt(double x)
Definition: lo-specfun.cc:662
double log1p(double x)
Definition: lo-specfun.cc:620
double erfcinv(double x)
Definition: lo-specfun.cc:3151
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-scalar.cc:203
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void gripe_load(const char *type) const
Definition: ov-base.cc:1258
Complex rc_acos(double x)
Definition: lo-mappers.cc:512
function atanh(X)
Definition: atanh.f:2
void error(const char *fmt,...)
Definition: error.cc:476
void * get_data(void) const
Definition: mxarray.h:433
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1226
bool xisinf(double x)
Definition: lo-mappers.cc:160
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
#define SCALAR_MAPPER(UMAP, FCN)
void gripe_save(const char *type) const
Definition: ov-base.cc:1267
double expm1(double x)
Definition: lo-specfun.cc:510
builtin_type_t
Definition: ov-base.h:59
std::complex< double > erf(std::complex< double > z, double relerr=0)
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-scalar.cc:95
octave_value map(unary_mapper_t umap) const
Definition: ov-scalar.cc:297
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
F77_RET_T const double const double double * d
subroutine xgamma(x, result)
Definition: xgamma.f:1
double signum(double x)
Definition: lo-mappers.cc:80
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2977
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-scalar.cc:385
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:411
#define CAST_CONV_ARG(t)
Definition: ops.h:83
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-scalar.cc:175
function asinh(X)
Definition: asinh.f:2
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:275
static int static_type_id(void)
Definition: ov-float.h:255
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-scalar.cc:61
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-scalar.cc:186
int error_state
Definition: error.cc:101
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:606
void gripe_nan_to_character_conversion(void)
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-scalar.cc:118
Complex rc_log(double x)
Definition: lo-mappers.cc:561
Complex rc_log2(double x)
Definition: lo-mappers.cc:577
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
double xroundb(double x)
Definition: lo-mappers.cc:69
Complex rc_atanh(double x)
Definition: lo-mappers.cc:548
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
int NINT(double x)
Definition: lo-mappers.cc:657
double erfinv(double x)
Definition: lo-specfun.cc:3063
Complex rc_log10(double x)
Definition: lo-mappers.cc:591
void mxArray
Definition: mex.h:53
function acosh(X)
Definition: acosh.f:2
double arg(double x)
Definition: lo-mappers.h:37
friend class octave_value
Definition: ov-base.h:206
void warning(const char *fmt,...)
Definition: error.cc:681
type_conv_info numeric_demotion_function(void) const
Definition: ov-scalar.cc:69
double double_value(bool=false) const
Definition: ov-scalar.h:141
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
mxArray * as_mxArray(void) const
Definition: ov-scalar.cc:285
bool save_ascii(std::ostream &os)
Definition: ov-scalar.cc:150
bool xfinite(double x)
Definition: lo-mappers.cc:152
Complex rc_acosh(double x)
Definition: lo-mappers.cc:524
function gamma(X)
Definition: gamma.f:2
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1276
double fix(double x)
Definition: lo-mappers.h:39
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-scalar.cc:245
float dawson(float x)
Definition: lo-specfun.cc:349
Complex asin(const Complex &x)
Definition: lo-mappers.cc:204
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-scalar.cc:124
Definition: mxarray.h:52
std::complex< double > Complex
Definition: oct-cmplx.h:29
Complex acos(const Complex &x)
Definition: lo-mappers.cc:177
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
Definition: ov-base.cc:373
T abs(T x)
Definition: pr-output.cc:3062
Complex atan(const Complex &x)
Definition: lo-mappers.cc:231
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-scalar.cc:77
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