Skip to content

Commit 6a5c48d

Browse files
committed
add whereND_both
1 parent db6255e commit 6a5c48d

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

Changes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- badflag now not propagated back to inputs if set on outputs (#517)
2828
- add new_around_pointer (#505) - thanks @chrisarg for idea
2929
- add do_print to perldl to match pdl2
30-
- add whichND_both - thanks @guillepo for inspiration
30+
- add {which,where}ND_both - thanks @guillepo for inspiration
3131

3232
2.098 2025-01-03
3333
- fix Windows build problems

lib/PDL/Primitive.pd

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,7 +3644,7 @@ sub PDL::where_both {
36443644
*where_both = \&PDL::where_both;
36453645
EOD
36463646

3647-
pp_add_exported("", 'whereND');
3647+
pp_add_exported("", 'whereND whereND_both');
36483648
pp_addpm(<<'EOD');
36493649
=head2 whereND
36503650

@@ -3670,7 +3670,7 @@ to the original data PDLs, for the purpose of dataflow.
36703670
$sdata = whereND $data, $mask
36713671
($s1, $s2, ..., $sn) = whereND $d1, $d2, ..., $dn, $mask
36723672

3673-
where
3673+
where
36743674

36753675
$data is M dimensional
36763676
$mask is N < M dimensional
@@ -3727,6 +3727,35 @@ sub PDL::whereND :lvalue {
37273727
}
37283728
*whereND = \&PDL::whereND;
37293729

3730+
=head2 whereND_both
3731+
3732+
=for ref
3733+
3734+
C<where_both> with support for ND masks and broadcasting
3735+
3736+
This works like L</whichND_both>, but data-flowing slices
3737+
rather than index-sets are returned.
3738+
3739+
C<whereND_both> differs from C<where_both> in that the mask
3740+
dimensionality is preserved which allows for
3741+
proper broadcasting of the selection operation over
3742+
higher dimensions.
3743+
3744+
As with C<where_both> the output PDLs are still connected
3745+
to the original data PDLs, for the purpose of dataflow.
3746+
3747+
=for usage
3748+
3749+
($match_vals, $non_match_vals) = whereND_both($pdl, $mask);
3750+
3751+
=cut
3752+
3753+
sub PDL::whereND_both :lvalue {
3754+
barf "Usage: whereND_both(\$pdl, \$mask)\n" if @_ != 2;
3755+
my ($arr, $mask) = @_; # $mask has 0==false, 1==true
3756+
map $arr->indexND($_), PDL::whichND_both($mask);
3757+
}
3758+
*whereND_both = \&PDL::whereND_both;
37303759
EOD
37313760

37323761
pp_add_exported("", 'whichND whichND_both');

t/primitive-selector.t

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ subtest 'where' => sub {
2323
'dataflow affected orig';
2424
};
2525

26+
subtest 'whereND_both' => sub {
27+
my ( $t, $f ) = whereND_both(sequence(2,2,2), pdl(0,1));
28+
is_pdl $t, pdl('[1;3] [5;7]'), 'nonzero vals';
29+
is_pdl $f, pdl('[0;2] [4;6]'), 'zero vals';
30+
};
31+
2632
subtest 'whereND' => sub {
2733
is_deeply( [ zeroes( 2, 3, 1 )->whereND( pdl '0 0' )->dims ], [ 0, 3, 1 ] );
2834

0 commit comments

Comments
 (0)