237
|
1 // MArray.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
237
|
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, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
237
|
26 #endif |
|
27 |
|
28 #include "MArray.h" |
|
29 #include "lo-error.h" |
|
30 |
|
31 // Nothing like a little CPP abuse to brighten everyone's day. Would |
|
32 // have been nice to do this with template functions but as of 2.5.x, |
|
33 // g++ seems to fail to resolve them properly. |
|
34 |
|
35 #define DO_VS_OP(OP) \ |
|
36 int l = a.length (); \ |
|
37 T *result = 0; \ |
|
38 if (l > 0) \ |
|
39 { \ |
|
40 result = new T [l]; \ |
|
41 const T *x = a.data (); \ |
|
42 for (int i = 0; i < l; i++) \ |
|
43 result[i] = x[i] OP s; \ |
|
44 } |
|
45 |
|
46 #define DO_SV_OP(OP) \ |
|
47 int l = a.length (); \ |
|
48 T *result = 0; \ |
|
49 if (l > 0) \ |
|
50 { \ |
|
51 result = new T [l]; \ |
|
52 const T *x = a.data (); \ |
|
53 for (int i = 0; i < l; i++) \ |
|
54 result[i] = s OP x[i]; \ |
|
55 } |
|
56 |
|
57 #define DO_VV_OP(OP) \ |
|
58 T *result = 0; \ |
|
59 if (l > 0) \ |
|
60 { \ |
|
61 result = new T [l]; \ |
|
62 const T *x = a.data (); \ |
|
63 const T *y = b.data (); \ |
|
64 for (int i = 0; i < l; i++) \ |
|
65 result[i] = x[i] OP y[i]; \ |
|
66 } |
|
67 |
|
68 #define NEG_V \ |
|
69 int l = a.length (); \ |
|
70 T *result = 0; \ |
|
71 if (l > 0) \ |
|
72 { \ |
|
73 result = new T [l]; \ |
|
74 const T *x = a.data (); \ |
|
75 for (int i = 0; i < l; i++) \ |
|
76 result[i] = -x[i]; \ |
|
77 } |
|
78 |
1213
|
79 #define DO_VS_OP2(OP) \ |
1230
|
80 int l = a.length (); \ |
1213
|
81 if (l > 0) \ |
|
82 { \ |
1230
|
83 T *tmp = a.fortran_vec (); \ |
1213
|
84 for (int i = 0; i < l; i++) \ |
|
85 tmp[i] OP s; \ |
|
86 } |
|
87 |
|
88 #define DO_VV_OP2(OP) \ |
|
89 do \ |
|
90 { \ |
1230
|
91 T *a_tmp = a.fortran_vec (); \ |
|
92 const T *b_tmp = b.data (); \ |
1213
|
93 for (int i = 0; i < l; i++) \ |
1230
|
94 a_tmp[i] += b_tmp[i]; \ |
1213
|
95 } \ |
|
96 while (0) |
|
97 |
237
|
98 /* |
|
99 * One dimensional array with math ops. |
|
100 */ |
|
101 |
|
102 // Element by element MArray by scalar ops. |
|
103 |
|
104 template <class T> |
1213
|
105 MArray<T>& |
1230
|
106 operator += (MArray<T>& a, const T& s) |
237
|
107 { |
1213
|
108 DO_VS_OP2 (+=) |
1230
|
109 return a; |
237
|
110 } |
|
111 |
|
112 template <class T> |
1213
|
113 MArray<T>& |
1230
|
114 operator -= (MArray<T>& a, const T& s) |
237
|
115 { |
1213
|
116 DO_VS_OP2 (-=) |
1230
|
117 return a; |
237
|
118 } |
|
119 |
|
120 // Element by element MArray by MArray ops. |
|
121 |
|
122 template <class T> |
1213
|
123 MArray<T>& |
1230
|
124 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
125 { |
1230
|
126 int l = a.length (); |
1213
|
127 if (l > 0) |
237
|
128 { |
1230
|
129 if (l != b.length ()) |
1213
|
130 (*current_liboctave_error_handler) \ |
|
131 ("nonconformant += array operation attempted"); \ |
|
132 else |
|
133 DO_VV_OP2 (+=); |
237
|
134 } |
1230
|
135 return a; |
237
|
136 } |
|
137 |
|
138 template <class T> |
1213
|
139 MArray<T>& |
1230
|
140 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
141 { |
1230
|
142 int l = a.length (); |
1213
|
143 if (l > 0) |
237
|
144 { |
1230
|
145 if (l != b.length ()) |
1213
|
146 (*current_liboctave_error_handler) \ |
|
147 ("nonconformant -= array operation attempted"); \ |
|
148 else |
|
149 DO_VV_OP2 (-=); |
237
|
150 } |
1230
|
151 return a; |
237
|
152 } |
|
153 |
1213
|
154 // Element by element MArray by scalar ops. |
|
155 |
|
156 #define MARRAY_AS_OP(OP) \ |
|
157 template <class T> \ |
|
158 MArray<T> \ |
|
159 operator OP (const MArray<T>& a, const T& s) \ |
|
160 { \ |
|
161 DO_VS_OP (OP); \ |
|
162 return MArray<T> (result, l); \ |
|
163 } |
237
|
164 |
1213
|
165 MARRAY_AS_OP (+) |
|
166 MARRAY_AS_OP (-) |
|
167 MARRAY_AS_OP (*) |
|
168 MARRAY_AS_OP (/) |
|
169 |
|
170 // Element by element scalar by MArray ops. |
237
|
171 |
1213
|
172 #define MARRAY_SA_OP(OP) \ |
|
173 template <class T> \ |
|
174 MArray<T> \ |
|
175 operator OP (const T& s, const MArray<T>& a) \ |
|
176 { \ |
|
177 DO_SV_OP (OP); \ |
|
178 return MArray<T> (result, l); \ |
|
179 } |
237
|
180 |
1213
|
181 MARRAY_SA_OP(+) |
|
182 MARRAY_SA_OP(-) |
|
183 MARRAY_SA_OP(*) |
|
184 MARRAY_SA_OP(/) |
|
185 |
|
186 // Element by element MArray by MArray ops. |
237
|
187 |
1213
|
188 #define MARRAY_AA_OP(FCN, OP, OP_STR) \ |
|
189 template <class T> \ |
|
190 MArray<T> \ |
|
191 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
192 { \ |
|
193 int l = a.length (); \ |
|
194 if (l != b.length ()) \ |
|
195 { \ |
|
196 (*current_liboctave_error_handler) \ |
|
197 ("nonconformant array " OP_STR " attempted"); \ |
|
198 return MArray<T> (); \ |
|
199 } \ |
|
200 if (l == 0) \ |
|
201 return MArray<T> (); \ |
|
202 DO_VV_OP (OP); \ |
|
203 return MArray<T> (result, l); \ |
|
204 } |
237
|
205 |
1213
|
206 MARRAY_AA_OP (operator +, +, "addition") |
|
207 MARRAY_AA_OP (operator -, -, "subtraction") |
|
208 MARRAY_AA_OP (product, *, "multiplication") |
|
209 MARRAY_AA_OP (quotient, /, "division") |
237
|
210 |
|
211 // Unary MArray ops. |
|
212 |
|
213 template <class T> |
|
214 MArray<T> |
|
215 operator - (const MArray<T>& a) |
|
216 { |
|
217 NEG_V; |
|
218 return MArray<T> (result, l); |
|
219 } |
|
220 |
|
221 /* |
|
222 * Two dimensional array with math ops. |
|
223 */ |
|
224 |
|
225 template <class T> |
|
226 MArray2<T>::MArray2 (const MDiagArray<T>& a) |
|
227 : Array2<T> (a.rows (), a.cols (), T (0)) |
|
228 { |
|
229 for (int i = 0; i < a.length (); i++) |
|
230 elem (i, i) = a.elem (i, i); |
|
231 } |
|
232 |
|
233 // Element by element MArray2 by scalar ops. |
|
234 |
|
235 template <class T> |
1213
|
236 MArray2<T>& |
1230
|
237 operator += (MArray2<T>& a, const T& s) |
237
|
238 { |
1213
|
239 DO_VS_OP2 (+=) |
1230
|
240 return a; |
237
|
241 } |
|
242 |
|
243 template <class T> |
1213
|
244 MArray2<T>& |
1230
|
245 operator -= (MArray2<T>& a, const T& s) |
237
|
246 { |
1213
|
247 DO_VS_OP2 (-=) |
1230
|
248 return a; |
237
|
249 } |
|
250 |
|
251 // Element by element MArray2 by MArray2 ops. |
|
252 |
|
253 template <class T> |
1213
|
254 MArray2<T>& |
1230
|
255 operator += (MArray2<T>& a, const MArray2<T>& b) |
237
|
256 { |
1230
|
257 int r = a.rows (); |
|
258 int c = a.cols (); |
|
259 if (r != b.rows () || c != b.cols ()) |
237
|
260 { |
|
261 (*current_liboctave_error_handler) |
1213
|
262 ("nonconformant += array operation attempted"); |
237
|
263 } |
1213
|
264 else |
|
265 { |
|
266 if (r > 0 && c > 0) |
|
267 { |
|
268 int l = a.length (); |
|
269 DO_VV_OP2 (+=); |
|
270 } |
|
271 } |
1230
|
272 return a; |
237
|
273 } |
|
274 |
|
275 template <class T> |
1213
|
276 MArray2<T>& |
1230
|
277 operator -= (MArray2<T>& a, const MArray2<T>& b) |
237
|
278 { |
1230
|
279 int r = a.rows (); |
|
280 int c = a.cols (); |
|
281 if (r != b.rows () || c != b.cols ()) |
237
|
282 { |
|
283 (*current_liboctave_error_handler) |
1213
|
284 ("nonconformant -= array operation attempted"); |
237
|
285 } |
1213
|
286 else |
|
287 { |
|
288 if (r > 0 && c > 0) |
|
289 { |
|
290 int l = a.length (); |
|
291 DO_VV_OP2 (-=); |
|
292 } |
|
293 } |
1230
|
294 return a; |
237
|
295 } |
|
296 |
1213
|
297 // Element by element MArray2 by scalar ops. |
|
298 |
|
299 #define MARRAY_A2S_OP(OP) \ |
|
300 template <class T> \ |
|
301 MArray2<T> \ |
|
302 operator OP (const MArray2<T>& a, const T& s) \ |
|
303 { \ |
|
304 DO_VS_OP (OP); \ |
|
305 return MArray2<T> (result, a.rows (), a.cols ()); \ |
|
306 } |
|
307 |
|
308 MARRAY_A2S_OP (+) |
|
309 MARRAY_A2S_OP (-) |
|
310 MARRAY_A2S_OP (*) |
|
311 MARRAY_A2S_OP (/) |
|
312 |
|
313 // Element by element scalar by MArray2 ops. |
|
314 |
|
315 #define MARRAY_SA2_OP(OP) \ |
|
316 template <class T> \ |
|
317 MArray2<T> \ |
|
318 operator OP (const T& s, const MArray2<T>& a) \ |
|
319 { \ |
|
320 DO_SV_OP (OP); \ |
|
321 return MArray2<T> (result, a.rows (), a.cols ()); \ |
|
322 } |
237
|
323 |
1213
|
324 MARRAY_SA2_OP (+) |
|
325 MARRAY_SA2_OP (-) |
|
326 MARRAY_SA2_OP (*) |
|
327 MARRAY_SA2_OP (/) |
|
328 |
|
329 // Element by element MArray2 by MArray2 ops. |
237
|
330 |
1213
|
331 #define MARRAY_A2A2_OP(FCN, OP, OP_STR) \ |
|
332 template <class T> \ |
|
333 MArray2<T> \ |
|
334 FCN (const MArray2<T>& a, const MArray2<T>& b) \ |
|
335 { \ |
|
336 int r = a.rows (); \ |
|
337 int c = a.cols (); \ |
|
338 if (r != b.rows () || c != b.cols ()) \ |
|
339 { \ |
|
340 (*current_liboctave_error_handler) \ |
|
341 ("nonconformant array " OP_STR " attempted"); \ |
|
342 return MArray2<T> (); \ |
|
343 } \ |
|
344 if (r == 0 || c == 0) \ |
|
345 return MArray2<T> (); \ |
|
346 int l = a.length (); \ |
|
347 DO_VV_OP (OP); \ |
|
348 return MArray2<T> (result, r, c); \ |
|
349 } |
|
350 |
|
351 MARRAY_A2A2_OP (operator +, +, "addition") |
|
352 MARRAY_A2A2_OP (operator -, -, "subtraction") |
|
353 MARRAY_A2A2_OP (product, *, "product") |
|
354 MARRAY_A2A2_OP (quotient, /, "quotient") |
237
|
355 |
|
356 // Unary MArray2 ops. |
|
357 |
|
358 template <class T> |
|
359 MArray2<T> |
|
360 operator - (const MArray2<T>& a) |
|
361 { |
|
362 NEG_V; |
|
363 return MArray2<T> (result, a.rows (), a.cols ()); |
|
364 } |
|
365 |
|
366 /* |
|
367 * Two dimensional diagonal array with math ops. |
|
368 */ |
|
369 |
1213
|
370 // Element by element MDiagArray by MDiagArray ops. |
237
|
371 |
|
372 template <class T> |
1213
|
373 MDiagArray<T>& |
1230
|
374 operator += (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
375 { |
1230
|
376 int r = a.rows (); |
|
377 int c = a.cols (); |
|
378 if (r != b.rows () || c != b.cols ()) |
|
379 { |
|
380 (*current_liboctave_error_handler) |
|
381 ("nonconformant array " OP_STR " attempted"); |
|
382 return MArray2<T> (); |
|
383 } |
|
384 else |
|
385 { |
|
386 int l = a.length (); |
|
387 T *a_tmp = a.fortran_vec (); |
|
388 const T *b_tmp = b.data (); |
|
389 for (int i = 0; i < l; i++) |
|
390 a_tmp[i] += b_tmp[i]; |
|
391 } |
|
392 return a; |
237
|
393 } |
|
394 |
|
395 template <class T> |
1213
|
396 MDiagArray<T>& |
1230
|
397 operator -= (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
398 { |
1230
|
399 int r = a.rows (); |
|
400 int c = a.cols (); |
|
401 if (r != b.rows () || c != b.cols ()) |
|
402 { |
|
403 (*current_liboctave_error_handler) |
|
404 ("nonconformant array " OP_STR " attempted"); |
|
405 return MArray2<T> (); |
|
406 } |
|
407 else |
|
408 { |
|
409 int l = a.length (); |
|
410 T *a_tmp = a.fortran_vec (); |
|
411 const T *b_tmp = b.data (); |
|
412 for (int i = 0; i < l; i++) |
|
413 a_tmp[i] -= b_tmp[i]; |
|
414 } |
|
415 return a; |
237
|
416 } |
|
417 |
1213
|
418 // Element by element MDiagArray by scalar ops. |
|
419 |
|
420 #define MARRAY_DAS_OP(OP) \ |
|
421 template <class T> \ |
|
422 MDiagArray<T> \ |
|
423 operator OP (const MDiagArray<T>& a, const T& s) \ |
|
424 { \ |
|
425 DO_VS_OP (OP); \ |
|
426 return MDiagArray<T> (result, a.rows (), a.cols ()); \ |
|
427 } |
|
428 |
|
429 MARRAY_DAS_OP (*) |
|
430 MARRAY_DAS_OP (/) |
|
431 |
237
|
432 // Element by element scalar by MDiagArray ops. |
|
433 |
|
434 template <class T> |
|
435 MDiagArray<T> |
|
436 operator * (const T& s, const MDiagArray<T>& a) |
|
437 { |
|
438 DO_SV_OP (*); |
|
439 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
440 } |
|
441 |
|
442 // Element by element MDiagArray by MDiagArray ops. |
|
443 |
1213
|
444 #define MARRAY_DADA_OP(FCN, OP, OP_STR) \ |
|
445 template <class T> \ |
|
446 MDiagArray<T> \ |
|
447 FCN (const MDiagArray<T>& a, const MDiagArray<T>& b) \ |
|
448 { \ |
|
449 int r = a.rows (); \ |
|
450 int c = a.cols (); \ |
|
451 if (r != b.rows () || c != b.cols ()) \ |
|
452 { \ |
|
453 (*current_liboctave_error_handler) \ |
|
454 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
455 return MDiagArray<T> (); \ |
|
456 } \ |
|
457 if (c == 0 || r == 0) \ |
|
458 return MDiagArray<T> (); \ |
|
459 int l = a.length (); \ |
|
460 DO_VV_OP (OP); \ |
|
461 return MDiagArray<T> (result, r, c); \ |
|
462 } |
237
|
463 |
1213
|
464 MARRAY_DADA_OP (operator +, +, "addition") |
|
465 MARRAY_DADA_OP (operator -, -, "subtraction") |
|
466 MARRAY_DADA_OP (product, *, "product") |
237
|
467 |
|
468 // Unary MDiagArray ops. |
|
469 |
|
470 template <class T> |
|
471 MDiagArray<T> |
|
472 operator - (const MDiagArray<T>& a) |
|
473 { |
|
474 NEG_V; |
|
475 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
476 } |
|
477 |
|
478 /* |
|
479 ;;; Local Variables: *** |
|
480 ;;; mode: C++ *** |
|
481 ;;; page-delimiter: "^/\\*" *** |
|
482 ;;; End: *** |
|
483 */ |