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