Skip to content

Commit

Permalink
add Core::tocomplex - #527
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 22, 2025
1 parent 614e658 commit 81a6883
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- add new_around_pointer (#505) - thanks @chrisarg for idea
- add do_print to perldl to match pdl2
- add {which,where}ND_both - thanks @guillepo for inspiration
- add Core::tocomplex (#527) - thanks @wlmb for suggestion

2.098 2025-01-03
- fix Windows build problems
Expand Down
17 changes: 17 additions & 0 deletions lib/PDL/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,23 @@ below for usage).
$y = topdl $ndarray; # fall through
$x = topdl (1,2,3,4); # Convert 1D array
=head2 tocomplex
=for ref
Return a complex-typed ndarray, either as a no-op or a conversion.
$cplx = $pdl->tocomplex;
Not exported. Added in 2.099.
=cut

sub PDL::tocomplex {
my $pdl = PDL->topdl(@_);
$pdl->type->real ? $pdl->r2C : $pdl;
}

=head2 set_datatype
=for ref
Expand Down
5 changes: 5 additions & 0 deletions t/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ eval {PDL->topdl({})};
isnt $@, '', 'topdl({}) no segfault';
}

is_pdl pdl(1)->tocomplex, cdouble(1), 'tocomplex';
is_pdl cdouble(1)->tocomplex, cdouble(1), 'tocomplex already complex';
is_pdl float(1)->tocomplex, cfloat(1), 'tocomplex float';
is_pdl cfloat(1)->tocomplex, cfloat(1), 'tocomplex float already complex';

# stringification
{
my $x = pdl( -3..2 ) + 1e7;
Expand Down

0 comments on commit 81a6883

Please sign in to comment.