00001 #include <eh_utils.h>
00002 #include <check.h>
00003
00004 START_TEST ( test_nan )
00005 {
00006 double x;
00007
00008 x = eh_nan();
00009 fail_unless( isnan(x) , "eh_nan does not produce a NaN" );
00010 fail_unless( eh_isnan(x) , "Undetected NaN by eh_isnan" );
00011
00012
00013
00014
00015 x = sqrt(-1);
00016 fail_unless( eh_isnan(x) , "eh_isnan does not detect NaN" );
00017
00018 }
00019 END_TEST
00020
00021 START_TEST ( test_interpolate )
00022 {
00023 double x[5] = {1,2,3,4,5}, y[5]={.1,.2,.3,.4,.5};
00024 double xi, yi;
00025
00026 xi = -1;
00027 interpolate( x,y,5,&xi,&yi,1 );
00028 fail_unless( eh_isnan(yi) , "xi < x[0] should produce NaN" );
00029
00030 xi = 6;
00031 interpolate( x,y,5,&xi,&yi,1 );
00032 fail_unless( eh_isnan(yi) , "xi > x[end] should produce NaN" );
00033
00034 xi = 3;
00035 interpolate( x,y,5,&xi,&yi,1 );
00036 fail_unless( fabs(yi-.3)<1e-12 , "Incorrect interpolation" );
00037
00038 xi = 1;
00039 interpolate( x,y,1,&xi,&yi,1 );
00040 fail_unless( fabs(yi-.1)<1e-12 , "Incorrect interpolation for len==1" );
00041
00042 xi = .5;
00043 interpolate( x,y,1,&xi,&yi,1 );
00044 fail_unless( eh_isnan(yi) , "xi < x[0] should produce NaN for len==1" );
00045
00046 xi = 1.5;
00047 interpolate( x,y,1,&xi,&yi,1 );
00048 fail_unless( eh_isnan(yi) , "xi > x[end] should produce NaN for len==1" );
00049 }
00050 END_TEST
00051
00052 START_TEST ( test_poly_interpolate )
00053 {
00054 {
00055 double* x = eh_new( double , 2 );
00056 double* y = eh_new( double , 2 );
00057 double y_int, err;
00058
00059 x[0] = 0;
00060 x[1] = 1;
00061 y[0] = 3;
00062 y[1] = 5;
00063
00064 y_int = poly_interpolate( x , y , 2 , .75 , &err );
00065
00066 fail_unless( fabs(y_int-4.5)<1e-6 , "Linear interpolation failed" );
00067
00068
00069 eh_free( x );
00070 eh_free( y );
00071 }
00072
00073 {
00074 double* x = eh_new( double , 3 );
00075 double* y = eh_new( double , 3 );
00076 double y_int, err;
00077
00078 x[0] = -1;
00079 x[1] = 0;
00080 x[2] = 1;
00081 y[0] = pow(x[0],2);
00082 y[1] = pow(x[1],2);
00083 y[2] = pow(x[2],2);
00084
00085 y_int = poly_interpolate( x , y , 3 , .75 , &err );
00086
00087 fail_unless( fabs(y_int-pow(.75,2))<1e-6 , "Quadratic interpolation failed" );
00088
00089
00090 eh_free( x );
00091 eh_free( y );
00092 }
00093
00094 }
00095 END_TEST
00096
00097 double linear_f( double x , gpointer data )
00098 {
00099 return 2*x + 1;
00100 }
00101
00102 double exp_f( double x , gpointer data )
00103 {
00104 return exp(x);
00105 }
00106
00107 typedef struct
00108 {
00109 double c;
00110 double l;
00111 double y;
00112 }
00113 User_data;
00114
00115 #define M1 (1.2)
00116 #define M2 (0.6)
00117 #define P1 (0.8)
00118 #define F2 (0.9)
00119 #define XA (5.176)
00120
00121 #if HAVE_IEEEFP_H
00122 # include <ieeefp.h>
00123 #else
00124 int finite( double dsrc );
00125 #endif
00126
00127 double inventory( double x , double s , double l )
00128 {
00129 double i;
00130 double g = exp( pow(M2*s,2.) )*F2;
00131
00132 if ( !finite( g ) )
00133 i = 0;
00134 else
00135 i = exp( -pow(M1*s,2.) )
00136 * pow(XA/x,P1)
00137 * exp( -2*l/(3.*sqrt(XA))*( pow(x,1.5) - pow(XA,1.5) )*g );
00138 return i;
00139 }
00140
00141 double inventory_f( double x , gpointer data )
00142 {
00143 return inventory( ((User_data*)data)->y , ((User_data*)data)->c*x , ((User_data*)data)->l );
00144 }
00145
00146 START_TEST ( test_trapazoid )
00147 {
00148 gssize i;
00149 double sum;
00150
00151 sum = trapzd( linear_f , 0 , 1 , 1 , NULL );
00152 fail_unless( fabs(sum-2)<1e-6 , "Integration failed for n=1" );
00153
00154 for ( i=1 ; i<=5 ; i++ )
00155 sum = trapzd( linear_f , 0 , 1 , i , NULL );
00156 fail_unless( fabs(sum-2)<1e-6 , "Integration failed for n=5" );
00157
00158 for ( i=1 ; i<=9 ; i++ )
00159 sum = trapzd( exp_f , 0 , 1 , i , NULL );
00160 fail_unless( fabs(sum-(exp(1)-exp(0)))<1e-4 , "Integration of EXP failed for trapzd" );
00161
00162 sum = qtrap( exp_f , 0 , 1 , NULL );
00163 fail_unless( fabs(sum-(exp(1)-exp(0)))<1e-4 , "Integration of EXP failed for qtrap" );
00164
00165 }
00166 END_TEST
00167
00168 START_TEST ( test_integrate )
00169 {
00170 double sum;
00171
00172 sum = eh_integrate( linear_f , 0 , 1 );
00173 fail_unless( fabs(sum-2)<1e-4 , "Integration failed for integrate" );
00174
00175 sum = eh_integrate( exp_f , 0 , 1 );
00176 fail_unless( fabs(sum-(exp(1)-exp(0)))<1e-4 , "Integration of EXP failed for integrate" );
00177
00178 {
00179 User_data data;
00180 gssize i;
00181 double total=0;
00182 double dx = XA*.1;
00183
00184 for ( i=0 ; i<40 ; i++ )
00185 {
00186 data.l = ( 50 / 86400. )*1000. / 2.;
00187 data.y = XA + i*dx;
00188 data.c = 1./( sqrt(2)*.109)*data.y;
00189 sum = eh_integrate_with_data( inventory_f , 0 , 20 , &data );
00190 total += 2.*sum*dx;
00191 }
00192 }
00193 }
00194 END_TEST
00195
00196 START_TEST ( test_compare_dbl )
00197 {
00198 fail_unless( eh_compare_dbl(1,1,0) , "" );
00199 fail_unless( eh_compare_dbl(1,1-1e-10,1e-6) , "" );
00200 fail_unless( !eh_compare_dbl(1,1-1e-10,1e-12) , "" );
00201 fail_unless( eh_compare_dbl(1,1-.99e-10,1e-10) , "" );
00202 }
00203 END_TEST
00204
00205 START_TEST ( test_round )
00206 {
00207 double x;
00208
00209 x = eh_round( 23.1 , 1 );
00210 fail_unless( eh_compare_dbl( x , 23 , 1e-12 ) , "Round down" );
00211
00212 x = eh_round( 23.9 , 1 );
00213 fail_unless( eh_compare_dbl( x , 24 , 1e-12 ) , "Round up" );
00214
00215 x = eh_round( 23.5 , 1 );
00216 fail_unless( eh_compare_dbl( x , 24 , 1e-12 ) , "Round up" );
00217
00218 x = eh_round( 23. , 1 );
00219 fail_unless( eh_compare_dbl( x , 23 , 1e-12 ) , "Already rounded" );
00220
00221 x = eh_round( -1945.1 , 1 );
00222 fail_unless( eh_compare_dbl( x , -1945 , 1e-12 ) , "Round up" );
00223
00224 x = eh_round( -1945.9 , 1 );
00225 fail_unless( eh_compare_dbl( x , -1946 , 1e-12 ) , "Round down" );
00226
00227 x = eh_round( 0 , 1 );
00228 fail_unless( eh_compare_dbl( x , 0 , 1e-12 ) , "Already rounded" );
00229
00230 x = eh_round( 1973 , 10 );
00231 fail_unless( eh_compare_dbl( x , 1970 , 1e-12 ) , "Round down to nearest 10" );
00232
00233 x = eh_round( 1973 , 5 );
00234 fail_unless( eh_compare_dbl( x , 1975 , 1e-12 ) , "Round up to nearest 5" );
00235
00236 x = eh_round( G_PI , .01 );
00237 fail_unless( eh_compare_dbl( x , 3.14 , 1e-12 ) , "Round down to nearest .01" );
00238 }
00239 END_TEST
00240
00241 START_TEST ( test_reduce_angle )
00242 {
00243 double a;
00244
00245 a = eh_reduce_angle(G_PI + G_PI*4 );
00246 fail_unless( eh_compare_dbl( a , -G_PI , 1e-12 ) , "New angle is >= -PI and < PI" );
00247
00248 a = eh_reduce_angle(G_PI-1e-14 + G_PI*4 );
00249 fail_unless( eh_compare_dbl( a , G_PI , 1e-12 ) , "New angle is >= -PI and < PI" );
00250
00251 a = eh_reduce_angle(-G_PI - G_PI*4 );
00252 fail_unless( eh_compare_dbl( a , -G_PI , 1e-12 ) , "New angle is between -PI and PI" );
00253
00254 a = eh_reduce_angle(G_PI/4 + G_PI*22 );
00255 fail_unless( eh_compare_dbl( a , G_PI/4 , 1e-12 ) , "New angle is between -PI and PI" );
00256
00257 a = eh_reduce_angle( 0 );
00258 fail_unless( eh_compare_dbl( a , 0 , 1e-12 ) , "New angle is between -PI and PI" );
00259
00260 }
00261 END_TEST
00262
00263 START_TEST ( test_is_even )
00264 {
00265 fail_unless( eh_is_even(2) , "2 is an even number" );
00266 fail_unless( !eh_is_even(1) , "1 is not an even number" );
00267 fail_unless( eh_is_even(0) , "0 is an even number" );
00268 fail_unless( !eh_is_even(G_MAXINT32) , "MAXINR is not an even number" );
00269 fail_unless( !eh_is_even(-17) , "Ignore negative sign" );
00270 fail_unless( eh_is_even(-6) , "Ignore negative sign" );
00271 }
00272 END_TEST
00273
00274 START_TEST ( test_diff )
00275 {
00276 gssize i;
00277 gssize len = 100;
00278 double *f = eh_new( double , len );
00279 double *fx, *fxx, *fxxx, *fxxxx;
00280 double *x = eh_new( double , len );
00281
00282 for ( i=0 ; i<len ; i++ )
00283 {
00284 x[i] = i;
00285 f[i] = .1*pow( x[i] , 4 );
00286 }
00287
00288 fx = eh_dbl_array_diff( NULL , f , len , 1 );
00289 fxx = eh_dbl_array_diff( NULL , f , len , 2 );
00290 fxxx = eh_dbl_array_diff( NULL , f , len , 3 );
00291 fxxxx = eh_dbl_array_diff( NULL , f , len , 4 );
00292
00293 fail_unless( fx!=NULL , "A valid array should be returned" );
00294 fail_unless( fxx!=NULL , "A valid array should be returned" );
00295 fail_unless( fxxx!=NULL , "A valid array should be returned" );
00296 fail_unless( fxxxx!=NULL , "A valid array should be returned" );
00297
00298 for ( i=0 ; i<len-1 ; i++ )
00299 fail_unless( eh_compare_dbl( fx[i] , f[i+1]-f[i] , 1e-12 ) , "First derivative calculated incorrectly" );
00300
00301 for ( i=1 ; i<len-1 ; i++ )
00302 fail_unless( eh_compare_dbl( fxx[i] , f[i+1]-2*f[i]+f[i-1] , 1e-12 ) , "Second derivative calculated incorrectly" );
00303
00304 for ( i=1 ; i<len-2 ; i++ )
00305 fail_unless( eh_compare_dbl( fxxx[i] , f[i+2]+3*f[i]-3*f[i+1]-f[i-1] , 1e-12 ) , "Third derivative calculated incorrectly" );
00306
00307 for ( i=2 ; i<len-2 ; i++ )
00308 fail_unless( eh_compare_dbl( fxxxx[i] , f[i+2]+6*f[i]+f[i-2]-4*f[i+1]-4*f[i-1] , 1e-12 ) , "Fourth derivative calculated incorrectly" );
00309
00310 fail_unless( eh_compare_dbl(fx[len-1],fx[len-1],1e-12) , "First difference not calculated at n=N-1" );
00311
00312 fail_unless( eh_compare_dbl(fxx[0],fxx[1],1e-12) , "Second difference not calculated at n=0" );
00313 fail_unless( eh_compare_dbl(fxx[len-1],fxx[len-2],1e-12) , "Second difference not calculated at n=0" );
00314
00315 eh_free( fx );
00316 eh_free( fxx );
00317 eh_free( fxxx );
00318 eh_free( fxxxx );
00319 eh_free( f );
00320 eh_free( x );
00321 }
00322 END_TEST
00323
00324 START_TEST ( test_binomial_coef )
00325 {
00326 double c;
00327
00328 c = eh_binomial_coef( 1 , 0 );
00329 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00330
00331 c = eh_binomial_coef( 1 , 1 );
00332 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00333
00334 c = eh_binomial_coef( 2 , 0 );
00335 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00336
00337 c = eh_binomial_coef( 2 , 1 );
00338 fail_unless( eh_compare_dbl( c , 2 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00339
00340 c = eh_binomial_coef( 2 , 2 );
00341 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00342
00343 c = eh_binomial_coef( 3 , 0 );
00344 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00345
00346 c = eh_binomial_coef( 3 , 1 );
00347 fail_unless( eh_compare_dbl( c , 3 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00348
00349 c = eh_binomial_coef( 3 , 2 );
00350 fail_unless( eh_compare_dbl( c , 3 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00351
00352 c = eh_binomial_coef( 3 , 3 );
00353 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00354
00355 c = eh_binomial_coef( 4 , 0 );
00356 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00357
00358 c = eh_binomial_coef( 4 , 1 );
00359 fail_unless( eh_compare_dbl( c , 4 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00360
00361 c = eh_binomial_coef( 4 , 2 );
00362 fail_unless( eh_compare_dbl( c , 6 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00363
00364 c = eh_binomial_coef( 4 , 3 );
00365 fail_unless( eh_compare_dbl( c , 4 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00366
00367 c = eh_binomial_coef( 4 , 4 );
00368 fail_unless( eh_compare_dbl( c , 1 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00369
00370 c = eh_binomial_coef( 52 , 5 );
00371 fail_unless( eh_compare_dbl( c , 2598960 , 1e-12 ) , "Binomial coefficient calculated incorrectly" );
00372
00373 }
00374 END_TEST
00375
00376 START_TEST ( test_factorial )
00377 {
00378 double x;
00379 double prod;
00380
00381 x = eh_factorial( 0 );
00382 fail_unless( eh_compare_dbl(x,1,1e-12) , "Factorial of 0 is 1" );
00383
00384
00385 {
00386 gssize i;
00387 for ( i=1,prod=1 ; i<32 ; i++ )
00388 {
00389 prod *= i;
00390 x = eh_factorial( i );
00391 fail_unless( eh_compare_dbl(x,prod,1e-12) , "Factorial calculated incorrectly" );
00392 }
00393 }
00394
00395 x = eh_factorial( -1 );
00396 fail_unless( eh_isnan(x) , "Factorial of negative value is NaN" );
00397 }
00398 END_TEST
00399
00400 START_TEST ( test_gamma_p )
00401 {
00402 double x;
00403
00404 x = eh_gamma_p( g_random_double() , 0 );
00405 fail_unless( eh_compare_dbl( x , 0 , 1e-12 ) , "" );
00406
00407 x = eh_gamma_p( g_random_double() , G_MAXDOUBLE );
00408 fail_unless( eh_compare_dbl( x , 1 , 1e-12 ) , "" );
00409
00410 x = eh_gamma_p( 2. , 1. );
00411 fail_unless( eh_compare_dbl( x , 0.26424111765712 , 1e-6 ) , "" );
00412
00413 }
00414 END_TEST
00415
00416 START_TEST ( test_gamma_q )
00417 {
00418 double x;
00419
00420 x = eh_gamma_q( g_random_double() , 0 );
00421 fail_unless( eh_compare_dbl( x , 1 , 1e-12 ) , "" );
00422
00423 x = eh_gamma_q( g_random_double() , G_MAXDOUBLE );
00424 fail_unless( eh_compare_dbl( x , 0 , 1e-12 ) , "" );
00425
00426 x = eh_gamma_q( 2. , 1. );
00427 fail_unless( eh_compare_dbl( x , 1.-0.26424111765712 , 1e-6 ) , "" );
00428
00429 }
00430 END_TEST
00431
00432 START_TEST ( test_gamma_series )
00433 {
00434 double a = g_random_double();
00435 double p;
00436 double gam_log;
00437
00438 eh_gamma_series( &p , a , 0 , &gam_log );
00439 fail_unless( eh_compare_dbl( p , 0 , 1e-12 ) , "" );
00440 fail_unless( eh_compare_dbl( gam_log , eh_gamma_log(a) , 1e-12 ) , "" );
00441
00442 eh_gamma_series( &p , 2. , 1. , &gam_log );
00443 fail_unless( eh_compare_dbl( p , 0.26424111765712 , 1e-6 ) , "" );
00444
00445 eh_gamma_series( &p , .5 , .75 , &gam_log );
00446 fail_unless( eh_compare_dbl( p , 0.77932863808015 , 1e-6 ) , "" );
00447 fail_unless( eh_compare_dbl( gam_log , 0.57236494292470 , 1e-6 ) , "" );
00448
00449 eh_gamma_series( &p , 1 , 2 , &gam_log );
00450 fail_unless( eh_compare_dbl( p , 0.86466471676339 , 1e-6 ) , "" );
00451 fail_unless( eh_compare_dbl( gam_log , 0 , 1e-6 ) , "" );
00452 }
00453 END_TEST
00454
00455 START_TEST ( test_gamma_cf )
00456 {
00457 double a = g_random_double();
00458 double q;
00459 double gam_log;
00460
00461 eh_gamma_cf( &q , a , G_MAXDOUBLE , &gam_log );
00462 fail_unless( eh_compare_dbl( q , 0 , 1e-12 ) , "" );
00463 fail_unless( eh_compare_dbl( gam_log , eh_gamma_log(a) , 1e-12 ) , "" );
00464
00465 eh_gamma_cf( &q , .25 , 2. , &gam_log );
00466 fail_unless( eh_compare_dbl( q , 1.-0.98271398814048 , 1e-6 ) , "" );
00467 fail_unless( eh_compare_dbl( gam_log , 1.28802252469808 , 1e-6 ) , "" );
00468
00469 eh_gamma_cf( &q , 1 , 2 , &gam_log );
00470 fail_unless( eh_compare_dbl( q , 1-0.86466471676339 , 1e-6 ) , "" );
00471 fail_unless( eh_compare_dbl( gam_log , 0 , 1e-6 ) , "" );
00472 }
00473 END_TEST
00474
00475 START_TEST ( test_gamma_log )
00476 {
00477 double x;
00478
00479 x = eh_gamma_log( 1 );
00480 fail_unless( eh_compare_dbl( x , 0 , 1e-12 ) , "" );
00481
00482 x = eh_gamma_log( .5 );
00483 fail_unless( eh_compare_dbl( x , 0.57236494292470 , 1e-6 ) , "" );
00484
00485 x = eh_gamma_log( .1 );
00486 fail_unless( eh_compare_dbl( x , 2.25271265173421 , 1e-6 ) , "" );
00487
00488 }
00489 END_TEST
00490
00491 START_TEST ( test_linear_fit )
00492 {
00493 {
00494 double x[2] = { 0 , 1 };
00495 double y[2] = { 1 , 4 };
00496 double* p = eh_linear_fit( x , y , 2 );
00497
00498 fail_unless( p!=NULL , "" );
00499 fail_unless( eh_compare_dbl( p[0] , 1 , 1e-12 ) , "" );
00500 fail_unless( eh_compare_dbl( p[1] , 3 , 1e-12 ) , "" );
00501
00502 eh_free( p );
00503 }
00504
00505 {
00506 double x[1] = { 0 };
00507 double y[1] = { 1 };
00508 double* p = eh_linear_fit( x , y , 1 );
00509
00510 fail_unless( p==NULL , "" );
00511 }
00512
00513 {
00514 double y[3];
00515 double* p = eh_linear_fit( NULL , y , 3 );
00516
00517 fail_unless( p==NULL , "" );
00518 }
00519
00520 {
00521 double x[3];
00522 double* p = eh_linear_fit( x , NULL , 3 );
00523
00524 fail_unless( p==NULL , "" );
00525 }
00526
00527 {
00528 double x[3] = { 0 , .5 , 1 };
00529 double y[3] = { 1 , 2.5 , 4 };
00530 double* p = eh_linear_fit( x , y , 3 );
00531
00532 fail_unless( p!=NULL , "" );
00533 fail_unless( eh_compare_dbl( p[0] , 1 , 1e-12 ) , "" );
00534 fail_unless( eh_compare_dbl( p[1] , 3 , 1e-12 ) , "" );
00535
00536 eh_free( p );
00537 }
00538
00539 {
00540 double x[3] = { 1 , 2 , 3 };
00541 double y[3] = { 3 , 7 , 8 };
00542 double* p = eh_linear_fit( x , y , 3 );
00543
00544 fail_unless( p!=NULL , "" );
00545 fail_unless( eh_compare_dbl( p[0] , 1 , 1e-12 ) , "" );
00546 fail_unless( eh_compare_dbl( p[1] , 2.5 , 1e-12 ) , "" );
00547
00548 eh_free( p );
00549 }
00550
00551 {
00552 gssize i;
00553 double* p;
00554 double* x = eh_new( double , 100 );
00555 double* y = eh_new( double , 100 );
00556
00557 for ( i=0 ; i<100 ; i++ )
00558 {
00559 x[i] = i/100.;
00560 y[i] = exp(-x[i]);
00561 }
00562
00563 p = eh_linear_fit( x , y , 100 );
00564
00565 fail_unless( p!=NULL , "" );
00566 fail_unless( eh_compare_dbl( p[0] , 0.94463333233568 , 1e-12 ) , "" );
00567 fail_unless( eh_compare_dbl( p[1] , -0.62494323848573 , 1e-12 ) , "" );
00568
00569 eh_free( p );
00570 eh_free( y );
00571 eh_free( x );
00572 }
00573 }
00574 END_TEST
00575
00576 START_TEST ( test_poly_fit )
00577 {
00578 {
00579 double x[2] = { 0 , 1 };
00580 double y[2] = { 1 , 4 };
00581 double* p = eh_poly_fit ( x , y , 2 , 1 );
00582 double* p_lin = eh_linear_fit( x , y , 2 );
00583
00584 fail_unless( p!=NULL , "" );
00585 fail_unless( eh_compare_dbl( p[0] , p_lin[0] , 1e-12 ) , "" );
00586 fail_unless( eh_compare_dbl( p[1] , p_lin[1] , 1e-12 ) , "" );
00587
00588 eh_free( p );
00589 eh_free( p_lin );
00590 }
00591
00592 {
00593 gssize i;
00594 double* p;
00595 double* p_lin;
00596 double* x = eh_new( double , 100 );
00597 double* y = eh_new( double , 100 );
00598
00599 for ( i=0 ; i<100 ; i++ )
00600 {
00601 x[i] = i/100.;
00602 y[i] = exp(-x[i]);
00603 }
00604
00605 p = eh_poly_fit ( x , y , 100 , 1 );
00606 p_lin = eh_linear_fit( x , y , 100 );
00607
00608 fail_unless( p!=NULL , "" );
00609 fail_unless( eh_compare_dbl( p[0] , p_lin[0] , 1e-12 ) , "" );
00610 fail_unless( eh_compare_dbl( p[1] , p_lin[1] , 1e-12 ) , "" );
00611
00612 eh_free( p_lin );
00613 eh_free( p );
00614 eh_free( y );
00615 eh_free( x );
00616 }
00617
00618 {
00619 double x[3] = {0,1,2};
00620 double y[3] = {2,1,-8};
00621 double* p = eh_poly_fit( x , y , 3 , 2 );
00622
00623 fail_unless( p!=NULL , "" );
00624 fail_unless( eh_compare_dbl( p[0] , 2 , 1e-12 ) , "" );
00625 fail_unless( eh_compare_dbl( p[1] , 3 , 1e-12 ) , "" );
00626 fail_unless( eh_compare_dbl( p[2] , -4 , 1e-12 ) , "" );
00627
00628 eh_free( p );
00629 }
00630
00631 {
00632 gssize i;
00633 double* p;
00634 double* x = eh_new( double , 100 );
00635 double* y = eh_new( double , 100 );
00636
00637 for ( i=0 ; i<100 ; i++ )
00638 {
00639 x[i] = i/100.;
00640 y[i] = exp(-x[i]);
00641 }
00642
00643 p = eh_poly_fit ( x , y , 100 , 2 );
00644
00645 fail_unless( p!=NULL , "" );
00646 fail_unless( eh_compare_dbl( p[0] , 0.99480295546506 , 1e-12 ) , "" );
00647 fail_unless( eh_compare_dbl( p[1] , -0.93210419642070 , 1e-12 ) , "" );
00648 fail_unless( eh_compare_dbl( p[2] , 0.31026359387371 , 1e-12 ) , "" );
00649
00650 eh_free( p );
00651 eh_free( y );
00652 eh_free( x );
00653 }
00654
00655 {
00656 double x[2] = { 0 , 1 };
00657 double y[2] = { 1 , 4 };
00658 double* p = eh_poly_fit( x , y , 2 , 5 );
00659
00660 fail_unless( p==NULL , "" );
00661 }
00662
00663 }
00664 END_TEST
00665
00666 START_TEST ( test_r_squared )
00667 {
00668 {
00669 double x[3] = {0,1,2};
00670 double y[3] = {2,1,-8};
00671 double* p = eh_poly_fit( x , y , 3 , 2 );
00672 double r_sq = eh_poly_r_squared( x , y , 3 , p , 2 );
00673
00674 fail_unless( eh_compare_dbl( r_sq , 1. , 1e-12 ) , "" );
00675
00676 eh_free( p );
00677 }
00678 }
00679 END_TEST
00680
00681 START_TEST ( test_running_mean )
00682 {
00683 {
00684 double x[5] = { 1,1,2,3,5 };
00685
00686 eh_dbl_array_running_mean( x , 5 , 2 , 2 );
00687
00688 fail_unless( eh_compare_dbl( x[2] , 12./5. , 1e-12 ) , "" );
00689 }
00690
00691 {
00692 double x[5] = { 1,1,2,3,5 };
00693
00694 eh_dbl_array_running_mean( x , 5 , 0 , 4 );
00695
00696 fail_unless( eh_compare_dbl( x[0] , 12./5. , 1e-12 ) , "" );
00697 }
00698
00699 {
00700 double x[5] = { 1,1,2,3,5 };
00701
00702 eh_dbl_array_running_mean( x , 5 , 4 , 0 );
00703
00704 fail_unless( eh_compare_dbl( x[4] , 12./5. , 1e-12 ) , "" );
00705 }
00706
00707 {
00708 double x[5] = { 1,1,2,3,5 };
00709 double y[5] = { 1,1,2,3,5 };
00710
00711 eh_dbl_array_running_mean( x , 5 , 0 , 0 );
00712
00713 fail_unless( eh_dbl_array_compare(x,y,5,1e-12) , "" );
00714 }
00715
00716 {
00717 double x[5] = { 1,1 ,2,3 ,5 };
00718 double y[5] = { 0,4./3.,2,10./3.,0 };
00719
00720 eh_dbl_array_running_mean( x , 5 , 1 , 1 );
00721
00722 fail_unless( eh_dbl_array_compare(x+1,y+1,3,1e-12) , "" );
00723 }
00724 }
00725 END_TEST
00726
00727 START_TEST ( test_rebin )
00728 {
00729 double* s = eh_dbl_array_new( 100 );
00730 double* d;
00731 gint len;
00732
00733 eh_dbl_array_set( s , 100 , 1. );
00734 d = eh_dbl_array_rebin( s , 100 , 2. , &len );
00735
00736 fail_unless( d!=NULL );
00737 fail_unless( len==50 );
00738 fail_unless( eh_compare_dbl(eh_dbl_array_sum(d,len),100,1e-12) );
00739
00740 eh_free( d );
00741
00742 d = eh_dbl_array_rebin( s , 100 , 1.25 , &len );
00743
00744 fail_unless( d!=NULL );
00745 fail_unless( len==80 );
00746 fail_unless( eh_compare_dbl(eh_dbl_array_sum(d,len),100,1e-12) );
00747
00748 eh_free( d );
00749
00750 d = eh_dbl_array_rebin( s , 100 , sqrt(2) , &len );
00751
00752 fail_unless( d!=NULL );
00753 fail_unless( len==71 );
00754 fail_unless( eh_compare_dbl(eh_dbl_array_sum(d,len),100,1e-12) );
00755
00756 eh_free( d );
00757
00758 d = eh_dbl_array_rebin( s , 100 , 1. , &len );
00759
00760 fail_unless( d!=NULL );
00761 fail_if ( d==s );
00762 fail_unless( len==100 );
00763 fail_unless( eh_dbl_array_compare(s,d,100,1e-12) );
00764
00765 d = eh_dbl_array_rebin( s , 100 , 1. , NULL );
00766
00767 fail_unless( d!=NULL );
00768 fail_unless( eh_dbl_array_compare(s,d,100,1e-12) );
00769
00770 eh_free( d );
00771 eh_free( s );
00772
00773 s = eh_linspace( 1 , 100 , 100 );
00774 d = eh_dbl_array_rebin( s , 100 , G_PI , &len );
00775
00776 fail_unless( d!=NULL );
00777 fail_unless( eh_compare_dbl( eh_dbl_array_sum(s,100) , eh_dbl_array_sum(d,len) , 1e-12 ) );
00778
00779 eh_free( d );
00780
00781 eh_dbl_array_set( s , 100 , 1. );
00782 d = eh_dbl_array_rebin( s , 100 , .5*sqrt(2.) , &len );
00783
00784 fail_unless( d!=NULL );
00785 fail_unless( len==142 );
00786 fail_unless( eh_compare_dbl(eh_dbl_array_sum(d,len),100,1e-12) );
00787
00788 eh_free( d );
00789
00790 eh_dbl_array_set( s , 100 , 1. );
00791 d = eh_dbl_array_rebin( s , 100 , .75 , &len );
00792
00793 fail_unless( d!=NULL );
00794 fail_unless( len==134 );
00795 fail_unless( eh_compare_dbl(eh_dbl_array_sum(d,len),100,1e-12) );
00796
00797 eh_free( d );
00798
00799 }
00800 END_TEST
00801
00802 START_TEST ( test_convolve )
00803 {
00804 gssize len_x = 32;
00805 double* x = eh_new( double , len_x );
00806 double* z;
00807 gssize i;
00808
00809 for ( i=0 ; i<len_x ; i++ )
00810 {
00811 x[i] = pow(i,.5) + g_random_double()-.5;
00812 }
00813
00814
00815
00816 z = eh_low_pass_filter( x , len_x );
00817
00818
00819 }
00820 END_TEST
00821
00822 Suite* num_suite( void )
00823 {
00824 Suite *s = suite_create( "Number" );
00825 TCase *test_case_core = tcase_create( "Core" );
00826 TCase *test_case_gamma = tcase_create( "Gamma" );
00827 TCase *test_case_data_fit = tcase_create( "Data Fit" );
00828
00829 suite_add_tcase( s , test_case_core );
00830 suite_add_tcase( s , test_case_gamma );
00831 suite_add_tcase( s , test_case_data_fit );
00832
00833 tcase_add_test( test_case_core , test_nan );
00834 tcase_add_test( test_case_core , test_interpolate );
00835 tcase_add_test( test_case_core , test_poly_interpolate );
00836 tcase_add_test( test_case_core , test_trapazoid );
00837 tcase_add_test( test_case_core , test_integrate );
00838 tcase_add_test( test_case_core , test_compare_dbl );
00839 tcase_add_test( test_case_core , test_round );
00840 tcase_add_test( test_case_core , test_reduce_angle );
00841 tcase_add_test( test_case_core , test_is_even );
00842 tcase_add_test( test_case_core , test_diff );
00843 tcase_add_test( test_case_core , test_binomial_coef );
00844 tcase_add_test( test_case_core , test_factorial );
00845 tcase_add_test( test_case_core , test_convolve );
00846 tcase_add_test( test_case_core , test_running_mean );
00847 tcase_add_test( test_case_core , test_rebin );
00848
00849 tcase_add_test( test_case_gamma , test_gamma_p );
00850 tcase_add_test( test_case_gamma , test_gamma_q );
00851 tcase_add_test( test_case_gamma , test_gamma_series );
00852 tcase_add_test( test_case_gamma , test_gamma_cf );
00853 tcase_add_test( test_case_gamma , test_gamma_log );
00854
00855 tcase_add_test( test_case_data_fit , test_linear_fit );
00856 tcase_add_test( test_case_data_fit , test_poly_fit );
00857 tcase_add_test( test_case_data_fit , test_r_squared );
00858
00859 return s;
00860 }
00861
00862 int test_num( void )
00863 {
00864 int n;
00865
00866 {
00867 Suite *s = num_suite();
00868 SRunner *sr = srunner_create( s );
00869
00870 srunner_run_all( sr , CK_NORMAL );
00871 n = srunner_ntests_failed( sr );
00872 srunner_free( sr );
00873 }
00874
00875 return n;
00876 }