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