itbl_arithm.C

00001 /*
00002  * Arithmetical operations for class Itbl
00003  *
00004  */
00005 
00006 /*
00007  *   Copyright (c) 1999-2001 Philippe Grandclement
00008  *
00009  *   This file is part of LORENE.
00010  *
00011  *   LORENE is free software; you can redistribute it and/or modify
00012  *   it under the terms of the GNU General Public License as published by
00013  *   the Free Software Foundation; either version 2 of the License, or
00014  *   (at your option) any later version.
00015  *
00016  *   LORENE is distributed in the hope that it will be useful,
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *   GNU General Public License for more details.
00020  *
00021  *   You should have received a copy of the GNU General Public License
00022  *   along with LORENE; if not, write to the Free Software
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  */
00026 
00027 
00028 char itbl_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_arithm.C,v 1.2 2002/10/16 14:36:38 j_novak Exp $" ;
00029 
00030 /*
00031  * $Id: itbl_arithm.C,v 1.2 2002/10/16 14:36:38 j_novak Exp $
00032  * $Log: itbl_arithm.C,v $
00033  * Revision 1.2  2002/10/16 14:36:38  j_novak
00034  * Reorganization of #include instructions of standard C++, in order to
00035  * use experimental version 3 of gcc.
00036  *
00037  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00038  * LORENE
00039  *
00040  * Revision 2.0  1999/11/17  16:04:54  phil
00041  * *** empty log message ***
00042  *
00043  *
00044  * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_arithm.C,v 1.2 2002/10/16 14:36:38 j_novak Exp $
00045  *
00046  */
00047  
00048 // headers Lorene
00049 #include "itbl.h"
00050 
00051             //********************//
00052             // OPERATEURS UNAIRES //
00053             //********************//
00054 
00055 // + Itbl
00056 // -----
00057 Itbl operator+(const Itbl& t1)
00058 {
00059     // Protection
00060     assert(t1.get_etat() != ETATNONDEF) ;
00061     
00062     return t1 ;
00063 }
00064 
00065 // - Itbl
00066 // -----
00067 Itbl operator-(const Itbl& t1)
00068 {
00069     // Protection
00070     assert(t1.get_etat() != ETATNONDEF) ;
00071     
00072     // Cas particulier
00073     if (t1.get_etat() == ETATZERO) {
00074     return t1 ;
00075     }
00076     
00077     // Cas general
00078     Itbl r(t1.dim) ;            // Itbl resultat
00079     r.set_etat_qcq() ;
00080     for (int i=0 ; i<r.get_taille() ; i++) {
00081     (r.t)[i] = - (t1.t)[i] ;
00082     }
00083     return r ;
00084 }
00085 
00086             //**********//
00087             // ADDITION //
00088             //**********//
00089 
00090 // Itbl + Itbl
00091 // ---------
00092 Itbl operator+(const Itbl& t1, const Itbl& t2)
00093 {
00094 
00095     // Protection
00096     assert(t1.get_etat() != ETATNONDEF) ;
00097     assert(t2.get_etat() != ETATNONDEF) ;
00098     assert(t1.get_ndim() == t2.get_ndim()) ;
00099     for (int i=0 ; i<t1.get_ndim() ; i++) {
00100     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00101     }
00102 
00103     // Traitement des cas particuliers
00104     if (t1.get_etat() == ETATZERO) {
00105     return t2 ;
00106     }
00107     if (t2.get_etat() == ETATZERO) {
00108     return t1 ;
00109     }
00110 
00111     // Cas general
00112     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00113     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00114 
00115     Itbl r(t1) ;        // Itbl resultat
00116     for (int i=0 ; i<r.get_taille() ; i++) {
00117     (r.t)[i] += (t2.t)[i] ;
00118     }
00119     
00120     // Termine
00121     return r ;
00122 }
00123 
00124 // Itbl + int
00125 // ------------
00126 Itbl operator+(const Itbl& t1, int x)
00127 {
00128     // Protection
00129     assert(t1.get_etat() != ETATNONDEF) ;
00130 
00131     // Cas particulier
00132     if ( x == 0 ) {
00133     return t1 ;
00134     }
00135     
00136     // Cas general
00137     Itbl r(t1) ;        // Itbl resultat
00138     r.set_etat_qcq() ;
00139     for (int i=0 ; i<r.get_taille() ; i++) {
00140     (r.t)[i] += x ;
00141     }
00142     return r ;
00143 }
00144 
00145 // int + Itbl
00146 // ------------
00147 Itbl operator+(int x, const Itbl& t1)
00148 {
00149     return t1 + x ;
00150 }
00151 
00152 
00153             //**************//
00154             // SOUSTRACTION //
00155             //**************//
00156 
00157 // Itbl - Itbl
00158 // ---------
00159 Itbl operator-(const Itbl& t1, const Itbl& t2)
00160 {
00161 
00162     // Protection
00163     assert(t1.get_etat() != ETATNONDEF) ;
00164     assert(t2.get_etat() != ETATNONDEF) ;
00165     assert(t1.get_ndim() == t2.get_ndim()) ;
00166     for (int i=0 ; i<t1.get_ndim() ; i++) {
00167     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00168     }
00169 
00170     // Traitement des cas particuliers
00171     if (t1.get_etat() == ETATZERO) {
00172     return -t2 ;
00173     }
00174     if (t2.get_etat() == ETATZERO) {
00175     return t1 ;
00176     }
00177 
00178     // Cas general
00179     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00180     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00181 
00182     Itbl r(t1) ;        // Itbl resultat
00183     for (int i=0 ; i<r.get_taille() ; i++) {
00184     (r.t)[i] -= (t2.t)[i] ;
00185     }
00186     
00187     // Termine
00188     return r ;
00189 }
00190 
00191 
00192 // Itbl - int
00193 // ------------
00194 Itbl operator-(const Itbl& t1, int x)
00195 {
00196     // Protection
00197     assert(t1.get_etat() != ETATNONDEF) ;
00198 
00199     // Cas particulier
00200     if ( x == 0 ) {
00201     return t1 ;
00202     }
00203     
00204     // Cas general
00205     Itbl r(t1) ;        // Itbl resultat
00206     r.set_etat_qcq() ;
00207     for (int i=0 ; i<r.get_taille() ; i++) {
00208     (r.t)[i] -= x ;
00209     }
00210     return r ;
00211 }
00212 
00213 
00214 // int - Itbl
00215 // ------------
00216 Itbl operator-(int x, const Itbl& t1)
00217 {
00218     // Protection
00219     assert(t1.get_etat() != ETATNONDEF) ;
00220 
00221     // Cas particulier
00222     if ( x == 0 ) {
00223     return -t1 ;
00224     }
00225     
00226     // Cas general
00227     Itbl r(t1) ;        // Itbl resultat
00228     r.set_etat_qcq() ;
00229     for (int i=0 ; i<r.get_taille() ; i++) {
00230     (r.t)[i] -= x ;
00231     }
00232     return -r ;
00233 }
00234 
00235             //****************//
00236             // MULTIPLICATION //
00237             //****************//
00238 
00239 // Itbl * Itbl
00240 // ---------
00241 Itbl operator*(const Itbl& t1, const Itbl& t2)
00242 {
00243     // Protection
00244     assert(t1.get_etat() != ETATNONDEF) ;
00245     assert(t2.get_etat() != ETATNONDEF) ;
00246     assert(t1.get_ndim() == t2.get_ndim()) ;
00247     for (int i=0 ; i<t1.get_ndim() ; i++) {
00248     assert( t1.get_dim(i) == t2.get_dim(i) ) ;
00249     }
00250 
00251     // Cas particulier
00252     if (t1.get_etat() == ETATZERO) {
00253     return t1 ;
00254     }
00255     if (t2.get_etat() == ETATZERO) {
00256     return t2 ;
00257     }
00258     
00259     // Cas general
00260     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00261     assert(t2.get_etat() == ETATQCQ) ;  // sinon...
00262 
00263     Itbl r(t1) ;
00264     for (int i=0 ; i<r.get_taille() ; i++) {
00265     (r.t)[i] *= (t2.t)[i] ;
00266     }
00267     
00268     // Termine
00269     return r ;
00270 }
00271 
00272 // Itbl * int
00273 // ------------
00274 Itbl operator*(const Itbl& t1, int x)
00275 {
00276     // Protection
00277     assert(t1.get_etat() != ETATNONDEF) ;
00278 
00279     // Cas particulier
00280     if ((t1.get_etat() == ETATZERO) || ( x == 1 )) {
00281     return t1 ;
00282     }
00283 
00284     // Cas general
00285     assert(t1.get_etat() == ETATQCQ) ;  // sinon...
00286 
00287     Itbl r(t1) ;            // Itbl resultat
00288 
00289     if (x == 0) {
00290     r.set_etat_zero() ;
00291     }
00292     else {
00293     for (int i=0 ; i<r.get_taille() ; i++) {
00294         (r.t)[i] *= x ;
00295     }
00296     }
00297     
00298     // Termine
00299     return r ;
00300 }
00301 
00302 // int * Itbl
00303 // ------------
00304 Itbl operator*(int x, const Itbl& t1)
00305 {
00306     return t1 * x ;
00307 }
00308 
00309 
00310             //*******************//
00311             // operateurs +=,... //
00312             //*******************//
00313 
00314 void Itbl::operator+=(const Itbl & ti) {
00315 
00316     // Protection
00317     assert(dim == ti.dim) ;
00318     assert(etat != ETATNONDEF) ;
00319     assert(ti.get_etat() != ETATNONDEF) ;
00320     
00321     // Cas particulier
00322     if (ti.get_etat() == ETATZERO) {
00323         return ;
00324     }
00325     
00326     // Cas general
00327     int n = get_taille() ;
00328     switch(etat) {
00329     case ETATZERO:
00330     set_etat_qcq() ;
00331     for (int i=0 ; i<n ; i++) {
00332             t[i] = ti.t[i] ;
00333     }
00334     break ;
00335     
00336     case ETATQCQ:
00337     for (int i=0 ; i<n ; i++) {
00338             t[i] += ti.t[i] ;
00339     }
00340     break ;
00341     
00342     default:
00343     cout << "etat inconnu " << __FILE__ << endl ;
00344     abort() ;
00345     break ;
00346     }
00347     
00348     // Termine
00349 }
00350 
00351 void Itbl::operator+=(int x) {
00352 
00353     // Protection
00354     assert(etat != ETATNONDEF) ;
00355 
00356     // Cas particulier
00357     if ( x == 0 ) {
00358         return ;
00359     }
00360     
00361     // Cas general
00362     int n = get_taille() ;
00363     switch(etat) {
00364     case ETATZERO:
00365     set_etat_qcq() ;
00366     for (int i=0 ; i<n ; i++) {
00367             t[i] = x ;
00368     }
00369     break ;
00370     
00371     case ETATQCQ:
00372     for (int i=0 ; i<n ; i++) {
00373             t[i] += x ;
00374     }
00375     break ;
00376     
00377     default:
00378     cout << "etat inconnu " << __FILE__ << endl ;
00379     abort() ;
00380     break ;
00381     }
00382     
00383     // Termine
00384 }
00385 
00386 void Itbl::operator-=(const Itbl & ti) {
00387 
00388     // Protection
00389     assert(dim == ti.dim) ;
00390     assert(etat != ETATNONDEF) ;
00391     assert(ti.get_etat() != ETATNONDEF) ;
00392     
00393     // Cas particulier
00394     if (ti.get_etat() == ETATZERO) {
00395         return ;
00396     }
00397     
00398     // Cas general
00399     int n = get_taille() ;
00400     switch(etat) {
00401     case ETATZERO:
00402     set_etat_qcq() ;
00403     for (int i=0 ; i<n ; i++) {
00404             t[i] = - ti.t[i] ;
00405     }
00406     break ;
00407     
00408     case ETATQCQ:
00409     for (int i=0 ; i<n ; i++) {
00410             t[i] -= ti.t[i] ;
00411     }
00412     break ;
00413     
00414     default:
00415     cout << "etat inconnu " << __FILE__ << endl ;
00416     abort() ;
00417     break ;
00418     }
00419     
00420     // Termine
00421 }
00422 
00423 void Itbl::operator-=(int x) {
00424 
00425     // Protection
00426     assert(etat != ETATNONDEF) ;
00427 
00428     // Cas particulier
00429     if ( x == 0 ) {
00430         return ;
00431     }
00432     
00433     // Cas general
00434     int n = get_taille() ;
00435     switch(etat) {
00436     case ETATZERO:
00437     set_etat_qcq() ;
00438     for (int i=0 ; i<n ; i++) {
00439             t[i] = - x ;
00440     }
00441     break ;
00442     
00443     case ETATQCQ:
00444     for (int i=0 ; i<n ; i++) {
00445             t[i] -= x ;
00446     }
00447     break ;
00448     
00449     default:
00450     cout << "etat inconnu " << __FILE__ << endl ;
00451     abort() ;
00452     break ;
00453     }
00454     
00455     // Termine
00456 }
00457 
00458 void Itbl::operator*=(const Itbl & ti) {
00459 
00460     // Protection
00461     assert(dim == ti.dim) ;
00462     assert(etat != ETATNONDEF) ;
00463     assert(ti.get_etat() != ETATNONDEF) ;
00464     
00465     // Cas particulier
00466     if (etat == ETATZERO) {
00467         return ;
00468     }
00469     if (ti.get_etat() == ETATZERO) {
00470         set_etat_zero() ;
00471         return ;
00472     }
00473     
00474     // Cas general
00475     assert(etat == ETATQCQ) ;
00476     for (int i=0 ; i<get_taille() ; i++) {
00477         t[i] *= ti.t[i] ;
00478     }
00479     
00480     // Termine
00481 }
00482 
00483 void Itbl::operator*=(int x) {
00484 
00485     // Protection
00486     assert(etat != ETATNONDEF) ;
00487 
00488     // Cas particulier
00489     if ( x == 0 ) {
00490         set_etat_zero() ;
00491         return ;
00492     }
00493     if (etat == ETATZERO) {
00494         return ;
00495     }
00496     
00497     // Cas general
00498     assert(etat == ETATQCQ) ;
00499     for (int i=0 ; i<get_taille() ; i++) {
00500         t[i] *= x ;
00501     }
00502     
00503     // Termine
00504 }

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