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