Skip to content

Commit

Permalink
better indadd error, test+tweak for circ_mean{,_p} - #460
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 25, 2024
1 parent 1f12575 commit e83581c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Basic/Primitive/primitive.pd
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pp_def(
'loop(n) %{
register PDL_Indx this_ind = $ind();
if ( PDL_IF_BAD($ISBADVAR(this_ind,ind) ||,) this_ind<0 || this_ind>=$SIZE(m) )
$CROAK("invalid index");
$CROAK("invalid index %"IND_FLAG"; range 0..%"IND_FLAG, this_ind, $SIZE(m));
PDL_IF_BAD(
if ( $ISBAD(input()) ) { $SETBAD(sum(m => this_ind)); }
else,) { $sum(m => this_ind) += $input(); }
Expand Down
20 changes: 11 additions & 9 deletions Libtmp/ImageND/imagend.pd
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ sub PDL::convolve{
barf("Usage: convolve(a(*), b(*), [o]c(*)") if $#_<1 || $#_>2;
$c = PDL->null if $#_<2;
&PDL::_convolve_int( $x->flat, $y->flat,
long([$x->dims]), long([$y->dims]),
$x->shape, $y->shape,
$c->isnull ? $c : $c->flat,
);
$c->setdims([$x->dims]);
Expand Down Expand Up @@ -397,14 +397,15 @@ sub circ_mean_p {
my ($rad,$sum,$norm);
if (defined $opt) {
$rad = long PDL::rvals($x,$opt);
$rad = indx PDL::rvals($x,$opt);
}
else {
$rad = long rvals $x;
$rad = indx rvals $x;
}
$sum = zeroes($rad->max+1);
my $max1 = $rad->max->sclr+1;
$sum = zeroes($max1);
PDL::indadd $x->flat, $rad->flat, $sum; # this does the real work
$norm = zeroes($rad->max+1);
$norm = zeroes($max1);
PDL::indadd pdl(1), $rad->flat, $norm; # equivalent to get norm
$sum /= $norm;
return $sum;
Expand All @@ -430,14 +431,15 @@ sub circ_mean {
my ($rad,$sum,$norm,$a1);
if (defined $opt) {
$rad = long PDL::rvals($x,$opt);
$rad = indx PDL::rvals($x,$opt);
}
else {
$rad = long rvals $x;
$rad = indx rvals $x;
}
$sum = zeroes($rad->max+1);
my $max1 = $rad->max->sclr+1;
$sum = zeroes($max1);
PDL::indadd $x->flat, $rad->flat, $sum; # this does the real work
$norm = zeroes($rad->max+1);
$norm = zeroes($max1);
PDL::indadd pdl(1), $rad->flat, $norm; # equivalent to get norm
$sum /= $norm;
$a1 = $x->flat;
Expand Down
11 changes: 11 additions & 0 deletions Libtmp/ImageND/t/imagend.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,18 @@ my $pb = pdl( [-1,0],[0,1] );
my $pa = sequence(6,6);
my $ans = pdl([14,22,30],[62,70,78],[110,118,126]);
ok( all( $ans==rebin($pa,3,3,{Norm=>1}) ) );
}

{
my $got = circ_mean_p(sequence(8,8));
my $expected = pdl('[36 36 36 36 23.14285 14.4]');
ok all approx($got, $expected, 1e-3) or diag "got: $got\nexp: $expected";
}

{
my $got = circ_mean(sequence(2,2));
my $expected = pdl('[[1 1][1 3]]');
ok all approx($got, $expected, 1e-3) or diag "got: $got\nexp: $expected";
}

done_testing;

0 comments on commit e83581c

Please sign in to comment.