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