@@ -3304,7 +3304,7 @@ will not give the answer you expect.
33043304
33053305*one2nd = \&PDL::one2nd;
33063306sub PDL::one2nd {
3307- barf "Usage: one2nd \$array \$indices\n" if @_ != 2;
3307+ barf "Usage: one2nd \$array, \$indices\n" if @_ != 2;
33083308 my ($x, $ind)=@_;
33093309 my @dimension=$x->dims;
33103310 $ind = indx($ind);
@@ -3789,6 +3789,21 @@ L<PDL::Slices/indexND> can be fed the coordinates to return the values.
37893789
37903790=cut
37913791
3792+ sub _one2nd {
3793+ my ($mask, $ind) = @_;
3794+ my $ndims = my @mdims = $mask->dims;
3795+ # In the empty case, explicitly return the correct type of structured empty
3796+ return PDL->new_from_specification(indx, $ndims, 0) if !$ind->nelem;
3797+ my $mult = ones(indx, $ndims);
3798+ $mult->index($_+1) .= $mult->index($_) * $mdims[$_] for 0..$#mdims-1;
3799+ for my $i (0..$#mdims) {
3800+ my $s = $ind->index($i);
3801+ $s /= $mult->index($i);
3802+ $s %= $mdims[$i];
3803+ }
3804+ $ind;
3805+ }
3806+
37923807*whichND = \&PDL::whichND;
37933808sub PDL::whichND {
37943809 my $mask = PDL->topdl(shift);
@@ -3805,29 +3820,12 @@ sub PDL::whichND {
38053820 }
38063821
38073822 # Scalar context: generate an N-D index ndarray
3808- return PDL->new_from_specification(indx,$mask->ndims,0) if !$mask->nelem;
3809- return $mask ? pdl(indx,0) : PDL->new_from_specification(indx,0)
3810- if !$mask->getndims;
3811-
3812- my $ind = $mask->flat->which->dummy(0,$mask->getndims)->make_physical;
3813- # In the empty case, explicitly return the correct type of structured empty
3814- return PDL->new_from_specification(indx,$mask->ndims, 0) if !$ind->nelem;
3815-
3816- my $mult = ones(indx, $mask->getndims);
3817- my @mdims = $mask->dims;
3818-
3819- for my $i (0..$#mdims-1) {
3820- # use $tmp for 5.005_03 compatibility
3821- (my $tmp = $mult->index($i+1)) .= $mult->index($i)*$mdims[$i];
3822- }
3823-
3824- for my $i (0..$#mdims) {
3825- my($s) = $ind->index($i);
3826- $s /= $mult->index($i);
3827- $s %= $mdims[$i];
3828- }
3823+ my $ndims = $mask->getndims;
3824+ return PDL->new_from_specification(indx,$ndims,0) if !$mask->nelem;
3825+ return $mask ? pdl(indx,0) : PDL->new_from_specification(indx,0) if !$ndims;
38293826
3830- return $ind;
3827+ my $ind = $mask->flat->which->dummy(0,$ndims)->make_physical;
3828+ _one2nd($mask, $ind);
38313829}
38323830EOD
38333831
0 commit comments