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 |
|
216 NDArray::cat (NDArray& cat_arr, int dim, int add_dim) const |
|
217 { |
|
218 MX_ND_CAT; |
|
219 } |
|
220 |
4634
|
221 NDArray |
|
222 real (const ComplexNDArray& a) |
|
223 { |
|
224 int a_len = a.length (); |
|
225 NDArray retval; |
|
226 if (a_len > 0) |
|
227 retval = NDArray (mx_inline_real_dup (a.data (), a_len), a.dims ()); |
|
228 return retval; |
|
229 } |
|
230 |
|
231 NDArray |
|
232 imag (const ComplexNDArray& a) |
|
233 { |
|
234 int a_len = a.length (); |
|
235 NDArray retval; |
|
236 if (a_len > 0) |
|
237 retval = NDArray (mx_inline_imag_dup (a.data (), a_len), a.dims ()); |
|
238 return retval; |
|
239 } |
|
240 |
|
241 NDArray |
4569
|
242 NDArray::abs (void) const |
|
243 { |
4634
|
244 NDArray retval (dims ()); |
4569
|
245 |
4634
|
246 int nel = nelem (); |
|
247 |
|
248 for (int i = 0; i < nel; i++) |
|
249 retval(i) = fabs (elem (i)); |
4569
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
4532
|
254 Matrix |
|
255 NDArray::matrix_value (void) const |
|
256 { |
|
257 Matrix retval; |
|
258 |
|
259 int nd = ndims (); |
|
260 |
|
261 switch (nd) |
|
262 { |
|
263 case 1: |
|
264 retval = Matrix (Array2<double> (*this, dimensions(0), 1)); |
|
265 break; |
|
266 |
|
267 case 2: |
|
268 retval = Matrix (Array2<double> (*this, dimensions(0), dimensions(1))); |
|
269 break; |
|
270 |
|
271 default: |
|
272 (*current_liboctave_error_handler) |
|
273 ("invalid converstion of NDArray to Matrix"); |
|
274 break; |
|
275 } |
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
|
280 void |
|
281 NDArray::increment_index (Array<int>& ra_idx, |
|
282 const dim_vector& dimensions, |
|
283 int start_dimension) |
|
284 { |
|
285 ::increment_index (ra_idx, dimensions, start_dimension); |
|
286 } |
|
287 |
4556
|
288 int |
|
289 NDArray::compute_index (Array<int>& ra_idx, |
|
290 const dim_vector& dimensions) |
|
291 { |
|
292 return ::compute_index (ra_idx, dimensions); |
|
293 } |
|
294 |
4687
|
295 // This contains no information on the array structure !!! |
|
296 std::ostream& |
|
297 operator << (std::ostream& os, const NDArray& a) |
|
298 { |
|
299 int nel = a.nelem (); |
|
300 |
|
301 for (int i = 0; i < nel; i++) |
|
302 { |
|
303 os << " "; |
|
304 octave_write_double (os, a.elem (i)); |
|
305 os << "\n"; |
|
306 } |
|
307 return os; |
|
308 } |
|
309 |
|
310 std::istream& |
|
311 operator >> (std::istream& is, NDArray& a) |
|
312 { |
|
313 int nel = a.nelem (); |
|
314 |
|
315 if (nel < 1 ) |
|
316 is.clear (std::ios::badbit); |
|
317 else |
|
318 { |
|
319 double tmp; |
|
320 for (int i = 0; i < nel; i++) |
|
321 { |
|
322 tmp = octave_read_double (is); |
|
323 if (is) |
|
324 a.elem (i) = tmp; |
|
325 else |
|
326 goto done; |
|
327 } |
|
328 } |
|
329 |
|
330 done: |
|
331 |
|
332 return is; |
|
333 } |
|
334 |
4543
|
335 NDS_CMP_OPS(NDArray, , double, ) |
|
336 NDS_BOOL_OPS(NDArray, double, 0.0) |
|
337 |
|
338 SND_CMP_OPS(double, , NDArray, ) |
|
339 SND_BOOL_OPS(double, NDArray, 0.0) |
|
340 |
|
341 NDND_CMP_OPS(NDArray, , NDArray, ) |
|
342 NDND_BOOL_OPS(NDArray, NDArray, 0.0) |
|
343 |
4513
|
344 /* |
|
345 ;;; Local Variables: *** |
|
346 ;;; mode: C++ *** |
|
347 ;;; End: *** |
|
348 */ |