Mercurial > hg > octave-nkf
annotate liboctave/MArray2.cc @ 9585:06b8b51dca48
also handle user-defined graphics properties in new property name validation scheme
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 28 Aug 2009 18:37:31 -0400 |
parents | 3a1dd361f978 |
children | 66970dd627f6 |
rev | line source |
---|---|
1988 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2007 |
4 John W. Eaton | |
1988 | 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. | |
1988 | 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/>. | |
1988 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "MArray2.h" | |
4669 | 29 #include "Array-util.h" |
1988 | 30 #include "lo-error.h" |
31 | |
32 #include "MArray-defs.h" | |
33 | |
34 // Two dimensional array with math ops. | |
35 | |
36 // Element by element MArray2 by scalar ops. | |
37 | |
38 template <class T> | |
39 MArray2<T>& | |
40 operator += (MArray2<T>& a, const T& s) | |
41 { | |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
42 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
43 return a = a + s; |
4646 | 44 DO_VS_OP2 (T, a, +=, s) |
1988 | 45 return a; |
46 } | |
47 | |
48 template <class T> | |
49 MArray2<T>& | |
50 operator -= (MArray2<T>& a, const T& s) | |
51 { | |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
53 return a = a - s; |
4646 | 54 DO_VS_OP2 (T, a, -=, s) |
1988 | 55 return a; |
56 } | |
57 | |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 template <class T> |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 MArray2<T>& |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 operator *= (MArray2<T>& a, const T& s) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 { |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 return a = a * s; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 DO_VS_OP2 (T, a, *=, s) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 return a; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 } |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 template <class T> |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 MArray2<T>& |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 operator /= (MArray2<T>& a, const T& s) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 { |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 return a = a / s; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 DO_VS_OP2 (T, a, /=, s) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
75 return a; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 } |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
77 |
1988 | 78 // Element by element MArray2 by MArray2 ops. |
79 | |
80 template <class T> | |
81 MArray2<T>& | |
82 operator += (MArray2<T>& a, const MArray2<T>& b) | |
83 { | |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
84 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
85 return a = a + b; |
5275 | 86 octave_idx_type r = a.rows (); |
87 octave_idx_type c = a.cols (); | |
88 octave_idx_type br = b.rows (); | |
89 octave_idx_type bc = b.cols (); | |
2383 | 90 if (r != br || c != bc) |
91 gripe_nonconformant ("operator +=", r, c, br, bc); | |
1988 | 92 else |
93 { | |
94 if (r > 0 && c > 0) | |
95 { | |
5275 | 96 octave_idx_type l = a.length (); |
4646 | 97 DO_VV_OP2 (T, a, +=, b); |
1988 | 98 } |
99 } | |
100 return a; | |
101 } | |
102 | |
103 template <class T> | |
104 MArray2<T>& | |
105 operator -= (MArray2<T>& a, const MArray2<T>& b) | |
106 { | |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
107 if (a.is_shared ()) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
108 return a = a - b; |
5275 | 109 octave_idx_type r = a.rows (); |
110 octave_idx_type c = a.cols (); | |
111 octave_idx_type br = b.rows (); | |
112 octave_idx_type bc = b.cols (); | |
2383 | 113 if (r != br || c != bc) |
114 gripe_nonconformant ("operator -=", r, c, br, bc); | |
1988 | 115 else |
116 { | |
117 if (r > 0 && c > 0) | |
118 { | |
5275 | 119 octave_idx_type l = a.length (); |
4646 | 120 DO_VV_OP2 (T, a, -=, b); |
1988 | 121 } |
122 } | |
123 return a; | |
124 } | |
125 | |
9557
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
126 |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
127 template <class T> |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
128 MArray2<T>& |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
129 product_eq (MArray2<T>& a, const MArray2<T>& b) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
130 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
131 if (a.is_shared ()) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
132 return a = product (a, b); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
133 octave_idx_type r = a.rows (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
134 octave_idx_type c = a.cols (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
135 octave_idx_type br = b.rows (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
136 octave_idx_type bc = b.cols (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
137 if (r != br || c != bc) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
138 gripe_nonconformant ("operator .*=", r, c, br, bc); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
139 else |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
140 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
141 if (r > 0 && c > 0) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
142 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
143 octave_idx_type l = a.length (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
144 DO_VV_OP2 (T, a, *=, b); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
145 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
146 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
147 return a; |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
148 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
149 |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
150 template <class T> |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
151 MArray2<T>& |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
152 quotient_eq (MArray2<T>& a, const MArray2<T>& b) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
153 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
154 if (a.is_shared ()) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
155 return a = quotient (a, b); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
156 octave_idx_type r = a.rows (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
157 octave_idx_type c = a.cols (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
158 octave_idx_type br = b.rows (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
159 octave_idx_type bc = b.cols (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
160 if (r != br || c != bc) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
161 gripe_nonconformant ("operator ./=", r, c, br, bc); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
162 else |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
163 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
164 if (r > 0 && c > 0) |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
165 { |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
166 octave_idx_type l = a.length (); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
167 DO_VV_OP2 (T, a, /=, b); |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
168 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
169 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
170 return a; |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
171 } |
3a1dd361f978
optimize .*=, ./= operators
Jaroslav Hajek <highegg@gmail.com>
parents:
9546
diff
changeset
|
172 |
1988 | 173 // Element by element MArray2 by scalar ops. |
174 | |
175 #define MARRAY_A2S_OP(OP) \ | |
176 template <class T> \ | |
177 MArray2<T> \ | |
178 operator OP (const MArray2<T>& a, const T& s) \ | |
179 { \ | |
3504 | 180 MArray2<T> result (a.rows (), a.cols ()); \ |
181 T *r = result.fortran_vec (); \ | |
5275 | 182 octave_idx_type l = a.length (); \ |
3504 | 183 const T *v = a.data (); \ |
184 DO_VS_OP (r, l, v, OP, s); \ | |
185 return result; \ | |
1988 | 186 } |
187 | |
188 MARRAY_A2S_OP (+) | |
189 MARRAY_A2S_OP (-) | |
190 MARRAY_A2S_OP (*) | |
191 MARRAY_A2S_OP (/) | |
192 | |
193 // Element by element scalar by MArray2 ops. | |
194 | |
195 #define MARRAY_SA2_OP(OP) \ | |
196 template <class T> \ | |
197 MArray2<T> \ | |
198 operator OP (const T& s, const MArray2<T>& a) \ | |
199 { \ | |
3504 | 200 MArray2<T> result (a.rows (), a.cols ()); \ |
201 T *r = result.fortran_vec (); \ | |
5275 | 202 octave_idx_type l = a.length (); \ |
3504 | 203 const T *v = a.data (); \ |
204 DO_SV_OP (r, l, s, OP, v); \ | |
205 return result; \ | |
1988 | 206 } |
207 | |
208 MARRAY_SA2_OP (+) | |
209 MARRAY_SA2_OP (-) | |
210 MARRAY_SA2_OP (*) | |
211 MARRAY_SA2_OP (/) | |
212 | |
213 // Element by element MArray2 by MArray2 ops. | |
214 | |
2383 | 215 #define MARRAY_A2A2_OP(FCN, OP) \ |
1988 | 216 template <class T> \ |
217 MArray2<T> \ | |
218 FCN (const MArray2<T>& a, const MArray2<T>& b) \ | |
219 { \ | |
5275 | 220 octave_idx_type a_nr = a.rows (); \ |
221 octave_idx_type a_nc = a.cols (); \ | |
222 octave_idx_type b_nr = b.rows (); \ | |
223 octave_idx_type b_nc = b.cols (); \ | |
3504 | 224 if (a_nr != b_nr || a_nc != b_nc) \ |
1988 | 225 { \ |
3504 | 226 gripe_nonconformant (#FCN, a_nr, a_nc, b_nr, b_nc); \ |
1988 | 227 return MArray2<T> (); \ |
228 } \ | |
3504 | 229 if (a_nr == 0 || a_nc == 0) \ |
230 return MArray2<T> (a_nr, a_nc); \ | |
5275 | 231 octave_idx_type l = a.length (); \ |
3504 | 232 MArray2<T> result (a_nr, a_nc); \ |
233 T *r = result.fortran_vec (); \ | |
234 const T *x = a.data (); \ | |
235 const T *y = b.data (); \ | |
236 DO_VV_OP (r, l, x, OP, y); \ | |
237 return result; \ | |
1988 | 238 } |
239 | |
2383 | 240 MARRAY_A2A2_OP (operator +, +) |
241 MARRAY_A2A2_OP (operator -, -) | |
242 MARRAY_A2A2_OP (product, *) | |
243 MARRAY_A2A2_OP (quotient, /) | |
1988 | 244 |
245 // Unary MArray2 ops. | |
246 | |
247 template <class T> | |
248 MArray2<T> | |
3574 | 249 operator + (const MArray2<T>& a) |
250 { | |
251 return a; | |
252 } | |
253 | |
254 template <class T> | |
255 MArray2<T> | |
1988 | 256 operator - (const MArray2<T>& a) |
257 { | |
5275 | 258 octave_idx_type l = a.length (); |
3504 | 259 MArray2<T> result (a.rows (), a.cols ()); |
260 T *r = result.fortran_vec (); | |
261 const T *x = a.data (); | |
262 NEG_V (r, l, x); | |
263 return result; | |
1988 | 264 } |
265 | |
266 /* | |
267 ;;; Local Variables: *** | |
268 ;;; mode: C++ *** | |
269 ;;; End: *** | |
270 */ |