Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 166 lines (148 sloc) 5.695 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
package Math::GSL::Integration::Test;
use base 'Test::Class';
use Test::More tests => 29;
use Test::Exception;
use Math::GSL qw/:all/;
use Math::GSL::Test qw/:all/;
use Math::GSL::Errno qw/:all/;
use Math::GSL::Const qw/:all/;
use Math::GSL::Integration qw/:all/;
use Data::Dumper;
use strict;
use warnings;

BEGIN{ gsl_set_error_handler_off() };

sub make_fixture : Test(setup) {
    my $self = shift;
    $self->{wspace} = gsl_integration_workspace_alloc(1000);
}

sub teardown : Test(teardown) {
    my $self = shift;
    gsl_integration_workspace_free($self->{wspace});
}
sub TEST_CONSTANTS : Tests {
    ok(defined $GSL_INTEG_COSINE , '$GSL_INTEG_COSINE');
    ok(defined $GSL_INTEG_SINE , '$GSL_INTEG_SINE');
    ok(defined $GSL_INTEG_GAUSS15 , '$GSL_INTEG_GAUSS15');
    ok(defined $GSL_INTEG_GAUSS21 , '$GSL_INTEG_GAUSS21');
    ok(defined $GSL_INTEG_GAUSS31 , '$GSL_INTEG_GAUSS31');
    ok(defined $GSL_INTEG_GAUSS41 , '$GSL_INTEG_GAUSS41');
    ok(defined $GSL_INTEG_GAUSS51 , '$GSL_INTEG_GAUSS51');
    ok(defined $GSL_INTEG_GAUSS61 , '$GSL_INTEG_GAUSS61');
}

sub TEST_QAG : Tests {
    my $self = shift;
    my ($status, $result, $abserr) = gsl_integration_qag (
                                sub { $_[0]**2} ,
                                0, 1, 0, 1e-7, 1000,
                                6, $self->{wspace},
                           );
    ok_status($status);
    my $res = abs($result - 1/3);
    ok_similar(
        [$result], [1/3],
        sprintf('gsl_integration_qag: res=%.18f, abserr=%.18f',$res,$abserr),
        $abserr
    );
}

sub TEST_QAGI : Tests {
    my $integrator = 'gsl_integration_qagi';
    my $func = sub { my $x=shift; exp( -$x **2) };
    verify_integral($integrator, $func, sqrt($M_PI), 0, 1e-7);
    $func = sub { my $x = shift; 1/($x**2 + 1) };
    verify_integral($integrator, $func, $M_PI, 0, 1e-7);
}

sub TEST_QAGIU : Tests {
    my $self = shift;
    my ($status, $result, $abserr) = gsl_integration_qagiu (
                                sub { my $x=shift; log($x)/(1+100+$x**2) } ,
                                    0.0, 0.0, 1.0E-3, 1000,
                                $self->{wspace}
                            );
    ok_status($status);
    ok_similar([$abserr],[ 3.016716913328831851E-06], "gsl_integration_qagiu absolute error",1e-5);

    my $integrator = 'gsl_integration_qagiu';
    my $func = sub { my $x=shift; log($x)/(1+100+$x**2) };
    verify_integral($integrator, $func, 3.616892186127022568E-01, 0, 2e-3, 0);

    ok_similar([$result],[ 3.616892186127022568E-01], "gsl_integration_qagiu",2e-3);

}

sub verify_integral {
    my ($integrator,$func,$actual,$epsabs,$epsrel,$lower,$upper,$key) = @_;
    my $wspace = gsl_integration_workspace_alloc(1000);
    my $format = "$integrator: actual=%.9f relerr=%.18f abserr=%.18f";
    my ($status, $result, $abserr, @params);
    if ( $integrator eq 'gsl_integration_qag' ) {
         push @params, $func, $lower, $upper, $epsabs, $epsrel, 1000, $key, $wspace;
    } elsif ($integrator eq 'gsl_integration_qags' ) {
         push @params, $func, $lower, $upper, $epsabs, $epsrel, 1000, $wspace;
    } elsif ($integrator eq 'gsl_integration_qagi' ) {
         push @params, $func, $epsabs, $epsrel, 1000, $wspace;
    } elsif ($integrator eq 'gsl_integration_qagiu' ) {
         push @params, $func, $lower, $epsabs, $epsrel, 1000, $wspace;
    }
    { no strict 'refs'; ($status, $result, $abserr) = $integrator->( @params ) }
    ok_status($status);
    ok_similar( [$result], [$actual],
        sprintf( $format,$actual,
            abs($result-$actual)/abs($actual),$abserr
        ), $epsrel
    );
}


sub TEST_QAG2 : Tests {
    my $self = shift;
    my ($status, $result, $abserr) = gsl_integration_qag (
                                sub { 1/$_[0] } ,
                                1, 10, 0, 1e-7, 1000,
                                5, $self->{wspace},
                           );
    ok_status($status);
    my $res = abs($result - log 10);
    ok_similar(
        [$result],[log 10],
        sprintf('gsl_integration_qags: res=%.18f, abserr=%.18f',$res,$abserr),
        $abserr
    );
}
# f1(x) = x^alpha * log(1/x) */
# integ(f1,x,0,1) = 1/(alpha + 1)^2 */

sub TEST_QAGS3 : Tests {
    my $self = shift;
    my ($status, $result, $abserr) = gsl_integration_qags (
                                sub { my $x=shift; $x ** 2 * log (1/$x) } ,
                                0, 1, 0, 1e-7, 1000,
                                $self->{wspace},
                           );
    ok_status($status);
    my $res = abs($result - 1/9);
    ok_similar(
        [$result],[1/9],
        sprintf('gsl_integration_qags: res=%.18f, abserr=%.18f',$res,$abserr),
        $abserr
    );
}


sub TEST_WORKSPACE_ALLOC : Tests {
    my $self = shift;
    isa_ok($self->{wspace}, 'Math::GSL::Integration');
}

sub TEST_QNG : Tests {
    my ($status, $result, $abserr, $neval) = gsl_integration_qng (
                                sub { $_[0]**2} ,
                                0, 1, 0, 1e-7,
                           );
    ok_status($status);
    my $res = abs($result - 1/3);
    ok( $neval > 0, 'returned number of evaluations');
    ok_similar(
        [$result], [1/3],
        sprintf('gsl_integration_qng: res=%.18f, abserr=%.18f',$res,$abserr),
       $abserr
    );
}

sub QAWS_ALLOC : Tests {
   my $table = gsl_integration_qaws_table_alloc(0, 0, 1, 0);
   isa_ok($table, 'Math::GSL::Integration');
}

sub QAWO_ALLOC : Tests {
   my $table = gsl_integration_qawo_table_alloc(10.0 * $M_PI, 1.0,$GSL_INTEG_SINE, 1000);
   isa_ok($table, 'Math::GSL::Integration');
}
Test::Class->runtests;
Something went wrong with that request. Please try again.