From babdde8ae0a978b4ae93bcc82bae730b4b93faff Mon Sep 17 00:00:00 2001 From: Jonathan Leto Date: Fri, 30 May 2008 22:04:18 -0400 Subject: [PATCH] Generalize is_similar() to take array refs and fixed some tests. --- lib/Math/GSL.pm | 20 +++++++++++++++++++- lib/Math/GSL/Complex/Test.pm | 6 ++++-- lib/Math/GSL/Poly/Test.pm | 9 ++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/Math/GSL.pm b/lib/Math/GSL.pm index da5c59d..6909caa 100644 --- a/lib/Math/GSL.pm +++ b/lib/Math/GSL.pm @@ -145,7 +145,25 @@ sub verify_results sub is_similar { my ($x,$y, $eps) = @_; $eps ||= 1e-8; - abs($x-$y) < $eps ? 1 : 0; + if (ref $x eq 'ARRAY' && ref $y eq 'ARRAY') { + if ( $#$x != $#$y ){ + warn "is_similar(): argument of different length!"; + return 0; + } else { + map { + my $delta = abs($x->[$_] - $y->[$_]); + if($delta > $eps){ + warn "\n\tElements start differing at index $_, delta = $delta\n"; + warn qq{\t\t\$x->[$_] = } . $x->[$_] . "\n"; + warn qq{\t\t\$y->[$_] = } . $y->[$_] . "\n"; + return 0; + } + } (0..$#$x); + return 1; + } + } else { + abs($x-$y) <= $eps ? return 1 : return 0; + } } sub is_valid_double diff --git a/lib/Math/GSL/Complex/Test.pm b/lib/Math/GSL/Complex/Test.pm index 4c23f82..9b3bc0a 100644 --- a/lib/Math/GSL/Complex/Test.pm +++ b/lib/Math/GSL/Complex/Test.pm @@ -168,8 +168,10 @@ sub GSL_COMPLEX_SQRT : Tests { my $x = gsl_complex_rect(-7,24); my $z = gsl_complex_sqrt($x); - ok( is_similar(gsl_real($z),3), 'gsl_complex_sqrt'); - ok( is_similar(gsl_imag($z),4), 'gsl_complex_sqrt'); + ok( is_similar( [ gsl_parts($z) ], + [ 3 , 4 ], + ),'gsl_complex_sqrt' + ); } sub GSL_COMPLEX_SQRT_REAL : Tests { diff --git a/lib/Math/GSL/Poly/Test.pm b/lib/Math/GSL/Poly/Test.pm index da207d1..93a5f98 100644 --- a/lib/Math/GSL/Poly/Test.pm +++ b/lib/Math/GSL/Poly/Test.pm @@ -37,17 +37,16 @@ sub GSL_COMPLEX_POLY_COMPLEX_EVAL : Tests { my $z = gsl_complex_rect(2,1); # 2+i my $c1 = gsl_complex_rect(3,2); # 3+2i my $c2 = gsl_complex_rect(0,5); # 5i - print "foo\n"; my $got = gsl_complex_poly_complex_eval( [ $c2, $c1 ], 2, $z ); - print Dumper [ $got ]; + is_deeply( [ gsl_parts($got) ], [ 4, 16 ], 'gsl_complex_poly_eval' ); } -sub GSL_COMPLEX_POLY_COMPLEX_EVAL : Tests { +sub GSL_COMPLEX_POLY_COMPLEX_EVAL2 : Tests { my $z = gsl_complex_rect(0.674,-1.423); my $w = gsl_complex_rect(-1.44, 9.55); - $y = gsl_complex_poly_complex_eval ($z, 1, $w); + my $got = gsl_complex_poly_complex_eval ([ $z ], 1, $w); - is_deeply( [ gsl_parts($got) ] , [0.674,-1.423] ); + is_deeply( [ gsl_parts($got) ] , [0.674,-1.423], 'gsl_complex_poly_eval2' ); } sub GSL_POLY_SOLVE_CUBIC : Tests { -- 2.11.4.GIT