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 |
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 |
|
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 |
1360
|
366 // Two dimensional diagonal array with math ops. |
237
|
367 |
1213
|
368 // Element by element MDiagArray by MDiagArray ops. |
237
|
369 |
|
370 template <class T> |
1213
|
371 MDiagArray<T>& |
1230
|
372 operator += (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
373 { |
1230
|
374 int r = a.rows (); |
|
375 int c = a.cols (); |
|
376 if (r != b.rows () || c != b.cols ()) |
|
377 { |
|
378 (*current_liboctave_error_handler) |
|
379 ("nonconformant array " OP_STR " attempted"); |
|
380 return MArray2<T> (); |
|
381 } |
|
382 else |
|
383 { |
|
384 int l = a.length (); |
|
385 T *a_tmp = a.fortran_vec (); |
|
386 const T *b_tmp = b.data (); |
|
387 for (int i = 0; i < l; i++) |
|
388 a_tmp[i] += b_tmp[i]; |
|
389 } |
|
390 return a; |
237
|
391 } |
|
392 |
|
393 template <class T> |
1213
|
394 MDiagArray<T>& |
1230
|
395 operator -= (MDiagArray<T>& a, const MDiagArray<T>& b) |
237
|
396 { |
1230
|
397 int r = a.rows (); |
|
398 int c = a.cols (); |
|
399 if (r != b.rows () || c != b.cols ()) |
|
400 { |
|
401 (*current_liboctave_error_handler) |
|
402 ("nonconformant array " OP_STR " attempted"); |
|
403 return MArray2<T> (); |
|
404 } |
|
405 else |
|
406 { |
|
407 int l = a.length (); |
|
408 T *a_tmp = a.fortran_vec (); |
|
409 const T *b_tmp = b.data (); |
|
410 for (int i = 0; i < l; i++) |
|
411 a_tmp[i] -= b_tmp[i]; |
|
412 } |
|
413 return a; |
237
|
414 } |
|
415 |
1213
|
416 // Element by element MDiagArray by scalar ops. |
|
417 |
|
418 #define MARRAY_DAS_OP(OP) \ |
|
419 template <class T> \ |
|
420 MDiagArray<T> \ |
|
421 operator OP (const MDiagArray<T>& a, const T& s) \ |
|
422 { \ |
|
423 DO_VS_OP (OP); \ |
|
424 return MDiagArray<T> (result, a.rows (), a.cols ()); \ |
|
425 } |
|
426 |
|
427 MARRAY_DAS_OP (*) |
|
428 MARRAY_DAS_OP (/) |
|
429 |
237
|
430 // Element by element scalar by MDiagArray ops. |
|
431 |
|
432 template <class T> |
|
433 MDiagArray<T> |
|
434 operator * (const T& s, const MDiagArray<T>& a) |
|
435 { |
|
436 DO_SV_OP (*); |
|
437 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
438 } |
|
439 |
|
440 // Element by element MDiagArray by MDiagArray ops. |
|
441 |
1213
|
442 #define MARRAY_DADA_OP(FCN, OP, OP_STR) \ |
|
443 template <class T> \ |
|
444 MDiagArray<T> \ |
|
445 FCN (const MDiagArray<T>& a, const MDiagArray<T>& b) \ |
|
446 { \ |
|
447 int r = a.rows (); \ |
|
448 int c = a.cols (); \ |
|
449 if (r != b.rows () || c != b.cols ()) \ |
|
450 { \ |
|
451 (*current_liboctave_error_handler) \ |
|
452 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
453 return MDiagArray<T> (); \ |
|
454 } \ |
|
455 if (c == 0 || r == 0) \ |
|
456 return MDiagArray<T> (); \ |
|
457 int l = a.length (); \ |
|
458 DO_VV_OP (OP); \ |
|
459 return MDiagArray<T> (result, r, c); \ |
|
460 } |
237
|
461 |
1213
|
462 MARRAY_DADA_OP (operator +, +, "addition") |
|
463 MARRAY_DADA_OP (operator -, -, "subtraction") |
|
464 MARRAY_DADA_OP (product, *, "product") |
237
|
465 |
|
466 // Unary MDiagArray ops. |
|
467 |
|
468 template <class T> |
|
469 MDiagArray<T> |
|
470 operator - (const MDiagArray<T>& a) |
|
471 { |
|
472 NEG_V; |
|
473 return MDiagArray<T> (result, a.rows (), a.cols ()); |
|
474 } |
|
475 |
|
476 /* |
|
477 ;;; Local Variables: *** |
|
478 ;;; mode: C++ *** |
|
479 ;;; page-delimiter: "^/\\*" *** |
|
480 ;;; End: *** |
|
481 */ |