tbl_arithm.C

00001 /*
00002  * Arithmetical operations for class Tbl
00003  *
00004  */
00005 
00006 /*
00007  *   Copyright (c) 1999-2000 Jean-Alain Marck
00008  *   Copyright (c) 1999-2001 Eric Gourgoulhon
00009  *
00010  *   This file is part of LORENE.
00011  *
00012  *   LORENE is free software; you can redistribute it and/or modify
00013  *   it under the terms of the GNU General Public License as published by
00014  *   the Free Software Foundation; either version 2 of the License, or
00015  *   (at your option) any later version.
00016  *
00017  *   LORENE is distributed in the hope that it will be useful,
00018  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *   GNU General Public License for more details.
00021  *
00022  *   You should have received a copy of the GNU General Public License
00023  *   along with LORENE; if not, write to the Free Software
00024  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  */
00027 
00028 
00029 char tbl_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Tbl/tbl_arithm.C,v 1.3 2011/06/16 10:48:28 j_novak Exp $" ;
00030 
00031 /*
00032  * $Id: tbl_arithm.C,v 1.3 2011/06/16 10:48:28 j_novak Exp $
00033  * $Log: tbl_arithm.C,v $
00034  * Revision 1.3  2011/06/16 10:48:28  j_novak
00035  * Minor modif.
00036  *
00037  * Revision 1.2  2002/10/16 14:37:14  j_novak
00038  * Reorganization of #include instructions of standard C++, in order to
00039  * use experimental version 3 of gcc.
00040  *
00041  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00042  * LORENE
00043  *
00044  * Revision 2.4  2000/09/27  14:20:39  eric
00045  * Remplacement du text x == 0. par x == double(0)
00046  *  dans la multiplication par un double.
00047  *
00048  * Revision 2.3  1999/11/15  16:36:55  eric
00049  * Tbl::dim est desormais un Dim_tbl et non plus un Dim_tbl*.
00050  *
00051  * Revision 2.2  1999/10/01  10:09:34  eric
00052  * 0 -> double(0)
00053  *
00054  * Revision 2.1  1999/09/24  14:23:55  eric
00055  * Changement de prototypes.
00056  *
00057  * Revision 2.0  1999/02/15  10:42:45  hyc
00058  * *** empty log message ***
00059  *
00060  * $Header: /cvsroot/Lorene/C++/Source/Tbl/tbl_arithm.C,v 1.3 2011/06/16 10:48:28 j_novak Exp $
00061  *
00062  */
00063 
00064 // headers Lorene
00065 #include "tbl.h"
00066 
00067             //********************//
00068             // OPERATEURS UNAIRES //
00069             //********************//
00070 
00071 // + Tbl
00072 // -----
00073 Tbl operator+(const Tbl& t1)
00074 {
00075     // Protection
00076     assert(t1.get_etat() != ETATNONDEF) ;
00077     
00078     return t1 ;
00079 }
00080 
00081 // - Tbl
00082 // -----
00083 Tbl operator-(const Tbl& t1)
00084 {
00085     // Protection
00086     assert(t1.get_etat() != ETATNONDEF) ;
00087     
00088     // Cas particulier
00089     if (t1.get_etat() == ETATZERO) {
00090     return t1 ;
00091     }
00092     
00093     // Cas general
00094     Tbl r(t1.dim) ;         // Tbl resultat
00095     r.set_etat_qcq() ;
00096     for (int i=0 ; i<r.get_taille() ; i++) {
00097     (r.t)[i] = - (t1.t)[i] ;
00098     }
00099     return r ;
00100 }
00101 
00102             //**********//
00103             // ADDITION //
00104             //**********//
00105 
00106 // Tbl + Tbl
00107 // ---------
00108 Tbl operator+(const Tbl& t1, const Tbl& t2)
00109 {
00110 
00111     // Protection
00112     assert(t1.get_etat() != ETATNONDEF) ;
00113     assert(t2.get_etat() != ETATNONDEF) ;
00114     assert(t1.get_ndim() == t2.get_ndim()) ;
00115     for (int i=0 ; i<t1.get_ndim() ; i++) {
00116     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00117     }
00118 
00119     // Traitement des cas particuliers
00120     if (t1.get_etat() == ETATZERO) {
00121     return t2 ;
00122     }
00123     if (t2.get_etat() == ETATZERO) {
00124     return t1 ;
00125     }
00126 
00127     // Cas general
00128     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00129     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00130 
00131     Tbl r(t1) ;     // Tbl resultat
00132     for (int i=0 ; i<r.get_taille() ; i++) {
00133     (r.t)[i] += (t2.t)[i] ;
00134     }
00135     
00136     // Termine
00137     return r ;
00138 }
00139 
00140 // Tbl + double
00141 // ------------
00142 Tbl operator+(const Tbl& t1, double x)
00143 {
00144     // Protection
00145     assert(t1.get_etat() != ETATNONDEF) ;
00146 
00147     // Cas particulier
00148     if ( x == double(0) ) {
00149     return t1 ;
00150     }
00151     
00152     // Cas general
00153     Tbl r(t1) ;     // Tbl resultat
00154     r.set_etat_qcq() ;
00155     for (int i=0 ; i<r.get_taille() ; i++) {
00156     (r.t)[i] += x ;
00157     }
00158     return r ;
00159 }
00160 
00161 // double + Tbl
00162 // ------------
00163 Tbl operator+(double x, const Tbl& t1)
00164 {
00165     return t1 + x ;
00166 }
00167 
00168 // Tbl + int
00169 // ---------
00170 Tbl operator+(const Tbl& t1, int n)
00171 {
00172     return t1 + double(n) ;
00173 }
00174 
00175 // int + Tbl
00176 // ---------
00177 Tbl operator+(int n, const Tbl& t1)
00178 {
00179     return t1 + double(n) ;
00180 }
00181 
00182 
00183             //**************//
00184             // SOUSTRACTION //
00185             //**************//
00186 
00187 // Tbl - Tbl
00188 // ---------
00189 Tbl operator-(const Tbl& t1, const Tbl& t2)
00190 {
00191 
00192     // Protection
00193     assert(t1.get_etat() != ETATNONDEF) ;
00194     assert(t2.get_etat() != ETATNONDEF) ;
00195     assert(t1.get_ndim() == t2.get_ndim()) ;
00196     for (int i=0 ; i<t1.get_ndim() ; i++) {
00197     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00198     }
00199 
00200     // Traitement des cas particuliers
00201     if (t1.get_etat() == ETATZERO) {
00202     return -t2 ;
00203     }
00204     if (t2.get_etat() == ETATZERO) {
00205     return t1 ;
00206     }
00207 
00208     // Cas general
00209     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00210     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00211 
00212     Tbl r(t1) ;     // Tbl resultat
00213     for (int i=0 ; i<r.get_taille() ; i++) {
00214     (r.t)[i] -= (t2.t)[i] ;
00215     }
00216     
00217     // Termine
00218     return r ;
00219 }
00220 
00221 
00222 // Tbl - double
00223 // ------------
00224 Tbl operator-(const Tbl& t1, double x)
00225 {
00226     // Protection
00227     assert(t1.get_etat() != ETATNONDEF) ;
00228 
00229     // Cas particulier
00230     if ( x == double(0) ) {
00231     return t1 ;
00232     }
00233     
00234     // Cas general
00235     Tbl r(t1) ;     // Tbl resultat
00236     r.set_etat_qcq() ;
00237     for (int i=0 ; i<r.get_taille() ; i++) {
00238     (r.t)[i] -= x ;
00239     }
00240     return r ;
00241 }
00242 
00243 // Tbl - int
00244 // ---------
00245 Tbl operator-(const Tbl& t1, int n)
00246 {
00247     return t1 - double(n) ;
00248 }
00249 
00250 // double - Tbl
00251 // ------------
00252 Tbl operator-(double x, const Tbl& t1)
00253 {
00254     // Protection
00255     assert(t1.get_etat() != ETATNONDEF) ;
00256 
00257     // Cas particulier
00258     if ( x == double(0) ) {
00259     return -t1 ;
00260     }
00261     
00262     // Cas general
00263     Tbl r(t1) ;     // Tbl resultat
00264     r.set_etat_qcq() ;
00265     for (int i=0 ; i<r.get_taille() ; i++) {
00266     (r.t)[i] -= x ;
00267     }
00268     return -r ;
00269 }
00270 
00271 // int - Tbl
00272 // ---------
00273 Tbl operator-(int n, const Tbl& t1)
00274 {
00275     return double(n) - t1 ;
00276 }
00277 
00278             //****************//
00279             // MULTIPLICATION //
00280             //****************//
00281 
00282 // Tbl * Tbl
00283 // ---------
00284 Tbl operator*(const Tbl& t1, const Tbl& t2)
00285 {
00286     // Protection
00287     assert(t1.get_etat() != ETATNONDEF) ;
00288     assert(t2.get_etat() != ETATNONDEF) ;
00289     assert(t1.get_ndim() == t2.get_ndim()) ;
00290     for (int i=0 ; i<t1.get_ndim() ; i++) {
00291     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00292     }
00293 
00294     // Cas particulier
00295     if (t1.get_etat() == ETATZERO) {
00296     return t1 ;
00297     }
00298     if (t2.get_etat() == ETATZERO) {
00299     return t2 ;
00300     }
00301     
00302     // Cas general
00303     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00304     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00305 
00306     Tbl r(t1) ;
00307     for (int i=0 ; i<r.get_taille() ; i++) {
00308     (r.t)[i] *= (t2.t)[i] ;
00309     }
00310     
00311     // Termine
00312     return r ;
00313 }
00314 
00315 // Tbl * double
00316 // ------------
00317 Tbl operator*(const Tbl& t1, double x)
00318 {
00319     // Protection
00320     assert(t1.get_etat() != ETATNONDEF) ;
00321 
00322     // Cas particulier
00323     if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
00324     return t1 ;
00325     }
00326 
00327     // Cas general
00328     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00329 
00330     Tbl r(t1) ;         // Tbl resultat
00331 
00332     if (x == double(0)) {
00333     r.set_etat_zero() ;
00334     }
00335     else {
00336     for (int i=0 ; i<r.get_taille() ; i++) {
00337         (r.t)[i] *= x ;
00338     }
00339     }
00340     
00341     // Termine
00342     return r ;
00343 }
00344 
00345 // double * Tbl
00346 // ------------
00347 Tbl operator*(double x, const Tbl& t1)
00348 {
00349     return t1 * x ;
00350 }
00351 
00352 // Tbl * int
00353 // ---------
00354 Tbl operator*(const Tbl& t1, int n)
00355 {
00356     return t1 * double(n) ;
00357 }
00358 
00359 // int * Tbl
00360 // ---------
00361 Tbl operator*(int n, const Tbl& t1)
00362 {
00363     return t1 * double(n) ;
00364 }
00365 
00366             //**********//
00367             // DIVISION //
00368             //**********//
00369 
00370 // Tbl / Tbl
00371 // ---------
00372 Tbl operator/(const Tbl& t1, const Tbl& t2)
00373 {
00374     // Protection
00375     assert(t1.get_etat() != ETATNONDEF) ;
00376     assert(t2.get_etat() != ETATNONDEF) ;
00377     assert(t1.get_ndim() == t2.get_ndim()) ;
00378     for (int i=0 ; i<t1.get_ndim() ; i++) {
00379     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00380     }
00381 
00382     // Cas particuliers
00383     if (t2.get_etat() == ETATZERO) {
00384     cout << "Division by 0 in Tbl/Tbl !" << endl ;
00385     abort() ; 
00386     }
00387     if (t1.get_etat() == ETATZERO) {
00388     return t1 ;
00389     }
00390     
00391     // Cas general
00392     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00393     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00394 
00395     Tbl r(t1) ;         // Tbl resultat
00396     for (int i=0 ; i<r.get_taille() ; i++) {
00397     (r.t)[i] /= (t2.t)[i] ;
00398     }
00399     
00400     // Termine
00401     return r ;
00402 }
00403 
00404 // Tbl / double
00405 // ------------
00406 Tbl operator/(const Tbl& t1, double x)
00407 {
00408     // Protection
00409     assert(t1.get_etat() != ETATNONDEF) ;
00410     if ( x == double(0) ) {
00411     cout << "Division by 0 in Tbl/double !" << endl ;
00412     abort() ;
00413     }
00414     
00415     // Cas particulier
00416     if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) {
00417     return t1 ;
00418     }
00419     
00420     // Cas general
00421     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00422 
00423     Tbl r(t1) ;         // Tbl resultat
00424     for (int i=0 ; i<r.get_taille() ; i++) {
00425     (r.t)[i] /= x ;
00426     }
00427     return r ;
00428 }
00429 
00430 // Tbl / int
00431 // ---------
00432 Tbl operator/(const Tbl& t1, int n)
00433 {
00434     return t1 / double(n) ;
00435 }
00436 
00437 // double / Tbl
00438 // ------------
00439 Tbl operator/(double x, const Tbl& t1)
00440 {
00441     // Protection
00442     assert(t1.get_etat() != ETATNONDEF) ;
00443 
00444     // Cas particuliers
00445     if (t1.get_etat() == ETATZERO) {
00446     cout << "Division by 0 in double/Tbl !" << endl ;
00447     abort() ; 
00448     }
00449 
00450     // Cas general
00451     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00452 
00453     Tbl r(t1.dim) ;             // Tbl resultat, a priori NONDEF
00454     
00455     if ( x == double(0) ) {
00456     r.set_etat_zero() ;
00457     }
00458     else {
00459         r.set_etat_qcq() ;
00460     for (int i=0 ; i<r.get_taille() ; i++) {
00461         (r.t)[i] = x / (t1.t)[i] ;
00462     }
00463     }
00464     
00465     // Termine
00466     return r ;
00467 }
00468 
00469 // int / Tbl
00470 // ---------
00471 Tbl operator/(int n, const Tbl& t1)
00472 {
00473     return double(n) / t1 ;
00474 }
00475 
00476             //*******************//
00477             // operateurs +=,... //
00478             //*******************//
00479 
00480 void Tbl::operator+=(const Tbl & ti) {
00481 
00482     // Protection
00483     assert(dim == ti.dim) ;
00484     assert(etat != ETATNONDEF) ;
00485     assert(ti.get_etat() != ETATNONDEF) ;
00486     
00487     // Cas particulier
00488     if (ti.get_etat() == ETATZERO) {
00489         return ;
00490     }
00491     
00492     // Cas general
00493     int n = get_taille() ;
00494     switch(etat) {
00495     case ETATZERO:
00496     set_etat_qcq() ;
00497     for (int i=0 ; i<n ; i++) {
00498             t[i] = ti.t[i] ;
00499     }
00500     break ;
00501     
00502     case ETATQCQ:
00503     for (int i=0 ; i<n ; i++) {
00504             t[i] += ti.t[i] ;
00505     }
00506     break ;
00507     
00508     default:
00509     cout << "etat inconnu " << __FILE__ << endl ;
00510     abort() ;
00511     break ;
00512     }
00513     
00514     // Termine
00515 }
00516 
00517 void Tbl::operator+=(double x) {
00518 
00519     // Protection
00520     assert(etat != ETATNONDEF) ;
00521 
00522     // Cas particulier
00523     if ( x == double(0) ) {
00524         return ;
00525     }
00526     
00527     // Cas general
00528     int n = get_taille() ;
00529     switch(etat) {
00530     case ETATZERO:
00531     set_etat_qcq() ;
00532     for (int i=0 ; i<n ; i++) {
00533             t[i] = x ;
00534     }
00535     break ;
00536     
00537     case ETATQCQ:
00538     for (int i=0 ; i<n ; i++) {
00539             t[i] += x ;
00540     }
00541     break ;
00542     
00543     default:
00544     cout << "etat inconnu " << __FILE__ << endl ;
00545     abort() ;
00546     break ;
00547     }
00548     
00549     // Termine
00550 }
00551 
00552 void Tbl::operator-=(const Tbl & ti) {
00553 
00554     // Protection
00555     assert(dim == ti.dim) ;
00556     assert(etat != ETATNONDEF) ;
00557     assert(ti.get_etat() != ETATNONDEF) ;
00558     
00559     // Cas particulier
00560     if (ti.get_etat() == ETATZERO) {
00561         return ;
00562     }
00563     
00564     // Cas general
00565     int n = get_taille() ;
00566     switch(etat) {
00567     case ETATZERO:
00568     set_etat_qcq() ;
00569     for (int i=0 ; i<n ; i++) {
00570             t[i] = - ti.t[i] ;
00571     }
00572     break ;
00573     
00574     case ETATQCQ:
00575     for (int i=0 ; i<n ; i++) {
00576             t[i] -= ti.t[i] ;
00577     }
00578     break ;
00579     
00580     default:
00581     cout << "etat inconnu " << __FILE__ << endl ;
00582     abort() ;
00583     break ;
00584     }
00585     
00586     // Termine
00587 }
00588 
00589 void Tbl::operator-=(double x) {
00590 
00591     // Protection
00592     assert(etat != ETATNONDEF) ;
00593 
00594     // Cas particulier
00595     if ( x == double(0) ) {
00596         return ;
00597     }
00598     
00599     // Cas general
00600     int n = get_taille() ;
00601     switch(etat) {
00602     case ETATZERO:
00603     set_etat_qcq() ;
00604     for (int i=0 ; i<n ; i++) {
00605             t[i] = - x ;
00606     }
00607     break ;
00608     
00609     case ETATQCQ:
00610     for (int i=0 ; i<n ; i++) {
00611             t[i] -= x ;
00612     }
00613     break ;
00614     
00615     default:
00616     cout << "etat inconnu " << __FILE__ << endl ;
00617     abort() ;
00618     break ;
00619     }
00620     
00621     // Termine
00622 }
00623 
00624 void Tbl::operator*=(const Tbl & ti) {
00625 
00626     // Protection
00627     assert(dim == ti.dim) ;
00628     assert(etat != ETATNONDEF) ;
00629     assert(ti.get_etat() != ETATNONDEF) ;
00630     
00631     // Cas particulier
00632     if (etat == ETATZERO) {
00633         return ;
00634     }
00635     if (ti.get_etat() == ETATZERO) {
00636         set_etat_zero() ;
00637         return ;
00638     }
00639     
00640     // Cas general
00641     assert(etat == ETATQCQ) ;
00642     int n = get_taille() ;
00643     for (int i=0 ; i<n ; i++) {
00644         t[i] *= ti.t[i] ;
00645     }
00646     
00647     // Termine
00648 }
00649 
00650 void Tbl::operator*=(double x) {
00651 
00652     // Protection
00653     assert(etat != ETATNONDEF) ;
00654 
00655     // Cas particulier
00656     if ( x == double(0) ) {
00657         set_etat_zero() ;
00658         return ;
00659     }
00660     if (etat == ETATZERO) {
00661         return ;
00662     }
00663     
00664     // Cas general
00665     int n = get_taille() ;
00666     assert(etat == ETATQCQ) ;
00667     for (int i=0 ; i<n ; i++) {
00668         t[i] *= x ;
00669     }
00670     
00671     // Termine
00672 }
00673 
00674 void Tbl::operator/=(const Tbl & ti) {
00675 
00676     // Protection
00677     assert(dim == ti.dim) ;
00678     assert(etat != ETATNONDEF) ;
00679     assert(ti.get_etat() != ETATNONDEF) ;
00680     
00681     // Cas particulier
00682     if (ti.get_etat() == ETATZERO) {
00683         cout << "Division by 0 in Tbl::operator/=(const Tbl &) !" << endl ;
00684     abort() ;
00685     }
00686     if (etat == ETATZERO) {
00687         return ;
00688     }
00689     
00690     // Cas general
00691     assert(etat == ETATQCQ) ;
00692     assert(ti.get_etat() == ETATQCQ) ;
00693     int n = get_taille() ;
00694     for (int i=0 ; i<n ; i++) {
00695         t[i] /= ti.t[i] ;
00696     }
00697     
00698     // Termine
00699 }
00700 
00701 void Tbl::operator/=(double x) {
00702 
00703     // Protection
00704     assert(etat != ETATNONDEF) ;
00705 
00706     // Cas particulier
00707     if ( x == double(0) ) {
00708         cout << "Division by 0 in Tbl::operator/=(double ) !" << endl ;
00709     abort() ;
00710     }
00711     if (etat == ETATZERO) {
00712     return ;
00713     }
00714     
00715     // Cas general
00716     assert(etat == ETATQCQ) ;
00717     int n = get_taille() ;
00718     for (int i=0 ; i<n ; i++) {
00719         t[i] /= x ;
00720     }
00721     
00722     // Termine
00723 }
00724 

Generated on Tue Feb 7 01:35:20 2012 for LORENE by  doxygen 1.4.6