itbl_math.C

00001 /*
00002  *   Copyright (c) 1999-2001 Philippe Grandclement
00003  *
00004  *   This file is part of LORENE.
00005  *
00006  *   LORENE is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   LORENE is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with LORENE; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 
00023 char itbl_math_C[] = "$Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $" ;
00024 
00025 /*
00026  * $Id: itbl_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $
00027  * $Log: itbl_math.C,v $
00028  * Revision 1.1.1.1  2001/11/20 15:19:27  e_gourgoulhon
00029  * LORENE
00030  *
00031  * Revision 2.3  1999/11/25  13:02:12  phil
00032  * *** empty log message ***
00033  *
00034  * Revision 2.2  1999/11/25  12:42:12  phil
00035  * conversion double->int
00036  *
00037  * Revision 2.1  1999/11/24  09:32:01  eric
00038  * fabs -> abs
00039  *
00040  * Revision 2.0  1999/11/17  16:04:44  phil
00041  * *** empty log message ***
00042  *
00043  *
00044  * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_math.C,v 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon Exp $
00045  *
00046  */
00047  
00048 /*
00049  * Surcharge des
00050  * Fonctions mathematiques applicables aux classes de type
00051  * 
00052  *  Itbl
00053  *
00054  * Typiquement: max, norme ...
00055  *
00056  */
00057  
00058 // Headers C
00059 // ---------
00060 #include <math.h>
00061 #include <stdlib.h>
00062 
00063 // Headers Lorene
00064 // --------------
00065 #include "itbl.h"
00066 
00067 
00068                 //----------------//
00069                 // Absolute value //
00070                 //----------------//
00071 
00072 Itbl abs(const Itbl& ti)
00073 {
00074     // Protection
00075     assert(ti.get_etat() != ETATNONDEF) ;
00076     
00077     // Cas ETATZERO
00078     if (ti.get_etat() == ETATZERO) {
00079     return ti ;
00080     }
00081     
00082     // Cas general
00083     assert(ti.get_etat() == ETATQCQ) ;  // sinon...
00084 
00085     Itbl to(ti.dim) ;           // Itbl resultat
00086     to.set_etat_qcq() ;
00087 
00088     const int* xi = ti.t ; 
00089     int* xo = to.t ; 
00090     int taille = ti.get_taille() ;
00091 
00092     for (int i=0 ; i<taille ; i++) {
00093     xo[i] = abs( xi[i] ) ;
00094     }
00095     
00096     return to ;
00097 }
00098 
00099             //-------------------------------//
00100                     //            max                //
00101             //-------------------------------//
00102 
00103 int max(const Itbl& ti) {
00104     
00105     // Protection
00106     assert(ti.get_etat() != ETATNONDEF) ;
00107     
00108     // Cas particulier
00109     if (ti.get_etat() == ETATZERO) {
00110         return 0 ;
00111     }
00112     
00113     // Cas general
00114     assert(ti.get_etat() == ETATQCQ) ;     // sinon....
00115     
00116     const int* x = ti.t ; 
00117     int resu = x[0] ;
00118     for (int i=1; i<ti.get_taille(); i++) {
00119         if ( x[i] > resu ) resu = x[i] ;
00120     }
00121     
00122     return resu ; 
00123 }
00124 
00125             //-------------------------------//
00126                     //            min                //
00127             //-------------------------------//
00128 
00129 int min(const Itbl& ti) {
00130     
00131     // Protection
00132     assert(ti.get_etat() != ETATNONDEF) ;
00133     
00134     // Cas particulier
00135     if (ti.get_etat() == ETATZERO) {
00136         return 0 ;
00137     }
00138     
00139     // Cas general
00140     assert(ti.get_etat() == ETATQCQ) ;     // sinon....
00141     
00142     const int* x = ti.t ; 
00143     int resu = x[0] ;
00144     for (int i=1; i<ti.get_taille(); i++) {
00145         if ( x[i] < resu ) resu = x[i] ;
00146     }
00147     
00148     return resu ; 
00149 }
00150 
00151             //-------------------------------//
00152                     //            norme              //
00153             //-------------------------------//
00154 
00155 int norme(const Itbl& ti) {
00156 
00157     // Protection
00158     assert(ti.get_etat() != ETATNONDEF) ;
00159     
00160     int resu = 0 ; 
00161     
00162     if (ti.get_etat() != ETATZERO) {  // on n'effectue la somme que si necessaire
00163 
00164     assert(ti.get_etat() == ETATQCQ) ;     // sinon....
00165     const int* x = ti.t ; 
00166     for (int i=0; i<ti.get_taille(); i++) {
00167         resu += abs( x[i] ) ;
00168     }
00169 
00170     }
00171 
00172     return resu ;
00173 }
00174 
00175             //-------------------------------//
00176                     //          diffrel              //
00177             //-------------------------------//
00178 
00179 double diffrel(const Itbl& t1, const Itbl& t2) {
00180     
00181     // Protections
00182     assert(t1.get_etat() != ETATNONDEF) ;
00183     assert(t2.get_etat() != ETATNONDEF) ;
00184     
00185     int norm2 = norme(t2) ;
00186     int normdiff = norme(t1-t2) ;
00187     double resu ;  
00188     if ( norm2 == 0 ) {
00189     resu = double(normdiff) ; 
00190     }
00191     else {
00192     resu =  double(normdiff) / double(norm2) ; 
00193     }
00194     
00195     return resu ; 
00196     
00197 }
00198 
00199             //-------------------------------//
00200                     //       diffrelmax              //
00201             //-------------------------------//
00202 
00203 double diffrelmax(const Itbl& t1, const Itbl& t2) {
00204     
00205     // Protections
00206     assert(t1.get_etat() != ETATNONDEF) ;
00207     assert(t2.get_etat() != ETATNONDEF) ;
00208     
00209     int max2 = max(abs(t2)) ;
00210     int maxdiff = max(abs(t1-t2)) ;
00211     double resu ;  
00212     if ( max2 == 0 ) {
00213     resu = double(maxdiff) ; 
00214     }
00215     else {
00216     resu =  double(maxdiff) / double(max2) ; 
00217     }
00218     
00219     return resu ; 
00220     
00221 }

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