Skip to content

Commit

Permalink
Slatec, Simplex tests replace approx with is_pdl - #34
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 30, 2024
1 parent 72b7e71 commit 180687a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
22 changes: 11 additions & 11 deletions Libtmp/Simplex/t/simplex.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use warnings;
use Test::More;
use PDL::LiteF;
use PDL::Opt::Simplex;
use Test::PDL -atol => 1e-3;

sub test_simplex {
local $Test::Builder::Level = $Test::Builder::Level + 1;
Expand All @@ -15,24 +16,23 @@ sub test_simplex {
sumover( ( $_[0] - $dis )**2 ) + 1;
}, $nolog ? () : $logsub,
);
ok all PDL::Core::approx($opt, $dis, 1e-3), 'optimum' or diag "got=$opt";
ok PDL::Core::approx($ssize, 0, 1e-3), 'ssize' or diag "got=$ssize";
ok PDL::Core::approx($optval, 1, 1e-3), 'optval' or diag "got=$optval";
is_pdl $opt, $dis, 'optimum' or diag "got=$opt";
is_pdl $ssize, pdl(0), 'ssize' or diag "got=$ssize";
is_pdl $optval, pdl(1), 'optval' or diag "got=$optval";
ok $log_called, 'log called' if !$nolog;
my @init_dims = $init->dims;
my @exp_dims = ($init_dims[0], 1, @init_dims[2..$#init_dims]);
is_deeply [$opt->dims], \@exp_dims, 'dims optimum right';
}

test_simplex(pdl(2,2), 0.01, pdl(0,1));
test_simplex(pdl(2,2), pdl(0.01,0.01), pdl(0,1));
test_simplex(pdl(2,2), pdl(0.01,0.01), pdl(0,1), 1);
test_simplex(pdl(2,2,2), pdl(0.01,0.01,0.01), pdl(0,1,2));
test_simplex(my $p = pdl(q[-1 -1; -1.1 -1; -1.1 -0.9]), pdl(0.01,0.01), pdl(0,1));
test_simplex($p, undef, pdl(-1,1));
test_simplex(pdl(2,2), 0.01, pdl([[0,1]]));
test_simplex(pdl(2,2), pdl(0.01,0.01), pdl([[0,1]]));
test_simplex(pdl(2,2), pdl(0.01,0.01), pdl([[0,1]]), 1);
test_simplex(pdl(2,2,2), pdl(0.01,0.01,0.01), pdl([[0,1,2]]));
test_simplex(my $p = pdl(q[-1 -1; -1.1 -1; -1.1 -0.9]), pdl(0.01,0.01), pdl([[0,1]]));
test_simplex($p, undef, pdl([[-1,1]]));

my $s = make_simplex(pdl(0,0,0), pdl(0.12,0.12,0.12));
ok all approx $s, pdl '0 -0.06 -0.08; 0.12 -0.06 -0.08; 0 0.06 -0.08; 0 0 0.04'
or diag "got=$s";
is_pdl $s, pdl '0 -0.06 -0.08; 0.12 -0.06 -0.08; 0 0.06 -0.08; 0 0 0.04';

done_testing;
5 changes: 3 additions & 2 deletions Libtmp/Slatec/t/linfit.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use warnings;
use Test::More;
use PDL::Fit::Linfit;
use PDL::LiteF;
use Test::PDL;

{
# Simple Test Case:
Expand All @@ -20,7 +21,7 @@ my ($yfit, $coeffs) = PDL::linfit1d($data, $fitFuncs);

my @coefs = $coeffs->list;

ok all approx( $coeffs, pdl([3,2,3]) );
is_pdl $coeffs, pdl([3,2,3]);
}

{
Expand Down Expand Up @@ -92,7 +93,7 @@ my ($yfit, $coeffs) = linfit1d($data, $fitFuncs);

my @coefs = $coeffs->list;

ok all approx( $coeffs, pdl( \@expectedCoefs ) );
is_pdl $coeffs, pdl( \@expectedCoefs );
}

done_testing;
5 changes: 3 additions & 2 deletions Libtmp/Slatec/t/slatec-polyfit-weight.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use warnings;
use Test::More;
use Test::Exception;
use PDL::LiteF;
use Test::PDL;
use PDL::Slatec;

## Issue information
Expand All @@ -25,13 +26,13 @@ lives_ok { $polyfit_orig = polyfit( $x, $y, $x->ones, 4, .0001 ); } 'polyfit() w
subtest 'Passing the weight in a PDL of length 1' => sub {
my $polyfit_pdl_len_one;
lives_ok { $polyfit_pdl_len_one = polyfit( $x, $y, pdl(1), 4, .0001 ); };
ok( approx($polyfit_orig, $polyfit_pdl_len_one)->all, 'passing a PDL of length 1 expands to the correct length' );
is_pdl $polyfit_orig, $polyfit_pdl_len_one, 'passing a PDL of length 1 expands to the correct length';
};

subtest 'Passing the weight in a Perl scalar' => sub {
my $polyfit_perl_scalar;
lives_ok { $polyfit_perl_scalar = polyfit( $x, $y, 1, 4, .0001 ) };
ok( approx($polyfit_orig, $polyfit_perl_scalar)->all, 'passing a Perl scalar expands to the correct length' );
is_pdl $polyfit_orig, $polyfit_perl_scalar, 'passing a Perl scalar expands to the correct length';
};

done_testing;

0 comments on commit 180687a

Please sign in to comment.