comparison src/ov-float.cc @ 7789:82be108cc558

First attempt at single precision tyeps * * * corrections to qrupdate single precision routines * * * prefer demotion to single over promotion to double * * * Add single precision support to log2 function * * * Trivial PROJECT file update * * * Cache optimized hermitian/transpose methods * * * Add tests for tranpose/hermitian and ChangeLog entry for new transpose code
author David Bateman <dbateman@free.fr>
date Sun, 27 Apr 2008 22:34:17 +0200
parents
children 87865ed7405f
comparison
equal deleted inserted replaced
7788:45f5faba05a2 7789:82be108cc558
1 /*
2
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006,
4 2007 John W. Eaton
5
6 This file is part of Octave.
7
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <iostream>
29
30 #include "data-conv.h"
31 #include "mach-info.h"
32 #include "lo-specfun.h"
33 #include "lo-mappers.h"
34
35 #include "defun.h"
36 #include "gripes.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 octave_value retval;
64
65 if (idx.valid_scalar_indices ())
66 retval = scalar;
67 else
68 {
69 // FIXME -- this doesn't solve the problem of
70 //
71 // a = 1; a([1,1], [1,1], [1,1])
72 //
73 // and similar constructions. Hmm...
74
75 // FIXME -- using this constructor avoids narrowing the
76 // 1x1 matrix back to a scalar value. Need a better solution
77 // to this problem.
78
79 octave_value tmp (new octave_matrix (matrix_value ()));
80
81 retval = tmp.do_index_op (idx, resize_ok);
82 }
83
84 return retval;
85 }
86
87 std::streamoff
88 octave_float_scalar::streamoff_value (void) const
89 {
90 std::streamoff retval (-1);
91
92 if (D_NINT (scalar) == scalar)
93 retval = std::streamoff (static_cast<long> (scalar));
94 else
95 error ("conversion to streamoff value failed");
96
97 return retval;
98 }
99
100 streamoff_array
101 octave_float_scalar::streamoff_array_value (void) const
102 {
103 streamoff_array retval;
104
105 std::streamoff soff = streamoff_value ();
106
107 if (! error_state)
108 retval = streamoff_array (dim_vector (1, 1), soff);
109
110 return retval;
111 }
112
113 octave_value
114 octave_float_scalar::resize (const dim_vector& dv, bool fill) const
115 {
116 if (fill)
117 {
118 NDArray retval (dv, NDArray::resize_fill_value());
119
120 if (dv.numel ())
121 retval(0) = scalar;
122
123 return retval;
124 }
125 else
126 {
127 NDArray retval (dv);
128
129 if (dv.numel ())
130 retval(0) = scalar;
131
132 return retval;
133 }
134 }
135
136 octave_value
137 octave_float_scalar::convert_to_str_internal (bool, bool, char type) const
138 {
139 octave_value retval;
140
141 if (xisnan (scalar))
142 ::error ("invalid conversion from NaN to character");
143 else
144 {
145 int ival = NINT (scalar);
146
147 if (ival < 0 || ival > UCHAR_MAX)
148 {
149 // FIXME -- is there something better we could do?
150
151 ival = 0;
152
153 ::warning ("range error for conversion to character value");
154 }
155
156 retval = octave_value (std::string (1, static_cast<char> (ival)), type);
157 }
158
159 return retval;
160 }
161
162 bool
163 octave_float_scalar::save_ascii (std::ostream& os)
164 {
165 float d = float_value ();
166
167 octave_write_float (os, d);
168
169 os << "\n";
170
171 return true;
172 }
173
174 bool
175 octave_float_scalar::load_ascii (std::istream& is)
176 {
177 scalar = octave_read_float (is);
178 if (!is)
179 {
180 error ("load: failed to load scalar constant");
181 return false;
182 }
183
184 return true;
185 }
186
187 bool
188 octave_float_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
189 {
190 char tmp = LS_FLOAT;
191 os.write (reinterpret_cast<char *> (&tmp), 1);
192 float dtmp = float_value ();
193 os.write (reinterpret_cast<char *> (&dtmp), 4);
194
195 return true;
196 }
197
198 bool
199 octave_float_scalar::load_binary (std::istream& is, bool swap,
200 oct_mach_info::float_format fmt)
201 {
202 char tmp;
203 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
204 return false;
205
206 float dtmp;
207 read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
208 if (error_state || ! is)
209 return false;
210
211 scalar = dtmp;
212 return true;
213 }
214
215 #if defined (HAVE_HDF5)
216
217 bool
218 octave_float_scalar::save_hdf5 (hid_t loc_id, const char *name,
219 bool /* save_as_floats */)
220 {
221 hsize_t dimens[3];
222 hid_t space_hid = -1, data_hid = -1;
223 bool retval = true;
224
225 space_hid = H5Screate_simple (0, dimens, 0);
226 if (space_hid < 0) return false;
227
228 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
229 H5P_DEFAULT);
230 if (data_hid < 0)
231 {
232 H5Sclose (space_hid);
233 return false;
234 }
235
236 float tmp = float_value ();
237 retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
238 H5P_DEFAULT, &tmp) >= 0;
239
240 H5Dclose (data_hid);
241 H5Sclose (space_hid);
242
243 return retval;
244 }
245
246 bool
247 octave_float_scalar::load_hdf5 (hid_t loc_id, const char *name,
248 bool /* have_h5giterate_bug */)
249 {
250 hid_t data_hid = H5Dopen (loc_id, name);
251 hid_t space_id = H5Dget_space (data_hid);
252
253 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
254
255 if (rank != 0)
256 {
257 H5Dclose (data_hid);
258 return false;
259 }
260
261 float dtmp;
262 if (H5Dread (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
263 H5P_DEFAULT, &dtmp) < 0)
264 {
265 H5Dclose (data_hid);
266 return false;
267 }
268
269 scalar = dtmp;
270
271 H5Dclose (data_hid);
272
273 return true;
274 }
275
276 #endif
277
278 mxArray *
279 octave_float_scalar::as_mxArray (void) const
280 {
281 mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxREAL);
282
283 float *pr = static_cast<float *> (retval->get_data ());
284
285 pr[0] = scalar;
286
287 return retval;
288 }
289
290 #define SCALAR_MAPPER(MAP, FCN) \
291 octave_value \
292 octave_float_scalar::MAP (void) const \
293 { \
294 return octave_value (FCN (scalar)); \
295 }
296
297 #define CD_SCALAR_MAPPER(MAP, RFCN, CFCN, L1, L2) \
298 octave_value \
299 octave_float_scalar::MAP (void) const \
300 { \
301 return (scalar < L1 || scalar > L2 \
302 ? octave_value (CFCN (Complex (scalar))) \
303 : octave_value (RFCN (scalar))); \
304 }
305
306 static float
307 xconj (float x)
308 {
309 return x;
310 }
311
312 SCALAR_MAPPER (erf, ::erf)
313 SCALAR_MAPPER (erfc, ::erfc)
314 SCALAR_MAPPER (gamma, xgamma)
315 CD_SCALAR_MAPPER (lgamma, xlgamma, xlgamma, 0.0, octave_Inf)
316 SCALAR_MAPPER (abs, ::fabs)
317 SCALAR_MAPPER (acos, ::acos)
318 CD_SCALAR_MAPPER (acosh, ::acosh, ::acosh, 1.0, octave_Inf)
319 SCALAR_MAPPER (angle, ::arg)
320 SCALAR_MAPPER (arg, ::arg)
321 CD_SCALAR_MAPPER (asin, ::asin, ::asin, -1.0, 1.0)
322 SCALAR_MAPPER (asinh, ::asinh)
323 SCALAR_MAPPER (atan, ::atan)
324 CD_SCALAR_MAPPER (atanh, ::atanh, ::atanh, -1.0, 1.0)
325 SCALAR_MAPPER (ceil, ::ceil)
326 SCALAR_MAPPER (conj, xconj)
327 SCALAR_MAPPER (cos, ::cos)
328 SCALAR_MAPPER (cosh, ::cosh)
329 SCALAR_MAPPER (exp, ::exp)
330 SCALAR_MAPPER (expm1, ::expm1)
331 SCALAR_MAPPER (fix, ::fix)
332 SCALAR_MAPPER (floor, ::floor)
333 SCALAR_MAPPER (imag, ::imag)
334 CD_SCALAR_MAPPER (log, ::log, std::log, 0.0, octave_Inf)
335 CD_SCALAR_MAPPER (log2, xlog2, xlog2, 0.0, octave_Inf)
336 CD_SCALAR_MAPPER (log10, ::log10, std::log10, 0.0, octave_Inf)
337 CD_SCALAR_MAPPER (log1p, ::log1p, ::log1p, -1.0, octave_Inf)
338 SCALAR_MAPPER (real, ::real)
339 SCALAR_MAPPER (round, xround)
340 SCALAR_MAPPER (roundb, xroundb)
341 SCALAR_MAPPER (signum, ::signum)
342 SCALAR_MAPPER (sin, ::sin)
343 SCALAR_MAPPER (sinh, ::sinh)
344 CD_SCALAR_MAPPER (sqrt, ::sqrt, std::sqrt, 0.0, octave_Inf)
345 SCALAR_MAPPER (tan, ::tan)
346 SCALAR_MAPPER (tanh, ::tanh)
347 SCALAR_MAPPER (finite, xfinite)
348 SCALAR_MAPPER (isinf, xisinf)
349 SCALAR_MAPPER (isna, octave_is_NA)
350 SCALAR_MAPPER (isnan, xisnan)
351
352 /*
353 ;;; Local Variables: ***
354 ;;; mode: C++ ***
355 ;;; End: ***
356 */