LORENE
tbl_arithm.C
1/*
2 * Arithmetical operations for class Tbl
3 *
4 */
5
6/*
7 * Copyright (c) 1999-2000 Jean-Alain Marck
8 * Copyright (c) 1999-2001 Eric Gourgoulhon
9 *
10 * This file is part of LORENE.
11 *
12 * LORENE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * LORENE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with LORENE; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28
29
30
31/*
32 * $Id: tbl_arithm.C,v 1.5 2016/12/05 16:18:16 j_novak Exp $
33 * $Log: tbl_arithm.C,v $
34 * Revision 1.5 2016/12/05 16:18:16 j_novak
35 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
36 *
37 * Revision 1.4 2014/10/13 08:53:41 j_novak
38 * Lorene classes and functions now belong to the namespace Lorene.
39 *
40 * Revision 1.3 2011/06/16 10:48:28 j_novak
41 * Minor modif.
42 *
43 * Revision 1.2 2002/10/16 14:37:14 j_novak
44 * Reorganization of #include instructions of standard C++, in order to
45 * use experimental version 3 of gcc.
46 *
47 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
48 * LORENE
49 *
50 * Revision 2.4 2000/09/27 14:20:39 eric
51 * Remplacement du text x == 0. par x == double(0)
52 * dans la multiplication par un double.
53 *
54 * Revision 2.3 1999/11/15 16:36:55 eric
55 * Tbl::dim est desormais un Dim_tbl et non plus un Dim_tbl*.
56 *
57 * Revision 2.2 1999/10/01 10:09:34 eric
58 * 0 -> double(0)
59 *
60 * Revision 2.1 1999/09/24 14:23:55 eric
61 * Changement de prototypes.
62 *
63 * Revision 2.0 1999/02/15 10:42:45 hyc
64 * *** empty log message ***
65 *
66 * $Header: /cvsroot/Lorene/C++/Source/Tbl/tbl_arithm.C,v 1.5 2016/12/05 16:18:16 j_novak Exp $
67 *
68 */
69
70// headers Lorene
71#include "tbl.h"
72
73 //********************//
74 // OPERATEURS UNAIRES //
75 //********************//
76
77// + Tbl
78// -----
79namespace Lorene {
80Tbl operator+(const Tbl& t1)
81{
82 // Protection
83 assert(t1.get_etat() != ETATNONDEF) ;
84
85 return t1 ;
86}
87
88// - Tbl
89// -----
90Tbl operator-(const Tbl& t1)
91{
92 // Protection
93 assert(t1.get_etat() != ETATNONDEF) ;
94
95 // Cas particulier
96 if (t1.get_etat() == ETATZERO) {
97 return t1 ;
98 }
99
100 // Cas general
101 Tbl r(t1.dim) ; // Tbl resultat
102 r.set_etat_qcq() ;
103 for (int i=0 ; i<r.get_taille() ; i++) {
104 (r.t)[i] = - (t1.t)[i] ;
105 }
106 return r ;
107}
108
109 //**********//
110 // ADDITION //
111 //**********//
112
113// Tbl + Tbl
114// ---------
115Tbl operator+(const Tbl& t1, const Tbl& t2)
116{
117
118 // Protection
119 assert(t1.get_etat() != ETATNONDEF) ;
120 assert(t2.get_etat() != ETATNONDEF) ;
121 assert(t1.get_ndim() == t2.get_ndim()) ;
122 for (int i=0 ; i<t1.get_ndim() ; i++) {
123 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
124 }
125
126 // Traitement des cas particuliers
127 if (t1.get_etat() == ETATZERO) {
128 return t2 ;
129 }
130 if (t2.get_etat() == ETATZERO) {
131 return t1 ;
132 }
133
134 // Cas general
135 assert(t1.get_etat() == ETATQCQ) ; // sinon...
136 assert(t2.get_etat() == ETATQCQ) ; // sinon...
137
138 Tbl r(t1) ; // Tbl resultat
139 for (int i=0 ; i<r.get_taille() ; i++) {
140 (r.t)[i] += (t2.t)[i] ;
141 }
142
143 // Termine
144 return r ;
145}
146
147// Tbl + double
148// ------------
149Tbl operator+(const Tbl& t1, double x)
150{
151 // Protection
152 assert(t1.get_etat() != ETATNONDEF) ;
153
154 // Cas particulier
155 if ( x == double(0) ) {
156 return t1 ;
157 }
158
159 // Cas general
160 Tbl r(t1) ; // Tbl resultat
161 r.set_etat_qcq() ;
162 for (int i=0 ; i<r.get_taille() ; i++) {
163 (r.t)[i] += x ;
164 }
165 return r ;
166}
167
168// double + Tbl
169// ------------
170Tbl operator+(double x, const Tbl& t1)
171{
172 return t1 + x ;
173}
174
175// Tbl + int
176// ---------
177Tbl operator+(const Tbl& t1, int n)
178{
179 return t1 + double(n) ;
180}
181
182// int + Tbl
183// ---------
184Tbl operator+(int n, const Tbl& t1)
185{
186 return t1 + double(n) ;
187}
188
189
190 //**************//
191 // SOUSTRACTION //
192 //**************//
193
194// Tbl - Tbl
195// ---------
196Tbl operator-(const Tbl& t1, const Tbl& t2)
197{
198
199 // Protection
200 assert(t1.get_etat() != ETATNONDEF) ;
201 assert(t2.get_etat() != ETATNONDEF) ;
202 assert(t1.get_ndim() == t2.get_ndim()) ;
203 for (int i=0 ; i<t1.get_ndim() ; i++) {
204 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
205 }
206
207 // Traitement des cas particuliers
208 if (t1.get_etat() == ETATZERO) {
209 return -t2 ;
210 }
211 if (t2.get_etat() == ETATZERO) {
212 return t1 ;
213 }
214
215 // Cas general
216 assert(t1.get_etat() == ETATQCQ) ; // sinon...
217 assert(t2.get_etat() == ETATQCQ) ; // sinon...
218
219 Tbl r(t1) ; // Tbl resultat
220 for (int i=0 ; i<r.get_taille() ; i++) {
221 (r.t)[i] -= (t2.t)[i] ;
222 }
223
224 // Termine
225 return r ;
226}
227
228
229// Tbl - double
230// ------------
231Tbl operator-(const Tbl& t1, double x)
232{
233 // Protection
234 assert(t1.get_etat() != ETATNONDEF) ;
235
236 // Cas particulier
237 if ( x == double(0) ) {
238 return t1 ;
239 }
240
241 // Cas general
242 Tbl r(t1) ; // Tbl resultat
243 r.set_etat_qcq() ;
244 for (int i=0 ; i<r.get_taille() ; i++) {
245 (r.t)[i] -= x ;
246 }
247 return r ;
248}
249
250// Tbl - int
251// ---------
252Tbl operator-(const Tbl& t1, int n)
253{
254 return t1 - double(n) ;
255}
256
257// double - Tbl
258// ------------
259Tbl operator-(double x, const Tbl& t1)
260{
261 // Protection
262 assert(t1.get_etat() != ETATNONDEF) ;
263
264 // Cas particulier
265 if ( x == double(0) ) {
266 return -t1 ;
267 }
268
269 // Cas general
270 Tbl r(t1) ; // Tbl resultat
271 r.set_etat_qcq() ;
272 for (int i=0 ; i<r.get_taille() ; i++) {
273 (r.t)[i] -= x ;
274 }
275 return -r ;
276}
277
278// int - Tbl
279// ---------
280Tbl operator-(int n, const Tbl& t1)
281{
282 return double(n) - t1 ;
283}
284
285 //****************//
286 // MULTIPLICATION //
287 //****************//
288
289// Tbl * Tbl
290// ---------
291Tbl operator*(const Tbl& t1, const Tbl& t2)
292{
293 // Protection
294 assert(t1.get_etat() != ETATNONDEF) ;
295 assert(t2.get_etat() != ETATNONDEF) ;
296 assert(t1.get_ndim() == t2.get_ndim()) ;
297 for (int i=0 ; i<t1.get_ndim() ; i++) {
298 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
299 }
300
301 // Cas particulier
302 if (t1.get_etat() == ETATZERO) {
303 return t1 ;
304 }
305 if (t2.get_etat() == ETATZERO) {
306 return t2 ;
307 }
308
309 // Cas general
310 assert(t1.get_etat() == ETATQCQ) ; // sinon...
311 assert(t2.get_etat() == ETATQCQ) ; // sinon...
312
313 Tbl r(t1) ;
314 for (int i=0 ; i<r.get_taille() ; i++) {
315 (r.t)[i] *= (t2.t)[i] ;
316 }
317
318 // Termine
319 return r ;
320}
321
322// Tbl * double
323// ------------
324Tbl operator*(const Tbl& t1, double x)
325{
326 // Protection
327 assert(t1.get_etat() != ETATNONDEF) ;
328
329 // Cas particulier
330 if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
331 return t1 ;
332 }
333
334 // Cas general
335 assert(t1.get_etat() == ETATQCQ) ; // sinon...
336
337 Tbl r(t1) ; // Tbl resultat
338
339 if (x == double(0)) {
340 r.set_etat_zero() ;
341 }
342 else {
343 for (int i=0 ; i<r.get_taille() ; i++) {
344 (r.t)[i] *= x ;
345 }
346 }
347
348 // Termine
349 return r ;
350}
351
352// double * Tbl
353// ------------
354Tbl operator*(double x, const Tbl& t1)
355{
356 return t1 * x ;
357}
358
359// Tbl * int
360// ---------
361Tbl operator*(const Tbl& t1, int n)
362{
363 return t1 * double(n) ;
364}
365
366// int * Tbl
367// ---------
368Tbl operator*(int n, const Tbl& t1)
369{
370 return t1 * double(n) ;
371}
372
373 //**********//
374 // DIVISION //
375 //**********//
376
377// Tbl / Tbl
378// ---------
379Tbl operator/(const Tbl& t1, const Tbl& t2)
380{
381 // Protection
382 assert(t1.get_etat() != ETATNONDEF) ;
383 assert(t2.get_etat() != ETATNONDEF) ;
384 assert(t1.get_ndim() == t2.get_ndim()) ;
385 for (int i=0 ; i<t1.get_ndim() ; i++) {
386 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
387 }
388
389 // Cas particuliers
390 if (t2.get_etat() == ETATZERO) {
391 cout << "Division by 0 in Tbl/Tbl !" << endl ;
392 abort() ;
393 }
394 if (t1.get_etat() == ETATZERO) {
395 return t1 ;
396 }
397
398 // Cas general
399 assert(t1.get_etat() == ETATQCQ) ; // sinon...
400 assert(t2.get_etat() == ETATQCQ) ; // sinon...
401
402 Tbl r(t1) ; // Tbl resultat
403 for (int i=0 ; i<r.get_taille() ; i++) {
404 (r.t)[i] /= (t2.t)[i] ;
405 }
406
407 // Termine
408 return r ;
409}
410
411// Tbl / double
412// ------------
413Tbl operator/(const Tbl& t1, double x)
414{
415 // Protection
416 assert(t1.get_etat() != ETATNONDEF) ;
417 if ( x == double(0) ) {
418 cout << "Division by 0 in Tbl/double !" << endl ;
419 abort() ;
420 }
421
422 // Cas particulier
423 if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
424 return t1 ;
425 }
426
427 // Cas general
428 assert(t1.get_etat() == ETATQCQ) ; // sinon...
429
430 Tbl r(t1) ; // Tbl resultat
431 for (int i=0 ; i<r.get_taille() ; i++) {
432 (r.t)[i] /= x ;
433 }
434 return r ;
435}
436
437// Tbl / int
438// ---------
439Tbl operator/(const Tbl& t1, int n)
440{
441 return t1 / double(n) ;
442}
443
444// double / Tbl
445// ------------
446Tbl operator/(double x, const Tbl& t1)
447{
448 // Protection
449 assert(t1.get_etat() != ETATNONDEF) ;
450
451 // Cas particuliers
452 if (t1.get_etat() == ETATZERO) {
453 cout << "Division by 0 in double/Tbl !" << endl ;
454 abort() ;
455 }
456
457 // Cas general
458 assert(t1.get_etat() == ETATQCQ) ; // sinon...
459
460 Tbl r(t1.dim) ; // Tbl resultat, a priori NONDEF
461
462 if ( x == double(0) ) {
463 r.set_etat_zero() ;
464 }
465 else {
466 r.set_etat_qcq() ;
467 for (int i=0 ; i<r.get_taille() ; i++) {
468 (r.t)[i] = x / (t1.t)[i] ;
469 }
470 }
471
472 // Termine
473 return r ;
474}
475
476// int / Tbl
477// ---------
478Tbl operator/(int n, const Tbl& t1)
479{
480 return double(n) / t1 ;
481}
482
483 //*******************//
484 // operateurs +=,... //
485 //*******************//
486
487void Tbl::operator+=(const Tbl & ti) {
488
489 // Protection
490 assert(dim == ti.dim) ;
491 assert(etat != ETATNONDEF) ;
492 assert(ti.get_etat() != ETATNONDEF) ;
493
494 // Cas particulier
495 if (ti.get_etat() == ETATZERO) {
496 return ;
497 }
498
499 // Cas general
500 int n = get_taille() ;
501 switch(etat) {
502 case ETATZERO:
503 set_etat_qcq() ;
504 for (int i=0 ; i<n ; i++) {
505 t[i] = ti.t[i] ;
506 }
507 break ;
508
509 case ETATQCQ:
510 for (int i=0 ; i<n ; i++) {
511 t[i] += ti.t[i] ;
512 }
513 break ;
514
515 default:
516 cout << "etat inconnu " << __FILE__ << endl ;
517 abort() ;
518 break ;
519 }
520
521 // Termine
522}
523
524void Tbl::operator+=(double x) {
525
526 // Protection
527 assert(etat != ETATNONDEF) ;
528
529 // Cas particulier
530 if ( x == double(0) ) {
531 return ;
532 }
533
534 // Cas general
535 int n = get_taille() ;
536 switch(etat) {
537 case ETATZERO:
538 set_etat_qcq() ;
539 for (int i=0 ; i<n ; i++) {
540 t[i] = x ;
541 }
542 break ;
543
544 case ETATQCQ:
545 for (int i=0 ; i<n ; i++) {
546 t[i] += x ;
547 }
548 break ;
549
550 default:
551 cout << "etat inconnu " << __FILE__ << endl ;
552 abort() ;
553 break ;
554 }
555
556 // Termine
557}
558
559void Tbl::operator-=(const Tbl & ti) {
560
561 // Protection
562 assert(dim == ti.dim) ;
563 assert(etat != ETATNONDEF) ;
564 assert(ti.get_etat() != ETATNONDEF) ;
565
566 // Cas particulier
567 if (ti.get_etat() == ETATZERO) {
568 return ;
569 }
570
571 // Cas general
572 int n = get_taille() ;
573 switch(etat) {
574 case ETATZERO:
575 set_etat_qcq() ;
576 for (int i=0 ; i<n ; i++) {
577 t[i] = - ti.t[i] ;
578 }
579 break ;
580
581 case ETATQCQ:
582 for (int i=0 ; i<n ; i++) {
583 t[i] -= ti.t[i] ;
584 }
585 break ;
586
587 default:
588 cout << "etat inconnu " << __FILE__ << endl ;
589 abort() ;
590 break ;
591 }
592
593 // Termine
594}
595
596void Tbl::operator-=(double x) {
597
598 // Protection
599 assert(etat != ETATNONDEF) ;
600
601 // Cas particulier
602 if ( x == double(0) ) {
603 return ;
604 }
605
606 // Cas general
607 int n = get_taille() ;
608 switch(etat) {
609 case ETATZERO:
610 set_etat_qcq() ;
611 for (int i=0 ; i<n ; i++) {
612 t[i] = - x ;
613 }
614 break ;
615
616 case ETATQCQ:
617 for (int i=0 ; i<n ; i++) {
618 t[i] -= x ;
619 }
620 break ;
621
622 default:
623 cout << "etat inconnu " << __FILE__ << endl ;
624 abort() ;
625 break ;
626 }
627
628 // Termine
629}
630
631void Tbl::operator*=(const Tbl & ti) {
632
633 // Protection
634 assert(dim == ti.dim) ;
635 assert(etat != ETATNONDEF) ;
636 assert(ti.get_etat() != ETATNONDEF) ;
637
638 // Cas particulier
639 if (etat == ETATZERO) {
640 return ;
641 }
642 if (ti.get_etat() == ETATZERO) {
643 set_etat_zero() ;
644 return ;
645 }
646
647 // Cas general
648 assert(etat == ETATQCQ) ;
649 int n = get_taille() ;
650 for (int i=0 ; i<n ; i++) {
651 t[i] *= ti.t[i] ;
652 }
653
654 // Termine
655}
656
657void Tbl::operator*=(double x) {
658
659 // Protection
660 assert(etat != ETATNONDEF) ;
661
662 // Cas particulier
663 if ( x == double(0) ) {
664 set_etat_zero() ;
665 return ;
666 }
667 if (etat == ETATZERO) {
668 return ;
669 }
670
671 // Cas general
672 int n = get_taille() ;
673 assert(etat == ETATQCQ) ;
674 for (int i=0 ; i<n ; i++) {
675 t[i] *= x ;
676 }
677
678 // Termine
679}
680
681void Tbl::operator/=(const Tbl & ti) {
682
683 // Protection
684 assert(dim == ti.dim) ;
685 assert(etat != ETATNONDEF) ;
686 assert(ti.get_etat() != ETATNONDEF) ;
687
688 // Cas particulier
689 if (ti.get_etat() == ETATZERO) {
690 cout << "Division by 0 in Tbl::operator/=(const Tbl &) !" << endl ;
691 abort() ;
692 }
693 if (etat == ETATZERO) {
694 return ;
695 }
696
697 // Cas general
698 assert(etat == ETATQCQ) ;
699 assert(ti.get_etat() == ETATQCQ) ;
700 int n = get_taille() ;
701 for (int i=0 ; i<n ; i++) {
702 t[i] /= ti.t[i] ;
703 }
704
705 // Termine
706}
707
708void Tbl::operator/=(double x) {
709
710 // Protection
711 assert(etat != ETATNONDEF) ;
712
713 // Cas particulier
714 if ( x == double(0) ) {
715 cout << "Division by 0 in Tbl::operator/=(double ) !" << endl ;
716 abort() ;
717 }
718 if (etat == ETATZERO) {
719 return ;
720 }
721
722 // Cas general
723 assert(etat == ETATQCQ) ;
724 int n = get_taille() ;
725 for (int i=0 ; i<n ; i++) {
726 t[i] /= x ;
727 }
728
729 // Termine
730}
731
732}
Basic array class.
Definition tbl.h:161
Dim_tbl dim
Number of dimensions, size,...
Definition tbl.h:172
int get_ndim() const
Gives the number of dimensions (ie dim.ndim).
Definition tbl.h:400
void operator*=(const Tbl &)
Multiplication of this by a Tbl.
Definition tbl_arithm.C:631
Tbl(int size0)
1D constructor
Definition tbl.C:147
int get_etat() const
Gives the logical state.
Definition tbl.h:394
int etat
logical state (ETATNONDEF, ETATQCQ or ETATZERO).
Definition tbl.h:169
void operator/=(const Tbl &)
Division of this by a Tbl.
Definition tbl_arithm.C:681
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition tbl.C:350
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition tbl.C:364
void operator-=(const Tbl &)
Subtraction of a Tbl to this.
Definition tbl_arithm.C:559
int get_taille() const
Gives the total size (ie dim.taille).
Definition tbl.h:397
double * t
The array of double.
Definition tbl.h:173
void operator+=(const Tbl &)
Addition of a Tbl to this.
Definition tbl_arithm.C:487
int get_dim(int i) const
Gives the i-th dimension (ie dim.dim[i]).
Definition tbl.h:403
Base_val operator*(const Base_val &, const Base_val &)
This operator is used when calling multiplication or division of Valeur .
Cmp operator-(const Cmp &)
- Cmp
Definition cmp_arithm.C:111
Cmp operator/(const Cmp &, const Cmp &)
Cmp / Cmp.
Definition cmp_arithm.C:460
Cmp operator+(const Cmp &)
Definition cmp_arithm.C:107
Lorene prototypes.
Definition app_hor.h:67
Coord x
x coordinate centered on the grid
Definition map.h:738
Coord r
r coordinate centered on the grid
Definition map.h:730