2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
2901
|
29 |
4944
|
30 #include "data-conv.h" |
|
31 #include "mach-info.h" |
|
32 |
2376
|
33 #include "defun.h" |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
4944
|
36 #include "oct-stream.h" |
2376
|
37 #include "ov-scalar.h" |
3223
|
38 #include "ov-base.h" |
|
39 #include "ov-base-scalar.h" |
|
40 #include "ov-base-scalar.cc" |
2423
|
41 #include "ov-re-mat.h" |
2376
|
42 #include "ov-typeinfo.h" |
|
43 #include "pr-output.h" |
|
44 #include "xdiv.h" |
|
45 #include "xpow.h" |
|
46 |
4687
|
47 #include "ls-oct-ascii.h" |
|
48 #include "ls-hdf5.h" |
|
49 |
3223
|
50 template class octave_base_scalar<double>; |
|
51 |
3219
|
52 DEFINE_OCTAVE_ALLOCATOR (octave_scalar); |
2376
|
53 |
4612
|
54 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_scalar, "scalar", "double"); |
2376
|
55 |
|
56 octave_value |
3933
|
57 octave_scalar::do_index_op (const octave_value_list& idx, int resize_ok) |
2376
|
58 { |
|
59 octave_value retval; |
|
60 |
3933
|
61 if (idx.valid_scalar_indices ()) |
2376
|
62 retval = scalar; |
|
63 else |
|
64 { |
5775
|
65 // FIXME -- this doesn't solve the problem of |
2376
|
66 // |
|
67 // a = 1; a([1,1], [1,1], [1,1]) |
|
68 // |
|
69 // and similar constructions. Hmm... |
|
70 |
5775
|
71 // FIXME -- using this constructor avoids narrowing the |
2423
|
72 // 1x1 matrix back to a scalar value. Need a better solution |
|
73 // to this problem. |
|
74 |
|
75 octave_value tmp (new octave_matrix (matrix_value ())); |
2376
|
76 |
3933
|
77 retval = tmp.do_index_op (idx, resize_ok); |
2376
|
78 } |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
4645
|
83 std::streamoff |
|
84 octave_scalar::streamoff_value (void) const |
|
85 { |
|
86 std::streamoff retval (-1); |
|
87 |
|
88 if (D_NINT (scalar) == scalar) |
|
89 retval = std::streamoff (static_cast<long> (scalar)); |
|
90 else |
|
91 error ("conversion to streamoff value failed"); |
|
92 |
|
93 return retval; |
|
94 } |
|
95 |
4701
|
96 streamoff_array |
|
97 octave_scalar::streamoff_array_value (void) const |
|
98 { |
|
99 streamoff_array retval; |
|
100 |
|
101 std::streamoff soff = streamoff_value (); |
|
102 |
|
103 if (! error_state) |
|
104 retval = streamoff_array (dim_vector (1, 1), soff); |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
5731
|
109 octave_value |
|
110 octave_scalar::resize (const dim_vector& dv, bool fill) const |
|
111 { |
|
112 if (fill) |
|
113 { |
|
114 NDArray retval (dv, NDArray::resize_fill_value()); |
|
115 |
|
116 if (dv.numel ()) |
|
117 retval(0) = scalar; |
|
118 |
|
119 return retval; |
|
120 } |
|
121 else |
|
122 { |
|
123 NDArray retval (dv); |
|
124 |
|
125 if (dv.numel ()) |
|
126 retval(0) = scalar; |
|
127 |
|
128 return retval; |
|
129 } |
|
130 } |
|
131 |
2376
|
132 octave_value |
5279
|
133 octave_scalar::convert_to_str_internal (bool, bool, char type) const |
2376
|
134 { |
|
135 octave_value retval; |
|
136 |
|
137 if (xisnan (scalar)) |
|
138 ::error ("invalid conversion from NaN to character"); |
|
139 else |
|
140 { |
4100
|
141 int ival = NINT (scalar); |
|
142 |
|
143 if (ival < 0 || ival > UCHAR_MAX) |
|
144 { |
5775
|
145 // FIXME -- is there something better we could do? |
4100
|
146 |
|
147 ival = 0; |
|
148 |
|
149 ::warning ("range error for conversion to character value"); |
|
150 } |
|
151 |
5279
|
152 retval = octave_value (std::string (1, static_cast<char> (ival)), type); |
2376
|
153 } |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
4687
|
158 bool |
|
159 octave_scalar::save_ascii (std::ostream& os, bool& infnan_warned, |
|
160 bool strip_nan_and_inf) |
|
161 { |
|
162 double d = double_value (); |
|
163 |
|
164 if (strip_nan_and_inf) |
|
165 { |
|
166 if (xisnan (d)) |
|
167 { |
|
168 error ("only value to plot is NaN"); |
|
169 return false; |
|
170 } |
|
171 else |
|
172 { |
|
173 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
174 octave_write_double (os, d); |
|
175 os << "\n"; |
|
176 } |
|
177 } |
|
178 else |
|
179 { |
|
180 if (! infnan_warned && (xisnan (d) || xisinf (d))) |
|
181 { |
|
182 warning ("save: Inf or NaN values may not be reloadable"); |
|
183 infnan_warned = true; |
|
184 } |
|
185 |
|
186 octave_write_double (os, d); |
|
187 os << "\n"; |
|
188 } |
|
189 |
|
190 return true; |
|
191 } |
|
192 |
|
193 bool |
|
194 octave_scalar::load_ascii (std::istream& is) |
|
195 { |
|
196 scalar = octave_read_double (is); |
|
197 if (!is) |
|
198 { |
|
199 error ("load: failed to load scalar constant"); |
|
200 return false; |
|
201 } |
|
202 |
|
203 return true; |
|
204 } |
|
205 |
|
206 bool |
|
207 octave_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
208 { |
5760
|
209 char tmp = LS_DOUBLE; |
|
210 os.write (reinterpret_cast<char *> (&tmp), 1); |
4687
|
211 double dtmp = double_value (); |
5760
|
212 os.write (reinterpret_cast<char *> (&dtmp), 8); |
4687
|
213 |
|
214 return true; |
|
215 } |
|
216 |
|
217 bool |
|
218 octave_scalar::load_binary (std::istream& is, bool swap, |
4815
|
219 oct_mach_info::float_format fmt) |
4687
|
220 { |
|
221 char tmp; |
5760
|
222 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
223 return false; |
|
224 |
|
225 double dtmp; |
5760
|
226 read_doubles (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt); |
4687
|
227 if (error_state || ! is) |
|
228 return false; |
|
229 |
|
230 scalar = dtmp; |
|
231 return true; |
|
232 } |
|
233 |
|
234 #if defined (HAVE_HDF5) |
4944
|
235 |
4687
|
236 bool |
|
237 octave_scalar::save_hdf5 (hid_t loc_id, const char *name, |
|
238 bool /* save_as_floats */) |
|
239 { |
|
240 hsize_t dimens[3]; |
|
241 hid_t space_hid = -1, data_hid = -1; |
|
242 bool retval = true; |
|
243 |
4815
|
244 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
245 if (space_hid < 0) return false; |
|
246 |
|
247 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
248 H5P_DEFAULT); |
|
249 if (data_hid < 0) |
|
250 { |
|
251 H5Sclose (space_hid); |
|
252 return false; |
|
253 } |
|
254 |
|
255 double tmp = double_value (); |
|
256 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
257 H5P_DEFAULT, &tmp) >= 0; |
4687
|
258 |
|
259 H5Dclose (data_hid); |
|
260 H5Sclose (space_hid); |
4837
|
261 |
4687
|
262 return retval; |
|
263 } |
|
264 |
|
265 bool |
|
266 octave_scalar::load_hdf5 (hid_t loc_id, const char *name, |
|
267 bool /* have_h5giterate_bug */) |
|
268 { |
|
269 hid_t data_hid = H5Dopen (loc_id, name); |
|
270 hid_t space_id = H5Dget_space (data_hid); |
|
271 |
|
272 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
273 |
|
274 if (rank != 0) |
|
275 { |
|
276 H5Dclose (data_hid); |
|
277 return false; |
|
278 } |
|
279 |
|
280 double dtmp; |
|
281 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
282 H5P_DEFAULT, &dtmp) < 0) |
4687
|
283 { |
|
284 H5Dclose (data_hid); |
|
285 return false; |
|
286 } |
|
287 |
|
288 scalar = dtmp; |
4837
|
289 |
4687
|
290 H5Dclose (data_hid); |
4837
|
291 |
4687
|
292 return true; |
|
293 } |
4944
|
294 |
4687
|
295 #endif |
|
296 |
2376
|
297 /* |
|
298 ;;; Local Variables: *** |
|
299 ;;; mode: C++ *** |
|
300 ;;; End: *** |
|
301 */ |