Mercurial > hg > octave-lyh
annotate liboctave/intNDArray.cc @ 9513:9f870f73ab7d
implement built-in diff
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 11 Aug 2009 09:08:12 +0200 |
parents | 8145f2255276 |
children | a9b37bae1802 |
rev | line source |
---|---|
4902 | 1 // N-D Array manipulations. |
2 /* | |
3 | |
8920 | 4 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
4902 | 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4902 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4902 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "Array-util.h" | |
29 #include "mx-base.h" | |
30 #include "lo-ieee.h" | |
8741
008f3985c8c0
use new summation code for native integer summation
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
31 #include "mx-inlines.cc" |
4902 | 32 |
33 // unary operations | |
34 | |
35 template <class T> | |
36 boolNDArray | |
37 intNDArray<T>::operator ! (void) const | |
38 { | |
4932 | 39 boolNDArray b (this->dims ()); |
4902 | 40 |
5275 | 41 for (octave_idx_type i = 0; i < this->length (); i++) |
4932 | 42 b.elem (i) = ! this->elem (i); |
4902 | 43 |
44 return b; | |
45 } | |
46 | |
5943 | 47 template <class T> |
48 bool | |
49 intNDArray<T>::any_element_not_one_or_zero (void) const | |
50 { | |
51 octave_idx_type nel = this->nelem (); | |
52 | |
53 for (octave_idx_type i = 0; i < nel; i++) | |
54 { | |
55 T val = this->elem (i); | |
56 | |
57 if (val != 0.0 && val != 1.0) | |
58 return true; | |
59 } | |
60 | |
61 return false; | |
62 } | |
63 | |
6979 | 64 template <class T> |
65 intNDArray<T> | |
66 intNDArray<T>::diag (octave_idx_type k) const | |
67 { | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7600
diff
changeset
|
68 return MArrayN<T>::diag (k); |
6979 | 69 } |
70 | |
5775 | 71 // FIXME -- this is not quite the right thing. |
4902 | 72 |
73 template <class T> | |
74 boolNDArray | |
75 intNDArray<T>::all (int dim) const | |
76 { | |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
77 return do_mx_red_op<boolNDArray, T > (*this, dim, mx_inline_all); |
4902 | 78 } |
79 | |
80 template <class T> | |
81 boolNDArray | |
82 intNDArray<T>::any (int dim) const | |
83 { | |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
84 return do_mx_red_op<boolNDArray, T > (*this, dim, mx_inline_any); |
4902 | 85 } |
86 | |
87 template <class T> | |
88 void | |
5275 | 89 intNDArray<T>::increment_index (Array<octave_idx_type>& ra_idx, |
4902 | 90 const dim_vector& dimensions, |
91 int start_dimension) | |
92 { | |
93 ::increment_index (ra_idx, dimensions, start_dimension); | |
94 } | |
95 | |
96 template <class T> | |
5275 | 97 octave_idx_type |
98 intNDArray<T>::compute_index (Array<octave_idx_type>& ra_idx, | |
4902 | 99 const dim_vector& dimensions) |
100 { | |
101 return ::compute_index (ra_idx, dimensions); | |
102 } | |
103 | |
4915 | 104 template <class T> |
5073 | 105 intNDArray<T> |
5275 | 106 intNDArray<T>::concat (const intNDArray<T>& rb, const Array<octave_idx_type>& ra_idx) |
5073 | 107 { |
6482 | 108 if (rb.numel () > 0) |
5073 | 109 insert (rb, ra_idx); |
110 return *this; | |
111 } | |
112 | |
113 template <class T> | |
4915 | 114 intNDArray<T>& |
5275 | 115 intNDArray<T>::insert (const intNDArray<T>& a, octave_idx_type r, octave_idx_type c) |
4915 | 116 { |
117 Array<T>::insert (a, r, c); | |
118 return *this; | |
119 } | |
120 | |
121 template <class T> | |
122 intNDArray<T>& | |
5275 | 123 intNDArray<T>::insert (const intNDArray<T>& a, const Array<octave_idx_type>& ra_idx) |
4915 | 124 { |
125 Array<T>::insert (a, ra_idx); | |
126 return *this; | |
127 } | |
128 | |
4902 | 129 // This contains no information on the array structure !!! |
130 | |
131 template <class T> | |
132 std::ostream& | |
133 operator << (std::ostream& os, const intNDArray<T>& a) | |
134 { | |
5275 | 135 octave_idx_type nel = a.nelem (); |
4902 | 136 |
5275 | 137 for (octave_idx_type i = 0; i < nel; i++) |
4902 | 138 os << " " << a.elem (i) << "\n"; |
139 | |
140 return os; | |
141 } | |
142 | |
143 template <class T> | |
144 std::istream& | |
145 operator >> (std::istream& is, intNDArray<T>& a) | |
146 { | |
5275 | 147 octave_idx_type nel = a.nelem (); |
4902 | 148 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
149 if (nel > 0) |
4902 | 150 { |
151 T tmp; | |
152 | |
5275 | 153 for (octave_idx_type i = 0; i < nel; i++) |
4902 | 154 { |
155 is >> tmp; | |
156 | |
157 if (is) | |
158 a.elem (i) = tmp; | |
159 else | |
160 goto done; | |
161 } | |
162 } | |
163 | |
164 done: | |
165 | |
166 return is; | |
167 } | |
168 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
169 // FIXME -- should abs and signum just be mapper functions? |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
170 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
171 template <class T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
172 intNDArray<T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
173 intNDArray<T>::abs (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
174 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
175 octave_idx_type nel = this->nelem (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
176 intNDArray<T> ret (*this); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
177 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
178 for (octave_idx_type i = 0; i < nel; i++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
179 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
180 T val = this->elem (i); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
181 ret.xelem (i) = val.abs (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
182 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
183 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
184 return ret; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
185 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
186 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
187 template <class T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
188 intNDArray<T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
189 intNDArray<T>::signum (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
190 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
191 octave_idx_type nel = this->nelem (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
192 intNDArray<T> ret (*this); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
193 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
194 for (octave_idx_type i = 0; i < nel; i++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
195 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
196 T val = this->elem (i); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
197 ret.xelem (i) = val.signum (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
198 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
199 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
200 return ret; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
201 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
202 |
7113 | 203 template <class T> |
204 intNDArray<T> | |
205 intNDArray<T>::sum (int dim) const | |
206 { | |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
207 return do_mx_red_op<intNDArray<T> , T > (*this, dim, mx_inline_sum); |
7113 | 208 } |
209 | |
7189 | 210 template <class T> |
211 intNDArray<T> | |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
212 intNDArray<T>::cumsum (int dim) const |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
213 { |
9227
8145f2255276
use explicit template qualifs to please Intel C++ and MSVC++
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
214 return do_mx_cum_op<intNDArray<T> , T > (*this, dim, mx_inline_cumsum); |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
215 } |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
216 |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
217 template <class T> |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8777
diff
changeset
|
218 intNDArray<T> |
7189 | 219 intNDArray<T>::max (int dim) const |
220 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
221 return do_mx_minmax_op<intNDArray<T> > (*this, dim, mx_inline_max); |
7189 | 222 } |
223 | |
224 template <class T> | |
225 intNDArray<T> | |
226 intNDArray<T>::max (ArrayN<octave_idx_type>& idx_arg, int dim) const | |
227 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
228 return do_mx_minmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_max); |
7189 | 229 } |
230 | |
231 template <class T> | |
232 intNDArray<T> | |
233 intNDArray<T>::min (int dim) const | |
234 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
235 return do_mx_minmax_op<intNDArray<T> > (*this, dim, mx_inline_min); |
7189 | 236 } |
237 | |
238 template <class T> | |
239 intNDArray<T> | |
240 intNDArray<T>::min (ArrayN<octave_idx_type>& idx_arg, int dim) const | |
241 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
242 return do_mx_minmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_min); |
7189 | 243 } |
244 | |
8777
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
245 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
246 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
247 intNDArray<T>::cummax (int dim) const |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
248 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
249 return do_mx_cumminmax_op<intNDArray<T> > (*this, dim, mx_inline_cummax); |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
250 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
251 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
252 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
253 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
254 intNDArray<T>::cummax (ArrayN<octave_idx_type>& idx_arg, int dim) const |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
255 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
256 return do_mx_cumminmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_cummax); |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
257 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
258 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
259 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
260 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
261 intNDArray<T>::cummin (int dim) const |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
262 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
263 return do_mx_cumminmax_op<intNDArray<T> > (*this, dim, mx_inline_cummin); |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
264 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
265 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
266 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
267 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
268 intNDArray<T>::cummin (ArrayN<octave_idx_type>& idx_arg, int dim) const |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
269 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
270 return do_mx_cumminmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_cummin); |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
271 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
272 |
9513
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
273 template <class T> |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
274 intNDArray<T> |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
275 intNDArray<T>::diff (octave_idx_type order, int dim) const |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
276 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
277 return do_mx_diff_op<intNDArray<T> > (*this, dim, order, mx_inline_diff); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
278 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
279 |
4902 | 280 /* |
281 ;;; Local Variables: *** | |
282 ;;; mode: C++ *** | |
283 ;;; End: *** | |
284 */ |