mtbl_math.C

00001 /*
00002  *  Mathematical functions for class Mtbl
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 mtbl_math_C[] = "$Header: /cvsroot/Lorene/C++/Source/Mtbl/mtbl_math.C,v 1.2 2012/01/17 10:38:20 j_penner Exp $" ;
00030 
00031 /*
00032  * $Id: mtbl_math.C,v 1.2 2012/01/17 10:38:20 j_penner Exp $
00033  * $Log: mtbl_math.C,v $
00034  * Revision 1.2  2012/01/17 10:38:20  j_penner
00035  * added a Heaviside function
00036  *
00037  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00038  * LORENE
00039  *
00040  * Revision 2.4  1999/12/02  17:55:03  phil
00041  * *** empty log message ***
00042  *
00043  * Revision 2.3  1999/10/29  15:46:35  eric
00044  * *** empty log message ***
00045  *
00046  * Revision 2.2  1999/10/29  15:06:38  eric
00047  * Ajout de fonctions mathematiques (abs, norme, etc...).
00048  *
00049  * Revision 2.1  1999/03/01  15:09:56  eric
00050  * *** empty log message ***
00051  *
00052  * Revision 2.0  1999/02/24  15:25:41  hyc
00053  * *** empty log message ***
00054  *
00055  *
00056  * $Header: /cvsroot/Lorene/C++/Source/Mtbl/mtbl_math.C,v 1.2 2012/01/17 10:38:20 j_penner Exp $
00057  *
00058  */
00059 
00060 // Headers C
00061 // ---------
00062 #include <math.h>
00063 #include <stdlib.h>
00064 
00065 // Headers Lorene
00066 // --------------
00067 #include "mtbl.h"
00068 
00069                 //-------//
00070                 // Sinus //
00071                 //-------//
00072 
00073 Mtbl sin(const Mtbl& ti)
00074 {
00075     // Protection
00076     assert(ti.get_etat() != ETATNONDEF) ;
00077     
00078     // Cas ETATZERO
00079     if (ti.get_etat() == ETATZERO) {
00080     return ti ;
00081     }
00082     
00083     // Cas general
00084     assert(ti.get_etat() == ETATQCQ) ;      // sinon...
00085     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00086     to.set_etat_qcq() ;
00087     int nzone = ti.get_nzone() ;
00088     for (int i=0 ; i<nzone ; i++) {
00089     *(to.t[i]) = sin( *(ti.t[i]) ) ;
00090     }
00091     return to ;
00092 }
00093 
00094                 //---------//
00095                 // Cosinus //
00096                 //---------//
00097 
00098 Mtbl cos(const Mtbl& ti)
00099 {
00100     // Protection
00101     assert(ti.get_etat() != ETATNONDEF) ;
00102     
00103     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00104     to.set_etat_qcq() ;
00105     int nzone = ti.get_nzone() ;
00106 
00107     // Cas ETATZERO
00108     if (ti.get_etat() == ETATZERO) {
00109     for (int i=0 ; i<nzone ; i++) {
00110         *(to.t[i]) = 1. ;
00111     }
00112     return to ;
00113     }
00114     
00115     // Cas general
00116     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00117     for (int i=0 ; i<nzone ; i++) {
00118     *(to.t[i]) = cos( *(ti.t[i]) ) ;
00119     }
00120     return to ;
00121 }
00122 
00123                 //----------//
00124                 // Tangente //
00125                 //----------//
00126 
00127 Mtbl tan(const Mtbl& ti)
00128 {
00129     // Protection
00130     assert(ti.get_etat() != ETATNONDEF) ;
00131     
00132     // Cas ETATZERO
00133     if (ti.get_etat() == ETATZERO) {
00134     return ti ;
00135     }
00136     
00137     // Cas general
00138     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00139     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00140     to.set_etat_qcq() ;
00141     int nzone = ti.get_nzone() ;
00142     for (int i=0 ; i<nzone ; i++) {
00143     *(to.t[i]) = tan( *(ti.t[i]) ) ;
00144     }
00145     return to ;
00146 }
00147 
00148                 //----------//
00149                 // ArcSinus //
00150                 //----------//
00151 
00152 Mtbl asin(const Mtbl& ti)
00153 {
00154     // Protection
00155     assert(ti.get_etat() != ETATNONDEF) ;
00156     
00157     // Cas ETATZERO
00158     if (ti.get_etat() == ETATZERO) {
00159     return ti ;
00160     }
00161     
00162     // Cas general
00163     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00164     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00165     to.set_etat_qcq() ;
00166     int nzone = ti.get_nzone() ;
00167     for (int i=0 ; i<nzone ; i++) {
00168     *(to.t[i]) = asin( *(ti.t[i]) ) ;
00169     }
00170     return to ;
00171 }
00172 
00173                 //------------//
00174                 // ArcCosinus //
00175                 //------------//
00176 
00177 Mtbl acos(const Mtbl& ti)
00178 {
00179    // Protection
00180     assert(ti.get_etat() != ETATNONDEF) ;
00181     
00182     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00183     to.set_etat_qcq() ;
00184     int nzone = ti.get_nzone() ;
00185 
00186     // Cas ETATZERO
00187     if (ti.get_etat() == ETATZERO) {
00188     for (int i=0 ; i<nzone ; i++) {
00189         *(to.t[i]) = M_PI * .5 ;
00190     }
00191     return to ;
00192     }
00193     
00194     // Cas general
00195     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00196     for (int i=0 ; i<nzone ; i++) {
00197     *(to.t[i]) = acos( *(ti.t[i]) ) ;
00198     }
00199     return to ;
00200 }
00201 
00202                 //-------------//
00203                 // ArcTangente //
00204                 //-------------//
00205 
00206 Mtbl atan(const Mtbl& ti)
00207 {
00208     // Protection
00209     assert(ti.get_etat() != ETATNONDEF) ;
00210     
00211     // Cas ETATZERO
00212     if (ti.get_etat() == ETATZERO) {
00213     return ti ;
00214     }
00215     
00216     // Cas general
00217     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00218     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00219     to.set_etat_qcq() ;
00220     int nzone = ti.get_nzone() ;
00221     for (int i=0 ; i<nzone ; i++) {
00222     *(to.t[i]) = atan( *(ti.t[i]) ) ;
00223     }
00224     return to ;
00225 }
00226 
00227                 //------//
00228                 // Sqrt //
00229                 //------//
00230 
00231 Mtbl sqrt(const Mtbl& ti)
00232 {
00233     // Protection
00234     assert(ti.get_etat() != ETATNONDEF) ;
00235     
00236     // Cas ETATZERO
00237     if (ti.get_etat() == ETATZERO) {
00238     return ti ;
00239     }
00240     
00241     // Cas general
00242     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00243     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00244     to.set_etat_qcq() ;
00245     int nzone = ti.get_nzone() ;
00246     for (int i=0 ; i<nzone ; i++) {
00247     *(to.t[i]) = sqrt( *(ti.t[i]) ) ;
00248     }
00249     return to ;
00250 }
00251 
00252 
00253                 //------//
00254                 // Cubic //
00255                 //------//
00256 
00257 Mtbl racine_cubique(const Mtbl& ti)
00258 {
00259     // Protection
00260     assert(ti.get_etat() != ETATNONDEF) ;
00261     
00262     // Cas ETATZERO
00263     if (ti.get_etat() == ETATZERO) {
00264     return ti ;
00265     }
00266     
00267     // Cas general
00268     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00269     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00270     to.set_etat_qcq() ;
00271     int nzone = ti.get_nzone() ;
00272     for (int i=0 ; i<nzone ; i++) {
00273     *(to.t[i]) = racine_cubique( *(ti.t[i]) ) ;
00274     }
00275     return to ;
00276 }
00277                 //---------------//
00278                 // Exponantielle //
00279                 //---------------//
00280 
00281 Mtbl exp(const Mtbl& ti)
00282 {
00283     // Protection
00284     assert(ti.get_etat() != ETATNONDEF) ;
00285     
00286     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00287     to.set_etat_qcq() ;
00288     int nzone = ti.get_nzone() ;
00289 
00290     // Cas ETATZERO
00291     if (ti.get_etat() == ETATZERO) {
00292     for (int i=0 ; i<nzone ; i++) {
00293         *(to.t[i]) = 1. ;
00294     }
00295     return to ;
00296     }
00297     
00298     // Cas general
00299     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00300     for (int i=0 ; i<nzone ; i++) {
00301     *(to.t[i]) = exp( *(ti.t[i]) ) ;
00302     }
00303     return to ;
00304 }
00305 
00306                 //---------------//
00307                 // Heaviside     //
00308                 //---------------//
00309 
00310 Mtbl Heaviside(const Mtbl& ti)
00311 {
00312     // Protection
00313     assert(ti.get_etat() != ETATNONDEF) ;
00314     
00315     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00316     to.set_etat_qcq() ;
00317     int nzone = ti.get_nzone() ;
00318 
00319     // Cas ETATZERO
00320     if (ti.get_etat() == ETATZERO) {
00321     for (int i=0 ; i<nzone ; i++) {
00322         *(to.t[i]) = 0. ;
00323     }
00324     return to ;
00325     }
00326     
00327     // Cas general
00328     assert(ti.get_etat() == ETATQCQ) ;  // otherwise
00329     for (int i=0 ; i<nzone ; i++) {
00330     *(to.t[i]) = Heaviside( *(ti.t[i]) ) ;
00331     }
00332     return to ;
00333 }
00334 
00335                 //-------------//
00336                 // Log naturel //
00337                 //-------------//
00338 
00339 Mtbl log(const Mtbl& ti)
00340 {
00341     // Protection
00342     assert(ti.get_etat() != ETATNONDEF) ;
00343     
00344     // Cas ETATZERO
00345     if (ti.get_etat() == ETATZERO) {
00346     cout << "Mtbl log: log(ETATZERO) !" << endl  ;
00347     abort () ;
00348     }
00349     
00350     // Cas general
00351     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00352     Mtbl to(ti.get_mg()) ;      // Mtbl resultat
00353     to.set_etat_qcq() ;
00354     int nzone = ti.get_nzone() ;
00355     for (int i=0 ; i<nzone ; i++) {
00356     *(to.t[i]) = log( *(ti.t[i]) ) ;
00357     }
00358     return to ;
00359 }
00360 
00361                 //-------------//
00362                 // Log decimal //
00363                 //-------------//
00364 
00365 Mtbl log10(const Mtbl& ti)
00366 {
00367     // Protection
00368     assert(ti.get_etat() != ETATNONDEF) ;
00369     
00370     // Cas ETATZERO
00371     if (ti.get_etat() == ETATZERO) {
00372     cout << "Mtbl log10: log10(ETATZERO) !" << endl ;
00373     abort () ;
00374     }
00375     
00376     // Cas general
00377     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00378     Mtbl to(ti.get_mg()) ;      // Mtbl resultat
00379     to.set_etat_qcq() ;
00380     int nzone = ti.get_nzone() ;
00381     for (int i=0 ; i<nzone ; i++) {
00382     *(to.t[i]) = log10( *(ti.t[i]) ) ;
00383     }
00384     return to ;
00385 }
00386 
00387                 //--------------//
00388                 // Power entier //
00389                 //--------------//
00390 
00391 Mtbl pow(const Mtbl& ti, int n)
00392 {
00393     // Protection
00394     assert(ti.get_etat() != ETATNONDEF) ;
00395     
00396     // Cas ETATZERO
00397     if (ti.get_etat() == ETATZERO) {
00398     if (n > 0) {
00399         return ti ;
00400     }
00401     else {
00402         cout << "Mtbl pow: ETATZERO^n avec n<=0 ! "<< endl  ;
00403         abort () ;
00404     }
00405     }
00406     
00407     // Cas general
00408     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00409     Mtbl to(ti.get_mg()) ;      // Mtbl resultat
00410     to.set_etat_qcq() ;
00411     double x = n ;
00412     int nzone = ti.get_nzone() ;
00413     for (int i=0 ; i<nzone ; i++) {
00414     *(to.t[i]) = pow( *(ti.t[i]), x ) ;
00415     }
00416     return to ;
00417 }
00418 
00419                 //--------------//
00420                 // Power double //
00421                 //--------------//
00422 
00423 Mtbl pow(const Mtbl& ti, double x)
00424 {
00425     // Protection
00426     assert(ti.get_etat() != ETATNONDEF) ;
00427     
00428     // Cas ETATZERO
00429     if (ti.get_etat() == ETATZERO) {
00430     if (x > 0) {
00431         return ti ;
00432     }
00433     else {
00434         cout << "Mtbl pow: ETATZERO^x avec x<=0 !" << endl ;
00435         abort () ;
00436     }
00437     }
00438     
00439     // Cas general
00440     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00441     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00442     to.set_etat_qcq() ;
00443     int nzone = ti.get_nzone() ;
00444     for (int i=0 ; i<nzone ; i++) {
00445     *(to.t[i]) = pow( *(ti.t[i]), x ) ;
00446     }
00447     return to ;
00448 }
00449 
00450                 //----------------//
00451                 // Absolute value //
00452                 //----------------//
00453 
00454 Mtbl abs(const Mtbl& ti)
00455 {
00456     // Protection
00457     assert(ti.get_etat() != ETATNONDEF) ;
00458     
00459     // Cas ETATZERO
00460     if (ti.get_etat() == ETATZERO) {
00461     return ti ;
00462     }
00463     
00464     // Cas general
00465 
00466     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00467     Mtbl to(ti.get_mg()) ;          // Mtbl resultat
00468 
00469     to.set_etat_qcq() ;
00470 
00471     int nzone = ti.get_nzone() ;
00472 
00473     for (int l=0 ; l<nzone ; l++) {
00474     *(to.t[l]) = abs( *(ti.t[l]) ) ;    
00475     }
00476 
00477     return to ;
00478 }
00479 
00480 
00481 
00482 
00483             //-------------------------------//
00484                     //         total max             //
00485             //-------------------------------//
00486 
00487 double totalmax(const Mtbl& mti) {
00488 
00489     // Protection
00490     assert(mti.get_etat() != ETATNONDEF) ;
00491     
00492     int nz = mti.get_nzone() ;
00493     
00494     double resu = -1E99 ; // large negative to initialize 
00495     
00496     if (mti.get_etat() == ETATZERO) {
00497     resu = 0 ; 
00498     }
00499     else {      // Cas general
00500 
00501     assert(mti.get_etat() == ETATQCQ) ;     // sinon....
00502     
00503     for (int l=0 ; l<nz ; l++) {
00504         resu = max(resu,max( *(mti.t[l]) )) ;
00505     }
00506     }
00507      
00508     return resu ; 
00509 }
00510 
00511             //-------------------------------//
00512                     //         total min             //
00513             //-------------------------------//
00514 
00515 double totalmin(const Mtbl& mti) {
00516 
00517     // Protection
00518     assert(mti.get_etat() != ETATNONDEF) ;
00519     
00520     int nz = mti.get_nzone() ;
00521     
00522     double resu = 1E99 ; // large value to initialize
00523     
00524     if (mti.get_etat() == ETATZERO) {
00525     resu = 0 ; 
00526     }
00527     else {      // Cas general
00528 
00529     assert(mti.get_etat() == ETATQCQ) ;     // sinon....
00530     
00531     for (int l=0 ; l<nz ; l++) {
00532         resu = min(resu, min( *(mti.t[l]) )) ;
00533     }
00534     }
00535      
00536     return resu ; 
00537 }
00538 
00539 
00540             //-------------------------------//
00541                     //            max                //
00542             //-------------------------------//
00543 
00544 Tbl max(const Mtbl& mti) {
00545 
00546     // Protection
00547     assert(mti.get_etat() != ETATNONDEF) ;
00548     
00549     int nz = mti.get_nzone() ;
00550     
00551     Tbl resu(nz) ; 
00552     
00553     if (mti.get_etat() == ETATZERO) {
00554     resu.annule_hard() ; 
00555     }
00556     else {      // Cas general
00557 
00558     assert(mti.get_etat() == ETATQCQ) ;     // sinon....
00559     
00560     resu.set_etat_qcq() ; 
00561     for (int l=0 ; l<nz ; l++) {
00562         resu.set(l) = max( *(mti.t[l]) ) ;
00563     }
00564     }
00565      
00566     return resu ; 
00567 }
00568 
00569             //-------------------------------//
00570                     //            min                //
00571             //-------------------------------//
00572 
00573 Tbl min(const Mtbl& mti) {
00574 
00575     // Protection
00576     assert(mti.get_etat() != ETATNONDEF) ;
00577     
00578     int nz = mti.get_nzone() ;
00579     
00580     Tbl resu(nz) ; 
00581     
00582     if (mti.get_etat() == ETATZERO) {
00583     resu.annule_hard() ; 
00584     }
00585     else {      // Cas general
00586 
00587     assert(mti.get_etat() == ETATQCQ) ;     // sinon....
00588     
00589     resu.set_etat_qcq() ; 
00590     for (int l=0 ; l<nz ; l++) {
00591         resu.set(l) = min( *(mti.t[l]) ) ;
00592     }
00593     }
00594      
00595     return resu ; 
00596 }
00597 
00598             //-------------------------------//
00599                     //            norme              //
00600             //-------------------------------//
00601 
00602 Tbl norme(const Mtbl& mti) {
00603 
00604     // Protection
00605     assert(mti.get_etat() != ETATNONDEF) ;
00606     
00607     int nz = mti.get_nzone() ;
00608     
00609     Tbl resu(nz) ; 
00610     
00611     if (mti.get_etat() == ETATZERO) {
00612     resu.annule_hard() ; 
00613     }
00614     else {      // Cas general
00615 
00616     assert(mti.get_etat() == ETATQCQ) ;     // sinon....
00617     
00618     resu.set_etat_qcq() ; 
00619     for (int l=0 ; l<nz ; l++) {
00620         resu.set(l) = norme( *(mti.t[l]) ) ;
00621     }
00622     }
00623      
00624     return resu ; 
00625 }
00626 
00627             //-------------------------------//
00628                     //          diffrel              //
00629             //-------------------------------//
00630 
00631 Tbl diffrel(const Mtbl& mt1, const Mtbl& mt2) {
00632     
00633     // Protections
00634     assert(mt1.get_etat() != ETATNONDEF) ;
00635     assert(mt2.get_etat() != ETATNONDEF) ;
00636     
00637     int nz = mt1.get_nzone() ;
00638     Tbl resu(nz) ; 
00639     
00640     Tbl normdiff = norme(mt1 - mt2) ; 
00641     Tbl norme2 = norme(mt2) ;
00642     
00643     assert(normdiff.get_etat() == ETATQCQ) ;     
00644     assert(norme2.get_etat() == ETATQCQ) ; 
00645 
00646     resu.set_etat_qcq() ; 
00647     for (int l=0; l<nz; l++) {
00648     if ( norme2(l) == double(0) ) {
00649         resu.set(l) = normdiff(l) ; 
00650     }
00651     else{
00652         resu.set(l) = normdiff(l) / norme2(l) ;             
00653     }
00654     }
00655     
00656     return resu ; 
00657     
00658 }
00659 
00660             //-------------------------------//
00661                     //          diffrelmax           //
00662             //-------------------------------//
00663 
00664 Tbl diffrelmax(const Mtbl& mt1, const Mtbl& mt2) {
00665     
00666     // Protections
00667     assert(mt1.get_etat() != ETATNONDEF) ;
00668     assert(mt2.get_etat() != ETATNONDEF) ;
00669     
00670     int nz = mt1.get_nzone() ;
00671     Tbl resu(nz) ; 
00672     
00673     Tbl max2 = max(abs(mt2)) ;
00674     Tbl maxdiff = max(abs(mt1 - mt2)) ; 
00675     
00676     assert(maxdiff.get_etat() == ETATQCQ) ;     
00677     assert(max2.get_etat() == ETATQCQ) ; 
00678 
00679     resu.set_etat_qcq() ; 
00680     for (int l=0; l<nz; l++) {
00681     if ( max2(l) == double(0) ) {
00682         resu.set(l) = maxdiff(l) ; 
00683     }
00684     else{
00685         resu.set(l) = maxdiff(l) / max2(l) ;            
00686     }
00687     }
00688     
00689     return resu ; 
00690     
00691 }
00692 
00693 

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