LORENE
mtbl_arithm.C
1/*
2 * Arithmetical operations for class Mtbl
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
29char mtbl_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Mtbl/mtbl_arithm.C,v 1.2 2014/10/13 08:53:08 j_novak Exp $" ;
30
31/*
32 * $Id: mtbl_arithm.C,v 1.2 2014/10/13 08:53:08 j_novak Exp $
33 * $Log: mtbl_arithm.C,v $
34 * Revision 1.2 2014/10/13 08:53:08 j_novak
35 * Lorene classes and functions now belong to the namespace Lorene.
36 *
37 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
38 * LORENE
39 *
40 * Revision 2.8 2000/09/27 14:21:12 eric
41 * Multiplication par un double : on met le resultat a ETATZERO si
42 * x == 0
43 *
44 * Revision 2.7 2000/08/16 10:30:14 eric
45 * Suppression du membre dzpuis.
46 *
47 * Revision 2.6 1999/10/26 08:08:25 eric
48 * Ajout de protection dzpuis dans +=, -=
49 *
50 * Revision 2.5 1999/10/18 15:07:22 eric
51 * La fonction membre annule() est rebaptisee annule_hard().
52 *
53 * Revision 2.4 1999/10/15 13:58:04 eric
54 * L'arithmetique liee aux Coord's se trouve desormais dans le fichier
55 * arithm_coord.C.
56 *
57 * Revision 2.3 1999/10/01 10:09:01 eric
58 * Changement prototypes.
59 * Correction erreurs lorsque etat=ETATZERO
60 *
61 * Revision 2.2 1999/04/26 17:12:26 phil
62 * ajout de Coord * Mtbl
63 *
64 * Revision 2.1 1999/02/22 15:50:22 hyc
65 * *** empty log message ***
66 *
67 * Revision 2.0 1999/02/15 10:42:45 hyc
68 * *** empty log message ***
69 *
70 * Revision 2.1 1999/02/15 09:59:50 hyc
71 * *** empty log message ***
72 *
73 * Revision 2.0 1999/01/15 09:10:39 hyc
74 * *** empty log message ***
75 *
76 *
77 * $Header: /cvsroot/Lorene/C++/Source/Mtbl/mtbl_arithm.C,v 1.2 2014/10/13 08:53:08 j_novak Exp $
78 *
79 */
80
81// Headers Lorene
82#include "mtbl.h"
83#include "coord.h"
84
85 //********************//
86 // OPERATEURS UNAIRES //
87 //********************//
88
89// + Mtbl
90// ------
91namespace Lorene {
92Mtbl operator+(const Mtbl& t1) // + Mtbl
93{
94 // Protection
95 assert(t1.get_etat() != ETATNONDEF) ;
96
97 return t1 ;
98}
99
100// - Mtbl
101// ------
102Mtbl operator-(const Mtbl& t1) // - Mtbl
103{
104
105 // Protection
106 assert(t1.get_etat() != ETATNONDEF) ;
107
108 // Cas particulier
109 if (t1.get_etat() == ETATZERO) {
110 return t1 ;
111 }
112
113 // Cas general
114 assert(t1.get_etat() == ETATQCQ) ; // sinon...
115 Mtbl r(t1) ; // Mtbl resultat
116
117 for (int i=0 ; i<r.get_nzone() ; i++) {
118 *(r.t)[i] = -(*(t1.t)[i]) ;
119 }
120 return r ;
121}
122
123 //**********//
124 // ADDITION //
125 //**********//
126
127// Mtbl + Mtbl
128// -----------
129Mtbl operator+(const Mtbl& t1, const Mtbl& t2) // Mtbl + Mtbl
130{
131 // Protection
132 assert(t1.get_etat() != ETATNONDEF) ;
133 assert(t2.get_etat() != ETATNONDEF) ;
134 assert(t1.get_mg() == t2.get_mg()) ;
135
136 // Cas particulier
137 if (t1.get_etat() == ETATZERO) {
138 return t2 ;
139 }
140 if (t2.get_etat() == ETATZERO) {
141 return t1 ;
142 }
143 assert(t1.get_etat() == ETATQCQ) ; // sinon...
144 assert(t2.get_etat() == ETATQCQ) ; // sinon...
145
146 // Cas general
147 int nz = t1.get_nzone() ;
148
149 Mtbl r(t1) ; // Mtbl resultat
150
151 for (int i=0 ; i<nz ; i++) {
152 *(r.t)[i] += *(t2.t)[i] ;
153 }
154
155 return r ;
156}
157
158// Mtbl + double
159// -------------
160Mtbl operator+(const Mtbl& t1, double x) // Mtbl + double
161{
162 // Protection
163 assert(t1.get_etat() != ETATNONDEF) ;
164
165 // Cas particulier
166 if (x == double(0)) {
167 return t1 ;
168 }
169
170 int nz = t1.get_nzone() ;
171
172
173 Mtbl r(t1) ; // Mtbl resultat
174
175 if (r.get_etat() == ETATZERO) {
176 r.set_etat_qcq() ;
177 for (int i=0 ; i<nz ; i++) {
178 r.t[i]->set_etat_zero() ;
179 *(r.t)[i] += x ;
180 }
181 }
182 else{
183 assert(r.get_etat() == ETATQCQ) ;
184
185 for (int i=0 ; i<nz ; i++) {
186 *(r.t)[i] += x ;
187 }
188 }
189
190 return r ;
191}
192
193// double + Mtbl
194// -------------
195Mtbl operator+(double x, const Mtbl& t1) // double + Mtbl
196{
197 return t1 + x ;
198}
199
200// Mtbl + int
201// ----------
202Mtbl operator+(const Mtbl& t1, int m) // Mtbl + int
203{
204 return t1 + double(m) ;
205}
206
207// int + Mtbl
208// ----------
209Mtbl operator+(int m, const Mtbl& t1) // int + Mtbl
210{
211 return t1 + double(m) ;
212}
213
214
215 //**************//
216 // SOUSTRACTION //
217 //**************//
218
219// Mtbl - Mtbl
220// -----------
221Mtbl operator-(const Mtbl& t1, const Mtbl& t2) // Mtbl - Mtbl
222{
223 // Protection
224 assert(t1.get_etat() != ETATNONDEF) ;
225 assert(t2.get_etat() != ETATNONDEF) ;
226 assert(t1.get_mg() == t2.get_mg()) ;
227
228 // Cas particulier
229 if (t1.get_etat() == ETATZERO) {
230 return - t2 ;
231 }
232 if (t2.get_etat() == ETATZERO) {
233 return t1 ;
234 }
235 assert(t1.get_etat() == ETATQCQ) ; // sinon...
236 assert(t2.get_etat() == ETATQCQ) ; // sinon...
237
238 // Cas general
239 int nz = t1.get_nzone() ;
240
241 Mtbl r(t1) ; // Mtbl resultat
242
243 for (int i=0 ; i<nz ; i++) {
244 *(r.t)[i] -= *(t2.t)[i] ;
245 }
246
247 return r ;
248}
249
250// Mtbl - double
251// -------------
252Mtbl operator-(const Mtbl& t1, double x) // Mtbl - double
253{
254
255 // Protection
256 assert(t1.get_etat() != ETATNONDEF) ;
257
258 // Cas particulier
259 if (x == double(0)) {
260 return t1 ;
261 }
262
263 // Cas general
264 int nz = t1.get_nzone() ;
265
266 Mtbl r(t1) ; // Mtbl resultat
267
268 if (r.get_etat() == ETATZERO) {
269 r.set_etat_qcq() ;
270 for (int i=0 ; i<nz ; i++) {
271 r.t[i]->set_etat_zero() ;
272 *(r.t)[i] -= x ;
273 }
274 }
275 else{
276 assert(r.get_etat() == ETATQCQ) ;
277
278 for (int i=0 ; i<nz ; i++) {
279 *(r.t)[i] -= x ;
280 }
281 }
282
283 return r ;
284}
285
286// double - Mtbl
287// -------------
288Mtbl operator-(double x, const Mtbl& t1) // double - Mtbl
289{
290 return - (t1 -x) ;
291}
292
293// Mtbl - int
294// ----------
295Mtbl operator-(const Mtbl& t1, int m) // Mtbl - int
296{
297 return t1 - double(m) ;
298}
299
300// int - Mtbl
301// ----------
302Mtbl operator-(int m, const Mtbl& t1) // int - Mtbl
303{
304 return double(m) - t1 ;
305}
306
307 //****************//
308 // MULTIPLICATION //
309 //****************//
310
311// Mtbl * Mtbl
312// -----------
313Mtbl operator*(const Mtbl& t1, const Mtbl& t2) // Mtbl * Mtbl
314{
315 // Protection
316 assert(t1.get_etat() != ETATNONDEF) ;
317 assert(t2.get_etat() != ETATNONDEF) ;
318 assert(t1.get_mg() == t2.get_mg()) ;
319
320 // Cas particulier
321 if (t1.get_etat() == ETATZERO) {
322 return t1 ;
323 }
324 if (t2.get_etat() == ETATZERO) {
325 return t2 ;
326 }
327
328 // Cas general
329 assert(t1.get_etat() == ETATQCQ) ; // sinon...
330 assert(t2.get_etat() == ETATQCQ) ; // sinon...
331
332 Mtbl r(t1) ; // Mtbl resultat
333
334 int nz = t1.get_nzone() ;
335 for (int i=0 ; i<nz ; i++) {
336 *(r.t)[i] *= (*(t2.t)[i]) ;
337 }
338
339 return r ;
340}
341
342// Mtbl * double
343// -------------
344Mtbl operator*(const Mtbl& t1, double x) // Mtbl * double
345{
346
347 // Protection
348 assert(t1.get_etat() != ETATNONDEF) ;
349
350 // Cas particulier
351 if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
352 return t1 ;
353 }
354
355 // Cas general
356 assert(t1.get_etat() == ETATQCQ) ; // sinon...
357
358 Mtbl r(t1) ; // Mtbl resultat
359
360 if ( x == double(0) ) {
361 r.set_etat_zero() ;
362 }
363 else {
364 int nz = t1.get_nzone() ;
365 for (int i=0 ; i<nz ; i++) {
366 *(r.t)[i] *= x ;
367 }
368 }
369
370 return r ;
371}
372
373// double * Mtbl
374// -------------
375Mtbl operator*(double x, const Mtbl& t1) // double * Mtbl
376{
377 return t1 * x ;
378}
379
380// Mtbl * int
381// ----------
382Mtbl operator*(const Mtbl& t1, int m) // Mtbl * int
383{
384 return t1 * double(m) ;
385}
386
387// int * Mtbl
388// ----------
389Mtbl operator*(int m, const Mtbl& t1) // int * Mtbl
390{
391 return t1 * double(m) ;
392}
393
394 //**********//
395 // DIVISION //
396 //**********//
397
398// Mtbl / Mtbl
399// -----------
400Mtbl operator/(const Mtbl& t1, const Mtbl& t2) // Mtbl / Mtbl
401{
402
403 // Protection
404 assert(t1.get_etat() != ETATNONDEF) ;
405 assert(t2.get_etat() != ETATNONDEF) ;
406 assert(t1.get_mg() == t2.get_mg()) ;
407
408 // Cas particuliers
409 if (t2.get_etat() == ETATZERO) {
410 cout << "Mtbl division by 0 !" << endl ;
411 abort() ;
412 }
413 if (t1.get_etat() == ETATZERO) {
414 return t1 ;
415 }
416
417 // Cas general
418 assert(t1.get_etat() == ETATQCQ) ; // sinon...
419 assert(t2.get_etat() == ETATQCQ) ; // sinon...
420
421 Mtbl r(t1) ; // Mtbl resultat
422
423 int nz = t1.get_nzone() ;
424 for (int i=0 ; i<nz ; i++) {
425 *(r.t)[i] /= (*(t2.t)[i]) ;
426 }
427
428 return r ;
429}
430
431// Mtbl / double
432// -------------
433Mtbl operator/(const Mtbl& t1, double x) // Mtbl / double
434{
435
436 // Protection
437 assert(t1.get_etat() != ETATNONDEF) ;
438 if ( x == double(0) ) {
439 cout << "Mtbl division by 0 !" << endl ;
440 abort() ;
441 }
442
443 // Cas particulier
444 if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
445 return t1 ;
446 }
447
448 // Cas general
449 assert(t1.get_etat() == ETATQCQ) ; // sinon...
450
451 Mtbl r(t1) ; // Mtbl resultat
452 int nz = t1.get_nzone() ;
453 for (int i=0 ; i<nz ; i++) {
454 *(r.t)[i] /= x ;
455 }
456
457 return r ;
458}
459
460// Mtbl / int
461// ----------
462Mtbl operator/(const Mtbl& t1, int n) // Mtbl / int
463{
464 return t1/double(n) ;
465}
466
467// double / Mtbl
468// -------------
469Mtbl operator/(double x, const Mtbl& t1) // double / Mtbl
470{
471
472 // Protection
473 assert(t1.get_etat() != ETATNONDEF) ;
474
475 // Cas particuliers
476 if (t1.get_etat() == ETATZERO) {
477 cout << "Division by 0 in double / Mtbl !" << endl ;
478 abort() ;
479 }
480
481 // Cas general
482 assert(t1.get_etat() == ETATQCQ) ; // sinon...
483
484 Mtbl r( *(t1.get_mg()) ) ; // Mtbl resultat, a priori NONDEF
485
486 if ( x == double(0) ) {
487 r.set_etat_zero() ;
488 }
489 else {
490 r.set_etat_qcq() ;
491 int nz = t1.get_nzone() ;
492 for (int i=0 ; i<nz ; i++) {
493 *(r.t)[i] = x / (*(t1.t)[i]) ;
494 }
495 }
496
497 // Termine
498 return r ;
499}
500
501// int / Mtbl
502// ----------
503Mtbl operator/(int m, const Mtbl& t1) // int / Mtbl
504{
505 return double(m)/t1 ;
506}
507
508
509 //*******************//
510 // operateurs +=,... //
511 //*******************//
512
513void Mtbl::operator+=(const Mtbl & mi) {
514
515 // Protection
516 assert(mg == mi.get_mg()) ; // meme grille
517 assert(etat != ETATNONDEF) ; // etat defini
518 assert(mi.get_etat() != ETATNONDEF) ; // etat defini
519
520 // Cas particulier
521 if (mi.get_etat() == ETATZERO) {
522 return ;
523 }
524
525 // Cas general
526
527 if (etat == ETATZERO) {
528 annule_hard() ;
529 }
530 for (int i=0 ; i<nzone ; i++) {
531 *(t[i]) += *(mi.t[i]) ;
532 }
533}
534
535void Mtbl::operator-=(const Mtbl & mi) {
536
537 // Protection
538 assert(mg == mi.get_mg()) ; // meme grille
539 assert(etat != ETATNONDEF) ; // etat defini
540 assert(mi.get_etat() != ETATNONDEF) ; // etat defini
541
542 // Cas particulier
543 if (mi.get_etat() == ETATZERO) {
544 return ;
545 }
546
547 // Cas general
548
549 if (etat == ETATZERO) {
550 annule_hard() ;
551 }
552 for (int i=0 ; i<nzone ; i++) {
553 *(t[i]) -= *(mi.t[i]) ;
554 }
555}
556
557void Mtbl::operator*=(const Mtbl & mi) {
558
559 // Protection
560 assert(mg == mi.get_mg()) ; // meme grille
561 assert(etat != ETATNONDEF) ; // etat defini
562 assert(mi.get_etat() != ETATNONDEF) ; // etat defini
563
564 // Cas particuliers
565 if (etat == ETATZERO) {
566 return ;
567 }
568 if (mi.get_etat() == ETATZERO) {
569 set_etat_zero() ;
570 return ;
571 }
572
573 // Cas general
574 for (int i=0 ; i<nzone ; i++) {
575 *(t[i]) *= *(mi.t[i]) ;
576 }
577
578}
579}
Multi-domain array.
Definition mtbl.h:118
const Mg3d * get_mg() const
Gives the Mg3d on which the Mtbl is defined.
Definition mtbl.h:274
int get_etat() const
Gives the logical state.
Definition mtbl.h:277
const Mg3d * mg
Pointer on the multi-grid Mgd3 on which this is defined.
Definition mtbl.h:124
void operator+=(const Mtbl &)
+= Mtbl
Tbl ** t
Array (size nzone ) of pointers on the Tbl 's.
Definition mtbl.h:132
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition mtbl.C:299
int nzone
Number of domains (zones)
Definition mtbl.h:126
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition mtbl.C:287
int get_nzone() const
Gives the number of zones (domains)
Definition mtbl.h:280
void operator*=(const Mtbl &)
*= Mtbl
int etat
Logical state (ETATNONDEF , ETATQCQ or ETATZERO ).
Definition mtbl.h:128
void annule_hard()
Sets the Mtbl to zero in a hard way.
Definition mtbl.C:312
void operator-=(const Mtbl &)
-= Mtbl
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition tbl.C:347
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:108
Cmp operator/(const Cmp &, const Cmp &)
Cmp / Cmp.
Definition cmp_arithm.C:457
Cmp operator+(const Cmp &)
Definition cmp_arithm.C:104
Lorene prototypes.
Definition app_hor.h:64