4514
|
1 // N-D Array manipulations. |
|
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 |
4687
|
32 #include <cfloat> |
|
33 |
4588
|
34 #include "Array-util.h" |
4514
|
35 #include "CNDArray.h" |
|
36 #include "mx-base.h" |
|
37 #include "lo-ieee.h" |
4687
|
38 #include "lo-mappers.h" |
4514
|
39 |
4543
|
40 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
41 // here? |
|
42 |
|
43 ComplexNDArray::ComplexNDArray (const NDArray& a) |
|
44 : MArrayN<Complex> (a.dims ()) |
|
45 { |
|
46 for (int i = 0; i < a.length (); i++) |
|
47 elem (i) = a.elem (i); |
|
48 } |
|
49 |
|
50 ComplexNDArray::ComplexNDArray (const boolNDArray& a) |
|
51 : MArrayN<Complex> (a.dims ()) |
|
52 { |
|
53 for (int i = 0; i < a.length (); i++) |
|
54 elem (i) = a.elem (i); |
|
55 } |
|
56 |
|
57 ComplexNDArray::ComplexNDArray (const charNDArray& a) |
|
58 : MArrayN<Complex> (a.dims ()) |
|
59 { |
|
60 for (int i = 0; i < a.length (); i++) |
|
61 elem (i) = a.elem (i); |
|
62 } |
|
63 |
|
64 // unary operations |
|
65 |
|
66 boolNDArray |
|
67 ComplexNDArray::operator ! (void) const |
|
68 { |
|
69 boolNDArray b (dims ()); |
|
70 |
|
71 for (int i = 0; i < length (); i++) |
|
72 b.elem (i) = elem (i) != 0.0; |
|
73 |
|
74 return b; |
|
75 } |
|
76 |
4514
|
77 // XXX FIXME XXX -- this is not quite the right thing. |
|
78 |
4687
|
79 bool |
|
80 ComplexNDArray::any_element_is_inf_or_nan (void) const |
|
81 { |
|
82 int nel = nelem (); |
|
83 |
|
84 for (int i = 0; i < nel; i++) |
|
85 { |
|
86 Complex val = elem (i); |
|
87 if (xisinf (val) || xisnan (val)) |
|
88 return true; |
|
89 } |
|
90 return false; |
|
91 } |
|
92 |
|
93 // Return true if no elements have imaginary components. |
|
94 |
|
95 bool |
|
96 ComplexNDArray::all_elements_are_real (void) const |
|
97 { |
|
98 int nel = nelem (); |
|
99 |
|
100 for (int i = 0; i < nel; i++) |
|
101 { |
|
102 double ip = imag (elem (i)); |
|
103 |
|
104 if (ip != 0.0 || lo_ieee_signbit (ip)) |
|
105 return false; |
|
106 } |
|
107 |
|
108 return true; |
|
109 } |
|
110 |
|
111 // Return nonzero if any element of CM has a non-integer real or |
|
112 // imaginary part. Also extract the largest and smallest (real or |
|
113 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
114 |
|
115 bool |
|
116 ComplexNDArray::all_integers (double& max_val, double& min_val) const |
|
117 { |
|
118 int nel = nelem (); |
|
119 |
|
120 if (nel > 0) |
|
121 { |
|
122 Complex val = elem (0); |
|
123 |
|
124 double r_val = real (val); |
|
125 double i_val = imag (val); |
|
126 |
|
127 max_val = r_val; |
|
128 min_val = r_val; |
|
129 |
|
130 if (i_val > max_val) |
|
131 max_val = i_val; |
|
132 |
|
133 if (i_val < max_val) |
|
134 min_val = i_val; |
|
135 } |
|
136 else |
|
137 return false; |
|
138 |
|
139 for (int i = 0; i < nel; i++) |
|
140 { |
|
141 Complex val = elem (i); |
|
142 |
|
143 double r_val = real (val); |
|
144 double i_val = imag (val); |
|
145 |
|
146 if (r_val > max_val) |
|
147 max_val = r_val; |
|
148 |
|
149 if (i_val > max_val) |
|
150 max_val = i_val; |
|
151 |
|
152 if (r_val < min_val) |
|
153 min_val = r_val; |
|
154 |
|
155 if (i_val < min_val) |
|
156 min_val = i_val; |
|
157 |
|
158 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
|
159 return false; |
|
160 } |
|
161 |
|
162 return true; |
|
163 } |
|
164 |
|
165 bool |
|
166 ComplexNDArray::too_large_for_float (void) const |
|
167 { |
|
168 int nel = nelem (); |
|
169 |
|
170 for (int i = 0; i < nel; i++) |
|
171 { |
|
172 Complex val = elem (i); |
|
173 |
|
174 double r_val = real (val); |
|
175 double i_val = imag (val); |
|
176 |
|
177 if (r_val > FLT_MAX |
|
178 || i_val > FLT_MAX |
|
179 || r_val < FLT_MIN |
|
180 || i_val < FLT_MIN) |
|
181 return true; |
|
182 } |
|
183 |
|
184 return false; |
|
185 } |
|
186 |
4556
|
187 boolNDArray |
4514
|
188 ComplexNDArray::all (int dim) const |
|
189 { |
4569
|
190 MX_ND_ANY_ALL_REDUCTION |
|
191 (MX_ND_ALL_EVAL (elem (iter_idx) == Complex (0, 0)), true); |
4514
|
192 } |
|
193 |
4556
|
194 boolNDArray |
4514
|
195 ComplexNDArray::any (int dim) const |
|
196 { |
4569
|
197 MX_ND_ANY_ALL_REDUCTION |
|
198 (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false); |
|
199 } |
|
200 |
4584
|
201 ComplexNDArray |
4569
|
202 ComplexNDArray::cumprod (int dim) const |
|
203 { |
4584
|
204 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (1, 0), *); |
4569
|
205 } |
|
206 |
4584
|
207 ComplexNDArray |
4569
|
208 ComplexNDArray::cumsum (int dim) const |
|
209 { |
4584
|
210 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (0, 0), +); |
4569
|
211 } |
|
212 |
|
213 ComplexNDArray |
|
214 ComplexNDArray::prod (int dim) const |
|
215 { |
|
216 MX_ND_COMPLEX_OP_REDUCTION (*= elem (iter_idx), Complex (1, 0)); |
|
217 } |
|
218 |
|
219 ComplexNDArray |
|
220 ComplexNDArray::sumsq (int dim) const |
|
221 { |
|
222 MX_ND_COMPLEX_OP_REDUCTION |
|
223 (+= imag (elem (iter_idx)) |
|
224 ? elem (iter_idx) * conj (elem (iter_idx)) |
|
225 : std::pow (elem (iter_idx), 2), Complex (0, 0)); |
|
226 } |
|
227 |
|
228 ComplexNDArray |
|
229 ComplexNDArray::sum (int dim) const |
|
230 { |
|
231 MX_ND_COMPLEX_OP_REDUCTION (+= elem (iter_idx), Complex (0, 0)); |
|
232 } |
|
233 |
4758
|
234 bool |
|
235 ComplexNDArray::cat (ComplexNDArray& cat_arr, int dim, int add_dim) const |
|
236 { |
|
237 MX_ND_CAT; |
|
238 } |
|
239 |
4634
|
240 NDArray |
4569
|
241 ComplexNDArray::abs (void) const |
|
242 { |
4634
|
243 NDArray retval (dims ()); |
4569
|
244 |
4634
|
245 int nel = nelem (); |
|
246 |
|
247 for (int i = 0; i < nel; i++) |
|
248 retval(i) = ::abs (elem (i)); |
4569
|
249 |
|
250 return retval; |
4514
|
251 } |
|
252 |
|
253 ComplexMatrix |
|
254 ComplexNDArray::matrix_value (void) const |
|
255 { |
|
256 ComplexMatrix retval; |
|
257 |
|
258 int nd = ndims (); |
|
259 |
|
260 switch (nd) |
|
261 { |
|
262 case 1: |
|
263 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), 1)); |
|
264 break; |
|
265 |
|
266 case 2: |
|
267 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), |
|
268 dimensions(1))); |
|
269 break; |
|
270 |
|
271 default: |
|
272 (*current_liboctave_error_handler) |
|
273 ("invalid converstion of ComplexNDArray to ComplexMatrix"); |
|
274 break; |
|
275 } |
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
4532
|
280 void |
|
281 ComplexNDArray::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 ComplexNDArray::compute_index (Array<int>& ra_idx, |
|
290 const dim_vector& dimensions) |
|
291 { |
|
292 return ::compute_index (ra_idx, dimensions); |
|
293 } |
|
294 |
4687
|
295 |
|
296 // This contains no information on the array structure !!! |
|
297 std::ostream& |
|
298 operator << (std::ostream& os, const ComplexNDArray& a) |
|
299 { |
|
300 int nel = a.nelem (); |
|
301 |
|
302 for (int i = 0; i < nel; i++) |
|
303 { |
|
304 os << " "; |
|
305 octave_write_complex (os, a.elem (i)); |
|
306 os << "\n"; |
|
307 } |
|
308 return os; |
|
309 } |
|
310 |
|
311 std::istream& |
|
312 operator >> (std::istream& is, ComplexNDArray& a) |
|
313 { |
|
314 int nel = a.nelem (); |
|
315 |
|
316 if (nel < 1 ) |
|
317 is.clear (std::ios::badbit); |
|
318 else |
|
319 { |
|
320 Complex tmp; |
|
321 for (int i = 0; i < nel; i++) |
|
322 { |
|
323 tmp = octave_read_complex (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(ComplexNDArray, real, Complex, real) |
|
337 NDS_BOOL_OPS(ComplexNDArray, Complex, 0.0) |
|
338 |
|
339 SND_CMP_OPS(Complex, real, ComplexNDArray, real) |
|
340 SND_BOOL_OPS(Complex, ComplexNDArray, 0.0) |
|
341 |
|
342 NDND_CMP_OPS(ComplexNDArray, real, ComplexNDArray, real) |
|
343 NDND_BOOL_OPS(ComplexNDArray, ComplexNDArray, 0.0) |
|
344 |
4514
|
345 /* |
|
346 ;;; Local Variables: *** |
|
347 ;;; mode: C++ *** |
|
348 ;;; End: *** |
|
349 */ |