From a22bf7aa4ad5fb84f4a257682cd5a694afbccb03 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Fri, 31 Jan 2025 11:22:20 -0600 Subject: [PATCH] Allow an ndarray as Center in rvals --- lib/PDL/Basic.pm | 24 +++++++++++++++--------- t/basic.t | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/PDL/Basic.pm b/lib/PDL/Basic.pm index 764a3749f..9e07bc7b5 100644 --- a/lib/PDL/Basic.pm +++ b/lib/PDL/Basic.pm @@ -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; @@ -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 @@ -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
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 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 @@ -588,9 +592,11 @@ 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}){ + @pos = ref $opt{CENTRE} eq "ARRAY"? @{$opt{CENTRE}} : $opt{CENTRE}->list; + } + warn "Center has more coordinates than dimensions of ndarray" if @pos > $r->getndims; my $offset; - $r .= 0.0; my $tmp = $r->copy; my $i; diff --git a/t/basic.t b/t/basic.t index 2749519ba..59a9905ad 100644 --- a/t/basic.t +++ b/t/basic.t @@ -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]');