comparison liboctave/MArray.cc @ 1213:9689615b34f2

[project @ 1995-04-06 02:25:15 by jwe]
author jwe
date Thu, 06 Apr 1995 02:25:28 +0000
parents b6360f2d4fa6
children 92609e161b29
comparison
equal deleted inserted replaced
1212:7b9d52071a0c 1213:9689615b34f2
74 const T *x = a.data (); \ 74 const T *x = a.data (); \
75 for (int i = 0; i < l; i++) \ 75 for (int i = 0; i < l; i++) \
76 result[i] = -x[i]; \ 76 result[i] = -x[i]; \
77 } 77 }
78 78
79 #define DO_VS_OP2(OP) \
80 int l = length (); \
81 if (l > 0) \
82 { \
83 T *tmp = fortran_vec (); \
84 for (int i = 0; i < l; i++) \
85 tmp[i] OP s; \
86 }
87
88 #define DO_VV_OP2(OP) \
89 do \
90 { \
91 T *tmp = fortran_vec (); \
92 const T *rhs = a.data (); \
93 for (int i = 0; i < l; i++) \
94 tmp[i] += rhs[i]; \
95 } \
96 while (0)
97
79 /* 98 /*
80 * One dimensional array with math ops. 99 * One dimensional array with math ops.
81 */ 100 */
82 101
83 // Element by element MArray by scalar ops. 102 // Element by element MArray by scalar ops.
84 103
85 template <class T> 104 template <class T>
86 MArray<T> 105 MArray<T>&
87 operator + (const MArray<T>& a, const T& s) 106 MArray<T>::operator += (const T& s)
88 { 107 {
89 DO_VS_OP (+); 108 DO_VS_OP2 (+=)
90 return MArray<T> (result, l); 109 return *this;
91 } 110 }
92 111
93 template <class T> 112 template <class T>
94 MArray<T> 113 MArray<T>&
95 operator - (const MArray<T>& a, const T& s) 114 MArray<T>::operator -= (const T& s)
96 { 115 {
97 DO_VS_OP (-); 116 DO_VS_OP2 (-=)
98 return MArray<T> (result, l); 117 return *this;
99 } 118 }
100 119
101 template <class T> 120 // Element by element MArray by MArray ops.
102 MArray<T> 121
103 operator * (const MArray<T>& a, const T& s) 122 template <class T>
104 { 123 MArray<T>&
105 DO_VS_OP (*); 124 MArray<T>::operator += (const MArray<T>& a)
106 return MArray<T> (result, l); 125 {
107 } 126 int l = length ();
108 127 if (l > 0)
109 template <class T> 128 {
110 MArray<T> 129 if (l != a.length ())
111 operator / (const MArray<T>& a, const T& s) 130 (*current_liboctave_error_handler) \
112 { 131 ("nonconformant += array operation attempted"); \
113 DO_VS_OP (/); 132 else
114 return MArray<T> (result, l); 133 DO_VV_OP2 (+=);
115 } 134 }
135 return *this;
136 }
137
138 template <class T>
139 MArray<T>&
140 MArray<T>::operator -= (const MArray<T>& a)
141 {
142 int l = length ();
143 if (l > 0)
144 {
145 if (l != a.length ())
146 (*current_liboctave_error_handler) \
147 ("nonconformant -= array operation attempted"); \
148 else
149 DO_VV_OP2 (-=);
150 }
151 return *this;
152 }
153
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 }
164
165 MARRAY_AS_OP (+)
166 MARRAY_AS_OP (-)
167 MARRAY_AS_OP (*)
168 MARRAY_AS_OP (/)
116 169
117 // Element by element scalar by MArray ops. 170 // Element by element scalar by MArray ops.
118 171
119 template <class T> 172 #define MARRAY_SA_OP(OP) \
120 MArray<T> 173 template <class T> \
121 operator + (const T& s, const MArray<T>& a) 174 MArray<T> \
122 { 175 operator OP (const T& s, const MArray<T>& a) \
123 DO_SV_OP (+); 176 { \
124 return MArray<T> (result, l); 177 DO_SV_OP (OP); \
125 } 178 return MArray<T> (result, l); \
126 179 }
127 template <class T> 180
128 MArray<T> 181 MARRAY_SA_OP(+)
129 operator - (const T& s, const MArray<T>& a) 182 MARRAY_SA_OP(-)
130 { 183 MARRAY_SA_OP(*)
131 DO_SV_OP (-); 184 MARRAY_SA_OP(/)
132 return MArray<T> (result, l);
133 }
134
135 template <class T>
136 MArray<T>
137 operator * (const T& s, const MArray<T>& a)
138 {
139 DO_SV_OP (*);
140 return MArray<T> (result, l);
141 }
142
143 template <class T>
144 MArray<T>
145 operator / (const T& s, const MArray<T>& a)
146 {
147 DO_SV_OP (/);
148 return MArray<T> (result, l);
149 }
150 185
151 // Element by element MArray by MArray ops. 186 // Element by element MArray by MArray ops.
152 187
153 template <class T> 188 #define MARRAY_AA_OP(FCN, OP, OP_STR) \
154 MArray<T> 189 template <class T> \
155 operator + (const MArray<T>& a, const MArray<T>& b) 190 MArray<T> \
156 { 191 FCN (const MArray<T>& a, const MArray<T>& b) \
157 int l = a.length (); 192 { \
158 if (l != b.length ()) 193 int l = a.length (); \
159 { 194 if (l != b.length ()) \
160 (*current_liboctave_error_handler) 195 { \
161 ("nonconformant array addition attempted"); 196 (*current_liboctave_error_handler) \
162 return MArray<T> (); 197 ("nonconformant array " OP_STR " attempted"); \
163 } 198 return MArray<T> (); \
164 199 } \
165 if (l == 0) 200 if (l == 0) \
166 return MArray<T> (); 201 return MArray<T> (); \
167 202 DO_VV_OP (OP); \
168 DO_VV_OP (+); 203 return MArray<T> (result, l); \
169 return MArray<T> (result, l); 204 }
170 } 205
171 206 MARRAY_AA_OP (operator +, +, "addition")
172 template <class T> 207 MARRAY_AA_OP (operator -, -, "subtraction")
173 MArray<T> 208 MARRAY_AA_OP (product, *, "multiplication")
174 operator - (const MArray<T>& a, const MArray<T>& b) 209 MARRAY_AA_OP (quotient, /, "division")
175 {
176 int l = a.length ();
177 if (l != b.length ())
178 {
179 (*current_liboctave_error_handler)
180 ("nonconformant array subtraction attempted");
181 return MArray<T> ();
182 }
183
184 if (l == 0)
185 return MArray<T> ();
186
187 DO_VV_OP (-);
188 return MArray<T> (result, l);
189 }
190
191 template <class T>
192 MArray<T>
193 product (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 product attempted");
200 return MArray<T> ();
201 }
202
203 if (l == 0)
204 return MArray<T> ();
205
206 DO_VV_OP (*);
207 return MArray<T> (result, l);
208 }
209
210 template <class T>
211 MArray<T>
212 quotient (const MArray<T>& a, const MArray<T>& b)
213 {
214 int l = a.length ();
215 if (l != b.length ())
216 {
217 (*current_liboctave_error_handler)
218 ("nonconformant array quotient attempted");
219 return MArray<T> ();
220 }
221
222 if (l == 0)
223 return MArray<T> ();
224
225 DO_VV_OP (/);
226 return MArray<T> (result, l);
227 }
228 210
229 // Unary MArray ops. 211 // Unary MArray ops.
230 212
231 template <class T> 213 template <class T>
232 MArray<T> 214 MArray<T>
249 } 231 }
250 232
251 // Element by element MArray2 by scalar ops. 233 // Element by element MArray2 by scalar ops.
252 234
253 template <class T> 235 template <class T>
254 MArray2<T> 236 MArray2<T>&
255 operator + (const MArray2<T>& a, const T& s) 237 MArray2<T>::operator += (const T& s)
256 { 238 {
257 DO_VS_OP (+); 239 DO_VS_OP2 (+=)
258 return MArray2<T> (result, a.rows (), a.cols ()); 240 return *this;
259 } 241 }
260 242
261 template <class T> 243 template <class T>
262 MArray2<T> 244 MArray2<T>&
263 operator - (const MArray2<T>& a, const T& s) 245 MArray2<T>::operator -= (const T& s)
264 { 246 {
265 DO_VS_OP (-); 247 DO_VS_OP2 (-=)
266 return MArray2<T> (result, a.rows (), a.cols ()); 248 return *this;
267 } 249 }
268 250
269 template <class T> 251 // Element by element MArray2 by MArray2 ops.
270 MArray2<T> 252
271 operator * (const MArray2<T>& a, const T& s) 253 template <class T>
272 { 254 MArray2<T>&
273 DO_VS_OP (*); 255 MArray2<T>::operator += (const MArray2<T>& a)
274 return MArray2<T> (result, a.rows (), a.cols ()); 256 {
275 } 257 int r = rows ();
276 258 int c = cols ();
277 template <class T> 259 if (r != a.rows () || c != a.cols ())
278 MArray2<T> 260 {
279 operator / (const MArray2<T>& a, const T& s) 261 (*current_liboctave_error_handler)
280 { 262 ("nonconformant += array operation attempted");
281 DO_VS_OP (/); 263 }
282 return MArray2<T> (result, a.rows (), a.cols ()); 264 else
283 } 265 {
266 if (r > 0 && c > 0)
267 {
268 int l = a.length ();
269 DO_VV_OP2 (+=);
270 }
271 }
272 return *this;
273 }
274
275 template <class T>
276 MArray2<T>&
277 MArray2<T>::operator -= (const MArray2<T>& a)
278 {
279 int r = rows ();
280 int c = cols ();
281 if (r != a.rows () || c != a.cols ())
282 {
283 (*current_liboctave_error_handler)
284 ("nonconformant -= array operation attempted");
285 }
286 else
287 {
288 if (r > 0 && c > 0)
289 {
290 int l = a.length ();
291 DO_VV_OP2 (-=);
292 }
293 }
294 return *this;
295 }
296
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 (/)
284 312
285 // Element by element scalar by MArray2 ops. 313 // Element by element scalar by MArray2 ops.
286 314
287 template <class T> 315 #define MARRAY_SA2_OP(OP) \
288 MArray2<T> 316 template <class T> \
289 operator + (const T& s, const MArray2<T>& a) 317 MArray2<T> \
290 { 318 operator OP (const T& s, const MArray2<T>& a) \
291 DO_SV_OP (+); 319 { \
292 return MArray2<T> (result, a.rows (), a.cols ()); 320 DO_SV_OP (OP); \
293 } 321 return MArray2<T> (result, a.rows (), a.cols ()); \
294 322 }
295 template <class T> 323
296 MArray2<T> 324 MARRAY_SA2_OP (+)
297 operator - (const T& s, const MArray2<T>& a) 325 MARRAY_SA2_OP (-)
298 { 326 MARRAY_SA2_OP (*)
299 DO_SV_OP (-); 327 MARRAY_SA2_OP (/)
300 return MArray2<T> (result, a.rows (), a.cols ());
301 }
302
303 template <class T>
304 MArray2<T>
305 operator * (const T& s, const MArray2<T>& a)
306 {
307 DO_SV_OP (*);
308 return MArray2<T> (result, a.rows (), a.cols ());
309 }
310
311 template <class T>
312 MArray2<T>
313 operator / (const T& s, const MArray2<T>& a)
314 {
315 DO_SV_OP (/);
316 return MArray2<T> (result, a.rows (), a.cols ());
317 }
318 328
319 // Element by element MArray2 by MArray2 ops. 329 // Element by element MArray2 by MArray2 ops.
320 330
321 template <class T> 331 #define MARRAY_A2A2_OP(FCN, OP, OP_STR) \
322 MArray2<T> 332 template <class T> \
323 operator + (const MArray2<T>& a, const MArray2<T>& b) 333 MArray2<T> \
324 { 334 FCN (const MArray2<T>& a, const MArray2<T>& b) \
325 int r = a.rows (); 335 { \
326 int c = a.cols (); 336 int r = a.rows (); \
327 if (r != b.rows () || c != b.cols ()) 337 int c = a.cols (); \
328 { 338 if (r != b.rows () || c != b.cols ()) \
329 (*current_liboctave_error_handler) 339 { \
330 ("nonconformant array addition attempted"); 340 (*current_liboctave_error_handler) \
331 return MArray2<T> (); 341 ("nonconformant array " OP_STR " attempted"); \
332 } 342 return MArray2<T> (); \
333 343 } \
334 if (r == 0 || c == 0) 344 if (r == 0 || c == 0) \
335 return MArray2<T> (); 345 return MArray2<T> (); \
336 346 int l = a.length (); \
337 int l = a.length (); 347 DO_VV_OP (OP); \
338 DO_VV_OP (+); 348 return MArray2<T> (result, r, c); \
339 return MArray2<T> (result, r, c); 349 }
340 } 350
341 351 MARRAY_A2A2_OP (operator +, +, "addition")
342 template <class T> 352 MARRAY_A2A2_OP (operator -, -, "subtraction")
343 MArray2<T> 353 MARRAY_A2A2_OP (product, *, "product")
344 operator - (const MArray2<T>& a, const MArray2<T>& b) 354 MARRAY_A2A2_OP (quotient, /, "quotient")
345 {
346 int r = a.rows ();
347 int c = a.cols ();
348 if (r != b.rows () || c != b.cols ())
349 {
350 (*current_liboctave_error_handler)
351 ("nonconformant array subtraction attempted");
352 return MArray2<T> ();
353 }
354
355 if (r == 0 || c == 0)
356 return MArray2<T> ();
357
358 int l = a.length ();
359 DO_VV_OP (-);
360 return MArray2<T> (result, r, c);
361 }
362
363 template <class T>
364 MArray2<T>
365 product (const MArray2<T>& a, const MArray2<T>& b)
366 {
367 int r = a.rows ();
368 int c = a.cols ();
369 if (r != b.rows () || c != b.cols ())
370 {
371 (*current_liboctave_error_handler)
372 ("nonconformant array product attempted");
373 return MArray2<T> ();
374 }
375
376 if (r == 0 || c == 0)
377 return MArray2<T> ();
378
379 int l = a.length ();
380 DO_VV_OP (*);
381 return MArray2<T> (result, r, c);
382 }
383
384 template <class T>
385 MArray2<T>
386 quotient (const MArray2<T>& a, const MArray2<T>& b)
387 {
388 int r = a.rows ();
389 int c = a.cols ();
390 if (r != b.rows () || c != b.cols ())
391 {
392 (*current_liboctave_error_handler)
393 ("nonconformant array quotient attempted");
394 return MArray2<T> ();
395 }
396
397 if (r == 0 || c == 0)
398 return MArray2<T> ();
399
400 int l = a.length ();
401 DO_VV_OP (/);
402 return MArray2<T> (result, r, c);
403 }
404 355
405 // Unary MArray2 ops. 356 // Unary MArray2 ops.
406 357
407 template <class T> 358 template <class T>
408 MArray2<T> 359 MArray2<T>
414 365
415 /* 366 /*
416 * Two dimensional diagonal array with math ops. 367 * Two dimensional diagonal array with math ops.
417 */ 368 */
418 369
370 // Element by element MDiagArray by MDiagArray ops.
371
372 template <class T>
373 MDiagArray<T>&
374 MDiagArray<T>::operator += (const MDiagArray<T>& a)
375 {
376 // XXX FIXME XXX
377 return *this;
378 }
379
380 template <class T>
381 MDiagArray<T>&
382 MDiagArray<T>::operator -= (const MDiagArray<T>& a)
383 {
384 // XXX FIXME XXX
385 return *this;
386 }
387
419 // Element by element MDiagArray by scalar ops. 388 // Element by element MDiagArray by scalar ops.
420 389
421 template <class T> 390 #define MARRAY_DAS_OP(OP) \
422 MDiagArray<T> 391 template <class T> \
423 operator * (const MDiagArray<T>& a, const T& s) 392 MDiagArray<T> \
424 { 393 operator OP (const MDiagArray<T>& a, const T& s) \
425 DO_VS_OP (*); 394 { \
426 return MDiagArray<T> (result, a.rows (), a.cols ()); 395 DO_VS_OP (OP); \
427 } 396 return MDiagArray<T> (result, a.rows (), a.cols ()); \
428 397 }
429 template <class T> 398
430 MDiagArray<T> 399 MARRAY_DAS_OP (*)
431 operator / (const MDiagArray<T>& a, const T& s) 400 MARRAY_DAS_OP (/)
432 {
433 DO_VS_OP (/);
434 return MDiagArray<T> (result, a.rows (), a.cols ());
435 }
436 401
437 // Element by element scalar by MDiagArray ops. 402 // Element by element scalar by MDiagArray ops.
438 403
439 template <class T> 404 template <class T>
440 MDiagArray<T> 405 MDiagArray<T>
444 return MDiagArray<T> (result, a.rows (), a.cols ()); 409 return MDiagArray<T> (result, a.rows (), a.cols ());
445 } 410 }
446 411
447 // Element by element MDiagArray by MDiagArray ops. 412 // Element by element MDiagArray by MDiagArray ops.
448 413
449 template <class T> 414 #define MARRAY_DADA_OP(FCN, OP, OP_STR) \
450 MDiagArray<T> 415 template <class T> \
451 operator + (const MDiagArray<T>& a, const MDiagArray<T>& b) 416 MDiagArray<T> \
452 { 417 FCN (const MDiagArray<T>& a, const MDiagArray<T>& b) \
453 int r = a.rows (); 418 { \
454 int c = a.cols (); 419 int r = a.rows (); \
455 if (r != b.rows () || c != b.cols ()) 420 int c = a.cols (); \
456 { 421 if (r != b.rows () || c != b.cols ()) \
457 (*current_liboctave_error_handler) 422 { \
458 ("nonconformant diagonal array addition attempted"); 423 (*current_liboctave_error_handler) \
459 return MDiagArray<T> (); 424 ("nonconformant diagonal array " OP_STR " attempted"); \
460 } 425 return MDiagArray<T> (); \
461 426 } \
462 if (c == 0 || r == 0) 427 if (c == 0 || r == 0) \
463 return MDiagArray<T> (); 428 return MDiagArray<T> (); \
464 429 int l = a.length (); \
465 int l = a.length (); 430 DO_VV_OP (OP); \
466 DO_VV_OP (+); 431 return MDiagArray<T> (result, r, c); \
467 return MDiagArray<T> (result, r, c); 432 }
468 } 433
469 434 MARRAY_DADA_OP (operator +, +, "addition")
470 template <class T> 435 MARRAY_DADA_OP (operator -, -, "subtraction")
471 MDiagArray<T> 436 MARRAY_DADA_OP (product, *, "product")
472 operator - (const MDiagArray<T>& a, const MDiagArray<T>& b)
473 {
474 int r = a.rows ();
475 int c = a.cols ();
476 if (r != b.rows () || c != b.cols ())
477 {
478 (*current_liboctave_error_handler)
479 ("nonconformant diagonal array subtraction attempted");
480 return MDiagArray<T> ();
481 }
482
483 if (c == 0 || r == 0)
484 return MDiagArray<T> ();
485
486 int l = a.length ();
487 DO_VV_OP (-);
488 return MDiagArray<T> (result, r, c);
489 }
490
491 template <class T>
492 MDiagArray<T>
493 product (const MDiagArray<T>& a, const MDiagArray<T>& b)
494 {
495 int r = a.rows ();
496 int c = a.cols ();
497 if (r != b.rows () || c != b.cols ())
498 {
499 (*current_liboctave_error_handler)
500 ("nonconformant diagonal array product attempted");
501 return MDiagArray<T> ();
502 }
503
504 if (c == 0 || r == 0)
505 return MDiagArray<T> ();
506
507 int l = a.length ();
508 DO_VV_OP (*);
509 return MDiagArray<T> (result, r, c);
510 }
511 437
512 // Unary MDiagArray ops. 438 // Unary MDiagArray ops.
513 439
514 template <class T> 440 template <class T>
515 MDiagArray<T> 441 MDiagArray<T>
516 operator - (const MDiagArray<T>& a) 442 operator - (const MDiagArray<T>& a)
517 { 443 {
518 NEG_V; 444 NEG_V;
519 return MDiagArray<T> (result, a.rows (), a.cols ()); 445 return MDiagArray<T> (result, a.rows (), a.cols ());
520 } 446 }
521
522 #undef DO_SV_OP
523 #undef DO_VS_OP
524 #undef DO_VV_OP
525 #undef NEG_V
526
527 #if 0
528 #ifdef OCTAVE
529 typedefMArray<double> octave_mad_template_type;
530 typedefMArray2<double> octave_ma2d_template_type;
531 typedefMDiagArray<double> octave_mdad_template_type;
532
533 #include <Complex.h>
534 typedefMArray<Complex> octave_mac_template_type;
535 typedefMArray2<Complex> octave_ma2c_template_type;
536 typedefMDiagArray<Complex> octave_mdac_template_type;
537 #endif
538 #endif
539 447
540 /* 448 /*
541 ;;; Local Variables: *** 449 ;;; Local Variables: ***
542 ;;; mode: C++ *** 450 ;;; mode: C++ ***
543 ;;; page-delimiter: "^/\\*" *** 451 ;;; page-delimiter: "^/\\*" ***