tbl_math.C

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

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