Skip to content

Commit

Permalink
rehabilitate t/primitive-*.t somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Dec 18, 2024
1 parent 69c20e8 commit 1605a4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
18 changes: 8 additions & 10 deletions lib/PDL/Primitive.pd
Original file line number Diff line number Diff line change
Expand Up @@ -3201,19 +3201,18 @@ sub PDL::interpND {
barf 'Usage: interp_nd($source,$index,[{%options}])\n'
if(defined $options and ref $options ne 'HASH');
my($opt) = (defined $options) ? $options : {};
my $opt = defined $options ? $options : {};
my($method) = $opt->{m} || $opt->{meth} || $opt->{method} || $opt->{Method};
my $method = $opt->{m} || $opt->{meth} || $opt->{method} || $opt->{Method};
$method //= $source->type->integer ? 'sample' : 'linear';
my($boundary) = $opt->{b} || $opt->{boundary} || $opt->{Boundary} || $opt->{bound} || $opt->{Bound} || 'extend';
my($bad) = $opt->{bad} || $opt->{Bad} || 0.0;
my $boundary = $opt->{b} || $opt->{boundary} || $opt->{Boundary} || $opt->{bound} || $opt->{Bound} || 'extend';
my $bad = $opt->{bad} || $opt->{Bad} || 0.0;
if($method =~ m/^s(am(p(le)?)?)?/i) {
return $source->range(PDL::Math::floor($index+0.5),0,$boundary);
}
return $source->range(PDL::Math::floor($index+0.5),0,$boundary)
if $method =~ m/^s(am(p(le)?)?)?/i;
elsif (($method eq 1) || $method =~ m/^l(in(ear)?)?/i) {
if (($method eq 1) || $method =~ m/^l(in(ear)?)?/i) {
## key: (ith = index broadcast; cth = cube broadcast; sth = source broadcast)
my $d = $index->dim(0);
my $di = $index->ndims - 1;
Expand All @@ -3234,8 +3233,7 @@ sub PDL::interpND {
# a & b are the weighting coefficients.
my($x,$y);
my($indexwhere);
($indexwhere = $index->where( 0 * $index )) .= -10; # Change NaN to invalid
$index->where( 0 * $index ) .= -10; # Change NaN to invalid
{
my $bb = PDL::Math::floor($index);
$x = ($index - $bb) -> dummy(1,$crnr->dim(1)); # index, clst, ith
Expand Down
3 changes: 3 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ is_pdl rvals(3,3,{center=>[2,2]}), $x1->sqrt, "centre/center synonyms";
is_pdl rvals(3,3,{ceNteR=>[2,2]}), $x1->sqrt, "ceNteR option capitalization";
is_pdl rvals(3,3,{center=>[2,2],squared=>1}), $x1, "both center and squared options";

is_pdl ndcoords(2,2), pdl('[0 0; 1 0] [0 1; 1 1]');
is_pdl PDL::Basic::ndcoords(2,2), pdl('[0 0; 1 0] [0 1; 1 1]');

# test (x|y|z)(lin|log)vals: shape and values
{
my $a1=zeroes(101,51,26);
Expand Down
28 changes: 12 additions & 16 deletions t/primitive-interpolate.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,34 @@ use PDL::LiteF;
use Test::PDL;

subtest interpol => sub {

subtest real => sub {
my $yvalues = PDL->new( 0 .. 5 ) - 20;
my $xvalues = -PDL->new( 0 .. 5 ) * .5;
my $x = PDL->new(-2);
is( $x->interpol( $xvalues, $yvalues ), -16, "result" );
my $yvalues = pdl( 0 .. 5 ) - 20;
my $xvalues = -pdl( 0 .. 5 ) * .5;
my $x = pdl(-2);
is( $x->interpol( $xvalues, $yvalues ), pdl(-16), "result" );
};

subtest complex => sub {
my $yvalues = ( PDL->new( 0 .. 5 ) - 20 ) * ( 1 + i() );
my $xvalues = -PDL->new( 0 .. 5 ) * .5;
my $x = PDL->new(-2);


ok( all( $x->interpol( $xvalues, $yvalues ) == ( -16 - 16 * i ) ),
"result" );

my $yvalues = ( pdl( 0 .. 5 ) - 20 ) * ( 1 + i() );
my $xvalues = -pdl( 0 .. 5 ) * .5;
my $x = pdl(-2);
is_pdl $x->interpol( $xvalues, $yvalues ), -16 - 16 * i;
throws_ok { $x->interpol( $xvalues * i(), $yvalues ) }
qr/must be real/,
"x must be real";
};

};

subtest interpND => sub {
my $x = xvals( 10, 10 ) + yvals( 10, 10 ) * 10;
my $index = cat( 3 + xvals( 5, 5 ) * 0.25, 7 + yvals( 5, 5 ) * 0.25 )
->reorder( 2, 0, 1 );
my $z = 73 + xvals( 5, 5 ) * 0.25 + 2.5 * yvals( 5, 5 );
my $z = pdl '73 73.25 73.5 73.75 74; 75.5 75.75 76 76.25 76.5;
78 78.25 78.5 78.75 79; 80.5 80.75 81 81.25 81.5;
83 83.25 83.5 83.75 84';
my $y;
lives_ok { $y = $x->interpND($index) } 'interpND';
ok !any( $y != $z ), "result";
is_pdl $y, $z;
};

subtest PCHIP => sub {
Expand Down

0 comments on commit 1605a4b

Please sign in to comment.