cmp_math.C

00001 /*
00002  *  Mathematical functions for the Cmp class.
00003  *
00004  *  These functions are not member functions of the Cmp class.
00005  *
00006  */
00007 
00008 /*
00009  *   Copyright (c) 1999-2001 Eric Gourgoulhon
00010  *   Copyright (c) 1999-2001 Philippe Grandclement
00011  *
00012  *   This file is part of LORENE.
00013  *
00014  *   LORENE is free software; you can redistribute it and/or modify
00015  *   it under the terms of the GNU General Public License as published by
00016  *   the Free Software Foundation; either version 2 of the License, or
00017  *   (at your option) any later version.
00018  *
00019  *   LORENE is distributed in the hope that it will be useful,
00020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *   GNU General Public License for more details.
00023  *
00024  *   You should have received a copy of the GNU General Public License
00025  *   along with LORENE; if not, write to the Free Software
00026  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  *
00028  */
00029 
00030 char cmp_math_C[] = "$Header: /cvsroot/Lorene/C++/Source/Cmp/cmp_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $" ;
00031 
00032 /*
00033  * $Id: cmp_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $
00034  * $Log: cmp_math.C,v $
00035  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00036  * LORENE
00037  *
00038  * Revision 2.2  1999/12/02  17:59:16  phil
00039  * /
00040  *
00041  * Revision 2.1  1999/11/26  14:23:30  eric
00042  * Modif commentaires.
00043  *
00044  * Revision 2.0  1999/11/15  14:13:05  eric
00045  * *** empty log message ***
00046  *
00047  *
00048  * $Header: /cvsroot/Lorene/C++/Source/Cmp/cmp_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $
00049  *
00050  */
00051 
00052 // Headers C
00053 #include <math.h>
00054 
00055 // Headers Lorene
00056 #include "cmp.h"
00057 
00058                 //-------//
00059                 // Sine  //
00060                 //-------//
00061 
00062 Cmp sin(const Cmp& ci)
00063 {
00064     // Protection
00065     assert(ci.get_etat() != ETATNONDEF) ;
00066     
00067     // Cas ETATZERO
00068     if (ci.get_etat() == ETATZERO) {
00069     return ci ;
00070     }
00071     
00072     // Cas general
00073     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00074 
00075     Cmp co(ci.get_mp()) ;       // result
00076 
00077     co.set_etat_qcq() ; 
00078     co.va = sin( ci.va ) ; 
00079 
00080     return co ;
00081 }
00082 
00083                 //---------//
00084                 // Cosine  //
00085                 //---------//
00086 
00087 Cmp cos(const Cmp& ci)
00088 {
00089     // Protection
00090     assert(ci.get_etat() != ETATNONDEF) ;
00091     
00092     Cmp co(ci.get_mp()) ;       // result
00093 
00094     // Cas ETATZERO
00095     if (ci.get_etat() == ETATZERO) {
00096     co.set_etat_qcq() ; 
00097     co.va = double(1) ;
00098     }
00099     else {
00100         // Cas general
00101     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00102     co.set_etat_qcq() ; 
00103     co.va = cos( ci.va ) ; 
00104     }
00105 
00106     return co ;
00107 }
00108 
00109                 //----------//
00110                 // Tangent  //
00111                 //----------//
00112 
00113 Cmp tan(const Cmp& ci)
00114 {
00115     // Protection
00116     assert(ci.get_etat() != ETATNONDEF) ;
00117     
00118     // Cas ETATZERO
00119     if (ci.get_etat() == ETATZERO) {
00120     return ci ;
00121     }
00122     
00123     // Cas general
00124     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00125 
00126     Cmp co(ci.get_mp()) ;       // result
00127     co.set_etat_qcq() ; 
00128     co.va = tan( ci.va ) ; 
00129 
00130     return co ;
00131 }
00132 
00133                 //----------//
00134                 // Arcsine  //
00135                 //----------//
00136 
00137 Cmp asin(const Cmp& ci)
00138 {
00139     // Protection
00140     assert(ci.get_etat() != ETATNONDEF) ;
00141     
00142     // Cas ETATZERO
00143     if (ci.get_etat() == ETATZERO) {
00144     return ci ;
00145     }
00146     
00147     // Cas general
00148     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00149 
00150     Cmp co(ci.get_mp()) ;       // result
00151 
00152     co.set_etat_qcq() ; 
00153     co.va = asin( ci.va ) ; 
00154 
00155     return co ;
00156 }
00157 
00158                 //-------------//
00159                 // Arccossine  //
00160                 //-------------//
00161 
00162 Cmp acos(const Cmp& ci)
00163 {
00164     // Protection
00165     assert(ci.get_etat() != ETATNONDEF) ;
00166     
00167     Cmp co(ci.get_mp()) ;       // result
00168 
00169     // Cas ETATZERO
00170     if (ci.get_etat() == ETATZERO) {
00171     co.set_etat_qcq() ; 
00172     co.va = double(0.5) * M_PI ;
00173     }
00174     else {
00175         // Cas general
00176     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00177     co.set_etat_qcq() ; 
00178     co.va = acos( ci.va ) ; 
00179     }
00180 
00181     return co ;
00182 }
00183 
00184                 //-------------//
00185                 // Arctangent  //
00186                 //-------------//
00187 
00188 Cmp atan(const Cmp& ci)
00189 {
00190     // Protection
00191     assert(ci.get_etat() != ETATNONDEF) ;
00192     
00193     // Cas ETATZERO
00194     if (ci.get_etat() == ETATZERO) {
00195     return ci ;
00196     }
00197     
00198     // Cas general
00199     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00200 
00201     Cmp co(ci.get_mp()) ;       // result
00202 
00203     co.set_etat_qcq() ; 
00204     co.va = atan( ci.va ) ; 
00205 
00206     return co ;
00207 }
00208 
00209                 //-------------//
00210                 // Square root //
00211                 //-------------//
00212 
00213 Cmp sqrt(const Cmp& ci)
00214 {
00215     // Protection
00216     assert(ci.get_etat() != ETATNONDEF) ;
00217     
00218     // Cas ETATZERO
00219     if (ci.get_etat() == ETATZERO) {
00220     return ci ;
00221     }
00222     
00223     // Cas general
00224     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00225 
00226     Cmp co(ci.get_mp()) ;       // result
00227 
00228     co.set_etat_qcq() ; 
00229     co.va = sqrt( ci.va ) ; 
00230 
00231     return co ;
00232 }
00233 
00234                 //-------------//
00235                 // Cube root   //
00236                 //-------------//
00237 
00238 Cmp racine_cubique(const Cmp& ci)
00239 {
00240     // Protection
00241     assert(ci.get_etat() != ETATNONDEF) ;
00242     
00243     // Cas ETATZERO
00244     if (ci.get_etat() == ETATZERO) {
00245     return ci ;
00246     }
00247     
00248     // Cas general
00249     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00250 
00251     Cmp co(ci.get_mp()) ;       // result
00252 
00253     co.set_etat_qcq() ; 
00254     co.va = racine_cubique( ci.va ) ; 
00255 
00256     return co ;
00257 }
00258 
00259                 //--------------//
00260                 // Exponential  //
00261                 //--------------//
00262 
00263 Cmp exp(const Cmp& ci)
00264 {
00265     // Protection
00266     assert(ci.get_etat() != ETATNONDEF) ;
00267     
00268     Cmp co(ci.get_mp()) ;       // result
00269 
00270     // Cas ETATZERO
00271     if (ci.get_etat() == ETATZERO) {
00272     co.set_etat_qcq() ; 
00273     co.va = double(1) ;
00274     }
00275     else {
00276         // Cas general
00277     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00278     co.set_etat_qcq() ; 
00279     co.va = exp( ci.va ) ; 
00280     }
00281 
00282     return co ;
00283 }
00284 
00285                 //---------------------//
00286                 // Neperian logarithm  //
00287                 //---------------------//
00288 
00289 Cmp log(const Cmp& ci)
00290 {
00291     // Protection
00292     assert(ci.get_etat() != ETATNONDEF) ;
00293     
00294     // Cas ETATZERO
00295     if (ci.get_etat() == ETATZERO) {
00296     cout << "Argument of log is ZERO in log(Cmp) !" << endl ; 
00297     abort() ; 
00298     }
00299 
00300     // Cas general
00301     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00302 
00303     Cmp co(ci.get_mp()) ;       // result
00304 
00305     co.set_etat_qcq() ; 
00306     co.va = log( ci.va ) ; 
00307 
00308     return co ;
00309 }
00310 
00311                 //---------------------//
00312                 // Decimal  logarithm  //
00313                 //---------------------//
00314 
00315 Cmp log10(const Cmp& ci)
00316 {
00317     // Protection
00318     assert(ci.get_etat() != ETATNONDEF) ;
00319     
00320     // Cas ETATZERO
00321     if (ci.get_etat() == ETATZERO) {
00322     cout << "Argument of log10 is ZERO in log10(Cmp) !" << endl ; 
00323     abort() ; 
00324     }
00325 
00326     // Cas general
00327     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00328 
00329     Cmp co(ci.get_mp()) ;       // result
00330 
00331     co.set_etat_qcq() ; 
00332     co.va = log10( ci.va ) ; 
00333 
00334     return co ;
00335 }
00336 
00337                 //------------------//
00338                 // Power (integer)  //
00339                 //------------------//
00340 
00341 Cmp pow(const Cmp& ci, int n)
00342 {
00343     // Protection
00344     assert(ci.get_etat() != ETATNONDEF) ;
00345     
00346     // Cas ETATZERO
00347     if (ci.get_etat() == ETATZERO) {
00348     if (n > 0) {
00349         return ci ; 
00350     }
00351     else {
00352         cout << "pow(Cmp, int) : ETATZERO^n with n <= 0 !" << endl ; 
00353         abort() ;
00354     } 
00355     }
00356 
00357     // Cas general
00358     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00359 
00360     Cmp co(ci.get_mp()) ;       // result
00361 
00362     co.set_etat_qcq() ; 
00363     co.va = pow(ci.va,  n) ; 
00364 
00365     return co ;
00366 }
00367 
00368                 //-----------------//
00369                 // Power (double)  //
00370                 //-----------------//
00371 
00372 Cmp pow(const Cmp& ci, double x)
00373 {
00374     // Protection
00375     assert(ci.get_etat() != ETATNONDEF) ;
00376     
00377     // Cas ETATZERO
00378     if (ci.get_etat() == ETATZERO) {
00379     if (x > double(0)) {
00380         return ci ; 
00381     }
00382     else {
00383         cout << "pow(Cmp, double) : ETATZERO^x with x <= 0 !" << endl ; 
00384         abort() ;
00385     } 
00386     }
00387 
00388     // Cas general
00389     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00390 
00391     Cmp co(ci.get_mp()) ;       // result
00392 
00393     co.set_etat_qcq() ; 
00394     co.va = pow(ci.va, x) ; 
00395 
00396     return co ;
00397 }
00398 
00399                 //-----------------//
00400                 // Absolute value  //
00401                 //-----------------//
00402 
00403 Cmp abs(const Cmp& ci)
00404 {
00405     // Protection
00406     assert(ci.get_etat() != ETATNONDEF) ;
00407     
00408     // Cas ETATZERO
00409     if (ci.get_etat() == ETATZERO) {
00410     return ci ;
00411     }
00412     
00413     // Cas general
00414     assert(ci.get_etat() == ETATQCQ) ;  // sinon...
00415 
00416     Cmp co(ci.get_mp()) ;       // result
00417 
00418     co.set_etat_qcq() ; 
00419     co.va = abs( ci.va ) ; 
00420 
00421     return co ;
00422 }
00423 
00424             //-------------------------------//
00425                     //            max                //
00426             //-------------------------------//
00427 
00428 Tbl max(const Cmp& ci) {
00429 
00430     // Protection
00431     assert(ci.get_etat() != ETATNONDEF) ;
00432     
00433     Tbl resu( ci.get_mp()->get_mg()->get_nzone() ) ; 
00434     
00435     if (ci.get_etat() == ETATZERO) {
00436     resu.annule_hard() ; 
00437     }
00438     else {
00439     assert(ci.get_etat() == ETATQCQ) ;  
00440 
00441     resu = max( ci.va ) ;       // max(Valeur) 
00442     }
00443           
00444     return resu ; 
00445 }
00446 
00447             //-------------------------------//
00448                     //            min                //
00449             //-------------------------------//
00450 
00451 Tbl min(const Cmp& ci) {
00452 
00453     // Protection
00454     assert(ci.get_etat() != ETATNONDEF) ;
00455     
00456     Tbl resu( ci.get_mp()->get_mg()->get_nzone() ) ; 
00457     
00458     if (ci.get_etat() == ETATZERO) {
00459     resu.annule_hard() ; 
00460     }
00461     else {
00462     assert(ci.get_etat() == ETATQCQ) ;  
00463 
00464     resu = min( ci.va ) ;       // min(Valeur)  
00465     }
00466           
00467     return resu ; 
00468 }
00469 
00470             //-------------------------------//
00471                     //            norme              //
00472             //-------------------------------//
00473 
00474 Tbl norme(const Cmp& ci) {
00475 
00476     // Protection
00477     assert(ci.get_etat() != ETATNONDEF) ;
00478     
00479     Tbl resu( ci.get_mp()->get_mg()->get_nzone() ) ; 
00480     
00481     if (ci.get_etat() == ETATZERO) {
00482     resu.annule_hard() ; 
00483     }
00484     else {
00485     assert(ci.get_etat() == ETATQCQ) ;  
00486 
00487     resu = norme( ci.va ) ;     // norme(Valeur)    
00488     }
00489           
00490     return resu ; 
00491 }
00492 
00493             //-------------------------------//
00494                     //          diffrel              //
00495             //-------------------------------//
00496 
00497 Tbl diffrel(const Cmp& c1, const Cmp& c2) {
00498     
00499     // Protections
00500     assert(c1.get_etat() != ETATNONDEF) ;
00501     assert(c2.get_etat() != ETATNONDEF) ;
00502 
00503     int nz = c1.get_mp()->get_mg()->get_nzone() ;
00504     Tbl resu(nz) ; 
00505     
00506     Cmp diff = c1 - c2 ;     // la compatibilite dzpuis est testee a ce niveau
00507 
00508     Tbl normdiff = norme(diff) ; 
00509     Tbl norme2 = norme(c2) ;
00510     
00511     assert(normdiff.get_etat() == ETATQCQ) ;     
00512     assert(norme2.get_etat() == ETATQCQ) ; 
00513 
00514     resu.set_etat_qcq() ; 
00515     for (int l=0; l<nz; l++) {
00516     if ( norme2(l) == double(0) ) {
00517         resu.set(l) = normdiff(l) ; 
00518     }
00519     else{
00520         resu.set(l) = normdiff(l) / norme2(l) ;             
00521     }
00522     }
00523     
00524     return resu ; 
00525     
00526 }
00527 
00528             //-------------------------------//
00529                     //          diffrelmax           //
00530             //-------------------------------//
00531 
00532 Tbl diffrelmax(const Cmp& c1, const Cmp& c2) {
00533     
00534     // Protections
00535     assert(c1.get_etat() != ETATNONDEF) ;
00536     assert(c2.get_etat() != ETATNONDEF) ;
00537     
00538     int nz = c1.get_mp()->get_mg()->get_nzone() ;
00539     Tbl resu(nz) ; 
00540     
00541     Tbl max2 = max(abs(c2)) ;
00542     
00543     Cmp diff = c1 - c2 ;    // la compatibilite dzpuis est testee a ce niveau
00544 
00545     Tbl maxdiff = max(abs(diff)) ; 
00546     
00547     assert(maxdiff.get_etat() == ETATQCQ) ;     
00548     assert(max2.get_etat() == ETATQCQ) ; 
00549 
00550     resu.set_etat_qcq() ; 
00551     for (int l=0; l<nz; l++) {
00552     if ( max2(l) == double(0) ) {
00553         resu.set(l) = maxdiff(l) ; 
00554     }
00555     else{
00556         resu.set(l) = maxdiff(l) / max2(l) ;            
00557     }
00558     }
00559     
00560     return resu ; 
00561     
00562 }
00563 

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