1 package Math::GSL::Integration::Test;
2 use Math::GSL::Test qw/:all/;
3 use base 'Test::Class';
4 use Test::More 'no_plan';
5 use Math::GSL qw/:all/;
6 use Math::GSL::Integration qw/:all/;
7 use Math::GSL::Errno qw/:all/;
9 use Math::GSL::Const qw/:all/;
14 BEGIN{ gsl_set_error_handler_off() };
16 sub make_fixture : Test(setup) {
18 $self->{wspace} = gsl_integration_workspace_alloc(1000);
21 sub teardown : Test(teardown) {
23 gsl_integration_workspace_free($self->{wspace});
26 sub TEST_QAG : Tests {
28 my ($status, $result, $abserr) = gsl_integration_qag (
34 my $res = abs($result - 1/3);
37 sprintf('gsl_integration_qag: res=%.18f, abserr=%.18f',$res,$abserr),
43 my ($integrator,$func,$actual,$epsabs,$epsrel,$lower,$upper,$key) = @_;
44 my $wspace = gsl_integration_workspace_alloc(1000);
45 my $format = "$integrator: actual=%.9f res=%.18f abserr=%.18f";
46 my ($status, $result, $abserr, @params);
47 if ( $integrator eq 'gsl_integration_qag' ) {
48 push @params, $func, $lower, $upper, $epsabs, $epsrel, 1000, $key, $wspace;
49 } elsif ($integrator eq 'gsl_integration_qags' ) {
50 push @params, $func, $lower, $upper, $epsabs, $epsrel, 1000, $wspace;
51 } elsif ($integrator eq 'gsl_integration_qagi' ) {
52 push @params, $func, $epsabs, $epsrel, 1000, $wspace;
53 } elsif ($integrator eq 'gsl_integration_qagiu' ) {
54 push @params, $func, $lower, $epsabs, $epsrel, 1000, $wspace;
56 { no strict 'refs'; ($status, $result, $abserr) = $integrator->( @params ) }
58 ok_similar( [$result], [$actual],
59 sprintf($format,abs($result-$actual),$actual,$abserr), $abserr
63 sub TEST_QAGI : Tests {
64 my $integrator = 'gsl_integration_qagi';
65 my $func = sub { my $x=shift; exp( -$x **2) };
66 verify_integral($integrator, $func, sqrt($M_PI), 0, 1e-7);
67 $func = sub { my $x = shift; 1/($x**2 + 1) };
68 verify_integral($integrator, $func, $M_PI, 0, 1e-7);
71 sub TEST_QAGIU : Tests {
73 my ($status, $result, $abserr) = gsl_integration_qagiu (
74 sub { my $x=shift; log($x)/(1+100+$x**2) } ,
75 0.0, 0.0, 1.0E-3, 1000,
79 local $TODO = "Testing with the data from gsl tests, don't know where's the error";
80 # ok_similar_relative($result, -3.616892186127022568E-01, "gsl_integration_qagiu",1e-14);
81 # ok_similar_relative($abserr, 3.016716913328831851E-06, "gsl_integration_qagiu absolute error",1e-5);
86 # my $integrator = 'gsl_integration_qagiu';
87 # my $func = sub { my $x=shift; log($x)/(1+100+$x**2) };
88 # verify_integral($integrator, $func, -3.616892186127022568E-01, 0, 1.0e-3, 0);
91 sub TEST_QAG2 : Tests {
93 my ($status, $result, $abserr) = gsl_integration_qag (
99 my $res = abs($result - log 10);
102 sprintf('gsl_integration_qags: res=%.18f, abserr=%.18f',$res,$abserr),
106 # f1(x) = x^alpha * log(1/x) */
107 # integ(f1,x,0,1) = 1/(alpha + 1)^2 */
109 sub TEST_QAGS3 : Tests {
111 my ($status, $result, $abserr) = gsl_integration_qags (
112 sub { my $x=shift; $x ** 2 * log (1/$x) } ,
117 my $res = abs($result - 1/9);
120 sprintf('gsl_integration_qags: res=%.18f, abserr=%.18f',$res,$abserr),
126 sub TEST_WORKSPACE_ALLOC : Tests {
128 isa_ok($self->{wspace}, 'Math::GSL::Integration');
131 sub TEST_QNG : Tests {
132 my ($status, $result, $abserr, $neval) = gsl_integration_qng (
137 my $res = abs($result - 1/3);
140 sprintf('gsl_integration_qng: res=%.18f, abserr=%.18f',$res,$abserr),
145 sub QAWS_ALLOC : Tests {
146 my $table = gsl_integration_qaws_table_alloc(0, 0, 1, 0);
147 isa_ok($table, 'Math::GSL::Integration');
150 sub QAWO_ALLOC : Tests {
151 my $table = gsl_integration_qawo_table_alloc(10.0 * $M_PI, 1.0,$GSL_INTEG_SINE, 1000);
152 isa_ok($table, 'Math::GSL::Integration');
154 Test::Class->runtests;