Skip to content

Commit

Permalink
add Slices::meshgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Apr 30, 2024
1 parent 4f37bb1 commit 60b7432
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Basic/Slices/slices.pd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ PDL::Slices -- Indexing, slicing, and dicing
$y = $x->slice('-1:0,(1)');
$c = $x->dummy(2);
=head1 DESCRIPTION
This package provides many of the powerful PerlDL core index
Expand Down Expand Up @@ -1645,12 +1644,11 @@ EOD
);

pp_addpm(<<'EOD'
=head2 using
=for ref
Returns array of column numbers requested
Returns list of columns requested
=for usage
Expand All @@ -1675,10 +1673,39 @@ sub PDL::using {
@ind;
}
=head2 meshgrid
=for ref
Returns list of given 1-D vectors, but each expanded to match dims using
L<PDL::Core/dummy>.
=for usage
meshgrid($vec1, $vec2, $vec3);
=for example
print map $_->info, meshgrid(xvals(3), xvals(4), xvals(2));
# PDL: Double D [3,4,2] PDL: Double D [3,4,2] PDL: Double D [3,4,2]
=cut
*meshgrid = \&PDL::meshgrid;
sub PDL::meshgrid {
barf "meshgrid: only 1-dimensional inputs" if grep $_->ndims != 1, @_;
return @_ if @_ == 1;
my @dims = map $_->dims, @_;
my @out;
for my $ind (0..$#_) {
push @out, $_[$ind]->slice(join ',', map $_==$ind ? '' : "*$dims[$_]", 0..$#_);
}
@out;
}
EOD
);

pp_add_exported('', 'using');
pp_add_exported('', 'using meshgrid');

pp_def(
'lags',
Expand Down
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- add ANYVAL_FROM_SV parameter to control "undefval" warning
- move contour_segments from Graphics::TriD::Rout to ImageND
- add TriD::line3d_segs
- add Slices::meshgrid

2.088 2024-04-22
- Slatec::ch{ic,sp} work arrays now [t]
Expand Down
7 changes: 7 additions & 0 deletions t/slice.t
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,11 @@ is $off_got->type, $seqs->type, "rleseq():type";
ok all(approx($len_got->where($len_got), $lens->where($lens))), "rleseq():lens";
ok all(approx($off_got->where($len_got), $offs->where($lens))), "rleseq():offs";

eval {meshgrid(sequence(2,2))};
like $@, qr/1-dimensional/, 'meshgrid rejects >1-D';
my @vecs = (xvals(3), xvals(4)+5, xvals(2)+10);
my @mesh_got = meshgrid(@vecs);
is_deeply [$_->dims], [3,4,2] for @mesh_got;
ok all($mesh_got[$_]->mv($_,0)->slice(',(0),(0)')==$vecs[$_]), "meshgrid $_" for 0..$#vecs;

done_testing;

0 comments on commit 60b7432

Please sign in to comment.