4513
|
1 // N-D Array manipulations. |
4511
|
2 /* |
|
3 |
|
4 Copyright (C) 1996, 1997 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 2, or (at your option) any |
|
11 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, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
4634
|
32 #include <cfloat> |
|
33 |
4588
|
34 #include "Array-util.h" |
4513
|
35 #include "dNDArray.h" |
4511
|
36 #include "mx-base.h" |
4513
|
37 #include "lo-error.h" |
4511
|
38 #include "lo-ieee.h" |
4634
|
39 #include "lo-mappers.h" |
4511
|
40 |
4543
|
41 NDArray::NDArray (const boolNDArray& a) |
|
42 : MArrayN<double> (a.dims ()) |
|
43 { |
|
44 for (int i = 0; i < a.length (); i++) |
|
45 elem (i) = a.elem (i); |
|
46 } |
|
47 |
|
48 NDArray::NDArray (const charNDArray& a) |
|
49 : MArrayN<double> (a.dims ()) |
|
50 { |
|
51 for (int i = 0; i < a.length (); i++) |
|
52 elem (i) = a.elem (i); |
|
53 } |
|
54 |
|
55 // unary operations |
|
56 |
|
57 boolNDArray |
|
58 NDArray::operator ! (void) const |
|
59 { |
|
60 boolNDArray b (dims ()); |
|
61 |
|
62 for (int i = 0; i < length (); i++) |
|
63 b.elem (i) = ! elem (i); |
|
64 |
|
65 return b; |
|
66 } |
|
67 |
4634
|
68 bool |
|
69 NDArray::any_element_is_negative (bool neg_zero) const |
|
70 { |
|
71 int nel = nelem (); |
|
72 |
|
73 if (neg_zero) |
|
74 { |
|
75 for (int i = 0; i < nel; i++) |
|
76 if (lo_ieee_signbit (elem (i))) |
|
77 return true; |
|
78 } |
|
79 else |
|
80 { |
|
81 for (int i = 0; i < nel; i++) |
|
82 if (elem (i) < 0) |
|
83 return true; |
|
84 } |
|
85 |
|
86 return false; |
|
87 } |
|
88 |
|
89 |
|
90 bool |
|
91 NDArray::any_element_is_inf_or_nan (void) const |
|
92 { |
|
93 int nel = nelem (); |
|
94 |
|
95 for (int i = 0; i < nel; i++) |
|
96 { |
|
97 double val = elem (i); |
|
98 if (xisinf (val) || xisnan (val)) |
|
99 return true; |
|
100 } |
|
101 |
|
102 return false; |
|
103 } |
|
104 |
|
105 bool |
|
106 NDArray::all_elements_are_int_or_inf_or_nan (void) const |
|
107 { |
|
108 int nel = nelem (); |
|
109 |
|
110 for (int i = 0; i < nel; i++) |
|
111 { |
|
112 double val = elem (i); |
|
113 if (xisnan (val) || D_NINT (val) == val) |
|
114 continue; |
|
115 else |
|
116 return false; |
|
117 } |
|
118 |
|
119 return true; |
|
120 } |
|
121 |
|
122 // Return nonzero if any element of M is not an integer. Also extract |
|
123 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
124 |
|
125 bool |
|
126 NDArray::all_integers (double& max_val, double& min_val) const |
|
127 { |
|
128 int nel = nelem (); |
|
129 |
|
130 if (nel > 0) |
|
131 { |
|
132 max_val = elem (0); |
|
133 min_val = elem (0); |
|
134 } |
|
135 else |
|
136 return false; |
|
137 |
|
138 for (int i = 0; i < nel; i++) |
|
139 { |
|
140 double val = elem (i); |
|
141 |
|
142 if (val > max_val) |
|
143 max_val = val; |
|
144 |
|
145 if (val < min_val) |
|
146 min_val = val; |
|
147 |
|
148 if (D_NINT (val) != val) |
|
149 return false; |
|
150 } |
|
151 |
|
152 return true; |
|
153 } |
|
154 |
|
155 bool |
|
156 NDArray::too_large_for_float (void) const |
|
157 { |
|
158 int nel = nelem (); |
|
159 |
|
160 for (int i = 0; i < nel; i++) |
|
161 { |
|
162 double val = elem (i); |
|
163 |
|
164 if (val > FLT_MAX || val < FLT_MIN) |
|
165 return true; |
|
166 } |
|
167 |
|
168 return false; |
|
169 } |
|
170 |
4513
|
171 // XXX FIXME XXX -- this is not quite the right thing. |
|
172 |
4556
|
173 boolNDArray |
4513
|
174 NDArray::all (int dim) const |
|
175 { |
4569
|
176 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true); |
4513
|
177 } |
|
178 |
4556
|
179 boolNDArray |
4513
|
180 NDArray::any (int dim) const |
|
181 { |
4569
|
182 MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false); |
|
183 } |
|
184 |
4584
|
185 NDArray |
4569
|
186 NDArray::cumprod (int dim) const |
|
187 { |
4584
|
188 MX_ND_CUMULATIVE_OP (NDArray, double, 1, *); |
4569
|
189 } |
|
190 |
4584
|
191 NDArray |
4569
|
192 NDArray::cumsum (int dim) const |
|
193 { |
4584
|
194 MX_ND_CUMULATIVE_OP (NDArray, double, 0, +); |
4513
|
195 } |
|
196 |
4569
|
197 NDArray |
|
198 NDArray::prod (int dim) const |
|
199 { |
|
200 MX_ND_REAL_OP_REDUCTION (*= elem (iter_idx), 1); |
|
201 } |
|
202 |
|
203 NDArray |
|
204 NDArray::sumsq (int dim) const |
|
205 { |
|
206 MX_ND_REAL_OP_REDUCTION (+= std::pow (elem (iter_idx), 2), 0); |
|
207 } |
|
208 |
|
209 NDArray |
|
210 NDArray::sum (int dim) const |
|
211 { |
|
212 MX_ND_REAL_OP_REDUCTION (+= elem (iter_idx), 0); |
|
213 } |
|
214 |
4758
|
215 bool |
4762
|
216 NDArray::cat (const NDArray& ra_arg, int dim, int add_dim) |
4758
|
217 { |
4762
|
218 // MX_ND_CAT; |
|
219 return ::cat_ra (*this, ra_arg, dim, add_dim); |
4758
|
220 } |
|
221 |
4634
|
222 NDArray |
|
223 real (const ComplexNDArray& a) |
|
224 { |
|
225 int a_len = a.length (); |
|
226 NDArray retval; |
|
227 if (a_len > 0) |
|
228 retval = NDArray (mx_inline_real_dup (a.data (), a_len), a.dims ()); |
|
229 return retval; |
|
230 } |
|
231 |
|
232 NDArray |
|
233 imag (const ComplexNDArray& a) |
|
234 { |
|
235 int a_len = a.length (); |
|
236 NDArray retval; |
|
237 if (a_len > 0) |
|
238 retval = NDArray (mx_inline_imag_dup (a.data (), a_len), a.dims ()); |
|
239 return retval; |
|
240 } |
|
241 |
|
242 NDArray |
4569
|
243 NDArray::abs (void) const |
|
244 { |
4634
|
245 NDArray retval (dims ()); |
4569
|
246 |
4634
|
247 int nel = nelem (); |
|
248 |
|
249 for (int i = 0; i < nel; i++) |
|
250 retval(i) = fabs (elem (i)); |
4569
|
251 |
|
252 return retval; |
|
253 } |
|
254 |
4532
|
255 Matrix |
|
256 NDArray::matrix_value (void) const |
|
257 { |
|
258 Matrix retval; |
|
259 |
|
260 int nd = ndims (); |
|
261 |
|
262 switch (nd) |
|
263 { |
|
264 case 1: |
|
265 retval = Matrix (Array2<double> (*this, dimensions(0), 1)); |
|
266 break; |
|
267 |
|
268 case 2: |
|
269 retval = Matrix (Array2<double> (*this, dimensions(0), dimensions(1))); |
|
270 break; |
|
271 |
|
272 default: |
|
273 (*current_liboctave_error_handler) |
4770
|
274 ("invalid conversion of NDArray to Matrix"); |
4532
|
275 break; |
|
276 } |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 void |
|
282 NDArray::increment_index (Array<int>& ra_idx, |
|
283 const dim_vector& dimensions, |
|
284 int start_dimension) |
|
285 { |
|
286 ::increment_index (ra_idx, dimensions, start_dimension); |
|
287 } |
|
288 |
4556
|
289 int |
|
290 NDArray::compute_index (Array<int>& ra_idx, |
|
291 const dim_vector& dimensions) |
|
292 { |
|
293 return ::compute_index (ra_idx, dimensions); |
|
294 } |
|
295 |
4687
|
296 // This contains no information on the array structure !!! |
|
297 std::ostream& |
|
298 operator << (std::ostream& os, const NDArray& a) |
|
299 { |
|
300 int nel = a.nelem (); |
|
301 |
|
302 for (int i = 0; i < nel; i++) |
|
303 { |
|
304 os << " "; |
|
305 octave_write_double (os, a.elem (i)); |
|
306 os << "\n"; |
|
307 } |
|
308 return os; |
|
309 } |
|
310 |
|
311 std::istream& |
|
312 operator >> (std::istream& is, NDArray& a) |
|
313 { |
|
314 int nel = a.nelem (); |
|
315 |
|
316 if (nel < 1 ) |
|
317 is.clear (std::ios::badbit); |
|
318 else |
|
319 { |
|
320 double tmp; |
|
321 for (int i = 0; i < nel; i++) |
|
322 { |
|
323 tmp = octave_read_double (is); |
|
324 if (is) |
|
325 a.elem (i) = tmp; |
|
326 else |
|
327 goto done; |
|
328 } |
|
329 } |
|
330 |
|
331 done: |
|
332 |
|
333 return is; |
|
334 } |
|
335 |
4543
|
336 NDS_CMP_OPS(NDArray, , double, ) |
|
337 NDS_BOOL_OPS(NDArray, double, 0.0) |
|
338 |
|
339 SND_CMP_OPS(double, , NDArray, ) |
|
340 SND_BOOL_OPS(double, NDArray, 0.0) |
|
341 |
|
342 NDND_CMP_OPS(NDArray, , NDArray, ) |
|
343 NDND_BOOL_OPS(NDArray, NDArray, 0.0) |
|
344 |
4513
|
345 /* |
|
346 ;;; Local Variables: *** |
|
347 ;;; mode: C++ *** |
|
348 ;;; End: *** |
|
349 */ |