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