Generalize is_similar() to take array refs and fixed some tests.
[Math-GSL.git] / lib / Math / GSL.pm
blob6909caaa03853edbd8101976ed8356eafa428d7d
1 package Math::GSL;
2 use strict;
3 use warnings;
4 use Math::GSL::Machine qw/:all/;
5 use Math::GSL::Const qw/:all/;
6 use Config;
7 use Data::Dumper;
8 use Test::More;
9 use Scalar::Util qw/looks_like_number/;
10 require DynaLoader;
11 require Exporter;
12 our @ISA = qw(Exporter DynaLoader);
13 our @EXPORT = qw();
14 our @EXPORT_OK = qw( is_similar $GSL_MODE_DEFAULT $GSL_PREC_DOUBLE $GSL_PREC_SINGLE $GSL_PREC_APPROX);
15 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
17 our ($GSL_PREC_DOUBLE, $GSL_PREC_SINGLE, $GSL_PREC_APPROX ) = 0..2;
18 our $GSL_MODE_DEFAULT = $GSL_PREC_DOUBLE;
20 use constant MAX_DOUBLE => 1.7976931348623157e+308;
21 use constant MIN_DOUBLE => 2.2250738585072014e-308;
22 use constant MAX_FLOAT => 3.40282347e+38;
23 use constant MIN_FLOAT => 1.175494351e-38;
24 our $VERSION = 0.043;
26 =head1 NAME
28 Math::GSL - Perl interface to the GNU Scientific Library (GSL) using SWIG
30 =head1 VERSION
32 Version 0.43
34 =cut
36 =head1 SYNOPSIS
38 use Math::GSL::RNG qw/:all/;
41 =head1 AUTHOR
43 Jonathan Leto, C<< <jonathan@leto.net> >> and Thierry Moisan C<< <thierry.moisan@gmail.com> >>
45 =head1 BUGS
47 Please report any bugs or feature requests to the authors directly.
50 =head1 SUPPORT
52 You can find documentation for this module with the perldoc command.
54 perldoc Math::GSL
56 or online at L<http://leto.net/code/Math-GSL/>
58 =over 4
60 =item * AnnoCPAN: Annotated CPAN documentation
62 L<http://annocpan.org/dist/Math::GSL>
64 =item * CPAN Ratings
66 L<http://cpanratings.perl.org/d/Math::GSL>
68 =item * Search CPAN
70 L<http://search.cpan.org/dist/Math::GSL>
72 =back
75 =head1 ACKNOWLEDGEMENTS
78 =head1 COPYRIGHT & LICENSE
80 Copyright 2008 Jonathan Leto, Thierry Moisan all rights reserved.
82 This program is free software; you can redistribute it and/or modify it
83 under the same terms as Perl itself.
85 =cut
87 sub new
89 my ($self,$args) = @_;
90 my $class = ref $self || $self || 'Math::GSL';
91 my $this = { };
92 bless $this, $class;
95 sub subsystems
97 return qw/
98 BLAS Diff Machine Permute Statistics
99 Block Eigen Matrix Poly Sum
100 BSpline Errno PowInt Sys
101 CBLAS FFT Min IEEEUtils
102 CDF Fit Mode QRNG Types
103 Chebyshev Function Monte RNG Vector
104 Heapsort Multifit Randist
105 Combination Histogram Multimin Roots Wavelet
106 Complex Histogram2d Multiroots SF Wavelet2D
107 Const Siman
108 DFT Integration NTuple Sort
109 DHT Interp ODEIV
110 Deriv Linalg Permutation Spline
114 sub verify_results
116 my ($self,$results,$class) = @_;
117 my ($x,$val);
119 while (my($k,$v)=each %$results){
120 my $eps = 2048*$Math::GSL::Machine::GSL_DBL_EPSILON; # TOL3
122 defined $class ? ( $x = eval qq{${class}::$k} )
123 : ( $x = eval $k);
125 print $@ if $@;
126 print "got $x for $k\n" if defined $ENV{DEBUG};
128 if (ref $v eq 'ARRAY'){
129 ($val, $eps) = @$v;
130 } else {
131 $val = $v;
133 if (!defined $x ){
134 ok(0, qq{'$k' died} );
135 } elsif ($x =~ /nan|inf/i){
136 ok( $val eq $x, "'$val'?='$x'" );
137 } else {
138 my $res = abs($x-$val);
139 $@ ? ok(0)
140 : ok( $res <= $eps, "$k ?= $x,\n+- $res, tol=$eps" );
145 sub is_similar {
146 my ($x,$y, $eps) = @_;
147 $eps ||= 1e-8;
148 if (ref $x eq 'ARRAY' && ref $y eq 'ARRAY') {
149 if ( $#$x != $#$y ){
150 warn "is_similar(): argument of different length!";
151 return 0;
152 } else {
153 map {
154 my $delta = abs($x->[$_] - $y->[$_]);
155 if($delta > $eps){
156 warn "\n\tElements start differing at index $_, delta = $delta\n";
157 warn qq{\t\t\$x->[$_] = } . $x->[$_] . "\n";
158 warn qq{\t\t\$y->[$_] = } . $y->[$_] . "\n";
159 return 0;
161 } (0..$#$x);
162 return 1;
164 } else {
165 abs($x-$y) <= $eps ? return 1 : return 0;
169 sub is_valid_double
171 my $x=shift;
172 return 0 unless ( defined $x && looks_like_number($x) );
174 return 1 if ($x == 0);
176 $x = abs $x;
178 $x > MIN_DOUBLE &&
179 $x < MAX_DOUBLE
180 ) ? 1 : 0;
182 sub is_valid_float
184 my $x=shift;
185 return 0 unless ( defined $x && looks_like_number($x) );
187 return 1 if ($x == 0);
189 $x = abs $x ;
191 $x > MIN_FLOAT &&
192 $x < MAX_FLOAT
193 ) ? 1 : 0;
196 sub _has_quads { $Config{use64bitint} eq 'define' || ($Config{longsize} >= 8) }
197 sub _has_long_doubles { $Config{d_longdbl} eq 'define' }
198 sub _has_long_doubles_as_default { $Config{uselongdouble} eq 'define' }
199 sub _has_long_doubles_same_as_doubles { $Config{doublesize} == $Config{longdblsize} }
201 sub _assert_dies($;$)
203 my ($code,$msg) = @_;
204 my $status = eval { &$code };
205 print "status=||$status||\n\$\?=$?\n\$\!=$!\n" if 0;
206 $@ ? ok(1, $msg) : ok (0, join "\n", $@, $msg );