Skip to content

Commit

Permalink
Allow an ndarray as Center in rvals
Browse files Browse the repository at this point in the history
  • Loading branch information
wlmb authored and mohawk2 committed Jan 31, 2025
1 parent a47768e commit 26a3269
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/PDL/Basic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ our @EXPORT_OK = qw/ ndcoords rvals axisvals allaxisvals xvals yvals zvals sec i
our %EXPORT_TAGS = (Func=>[@EXPORT_OK]);

# Exportable functions
*axisvals = \&PDL::axisvals;
*allaxisvals = \&PDL::allaxisvals;
*sec = \&PDL::sec;
*ins = \&PDL::ins;
*hist = \&PDL::hist;
*whist = \&PDL::whist;
*axisvals = \&PDL::axisvals;
*allaxisvals = \&PDL::allaxisvals;
*sec = \&PDL::sec;
*ins = \&PDL::ins;
*hist = \&PDL::hist;
*whist = \&PDL::whist;
*similar_assign = \&PDL::similar_assign;
*transpose = \&PDL::transpose;
*xlinvals = \&PDL::xlinvals;
Expand Down Expand Up @@ -530,7 +530,7 @@ Fills an ndarray with radial distance values from some centre.
Centre => [$x,$y,$z...] # Specify centre
Center => [$x,$y.$z...] # synonym.
Center => $c # as 1d array
Squared => 1 # return distance squared (i.e., don't take the square root)
=for example
Expand All @@ -552,6 +552,10 @@ on an exact pixel point in the data. For dimensions of even size,
that means the midpoint is shifted by 1/2 pixel from the true center
of that dimension.
If C<Center> has less components than the number of dimensions of the
array, its remaining components are computed as above. If it has more,
a warning is issued.
Also note that the calculation for C<rvals> for integer values
does not promote the datatype so you will have wraparound when
the value calculated for C< r**2 > is greater than the datatype
Expand Down Expand Up @@ -588,9 +592,13 @@ sub PDL::rvals { # Return radial distance from given point and offset
}, $opt ) : ();
my $r = &PDL::Core::_construct;
my @pos;
@pos = @{$opt{CENTRE}} if defined $opt{CENTRE};
if(defined $opt{CENTRE}){
my $pos = PDL->topdl($opt{CENTRE});
barf "Center should be a 1D vector" unless $pos->getndims==1;
barf "Center has more coordinates than dimensions of ndarray" if $pos->dim(0) > $r->getndims;
@pos = $pos->list;
}
my $offset;

$r .= 0.0;
my $tmp = $r->copy;
my $i;
Expand Down
1 change: 1 addition & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ is_pdl rvals(3,3,{centre=>[2,2]}), $x1->sqrt, "non-centered rvals";
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 rvals(3,3,{center=>pdl(2,2)}), $x1->sqrt, "rvals with center as ndarray";

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]');
Expand Down

0 comments on commit 26a3269

Please sign in to comment.