00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include <math.h>
00061 #include <stdlib.h>
00062
00063
00064
00065 #include "itbl.h"
00066
00067
00068
00069
00070
00071
00072 Itbl abs(const Itbl& ti)
00073 {
00074
00075 assert(ti.get_etat() != ETATNONDEF) ;
00076
00077
00078 if (ti.get_etat() == ETATZERO) {
00079 return ti ;
00080 }
00081
00082
00083 assert(ti.get_etat() == ETATQCQ) ;
00084
00085 Itbl to(ti.dim) ;
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
00101
00102
00103 int max(const Itbl& ti) {
00104
00105
00106 assert(ti.get_etat() != ETATNONDEF) ;
00107
00108
00109 if (ti.get_etat() == ETATZERO) {
00110 return 0 ;
00111 }
00112
00113
00114 assert(ti.get_etat() == ETATQCQ) ;
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
00127
00128
00129 int min(const Itbl& ti) {
00130
00131
00132 assert(ti.get_etat() != ETATNONDEF) ;
00133
00134
00135 if (ti.get_etat() == ETATZERO) {
00136 return 0 ;
00137 }
00138
00139
00140 assert(ti.get_etat() == ETATQCQ) ;
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
00153
00154
00155 int norme(const Itbl& ti) {
00156
00157
00158 assert(ti.get_etat() != ETATNONDEF) ;
00159
00160 int resu = 0 ;
00161
00162 if (ti.get_etat() != ETATZERO) {
00163
00164 assert(ti.get_etat() == ETATQCQ) ;
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
00177
00178
00179 double diffrel(const Itbl& t1, const Itbl& t2) {
00180
00181
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
00201
00202
00203 double diffrelmax(const Itbl& t1, const Itbl& t2) {
00204
00205
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 }