237
|
1 // MArray.cc -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
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 |
1360
|
102 // One dimensional array with math ops. |
237
|
103 |
|
104 // Element by element MArray by scalar ops. |
|
105 |
|
106 template <class T> |
1213
|
107 MArray<T>& |
1230
|
108 operator += (MArray<T>& a, const T& s) |
237
|
109 { |
1213
|
110 DO_VS_OP2 (+=) |
1230
|
111 return a; |
237
|
112 } |
|
113 |
|
114 template <class T> |
1213
|
115 MArray<T>& |
1230
|
116 operator -= (MArray<T>& a, const T& s) |
237
|
117 { |
1213
|
118 DO_VS_OP2 (-=) |
1230
|
119 return a; |
237
|
120 } |
|
121 |
|
122 // Element by element MArray by MArray ops. |
|
123 |
|
124 template <class T> |
1213
|
125 MArray<T>& |
1230
|
126 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
127 { |
1230
|
128 int l = a.length (); |
1213
|
129 if (l > 0) |
237
|
130 { |
1230
|
131 if (l != b.length ()) |
1213
|
132 (*current_liboctave_error_handler) \ |
|
133 ("nonconformant += array operation attempted"); \ |
|
134 else |
|
135 DO_VV_OP2 (+=); |
237
|
136 } |
1230
|
137 return a; |
237
|
138 } |
|
139 |
|
140 template <class T> |
1213
|
141 MArray<T>& |
1230
|
142 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
143 { |
1230
|
144 int l = a.length (); |
1213
|
145 if (l > 0) |
237
|
146 { |
1230
|
147 if (l != b.length ()) |
1213
|
148 (*current_liboctave_error_handler) \ |
|
149 ("nonconformant -= array operation attempted"); \ |
|
150 else |
|
151 DO_VV_OP2 (-=); |
237
|
152 } |
1230
|
153 return a; |
237
|
154 } |
|
155 |
1213
|
156 // Element by element MArray by scalar ops. |
|
157 |
|
158 #define MARRAY_AS_OP(OP) \ |
|
159 template <class T> \ |
|
160 MArray<T> \ |
|
161 operator OP (const MArray<T>& a, const T& s) \ |
|
162 { \ |
|
163 DO_VS_OP (OP); \ |
|
164 return MArray<T> (result, l); \ |
|
165 } |
237
|
166 |
1213
|
167 MARRAY_AS_OP (+) |
|
168 MARRAY_AS_OP (-) |
|
169 MARRAY_AS_OP (*) |
|
170 MARRAY_AS_OP (/) |
|
171 |
|
172 // Element by element scalar by MArray ops. |
237
|
173 |
1213
|
174 #define MARRAY_SA_OP(OP) \ |
|
175 template <class T> \ |
|
176 MArray<T> \ |
|
177 operator OP (const T& s, const MArray<T>& a) \ |
|
178 { \ |
|
179 DO_SV_OP (OP); \ |
|
180 return MArray<T> (result, l); \ |
|
181 } |
237
|
182 |
1213
|
183 MARRAY_SA_OP(+) |
|
184 MARRAY_SA_OP(-) |
|
185 MARRAY_SA_OP(*) |
|
186 MARRAY_SA_OP(/) |
|
187 |
|
188 // Element by element MArray by MArray ops. |
237
|
189 |
1213
|
190 #define MARRAY_AA_OP(FCN, OP, OP_STR) \ |
|
191 template <class T> \ |
|
192 MArray<T> \ |
|
193 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
194 { \ |
|
195 int l = a.length (); \ |
|
196 if (l != b.length ()) \ |
|
197 { \ |
|
198 (*current_liboctave_error_handler) \ |
|
199 ("nonconformant array " OP_STR " attempted"); \ |
|
200 return MArray<T> (); \ |
|
201 } \ |
|
202 if (l == 0) \ |
|
203 return MArray<T> (); \ |
|
204 DO_VV_OP (OP); \ |
|
205 return MArray<T> (result, l); \ |
|
206 } |
237
|
207 |
1213
|
208 MARRAY_AA_OP (operator +, +, "addition") |
|
209 MARRAY_AA_OP (operator -, -, "subtraction") |
|
210 MARRAY_AA_OP (product, *, "multiplication") |
|
211 MARRAY_AA_OP (quotient, /, "division") |
237
|
212 |
|
213 // Unary MArray ops. |
|
214 |
|
215 template <class T> |
|
216 MArray<T> |
|
217 operator - (const MArray<T>& a) |
|
218 { |
|
219 NEG_V; |
|
220 return MArray<T> (result, l); |
|
221 } |
|
222 |
1360
|
223 // Two dimensional array with math ops. |
237
|
224 |
1574
|
225 #ifndef NO_DIAG_ARRAY |
237
|
226 template <class T> |
|
227 MArray2<T>::MArray2 (const MDiagArray<T>& a) |
|
228 : Array2<T> (a.rows (), a.cols (), T (0)) |
|
229 { |
|
230 for (int i = 0; i < a.length (); i++) |
|
231 elem (i, i) = a.elem (i, i); |
|
232 } |
1574
|
233 #endif |
237
|
234 |
|
235 // Element by element MArray2 by scalar ops. |
|
236 |
|
237 template <class T> |
1213
|
238 MArray2<T>& |
1230
|
239 operator += (MArray2<T>& a, const T& s) |
237
|
240 { |
1213
|
241 DO_VS_OP2 (+=) |
1230
|
242 return a; |
237
|
243 } |
|
244 |
|
245 template <class T> |
1213
|
246 MArray2<T>& |
1230
|
247 operator -= (MArray2<T>& a, const T& s) |
237
|
248 { |
1213
|
249 DO_VS_OP2 (-=) |
1230
|
250 return a; |
237
|
251 } |
|
252 |
|
253 // Element by element MArray2 by MArray2 ops. |
|
254 |
|
255 template <class T> |
1213
|
256 MArray2<T>& |
1230
|
257 operator += (MArray2<T>& a, const MArray2<T>& b) |
237
|
258 { |
1230
|
259 int r = a.rows (); |
|
260 int c = a.cols (); |
|
261 if (r != b.rows () || c != b.cols ()) |
237
|
262 { |
|
263 (*current_liboctave_error_handler) |
1213
|
264 ("nonconformant += array operation attempted"); |
237
|
265 } |
1213
|
266 else |
|
267 { |
|
268 if (r > 0 && c > 0) |
|
269 { |
|
270 int l = a.length (); |
|
271 DO_VV_OP2 (+=); |
|
272 } |
|
273 } |
1230
|
274 return a; |
237
|
275 } |
|
276 |
|
277 template <class T> |
1213
|
278 MArray2<T>& |
1230
|
279 operator -= (MArray2<T>& a, const MArray2<T>& b) |
237
|
280 { |
1230
|
281 int r = a.rows (); |
|
282 int c = a.cols (); |
|
283 if (r != b.rows () || c != b.cols ()) |
237
|
284 { |
|
285 (*current_liboctave_error_handler) |
1213
|
286 ("nonconformant -= array operation attempted"); |
237
|
287 } |
1213
|
288 else |
|
289 { |
|
290 if (r > 0 && c > 0) |
|
291 { |
|
292 int l = a.length (); |
|
293 DO_VV_OP2 (-=); |
|
294 } |
|
295 } |
1230
|
296 return a; |
237
|
297 } |
|
298 |
1213
|
299 // Element by element MArray2 by scalar ops. |
|
300 |
|
301 #define MARRAY_A2S_OP(OP) \ |
|
302 template <class T> \ |
|
303 MArray2<T> \ |
|
304 operator OP (const MArray2<T>& a, const T& s) \ |
|
305 { \ |
|
306 DO_VS_OP (OP); \ |
|
307 return MArray2<T> (result, a.rows (), a.cols ()); \ |
|
308 } |
|
309 |
|
310 MARRAY_A2S_OP (+) |
|
311 MARRAY_A2S_OP (-) |
|
312 MARRAY_A2S_OP (*) |
|
313 MARRAY_A2S_OP (/) |
|
314 |
|
315 // Element by element scalar by MArray2 ops. |
|
316 |
|
317 #define MARRAY_SA2_OP(OP) \ |
|
318 template <class T> \ |
|
319 MArray2<T> \ |
|
320 operator OP (const T& s, const MArray2<T>& a) \ |
|
321 { \ |
|
322 DO_SV_OP (OP); \ |
|
323 return MArray2<T> (result, a.rows (), a.cols ()); \ |
|
324 } |
237
|
325 |
1213
|
326 MARRAY_SA2_OP (+) |
|
327 MARRAY_SA2_OP (-) |
|
328 MARRAY_SA2_OP (*) |
|
329 MARRAY_SA2_OP (/) |
|
330 |
|
331 // Element by element MArray2 by MArray2 ops. |
237
|
332 |
1213
|
333 #define MARRAY_A2A2_OP(FCN, OP, OP_STR) \ |
|
334 template <class T> \ |
|
335 MArray2<T> \ |
|
336 FCN (const MArray2<T>& a, const MArray2<T>& b) \ |
|
337 { \ |
|
338 int r = a.rows (); \ |
|
339 int c = a.cols (); \ |
|
340 if (r != b.rows () || c != b.cols ()) \ |
|
341 { \ |
|
342 (*current_liboctave_error_handler) \ |
|
343 ("nonconformant array " OP_STR " attempted"); \ |
|
344 return MArray2<T> (); \ |
|
345 } \ |
|
346 if (r == 0 || c == 0) \ |
|
347 return MArray2<T> (); \ |
|
348 int l = a.length (); \ |
|
349 DO_VV_OP (OP); \ |
|
350 return MArray2<T> (result, r, c); \ |
|
351 } |
|
352 |
|
353 MARRAY_A2A2_OP (operator +, +, "addition") |
|
354 MARRAY_A2A2_OP (operator -, -, "subtraction") |
|
355 MARRAY_A2A2_OP (product, *, "product") |
|
356 MARRAY_A2A2_OP (quotient, /, "quotient") |
237
|
357 |
|
358 // Unary MArray2 ops. |
|
359 |
|
360 template <class T> |
|
361 MArray2<T> |
|
362 operator - (const MArray2<T>& a) |
|
363 { |
|
364 NEG_V; |
|
365 return MArray2<T> (result, a.rows (), a.cols ()); |
|
366 } |
|
367 |
1360
|
368 // Two dimensional diagonal array with math ops. |
237
|
369 |
1574
|
370 #ifndef NO_DIAG_ARRAY |
|
371 |
1213
|
372 // Element by element MDiagArray by MDiagArray ops. |
237
|
373 |
|
374 template <class T> |
1213
|
375 MDiagArray<T>& |
1230
|
376 operator += (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
377 { |
1230
|
378 int r = a.rows (); |
|
379 int c = a.cols (); |
|
380 if (r != b.rows () || c != b.cols ()) |
|
381 { |
|
382 (*current_liboctave_error_handler) |
|
383 ("nonconformant array " OP_STR " attempted"); |
|
384 return MArray2<T> (); |
|
385 } |
|
386 else |
|
387 { |
|
388 int l = a.length (); |
|
389 T *a_tmp = a.fortran_vec (); |
|
390 const T *b_tmp = b.data (); |
|
391 for (int i = 0; i < l; i++) |
|
392 a_tmp[i] += b_tmp[i]; |
|
393 } |
|
394 return a; |
237
|
395 } |
|
396 |
|
397 template <class T> |
1213
|
398 MDiagArray<T>& |
1230
|
399 operator -= (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
400 { |
1230
|
401 int r = a.rows (); |
|
402 int c = a.cols (); |
|
403 if (r != b.rows () || c != b.cols ()) |
|
404 { |
|
405 (*current_liboctave_error_handler) |
|
406 ("nonconformant array " OP_STR " attempted"); |
|
407 return MArray2<T> (); |
|
408 } |
|
409 else |
|
410 { |
|
411 int l = a.length (); |
|
412 T *a_tmp = a.fortran_vec (); |
|
413 const T *b_tmp = b.data (); |
|
414 for (int i = 0; i < l; i++) |
|
415 a_tmp[i] -= b_tmp[i]; |
|
416 } |
|
417 return a; |
237
|
418 } |
|
419 |
1213
|
420 // Element by element MDiagArray by scalar ops. |
|
421 |
|
422 #define MARRAY_DAS_OP(OP) \ |
|
423 template <class T> \ |
|
424 MDiagArray<T> \ |
|
425 operator OP (const MDiagArray<T>& a, const T& s) \ |
|
426 { \ |
|
427 DO_VS_OP (OP); \ |
|
428 return MDiagArray<T> (result, a.rows (), a.cols ()); \ |
|
429 } |
|
430 |
|
431 MARRAY_DAS_OP (*) |
|
432 MARRAY_DAS_OP (/) |
|
433 |
237
|
434 // Element by element scalar by MDiagArray ops. |
|
435 |
|
436 template <class T> |
|
437 MDiagArray<T> |
|
438 operator * (const T& s, const MDiagArray<T>& a) |
|
439 { |
|
440 DO_SV_OP (*); |
|
441 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
442 } |
|
443 |
|
444 // Element by element MDiagArray by MDiagArray ops. |
|
445 |
1213
|
446 #define MARRAY_DADA_OP(FCN, OP, OP_STR) \ |
|
447 template <class T> \ |
|
448 MDiagArray<T> \ |
|
449 FCN (const MDiagArray<T>& a, const MDiagArray<T>& b) \ |
|
450 { \ |
|
451 int r = a.rows (); \ |
|
452 int c = a.cols (); \ |
|
453 if (r != b.rows () || c != b.cols ()) \ |
|
454 { \ |
|
455 (*current_liboctave_error_handler) \ |
|
456 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
457 return MDiagArray<T> (); \ |
|
458 } \ |
|
459 if (c == 0 || r == 0) \ |
|
460 return MDiagArray<T> (); \ |
|
461 int l = a.length (); \ |
|
462 DO_VV_OP (OP); \ |
|
463 return MDiagArray<T> (result, r, c); \ |
|
464 } |
237
|
465 |
1213
|
466 MARRAY_DADA_OP (operator +, +, "addition") |
|
467 MARRAY_DADA_OP (operator -, -, "subtraction") |
|
468 MARRAY_DADA_OP (product, *, "product") |
237
|
469 |
|
470 // Unary MDiagArray ops. |
|
471 |
|
472 template <class T> |
|
473 MDiagArray<T> |
|
474 operator - (const MDiagArray<T>& a) |
|
475 { |
|
476 NEG_V; |
|
477 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
478 } |
1574
|
479 #endif |
237
|
480 |
|
481 /* |
|
482 ;;; Local Variables: *** |
|
483 ;;; mode: C++ *** |
|
484 ;;; page-delimiter: "^/\\*" *** |
|
485 ;;; End: *** |
|
486 */ |