LORENE
itbl_arithm.C
1/*
2 * Arithmetical operations for class Itbl
3 *
4 */
5
6/*
7 * Copyright (c) 1999-2001 Philippe Grandclement
8 *
9 * This file is part of LORENE.
10 *
11 * LORENE is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * LORENE is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with LORENE; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28
29
30/*
31 * $Id: itbl_arithm.C,v 1.4 2016/12/05 16:17:56 j_novak Exp $
32 * $Log: itbl_arithm.C,v $
33 * Revision 1.4 2016/12/05 16:17:56 j_novak
34 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
35 *
36 * Revision 1.3 2014/10/13 08:53:01 j_novak
37 * Lorene classes and functions now belong to the namespace Lorene.
38 *
39 * Revision 1.2 2002/10/16 14:36:38 j_novak
40 * Reorganization of #include instructions of standard C++, in order to
41 * use experimental version 3 of gcc.
42 *
43 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
44 * LORENE
45 *
46 * Revision 2.0 1999/11/17 16:04:54 phil
47 * *** empty log message ***
48 *
49 *
50 * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_arithm.C,v 1.4 2016/12/05 16:17:56 j_novak Exp $
51 *
52 */
53
54// headers Lorene
55#include "itbl.h"
56
57 //********************//
58 // OPERATEURS UNAIRES //
59 //********************//
60
61// + Itbl
62// -----
63namespace Lorene {
65{
66 // Protection
67 assert(t1.get_etat() != ETATNONDEF) ;
68
69 return t1 ;
70}
71
72// - Itbl
73// -----
75{
76 // Protection
77 assert(t1.get_etat() != ETATNONDEF) ;
78
79 // Cas particulier
80 if (t1.get_etat() == ETATZERO) {
81 return t1 ;
82 }
83
84 // Cas general
85 Itbl r(t1.dim) ; // Itbl resultat
86 r.set_etat_qcq() ;
87 for (int i=0 ; i<r.get_taille() ; i++) {
88 (r.t)[i] = - (t1.t)[i] ;
89 }
90 return r ;
91}
92
93 //**********//
94 // ADDITION //
95 //**********//
96
97// Itbl + Itbl
98// ---------
99Itbl operator+(const Itbl& t1, const Itbl& t2)
100{
101
102 // Protection
103 assert(t1.get_etat() != ETATNONDEF) ;
104 assert(t2.get_etat() != ETATNONDEF) ;
105 assert(t1.get_ndim() == t2.get_ndim()) ;
106 for (int i=0 ; i<t1.get_ndim() ; i++) {
107 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
108 }
109
110 // Traitement des cas particuliers
111 if (t1.get_etat() == ETATZERO) {
112 return t2 ;
113 }
114 if (t2.get_etat() == ETATZERO) {
115 return t1 ;
116 }
117
118 // Cas general
119 assert(t1.get_etat() == ETATQCQ) ; // sinon...
120 assert(t2.get_etat() == ETATQCQ) ; // sinon...
121
122 Itbl r(t1) ; // Itbl resultat
123 for (int i=0 ; i<r.get_taille() ; i++) {
124 (r.t)[i] += (t2.t)[i] ;
125 }
126
127 // Termine
128 return r ;
129}
130
131// Itbl + int
132// ------------
133Itbl operator+(const Itbl& t1, int x)
134{
135 // Protection
136 assert(t1.get_etat() != ETATNONDEF) ;
137
138 // Cas particulier
139 if ( x == 0 ) {
140 return t1 ;
141 }
142
143 // Cas general
144 Itbl r(t1) ; // Itbl resultat
145 r.set_etat_qcq() ;
146 for (int i=0 ; i<r.get_taille() ; i++) {
147 (r.t)[i] += x ;
148 }
149 return r ;
150}
151
152// int + Itbl
153// ------------
154Itbl operator+(int x, const Itbl& t1)
155{
156 return t1 + x ;
157}
158
159
160 //**************//
161 // SOUSTRACTION //
162 //**************//
163
164// Itbl - Itbl
165// ---------
166Itbl operator-(const Itbl& t1, const Itbl& t2)
167{
168
169 // Protection
170 assert(t1.get_etat() != ETATNONDEF) ;
171 assert(t2.get_etat() != ETATNONDEF) ;
172 assert(t1.get_ndim() == t2.get_ndim()) ;
173 for (int i=0 ; i<t1.get_ndim() ; i++) {
174 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
175 }
176
177 // Traitement des cas particuliers
178 if (t1.get_etat() == ETATZERO) {
179 return -t2 ;
180 }
181 if (t2.get_etat() == ETATZERO) {
182 return t1 ;
183 }
184
185 // Cas general
186 assert(t1.get_etat() == ETATQCQ) ; // sinon...
187 assert(t2.get_etat() == ETATQCQ) ; // sinon...
188
189 Itbl r(t1) ; // Itbl resultat
190 for (int i=0 ; i<r.get_taille() ; i++) {
191 (r.t)[i] -= (t2.t)[i] ;
192 }
193
194 // Termine
195 return r ;
196}
197
198
199// Itbl - int
200// ------------
201Itbl operator-(const Itbl& t1, int x)
202{
203 // Protection
204 assert(t1.get_etat() != ETATNONDEF) ;
205
206 // Cas particulier
207 if ( x == 0 ) {
208 return t1 ;
209 }
210
211 // Cas general
212 Itbl r(t1) ; // Itbl resultat
213 r.set_etat_qcq() ;
214 for (int i=0 ; i<r.get_taille() ; i++) {
215 (r.t)[i] -= x ;
216 }
217 return r ;
218}
219
220
221// int - Itbl
222// ------------
223Itbl operator-(int x, const Itbl& t1)
224{
225 // Protection
226 assert(t1.get_etat() != ETATNONDEF) ;
227
228 // Cas particulier
229 if ( x == 0 ) {
230 return -t1 ;
231 }
232
233 // Cas general
234 Itbl r(t1) ; // Itbl resultat
235 r.set_etat_qcq() ;
236 for (int i=0 ; i<r.get_taille() ; i++) {
237 (r.t)[i] -= x ;
238 }
239 return -r ;
240}
241
242 //****************//
243 // MULTIPLICATION //
244 //****************//
245
246// Itbl * Itbl
247// ---------
248Itbl operator*(const Itbl& t1, const Itbl& t2)
249{
250 // Protection
251 assert(t1.get_etat() != ETATNONDEF) ;
252 assert(t2.get_etat() != ETATNONDEF) ;
253 assert(t1.get_ndim() == t2.get_ndim()) ;
254 for (int i=0 ; i<t1.get_ndim() ; i++) {
255 assert( t1.get_dim(i) == t2.get_dim(i) ) ;
256 }
257
258 // Cas particulier
259 if (t1.get_etat() == ETATZERO) {
260 return t1 ;
261 }
262 if (t2.get_etat() == ETATZERO) {
263 return t2 ;
264 }
265
266 // Cas general
267 assert(t1.get_etat() == ETATQCQ) ; // sinon...
268 assert(t2.get_etat() == ETATQCQ) ; // sinon...
269
270 Itbl r(t1) ;
271 for (int i=0 ; i<r.get_taille() ; i++) {
272 (r.t)[i] *= (t2.t)[i] ;
273 }
274
275 // Termine
276 return r ;
277}
278
279// Itbl * int
280// ------------
281Itbl operator*(const Itbl& t1, int x)
282{
283 // Protection
284 assert(t1.get_etat() != ETATNONDEF) ;
285
286 // Cas particulier
287 if ((t1.get_etat() == ETATZERO) || ( x == 1 )) {
288 return t1 ;
289 }
290
291 // Cas general
292 assert(t1.get_etat() == ETATQCQ) ; // sinon...
293
294 Itbl r(t1) ; // Itbl resultat
295
296 if (x == 0) {
297 r.set_etat_zero() ;
298 }
299 else {
300 for (int i=0 ; i<r.get_taille() ; i++) {
301 (r.t)[i] *= x ;
302 }
303 }
304
305 // Termine
306 return r ;
307}
308
309// int * Itbl
310// ------------
311Itbl operator*(int x, const Itbl& t1)
312{
313 return t1 * x ;
314}
315
316
317 //*******************//
318 // operateurs +=,... //
319 //*******************//
320
321void Itbl::operator+=(const Itbl & ti) {
322
323 // Protection
324 assert(dim == ti.dim) ;
325 assert(etat != ETATNONDEF) ;
326 assert(ti.get_etat() != ETATNONDEF) ;
327
328 // Cas particulier
329 if (ti.get_etat() == ETATZERO) {
330 return ;
331 }
332
333 // Cas general
334 int n = get_taille() ;
335 switch(etat) {
336 case ETATZERO:
337 set_etat_qcq() ;
338 for (int i=0 ; i<n ; i++) {
339 t[i] = ti.t[i] ;
340 }
341 break ;
342
343 case ETATQCQ:
344 for (int i=0 ; i<n ; i++) {
345 t[i] += ti.t[i] ;
346 }
347 break ;
348
349 default:
350 cout << "etat inconnu " << __FILE__ << endl ;
351 abort() ;
352 break ;
353 }
354
355 // Termine
356}
357
359
360 // Protection
361 assert(etat != ETATNONDEF) ;
362
363 // Cas particulier
364 if ( x == 0 ) {
365 return ;
366 }
367
368 // Cas general
369 int n = get_taille() ;
370 switch(etat) {
371 case ETATZERO:
372 set_etat_qcq() ;
373 for (int i=0 ; i<n ; i++) {
374 t[i] = x ;
375 }
376 break ;
377
378 case ETATQCQ:
379 for (int i=0 ; i<n ; i++) {
380 t[i] += x ;
381 }
382 break ;
383
384 default:
385 cout << "etat inconnu " << __FILE__ << endl ;
386 abort() ;
387 break ;
388 }
389
390 // Termine
391}
392
393void Itbl::operator-=(const Itbl & ti) {
394
395 // Protection
396 assert(dim == ti.dim) ;
397 assert(etat != ETATNONDEF) ;
398 assert(ti.get_etat() != ETATNONDEF) ;
399
400 // Cas particulier
401 if (ti.get_etat() == ETATZERO) {
402 return ;
403 }
404
405 // Cas general
406 int n = get_taille() ;
407 switch(etat) {
408 case ETATZERO:
409 set_etat_qcq() ;
410 for (int i=0 ; i<n ; i++) {
411 t[i] = - ti.t[i] ;
412 }
413 break ;
414
415 case ETATQCQ:
416 for (int i=0 ; i<n ; i++) {
417 t[i] -= ti.t[i] ;
418 }
419 break ;
420
421 default:
422 cout << "etat inconnu " << __FILE__ << endl ;
423 abort() ;
424 break ;
425 }
426
427 // Termine
428}
429
431
432 // Protection
433 assert(etat != ETATNONDEF) ;
434
435 // Cas particulier
436 if ( x == 0 ) {
437 return ;
438 }
439
440 // Cas general
441 int n = get_taille() ;
442 switch(etat) {
443 case ETATZERO:
444 set_etat_qcq() ;
445 for (int i=0 ; i<n ; i++) {
446 t[i] = - x ;
447 }
448 break ;
449
450 case ETATQCQ:
451 for (int i=0 ; i<n ; i++) {
452 t[i] -= x ;
453 }
454 break ;
455
456 default:
457 cout << "etat inconnu " << __FILE__ << endl ;
458 abort() ;
459 break ;
460 }
461
462 // Termine
463}
464
465void Itbl::operator*=(const Itbl & ti) {
466
467 // Protection
468 assert(dim == ti.dim) ;
469 assert(etat != ETATNONDEF) ;
470 assert(ti.get_etat() != ETATNONDEF) ;
471
472 // Cas particulier
473 if (etat == ETATZERO) {
474 return ;
475 }
476 if (ti.get_etat() == ETATZERO) {
477 set_etat_zero() ;
478 return ;
479 }
480
481 // Cas general
482 assert(etat == ETATQCQ) ;
483 for (int i=0 ; i<get_taille() ; i++) {
484 t[i] *= ti.t[i] ;
485 }
486
487 // Termine
488}
489
491
492 // Protection
493 assert(etat != ETATNONDEF) ;
494
495 // Cas particulier
496 if ( x == 0 ) {
497 set_etat_zero() ;
498 return ;
499 }
500 if (etat == ETATZERO) {
501 return ;
502 }
503
504 // Cas general
505 assert(etat == ETATQCQ) ;
506 for (int i=0 ; i<get_taille() ; i++) {
507 t[i] *= x ;
508 }
509
510 // Termine
511}
512}
Basic integer array class.
Definition itbl.h:122
void operator-=(const Itbl &)
Subtraction of a Itbl to this.
Itbl(int size0)
1D constructor.
Definition itbl.C:100
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition itbl.C:264
void operator*=(const Itbl &)
Multiplication of this by a Itbl.
int etat
logical state (ETATNONDEF , ETATQCQ or ETATZERO ).
Definition itbl.h:128
Dim_tbl dim
Number of dimensions, size,...
Definition itbl.h:131
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition itbl.C:250
int get_dim(int i) const
Gives the i th dimension (ie {tt dim.dim[i] ).
Definition itbl.h:326
int get_etat() const
Gives the logical state.
Definition itbl.h:317
int * t
The array of int 's.
Definition itbl.h:132
void operator+=(const Itbl &)
Addition of a Itbl to this.
int get_taille() const
Gives the total size (ie dim.taille ).
Definition itbl.h:320
int get_ndim() const
Gives the number of dimensions (ie dim.ndim ).
Definition itbl.h:323
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 &)
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