Mercurial > hg > octave-nkf
annotate liboctave/intNDArray.cc @ 8777:724c0f46d9d4
implement cummin/cummax functions
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 17 Feb 2009 11:26:29 +0100 |
parents | 9f7ce4bf7650 |
children | ea76466605ba |
rev | line source |
---|---|
4902 | 1 // N-D Array manipulations. |
2 /* | |
3 | |
7017 | 4 Copyright (C) 2004, 2005, 2006, 2007 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 { | |
8743
1bd918cfb6e2
reimplement any & all using the new reduction code
Jaroslav Hajek <highegg@gmail.com>
parents:
8741
diff
changeset
|
77 return do_mx_red_op<boolNDArray> (*this, dim, mx_inline_all); |
4902 | 78 } |
79 | |
80 template <class T> | |
81 boolNDArray | |
82 intNDArray<T>::any (int dim) const | |
83 { | |
8743
1bd918cfb6e2
reimplement any & all using the new reduction code
Jaroslav Hajek <highegg@gmail.com>
parents:
8741
diff
changeset
|
84 return do_mx_red_op<boolNDArray> (*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 |
149 if (nel < 1 ) | |
150 is.clear (std::ios::badbit); | |
151 else | |
152 { | |
153 T tmp; | |
154 | |
5275 | 155 for (octave_idx_type i = 0; i < nel; i++) |
4902 | 156 { |
157 is >> tmp; | |
158 | |
159 if (is) | |
160 a.elem (i) = tmp; | |
161 else | |
162 goto done; | |
163 } | |
164 } | |
165 | |
166 done: | |
167 | |
168 return is; | |
169 } | |
170 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
171 // 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
|
172 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
173 template <class T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
174 intNDArray<T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
175 intNDArray<T>::abs (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
176 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
177 octave_idx_type nel = this->nelem (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
178 intNDArray<T> ret (*this); |
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 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
|
181 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
182 T val = this->elem (i); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
183 ret.xelem (i) = val.abs (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
184 } |
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 return ret; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
187 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
188 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
189 template <class T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
190 intNDArray<T> |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
191 intNDArray<T>::signum (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
192 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
193 octave_idx_type nel = this->nelem (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
194 intNDArray<T> ret (*this); |
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 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
|
197 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
198 T val = this->elem (i); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
199 ret.xelem (i) = val.signum (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
200 } |
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 return ret; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
203 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7457
diff
changeset
|
204 |
7113 | 205 template <class T> |
206 intNDArray<T> | |
207 intNDArray<T>::sum (int dim) const | |
208 { | |
8741
008f3985c8c0
use new summation code for native integer summation
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
209 return do_mx_red_op<intNDArray<T> > (*this, dim, mx_inline_sum); |
7113 | 210 } |
211 | |
7189 | 212 template <class T> |
213 intNDArray<T> | |
214 intNDArray<T>::max (int dim) const | |
215 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
216 return do_mx_minmax_op<intNDArray<T> > (*this, dim, mx_inline_max); |
7189 | 217 } |
218 | |
219 template <class T> | |
220 intNDArray<T> | |
221 intNDArray<T>::max (ArrayN<octave_idx_type>& idx_arg, int dim) const | |
222 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
223 return do_mx_minmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_max); |
7189 | 224 } |
225 | |
226 template <class T> | |
227 intNDArray<T> | |
228 intNDArray<T>::min (int dim) const | |
229 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
230 return do_mx_minmax_op<intNDArray<T> > (*this, dim, mx_inline_min); |
7189 | 231 } |
232 | |
233 template <class T> | |
234 intNDArray<T> | |
235 intNDArray<T>::min (ArrayN<octave_idx_type>& idx_arg, int dim) const | |
236 { | |
8751
9f7ce4bf7650
optimize min/max functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8743
diff
changeset
|
237 return do_mx_minmax_op<intNDArray<T> > (*this, idx_arg, dim, mx_inline_min); |
7189 | 238 } |
239 | |
8777
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
240 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
241 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
242 intNDArray<T>::cummax (int dim) const |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
243 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
244 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
|
245 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
246 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
247 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
248 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
249 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
|
250 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
251 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
|
252 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
253 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
254 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
255 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
256 intNDArray<T>::cummin (int dim) const |
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 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
|
259 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
260 |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
261 template <class T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
262 intNDArray<T> |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
263 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
|
264 { |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
265 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
|
266 } |
724c0f46d9d4
implement cummin/cummax functions
Jaroslav Hajek <highegg@gmail.com>
parents:
8751
diff
changeset
|
267 |
4902 | 268 /* |
269 ;;; Local Variables: *** | |
270 ;;; mode: C++ *** | |
271 ;;; End: *** | |
272 */ |