Mercurial > hg > octave-nkf
annotate liboctave/MArray.cc @ 8934:c2099a4d12ea
partially optimize accumarray
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 09 Mar 2009 10:59:19 +0100 |
parents | eb63fbe60fab |
children | 1beb23d2b892 |
rev | line source |
---|---|
237 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1995, 1996, 1997, 2000, 2002, 2003, 2004, 2005, |
8920 | 4 2007, 2008 John W. Eaton |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
237 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
237 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
237 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
1192 | 26 #include <config.h> |
237 | 27 #endif |
28 | |
29 #include "MArray.h" | |
4669 | 30 #include "Array-util.h" |
237 | 31 #include "lo-error.h" |
32 | |
1989 | 33 #include "MArray-defs.h" |
1213 | 34 |
1360 | 35 // One dimensional array with math ops. |
237 | 36 |
6508 | 37 template <class T> |
38 double | |
39 MArray<T>::norm (double) const | |
40 { | |
41 (*current_liboctave_error_handler) | |
42 ("norm: only implemented for double and complex values"); | |
43 | |
44 return 0; | |
45 } | |
46 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
47 template <class T> |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
48 float |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
49 MArray<T>::norm (float) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
50 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
51 (*current_liboctave_error_handler) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
52 ("norm: only implemented for double and complex values"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
53 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
54 return 0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
55 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
56 |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
57 template <class T> |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
58 struct _idxadds_helper |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
59 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
60 T *array; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
61 T val; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
62 _idxadds_helper (T *a, T v) : array (a), val (v) { } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
63 void operator () (octave_idx_type i) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
64 { array[i] += val; } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
65 }; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
66 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
67 template <class T> |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
68 struct _idxadda_helper |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
69 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
70 T *array; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
71 const T *vals; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
72 _idxadda_helper (T *a, const T *v) : array (a), vals (v) { } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
73 void operator () (octave_idx_type i) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
74 { array[i] += *vals++; } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
75 }; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
76 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
77 template <class T> |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
78 void |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
79 MArray<T>::idx_add (const idx_vector& idx, T val) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
80 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
81 octave_idx_type n = this->length (); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
82 octave_idx_type ext = idx.extent (n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
83 if (ext > n) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
84 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
85 this->resize (ext); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
86 n = ext; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
87 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
88 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
89 OCTAVE_QUIT; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
90 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
91 octave_idx_type len = idx.length (n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
92 idx.loop (len, _idxadds_helper<T> (this->fortran_vec (), val)); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
93 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
94 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
95 template <class T> |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
96 void |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
97 MArray<T>::idx_add (const idx_vector& idx, const MArray<T>& vals) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
98 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
99 octave_idx_type n = this->length (); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
100 octave_idx_type ext = idx.extent (n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
101 if (ext > n) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
102 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
103 this->resize (ext); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
104 n = ext; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
105 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
106 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
107 OCTAVE_QUIT; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
108 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
109 octave_idx_type len = std::min (idx.length (n), vals.length ()); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
110 idx.loop (len, _idxadda_helper<T> (this->fortran_vec (), vals.data ())); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
111 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
112 |
237 | 113 // Element by element MArray by scalar ops. |
114 | |
115 template <class T> | |
1213 | 116 MArray<T>& |
1230 | 117 operator += (MArray<T>& a, const T& s) |
237 | 118 { |
4646 | 119 DO_VS_OP2 (T, a, +=, s) |
1230 | 120 return a; |
237 | 121 } |
122 | |
123 template <class T> | |
1213 | 124 MArray<T>& |
1230 | 125 operator -= (MArray<T>& a, const T& s) |
237 | 126 { |
4646 | 127 DO_VS_OP2 (T, a, -=, s) |
1230 | 128 return a; |
237 | 129 } |
130 | |
131 // Element by element MArray by MArray ops. | |
132 | |
133 template <class T> | |
1213 | 134 MArray<T>& |
1230 | 135 operator += (MArray<T>& a, const MArray<T>& b) |
237 | 136 { |
5275 | 137 octave_idx_type l = a.length (); |
1213 | 138 if (l > 0) |
237 | 139 { |
5275 | 140 octave_idx_type bl = b.length (); |
2383 | 141 if (l != bl) |
142 gripe_nonconformant ("operator +=", l, bl); | |
1213 | 143 else |
4646 | 144 DO_VV_OP2 (T, a, +=, b); |
237 | 145 } |
1230 | 146 return a; |
237 | 147 } |
148 | |
149 template <class T> | |
1213 | 150 MArray<T>& |
1230 | 151 operator -= (MArray<T>& a, const MArray<T>& b) |
237 | 152 { |
5275 | 153 octave_idx_type l = a.length (); |
1213 | 154 if (l > 0) |
237 | 155 { |
5275 | 156 octave_idx_type bl = b.length (); |
2383 | 157 if (l != bl) |
158 gripe_nonconformant ("operator -=", l, bl); | |
1213 | 159 else |
4646 | 160 DO_VV_OP2 (T, a, -=, b); |
237 | 161 } |
1230 | 162 return a; |
237 | 163 } |
164 | |
1213 | 165 // Element by element MArray by scalar ops. |
166 | |
167 #define MARRAY_AS_OP(OP) \ | |
168 template <class T> \ | |
169 MArray<T> \ | |
170 operator OP (const MArray<T>& a, const T& s) \ | |
171 { \ | |
3504 | 172 MArray<T> result (a.length ()); \ |
173 T *r = result.fortran_vec (); \ | |
5275 | 174 octave_idx_type l = a.length (); \ |
3504 | 175 const T *v = a.data (); \ |
176 DO_VS_OP (r, l, v, OP, s); \ | |
177 return result; \ | |
1213 | 178 } |
237 | 179 |
1213 | 180 MARRAY_AS_OP (+) |
181 MARRAY_AS_OP (-) | |
182 MARRAY_AS_OP (*) | |
183 MARRAY_AS_OP (/) | |
184 | |
185 // Element by element scalar by MArray ops. | |
237 | 186 |
1213 | 187 #define MARRAY_SA_OP(OP) \ |
188 template <class T> \ | |
189 MArray<T> \ | |
190 operator OP (const T& s, const MArray<T>& a) \ | |
191 { \ | |
3504 | 192 MArray<T> result (a.length ()); \ |
193 T *r = result.fortran_vec (); \ | |
5275 | 194 octave_idx_type l = a.length (); \ |
3504 | 195 const T *v = a.data (); \ |
196 DO_SV_OP (r, l, s, OP, v); \ | |
197 return result; \ | |
198 } | |
237 | 199 |
1213 | 200 MARRAY_SA_OP(+) |
201 MARRAY_SA_OP(-) | |
202 MARRAY_SA_OP(*) | |
203 MARRAY_SA_OP(/) | |
204 | |
205 // Element by element MArray by MArray ops. | |
237 | 206 |
2383 | 207 #define MARRAY_AA_OP(FCN, OP) \ |
1213 | 208 template <class T> \ |
209 MArray<T> \ | |
210 FCN (const MArray<T>& a, const MArray<T>& b) \ | |
211 { \ | |
5275 | 212 octave_idx_type l = a.length (); \ |
213 octave_idx_type bl = b.length (); \ | |
2383 | 214 if (l != bl) \ |
1213 | 215 { \ |
2383 | 216 gripe_nonconformant (#FCN, l, bl); \ |
1213 | 217 return MArray<T> (); \ |
218 } \ | |
219 if (l == 0) \ | |
220 return MArray<T> (); \ | |
3504 | 221 MArray<T> result (l); \ |
222 T *r = result.fortran_vec (); \ | |
223 const T *x = a.data (); \ | |
224 const T *y = b.data (); \ | |
225 DO_VV_OP (r, l, x, OP, y); \ | |
226 return result; \ | |
1213 | 227 } |
237 | 228 |
2383 | 229 MARRAY_AA_OP (operator +, +) |
230 MARRAY_AA_OP (operator -, -) | |
231 MARRAY_AA_OP (product, *) | |
232 MARRAY_AA_OP (quotient, /) | |
237 | 233 |
234 // Unary MArray ops. | |
235 | |
236 template <class T> | |
237 MArray<T> | |
3574 | 238 operator + (const MArray<T>& a) |
239 { | |
240 return a; | |
241 } | |
242 | |
243 template <class T> | |
244 MArray<T> | |
237 | 245 operator - (const MArray<T>& a) |
246 { | |
5275 | 247 octave_idx_type l = a.length (); |
3504 | 248 MArray<T> result (l); |
249 T *r = result.fortran_vec (); | |
250 const T *x = a.data (); | |
251 NEG_V (r, l, x); | |
252 return result; | |
237 | 253 } |
254 | |
255 /* | |
256 ;;; Local Variables: *** | |
257 ;;; mode: C++ *** | |
258 ;;; End: *** | |
259 */ |