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 |
4513
|
32 #include "dNDArray.h" |
4511
|
33 #include "mx-base.h" |
4513
|
34 #include "lo-error.h" |
4511
|
35 #include "lo-ieee.h" |
|
36 |
4532
|
37 #include "ArrayN-inline.h" |
|
38 |
4543
|
39 NDArray::NDArray (const boolNDArray& a) |
|
40 : MArrayN<double> (a.dims ()) |
|
41 { |
|
42 for (int i = 0; i < a.length (); i++) |
|
43 elem (i) = a.elem (i); |
|
44 } |
|
45 |
|
46 NDArray::NDArray (const charNDArray& a) |
|
47 : MArrayN<double> (a.dims ()) |
|
48 { |
|
49 for (int i = 0; i < a.length (); i++) |
|
50 elem (i) = a.elem (i); |
|
51 } |
|
52 |
|
53 // unary operations |
|
54 |
|
55 boolNDArray |
|
56 NDArray::operator ! (void) const |
|
57 { |
|
58 boolNDArray b (dims ()); |
|
59 |
|
60 for (int i = 0; i < length (); i++) |
|
61 b.elem (i) = ! elem (i); |
|
62 |
|
63 return b; |
|
64 } |
|
65 |
4513
|
66 // XXX FIXME XXX -- this is not quite the right thing. |
|
67 |
4556
|
68 boolNDArray |
4513
|
69 NDArray::all (int dim) const |
|
70 { |
4569
|
71 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR), true); |
4513
|
72 } |
|
73 |
4556
|
74 boolNDArray |
4513
|
75 NDArray::any (int dim) const |
|
76 { |
4569
|
77 MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false); |
|
78 } |
|
79 |
|
80 Matrix |
|
81 NDArray::cumprod (int dim) const |
|
82 { |
|
83 if (dims () . length () == 2) |
|
84 { |
|
85 MX_CUMULATIVE_OP (Matrix, double, *=); |
|
86 } |
|
87 else |
|
88 { |
|
89 (*current_liboctave_error_handler) |
|
90 ("cumprod is not yet implemented for N-d arrays"); |
|
91 |
|
92 return Matrix (); |
|
93 } |
|
94 } |
|
95 |
|
96 Matrix |
|
97 NDArray::cumsum (int dim) const |
|
98 { |
|
99 if (dims () . length () == 2) |
|
100 { |
|
101 MX_CUMULATIVE_OP (Matrix, double, +=); |
|
102 } |
|
103 else |
|
104 { |
|
105 (*current_liboctave_error_handler) |
|
106 ("cumprod is not yet implemented for N-d arrays"); |
|
107 |
|
108 return Matrix (); |
|
109 } |
4513
|
110 } |
|
111 |
4569
|
112 NDArray |
|
113 NDArray::prod (int dim) const |
|
114 { |
|
115 MX_ND_REAL_OP_REDUCTION (*= elem (iter_idx), 1); |
|
116 } |
|
117 |
|
118 NDArray |
|
119 NDArray::sumsq (int dim) const |
|
120 { |
|
121 MX_ND_REAL_OP_REDUCTION (+= std::pow (elem (iter_idx), 2), 0); |
|
122 } |
|
123 |
|
124 NDArray |
|
125 NDArray::sum (int dim) const |
|
126 { |
|
127 MX_ND_REAL_OP_REDUCTION (+= elem (iter_idx), 0); |
|
128 } |
|
129 |
|
130 Matrix |
|
131 NDArray::abs (void) const |
|
132 { |
|
133 Matrix retval; |
|
134 |
|
135 if (dims () . length () == 2) |
|
136 { |
|
137 int nr = rows (); |
|
138 int nc = cols (); |
|
139 |
|
140 retval.resize (nr, nc); |
|
141 |
|
142 for (int j = 0; j < nc; j++) |
|
143 for (int i = 0; i < nr; i++) |
|
144 retval(i,j) = fabs (elem (i, j)); |
|
145 } |
|
146 else |
|
147 (*current_liboctave_error_handler) |
|
148 ("abs is not yet implemented for N-d arrays"); |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
|
153 |
4532
|
154 Matrix |
|
155 NDArray::matrix_value (void) const |
|
156 { |
|
157 Matrix retval; |
|
158 |
|
159 int nd = ndims (); |
|
160 |
|
161 switch (nd) |
|
162 { |
|
163 case 1: |
|
164 retval = Matrix (Array2<double> (*this, dimensions(0), 1)); |
|
165 break; |
|
166 |
|
167 case 2: |
|
168 retval = Matrix (Array2<double> (*this, dimensions(0), dimensions(1))); |
|
169 break; |
|
170 |
|
171 default: |
|
172 (*current_liboctave_error_handler) |
|
173 ("invalid converstion of NDArray to Matrix"); |
|
174 break; |
|
175 } |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 void |
|
181 NDArray::increment_index (Array<int>& ra_idx, |
|
182 const dim_vector& dimensions, |
|
183 int start_dimension) |
|
184 { |
|
185 ::increment_index (ra_idx, dimensions, start_dimension); |
|
186 } |
|
187 |
4556
|
188 int |
|
189 NDArray::compute_index (Array<int>& ra_idx, |
|
190 const dim_vector& dimensions) |
|
191 { |
|
192 return ::compute_index (ra_idx, dimensions); |
|
193 } |
|
194 |
4511
|
195 bool |
|
196 NDArray::any_element_is_negative (bool neg_zero) const |
|
197 { |
|
198 int n = length (); |
|
199 if (neg_zero) |
|
200 { |
|
201 for (int i = 0; i < n; i++) |
|
202 if (lo_ieee_signbit (Array<double>::elem (i))) |
|
203 return true; |
|
204 } |
|
205 else |
|
206 { |
|
207 for (int i = 0; i < n; i++) |
|
208 if (Array<double>::elem (i) < 0) |
|
209 return true; |
|
210 } |
|
211 |
|
212 return false; |
|
213 } |
|
214 |
|
215 bool |
|
216 NDArray::all_integers (double& max_val, double& min_val) const |
|
217 { |
|
218 int n = length (); |
|
219 |
|
220 if (n > 0) |
|
221 { |
|
222 max_val = Array<double>::elem (0); |
|
223 min_val = Array<double>::elem (0); |
|
224 } |
|
225 else |
|
226 return false; |
|
227 |
|
228 for (int i = 0; i < n; i++) |
|
229 { |
|
230 double val = Array<double>::elem (0); |
|
231 |
|
232 if (val > max_val) |
|
233 max_val = val; |
|
234 |
|
235 if (val < min_val) |
|
236 min_val = val; |
|
237 |
|
238 if (D_NINT (val) != val) |
|
239 return false; |
|
240 } |
|
241 |
|
242 return true; |
|
243 } |
4513
|
244 |
4543
|
245 NDS_CMP_OPS(NDArray, , double, ) |
|
246 NDS_BOOL_OPS(NDArray, double, 0.0) |
|
247 |
|
248 SND_CMP_OPS(double, , NDArray, ) |
|
249 SND_BOOL_OPS(double, NDArray, 0.0) |
|
250 |
|
251 NDND_CMP_OPS(NDArray, , NDArray, ) |
|
252 NDND_BOOL_OPS(NDArray, NDArray, 0.0) |
|
253 |
4513
|
254 /* |
|
255 ;;; Local Variables: *** |
|
256 ;;; mode: C++ *** |
|
257 ;;; End: *** |
|
258 */ |