00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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 "tbl.h"
00066
00067
00068
00069
00070
00071 Tbl sin(const Tbl& ti)
00072 {
00073
00074 assert(ti.get_etat() != ETATNONDEF) ;
00075
00076
00077 if (ti.get_etat() == ETATZERO) {
00078 return ti ;
00079 }
00080
00081
00082 assert(ti.get_etat() == ETATQCQ) ;
00083 Tbl to(ti.dim) ;
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
00094
00095
00096 Tbl cos(const Tbl& ti)
00097 {
00098
00099 assert(ti.get_etat() != ETATNONDEF) ;
00100
00101 Tbl to(ti.dim) ;
00102 to.set_etat_qcq() ;
00103
00104
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
00114 assert(ti.get_etat() == ETATQCQ) ;
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
00124
00125
00126 Tbl tan(const Tbl& ti)
00127 {
00128
00129 assert(ti.get_etat() != ETATNONDEF) ;
00130
00131
00132 if (ti.get_etat() == ETATZERO) {
00133 return ti ;
00134 }
00135
00136
00137 assert(ti.get_etat() == ETATQCQ) ;
00138 Tbl to(ti.dim) ;
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
00149
00150
00151 Tbl asin(const Tbl& ti)
00152 {
00153
00154 assert(ti.get_etat() != ETATNONDEF) ;
00155
00156
00157 if (ti.get_etat() == ETATZERO) {
00158 return ti ;
00159 }
00160
00161
00162 assert(ti.get_etat() == ETATQCQ) ;
00163 Tbl to(ti.dim) ;
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
00174
00175
00176 Tbl acos(const Tbl& ti)
00177 {
00178
00179 assert(ti.get_etat() != ETATNONDEF) ;
00180
00181 Tbl to(ti.dim) ;
00182 to.set_etat_qcq() ;
00183
00184
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
00194 assert(ti.get_etat() == ETATQCQ) ;
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
00204
00205
00206 Tbl atan(const Tbl& ti)
00207 {
00208
00209 assert(ti.get_etat() != ETATNONDEF) ;
00210
00211
00212 if (ti.get_etat() == ETATZERO) {
00213 return ti ;
00214 }
00215
00216
00217 assert(ti.get_etat() == ETATQCQ) ;
00218 Tbl to(ti.dim) ;
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
00229
00230
00231 Tbl sqrt(const Tbl& ti)
00232 {
00233
00234 assert(ti.get_etat() != ETATNONDEF) ;
00235
00236
00237 if (ti.get_etat() == ETATZERO) {
00238 return ti ;
00239 }
00240
00241
00242 assert(ti.get_etat() == ETATQCQ) ;
00243 Tbl to(ti.dim) ;
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
00254
00255
00256 Tbl exp(const Tbl& ti)
00257 {
00258
00259 assert(ti.get_etat() != ETATNONDEF) ;
00260
00261 Tbl to(ti.dim) ;
00262 to.set_etat_qcq() ;
00263
00264
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
00274 assert(ti.get_etat() == ETATQCQ) ;
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
00284
00285
00286 Tbl Heaviside(const Tbl& ti)
00287 {
00288
00289 assert(ti.get_etat() != ETATNONDEF) ;
00290
00291 Tbl to(ti.dim) ;
00292 to.set_etat_qcq() ;
00293
00294
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
00304 assert(ti.get_etat() == ETATQCQ) ;
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
00317
00318
00319 Tbl log(const Tbl& ti)
00320 {
00321
00322 assert(ti.get_etat() != ETATNONDEF) ;
00323
00324
00325 if (ti.get_etat() == ETATZERO) {
00326 cout << "Tbl log: log(ETATZERO) !" << endl ;
00327 abort () ;
00328 }
00329
00330
00331 assert(ti.get_etat() == ETATQCQ) ;
00332 Tbl to(ti.dim) ;
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
00343
00344
00345 Tbl log10(const Tbl& ti)
00346 {
00347
00348 assert(ti.get_etat() != ETATNONDEF) ;
00349
00350
00351 if (ti.get_etat() == ETATZERO) {
00352 cout << "Tbl log10: log10(ETATZERO) !" << endl ;
00353 abort () ;
00354 }
00355
00356
00357 assert(ti.get_etat() == ETATQCQ) ;
00358 Tbl to(ti.dim) ;
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
00369
00370
00371 Tbl pow(const Tbl& ti, int n)
00372 {
00373
00374 assert(ti.get_etat() != ETATNONDEF) ;
00375
00376
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
00388 assert(ti.get_etat() == ETATQCQ) ;
00389 Tbl to(ti.dim) ;
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
00401
00402
00403 Tbl pow(const Tbl& ti, double x)
00404 {
00405
00406 assert(ti.get_etat() != ETATNONDEF) ;
00407
00408
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
00420 assert(ti.get_etat() == ETATQCQ) ;
00421 Tbl to(ti.dim) ;
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
00432
00433
00434 Tbl abs(const Tbl& ti)
00435 {
00436
00437 assert(ti.get_etat() != ETATNONDEF) ;
00438
00439
00440 if (ti.get_etat() == ETATZERO) {
00441 return ti ;
00442 }
00443
00444
00445 assert(ti.get_etat() == ETATQCQ) ;
00446
00447 Tbl to(ti.dim) ;
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
00462
00463
00464 Tbl racine_cubique(const Tbl& ti)
00465 {
00466
00467 assert(ti.get_etat() != ETATNONDEF) ;
00468
00469
00470 if (ti.get_etat() == ETATZERO) {
00471 return ti ;
00472 }
00473
00474
00475 assert(ti.get_etat() == ETATQCQ) ;
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
00489
00490
00491 double max(const Tbl& ti) {
00492
00493
00494 assert(ti.get_etat() != ETATNONDEF) ;
00495
00496
00497 if (ti.get_etat() == ETATZERO) {
00498 return double(0) ;
00499 }
00500
00501
00502 assert(ti.get_etat() == ETATQCQ) ;
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
00515
00516
00517 double min(const Tbl& ti) {
00518
00519
00520 assert(ti.get_etat() != ETATNONDEF) ;
00521
00522
00523 if (ti.get_etat() == ETATZERO) {
00524 return double(0) ;
00525 }
00526
00527
00528 assert(ti.get_etat() == ETATQCQ) ;
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
00541
00542
00543 double norme(const Tbl& ti) {
00544
00545
00546 assert(ti.get_etat() != ETATNONDEF) ;
00547
00548 double resu = 0 ;
00549
00550 if (ti.get_etat() != ETATZERO) {
00551
00552 assert(ti.get_etat() == ETATQCQ) ;
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
00565
00566
00567 double diffrel(const Tbl& t1, const Tbl& t2) {
00568
00569
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
00589
00590
00591 double diffrelmax(const Tbl& t1, const Tbl& t2) {
00592
00593
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 }