comparison libinterp/octave-value/ov-float.cc @ 15195:2fc554ffbc28

split libinterp from src * libinterp: New directory. Move all files from src directory here except Makefile.am, main.cc, main-cli.cc, mkoctfile.in.cc, mkoctfilr.in.sh, octave-config.in.cc, octave-config.in.sh. * libinterp/Makefile.am: New file, extracted from src/Makefile.am. * src/Makefile.am: Delete everything except targets and definitions needed to build and link main and utility programs. * Makefile.am (SUBDIRS): Include libinterp in the list. * autogen.sh: Run config-module.sh in libinterp/dldfcn directory, not src/dldfcn directory. * configure.ac (AC_CONFIG_SRCDIR): Use libinterp/octave.cc, not src/octave.cc. (DL_LDFLAGS, LIBOCTINTERP): Use libinterp, not src. (AC_CONFIG_FILES): Include libinterp/Makefile in the list. * find-docstring-files.sh: Look in libinterp, not src. * gui/src/Makefile.am (liboctgui_la_CPPFLAGS): Find header files in libinterp, not src.
author John W. Eaton <jwe@octave.org>
date Sat, 18 Aug 2012 16:23:39 -0400
parents src/octave-value/ov-float.cc@62a35ae7d6a2
children 9020dddc925a
comparison
equal deleted inserted replaced
15194:0f0b795044c3 15195:2fc554ffbc28
1 /*
2
3 Copyright (C) 1996-2012 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-stream.h"
39 #include "ov-scalar.h"
40 #include "ov-float.h"
41 #include "ov-base.h"
42 #include "ov-base-scalar.h"
43 #include "ov-base-scalar.cc"
44 #include "ov-flt-re-mat.h"
45 #include "ov-typeinfo.h"
46 #include "pr-output.h"
47 #include "xdiv.h"
48 #include "xpow.h"
49 #include "ops.h"
50
51 #include "ls-oct-ascii.h"
52 #include "ls-hdf5.h"
53
54 template class octave_base_scalar<float>;
55
56 DEFINE_OCTAVE_ALLOCATOR (octave_float_scalar);
57
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_float_scalar, "float scalar", "single");
59
60 octave_value
61 octave_float_scalar::do_index_op (const octave_value_list& idx, bool resize_ok)
62 {
63 // FIXME -- this doesn't solve the problem of
64 //
65 // a = 1; a([1,1], [1,1], [1,1])
66 //
67 // and similar constructions. Hmm...
68
69 // FIXME -- using this constructor avoids narrowing the
70 // 1x1 matrix back to a scalar value. Need a better solution
71 // to this problem.
72
73 octave_value tmp (new octave_float_matrix (float_matrix_value ()));
74
75 return tmp.do_index_op (idx, resize_ok);
76 }
77
78 octave_value
79 octave_float_scalar::resize (const dim_vector& dv, bool fill) const
80 {
81 if (fill)
82 {
83 FloatNDArray retval (dv, 0);
84
85 if (dv.numel ())
86 retval(0) = scalar;
87
88 return retval;
89 }
90 else
91 {
92 FloatNDArray retval (dv);
93
94 if (dv.numel ())
95 retval(0) = scalar;
96
97 return retval;
98 }
99 }
100
101 octave_value
102 octave_float_scalar::diag (octave_idx_type m, octave_idx_type n) const
103 {
104 return FloatDiagMatrix (Array<float> (dim_vector (1, 1), scalar), m, n);
105 }
106
107 octave_value
108 octave_float_scalar::convert_to_str_internal (bool, bool, char type) const
109 {
110 octave_value retval;
111
112 if (xisnan (scalar))
113 gripe_nan_to_character_conversion ();
114 else
115 {
116 int ival = NINT (scalar);
117
118 if (ival < 0 || ival > UCHAR_MAX)
119 {
120 // FIXME -- is there something better we could do?
121
122 ival = 0;
123
124 ::warning ("range error for conversion to character value");
125 }
126
127 retval = octave_value (std::string (1, static_cast<char> (ival)), type);
128 }
129
130 return retval;
131 }
132
133 bool
134 octave_float_scalar::save_ascii (std::ostream& os)
135 {
136 float d = float_value ();
137
138 octave_write_float (os, d);
139
140 os << "\n";
141
142 return true;
143 }
144
145 bool
146 octave_float_scalar::load_ascii (std::istream& is)
147 {
148 scalar = octave_read_value<float> (is);
149 if (!is)
150 {
151 error ("load: failed to load scalar constant");
152 return false;
153 }
154
155 return true;
156 }
157
158 bool
159 octave_float_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
160 {
161 char tmp = LS_FLOAT;
162 os.write (reinterpret_cast<char *> (&tmp), 1);
163 float dtmp = float_value ();
164 os.write (reinterpret_cast<char *> (&dtmp), 4);
165
166 return true;
167 }
168
169 bool
170 octave_float_scalar::load_binary (std::istream& is, bool swap,
171 oct_mach_info::float_format fmt)
172 {
173 char tmp;
174 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
175 return false;
176
177 float dtmp;
178 read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
179 if (error_state || ! is)
180 return false;
181
182 scalar = dtmp;
183 return true;
184 }
185
186 #if defined (HAVE_HDF5)
187
188 bool
189 octave_float_scalar::save_hdf5 (hid_t loc_id, const char *name,
190 bool /* save_as_floats */)
191 {
192 hsize_t dimens[3];
193 hid_t space_hid = -1, data_hid = -1;
194 bool retval = true;
195
196 space_hid = H5Screate_simple (0, dimens, 0);
197 if (space_hid < 0) return false;
198 #if HAVE_HDF5_18
199 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
200 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
201 #else
202 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
203 H5P_DEFAULT);
204 #endif
205 if (data_hid < 0)
206 {
207 H5Sclose (space_hid);
208 return false;
209 }
210
211 float tmp = float_value ();
212 retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
213 H5P_DEFAULT, &tmp) >= 0;
214
215 H5Dclose (data_hid);
216 H5Sclose (space_hid);
217
218 return retval;
219 }
220
221 bool
222 octave_float_scalar::load_hdf5 (hid_t loc_id, const char *name)
223 {
224 #if HAVE_HDF5_18
225 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
226 #else
227 hid_t data_hid = H5Dopen (loc_id, name);
228 #endif
229 hid_t space_id = H5Dget_space (data_hid);
230
231 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
232
233 if (rank != 0)
234 {
235 H5Dclose (data_hid);
236 return false;
237 }
238
239 float dtmp;
240 if (H5Dread (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
241 H5P_DEFAULT, &dtmp) < 0)
242 {
243 H5Dclose (data_hid);
244 return false;
245 }
246
247 scalar = dtmp;
248
249 H5Dclose (data_hid);
250
251 return true;
252 }
253
254 #endif
255
256 mxArray *
257 octave_float_scalar::as_mxArray (void) const
258 {
259 mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxREAL);
260
261 float *pr = static_cast<float *> (retval->get_data ());
262
263 pr[0] = scalar;
264
265 return retval;
266 }
267
268 octave_value
269 octave_float_scalar::map (unary_mapper_t umap) const
270 {
271 switch (umap)
272 {
273 case umap_imag:
274 return 0.0f;
275
276 case umap_real:
277 case umap_conj:
278 return scalar;
279
280 #define SCALAR_MAPPER(UMAP, FCN) \
281 case umap_ ## UMAP: \
282 return octave_value (FCN (scalar))
283
284 SCALAR_MAPPER (abs, ::fabsf);
285 SCALAR_MAPPER (acos, rc_acos);
286 SCALAR_MAPPER (acosh, rc_acosh);
287 SCALAR_MAPPER (angle, ::arg);
288 SCALAR_MAPPER (arg, ::arg);
289 SCALAR_MAPPER (asin, rc_asin);
290 SCALAR_MAPPER (asinh, ::asinhf);
291 SCALAR_MAPPER (atan, ::atanf);
292 SCALAR_MAPPER (atanh, rc_atanh);
293 SCALAR_MAPPER (erf, ::erff);
294 SCALAR_MAPPER (erfinv, ::erfinv);
295 SCALAR_MAPPER (erfcinv, ::erfcinv);
296 SCALAR_MAPPER (erfc, ::erfcf);
297 SCALAR_MAPPER (erfcx, ::erfcx);
298 SCALAR_MAPPER (gamma, xgamma);
299 SCALAR_MAPPER (lgamma, rc_lgamma);
300 SCALAR_MAPPER (cbrt, ::cbrtf);
301 SCALAR_MAPPER (ceil, ::ceilf);
302 SCALAR_MAPPER (cos, ::cosf);
303 SCALAR_MAPPER (cosh, ::coshf);
304 SCALAR_MAPPER (exp, ::expf);
305 SCALAR_MAPPER (expm1, ::expm1f);
306 SCALAR_MAPPER (fix, ::fix);
307 SCALAR_MAPPER (floor, ::floorf);
308 SCALAR_MAPPER (log, rc_log);
309 SCALAR_MAPPER (log2, rc_log2);
310 SCALAR_MAPPER (log10, rc_log10);
311 SCALAR_MAPPER (log1p, rc_log1p);
312 SCALAR_MAPPER (round, xround);
313 SCALAR_MAPPER (roundb, xroundb);
314 SCALAR_MAPPER (signum, ::signum);
315 SCALAR_MAPPER (sin, ::sinf);
316 SCALAR_MAPPER (sinh, ::sinhf);
317 SCALAR_MAPPER (sqrt, rc_sqrt);
318 SCALAR_MAPPER (tan, ::tanf);
319 SCALAR_MAPPER (tanh, ::tanhf);
320 SCALAR_MAPPER (finite, xfinite);
321 SCALAR_MAPPER (isinf, xisinf);
322 SCALAR_MAPPER (isna, octave_is_NA);
323 SCALAR_MAPPER (isnan, xisnan);
324
325 default:
326 return octave_base_value::map (umap);
327 }
328 }
329
330 bool
331 octave_float_scalar::fast_elem_insert_self (void *where, builtin_type_t btyp) const
332 {
333
334 // Support inline real->complex conversion.
335 if (btyp == btyp_float)
336 {
337 *(reinterpret_cast<float *>(where)) = scalar;
338 return true;
339 }
340 else if (btyp == btyp_float_complex)
341 {
342 *(reinterpret_cast<FloatComplex *>(where)) = scalar;
343 return true;
344 }
345 else
346 return false;
347 }