Debian Codegraph @

C++/Source/Tbl/tbl_arithm.C

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