cmp_arithm.C

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

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