Mercurial > hg > octave-lyh
comparison liboctave/MArray.cc @ 1989:a4b0826e240c
[project @ 1996-03-02 00:33:22 by jwe]
author | jwe |
---|---|
date | Sat, 02 Mar 1996 00:34:12 +0000 |
parents | 1281a23a34dd |
children | 1b57120c997b |
comparison
equal
deleted
inserted
replaced
1988:7b56630a1e05 | 1989:a4b0826e240c |
---|---|
30 #endif | 30 #endif |
31 | 31 |
32 #include "MArray.h" | 32 #include "MArray.h" |
33 #include "lo-error.h" | 33 #include "lo-error.h" |
34 | 34 |
35 // Nothing like a little CPP abuse to brighten everyone's day. Would | 35 #include "MArray-defs.h" |
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 | |
83 #define DO_VS_OP2(OP) \ | |
84 int l = a.length (); \ | |
85 if (l > 0) \ | |
86 { \ | |
87 T *tmp = a.fortran_vec (); \ | |
88 for (int i = 0; i < l; i++) \ | |
89 tmp[i] OP s; \ | |
90 } | |
91 | |
92 #define DO_VV_OP2(OP) \ | |
93 do \ | |
94 { \ | |
95 T *a_tmp = a.fortran_vec (); \ | |
96 const T *b_tmp = b.data (); \ | |
97 for (int i = 0; i < l; i++) \ | |
98 a_tmp[i] += b_tmp[i]; \ | |
99 } \ | |
100 while (0) | |
101 | 36 |
102 // One dimensional array with math ops. | 37 // One dimensional array with math ops. |
103 | 38 |
104 // Element by element MArray by scalar ops. | 39 // Element by element MArray by scalar ops. |
105 | 40 |
218 { | 153 { |
219 NEG_V; | 154 NEG_V; |
220 return MArray<T> (result, l); | 155 return MArray<T> (result, l); |
221 } | 156 } |
222 | 157 |
223 // Two dimensional array with math ops. | |
224 | |
225 #ifndef NO_DIAG_ARRAY | |
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 } | |
233 #endif | |
234 | |
235 // Element by element MArray2 by scalar ops. | |
236 | |
237 template <class T> | |
238 MArray2<T>& | |
239 operator += (MArray2<T>& a, const T& s) | |
240 { | |
241 DO_VS_OP2 (+=) | |
242 return a; | |
243 } | |
244 | |
245 template <class T> | |
246 MArray2<T>& | |
247 operator -= (MArray2<T>& a, const T& s) | |
248 { | |
249 DO_VS_OP2 (-=) | |
250 return a; | |
251 } | |
252 | |
253 // Element by element MArray2 by MArray2 ops. | |
254 | |
255 template <class T> | |
256 MArray2<T>& | |
257 operator += (MArray2<T>& a, const MArray2<T>& b) | |
258 { | |
259 int r = a.rows (); | |
260 int c = a.cols (); | |
261 if (r != b.rows () || c != b.cols ()) | |
262 { | |
263 (*current_liboctave_error_handler) | |
264 ("nonconformant += array operation attempted"); | |
265 } | |
266 else | |
267 { | |
268 if (r > 0 && c > 0) | |
269 { | |
270 int l = a.length (); | |
271 DO_VV_OP2 (+=); | |
272 } | |
273 } | |
274 return a; | |
275 } | |
276 | |
277 template <class T> | |
278 MArray2<T>& | |
279 operator -= (MArray2<T>& a, const MArray2<T>& b) | |
280 { | |
281 int r = a.rows (); | |
282 int c = a.cols (); | |
283 if (r != b.rows () || c != b.cols ()) | |
284 { | |
285 (*current_liboctave_error_handler) | |
286 ("nonconformant -= array operation attempted"); | |
287 } | |
288 else | |
289 { | |
290 if (r > 0 && c > 0) | |
291 { | |
292 int l = a.length (); | |
293 DO_VV_OP2 (-=); | |
294 } | |
295 } | |
296 return a; | |
297 } | |
298 | |
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 } | |
325 | |
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. | |
332 | |
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") | |
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 | |
368 // Two dimensional diagonal array with math ops. | |
369 | |
370 #ifndef NO_DIAG_ARRAY | |
371 | |
372 // Element by element MDiagArray by MDiagArray ops. | |
373 | |
374 template <class T> | |
375 MDiagArray<T>& | |
376 operator += (MDiagArray<T>& a, const MDiagArray<T>& b) | |
377 { | |
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; | |
395 } | |
396 | |
397 template <class T> | |
398 MDiagArray<T>& | |
399 operator -= (MDiagArray<T>& a, const MDiagArray<T>& b) | |
400 { | |
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; | |
418 } | |
419 | |
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 | |
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 | |
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 } | |
465 | |
466 MARRAY_DADA_OP (operator +, +, "addition") | |
467 MARRAY_DADA_OP (operator -, -, "subtraction") | |
468 MARRAY_DADA_OP (product, *, "product") | |
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 } | |
479 #endif | |
480 | |
481 /* | 158 /* |
482 ;;; Local Variables: *** | 159 ;;; Local Variables: *** |
483 ;;; mode: C++ *** | 160 ;;; mode: C++ *** |
484 ;;; page-delimiter: "^/\\*" *** | 161 ;;; page-delimiter: "^/\\*" *** |
485 ;;; End: *** | 162 ;;; End: *** |