Skip to content

Commit

Permalink
move csqrt* to Math - #527
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 22, 2025
1 parent 81a6883 commit 4719490
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- fix IO::Pic problem with older IO::GD installed (#522) - thanks @shawnlaffan
- r2C and i2C now handle long double both real and complex (#524) - thanks @wlmb for report
- add "update_data_from" method as streamlined get_dataref/upd_data
- add Ops::csqrt{,_up} (#524) - thanks @wlmb
- add Math::csqrt{,_up} (#524) - thanks @wlmb
- Core::upd_data now sets DATACHANGED so will work for flowing ops
- remove CopyBadStatusCode key from PP (#517)
- badflag now not propagated back to inputs if set on outputs (#517)
Expand Down
35 changes: 35 additions & 0 deletions lib/PDL/Math.pd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ my $F = [map $_->ppsym, grep $_->real && !$_->integer, types()];
my $C = [ppdefs_complex()];
my @Rtypes = grep $_->real, types();
my @Ctypes = grep !$_->real, types();
my $AF = [map $_->ppsym, grep !$_->integer, types];
$AF = [(grep $_ ne 'D', @$AF), 'D']; # so defaults to D if non-float given

pp_addpm({At=>'Top'},<<'EOD');
use strict;
Expand Down Expand Up @@ -487,6 +489,39 @@ works with native-complex data.
($yr, $yi) = polyval($cr, $ci, $xr, $xi);
',);

pp_def('csqrt',
GenericTypes => $AF,
Pars => 'i(); complex [o] o()',
Doc => <<'EOF',
Take the complex square root of a number choosing that with
non-negative real part, i.e., a square root with a branch cut
'infinitesimally' below the negative real axis, the standard choice.
EOF
Code => q{
$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp=$i();
tmp=csqrt(tmp);
$o() = tmp;
},
);

pp_def('csqrt_up',
GenericTypes => $AF,
Pars => 'i(); complex [o] o()',
Doc => <<'EOF',
Take the complex square root of a number choosing that whose imaginary
part is not negative, i.e., it is a square root with a branch cut
'infinitesimally' below the positive real axis.
EOF
Code => q{
$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp=$i();
tmp=csqrt(tmp);
if(cimag(tmp)<0){
tmp = -tmp;
}
$o() = tmp;
},
);

pp_addpm({At=>'Bot'},<<'EOD');
=head1 BUGS
Expand Down
33 changes: 0 additions & 33 deletions lib/PDL/Ops.pd
Original file line number Diff line number Diff line change
Expand Up @@ -422,39 +422,6 @@ EOF
Code => '$c() = $r() + $i() * I;'
);

pp_def('csqrt',
GenericTypes => $AF,
Pars => 'i(); complex [o] o()',
Doc => <<'EOF',
Take the complex square root of a number choosing that with
non-negative real part, i.e., a square root with a branch cut
'infinitesimally' below the negative real axis, the standard choice.
EOF
Code => q{
$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp=$i();
tmp=csqrt(tmp);
$o() = tmp;
},
);

pp_def('csqrt_up',
GenericTypes => $AF,
Pars => 'i(); complex [o] o()',
Doc => <<'EOF',
Take the complex square root of a number choosing that whose imaginary
part is not negative, i.e., it is a square root with a branch cut
'infinitesimally' below the positive real axis.
EOF
Code => q{
$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp=$i();
tmp=csqrt(tmp);
if(cimag(tmp)<0){
tmp = -tmp;
}
$o() = tmp;
},
);

pp_def('ipow',
Inplace => [qw(a ans)],
Doc => qq{
Expand Down
40 changes: 40 additions & 0 deletions t/math.t
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@ is_pdl erf(0.5), pdl(1.-erfc(0.5)), "erf and erfc";
is_pdl erf(erfi(0.5)), pdl(0.5), "erfi (both ways)";
is_pdl erfi(erf(0.5)), pdl(0.5), "erfi (both ways)";

{ # csqrt
my $pi=4*atan2(1,1);
my $eiO = exp(i()*(sequence(8)-3)*$pi/4);
my $eiO2 = exp(i()*(sequence(8)-3)*$pi/8);
is_pdl csqrt($eiO), $eiO2, "csqrt of complex";
is_pdl csqrt(-1), i(), "csqrt of real -1";
my $squares="-9 -4 -1 0 1 4 9";
my $roots="3i 2i i 0 1 2 3";
is_pdl long($squares)->csqrt, cdouble($roots), "csqrt of long";
is_pdl longlong($squares)->csqrt, cdouble($roots), "csqrt of longlong";
is_pdl float($squares)->csqrt, cfloat($roots), "csqrt of float";
is_pdl double($squares)->csqrt, cdouble($roots), "csqrt of double";
is_pdl ldouble($squares)->csqrt, cldouble($roots), "csqrt of ldouble";
is_pdl cfloat($squares)->csqrt, cfloat($roots), "csqrt of cfloat";
is_pdl cdouble($squares)->csqrt, cdouble($roots), "csqrt of cdouble";
is_pdl cldouble($squares)->csqrt, cldouble($roots), "csqrt of cldouble";
is_pdl pdl('-2i')->csqrt, pdl('1-i');
}

{ # csqrt_up
my $pi=4*atan2(1,1);
my $eiO = exp(i()*sequence(8)*$pi/4);
my $eiO2 = exp(i()*sequence(8)*$pi/8);
my $sqrt=csqrt_up($eiO);
is_pdl($sqrt, $eiO2, "Square of csqrt_up of complex");
my $i=csqrt_up(-1);
is_pdl($i, i(), "csqrt_up of real -1");
my $squares="-9 -4 -1 0 1 4 9";
my $roots="3i 2i i 0 1 2 3";
is_pdl long($squares)->csqrt_up, cdouble($roots), "csqrt_up of long";
is_pdl longlong($squares)->csqrt_up, cdouble($roots), "csqrt_up of longlong";
is_pdl float($squares)->csqrt_up, cfloat($roots), "csqrt_up of float";
is_pdl double($squares)->csqrt_up, cdouble($roots), "csqrt_up of double";
is_pdl ldouble($squares)->csqrt_up, cldouble($roots), "csqrt_up of ldouble";
is_pdl cfloat($squares)->csqrt_up, cfloat($roots), "csqrt_up of cfloat";
is_pdl cdouble($squares)->csqrt_up, cdouble($roots), "csqrt_up of cdouble";
is_pdl cldouble($squares)->csqrt_up, cldouble($roots), "csqrt_up of cldouble";
is_pdl pdl('-2i')->csqrt_up, pdl('-1+i');
}

{
my $pa = pdl(0.0,30.0);
$pa->inplace->erf;
Expand Down
40 changes: 0 additions & 40 deletions t/ops.t
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,6 @@ if ($can_complex_power) {
}
}

{ # csqrt
my $pi=4*atan2(1,1);
my $eiO = exp(i()*(sequence(8)-3)*$pi/4);
my $eiO2 = exp(i()*(sequence(8)-3)*$pi/8);
is_pdl csqrt($eiO), $eiO2, "csqrt of complex";
is_pdl csqrt(-1), i(), "csqrt of real -1";
my $squares="-9 -4 -1 0 1 4 9";
my $roots="3i 2i i 0 1 2 3";
is_pdl long($squares)->csqrt, cdouble($roots), "csqrt of long";
is_pdl longlong($squares)->csqrt, cdouble($roots), "csqrt of longlong";
is_pdl float($squares)->csqrt, cfloat($roots), "csqrt of float";
is_pdl double($squares)->csqrt, cdouble($roots), "csqrt of double";
is_pdl ldouble($squares)->csqrt, cldouble($roots), "csqrt of ldouble";
is_pdl cfloat($squares)->csqrt, cfloat($roots), "csqrt of cfloat";
is_pdl cdouble($squares)->csqrt, cdouble($roots), "csqrt of cdouble";
is_pdl cldouble($squares)->csqrt, cldouble($roots), "csqrt of cldouble";
is_pdl pdl('-2i')->csqrt, pdl('1-i');
}

{ # csqrt_up
my $pi=4*atan2(1,1);
my $eiO = exp(i()*sequence(8)*$pi/4);
my $eiO2 = exp(i()*sequence(8)*$pi/8);
my $sqrt=csqrt_up($eiO);
is_pdl($sqrt, $eiO2, "Square of csqrt_up of complex");
my $i=csqrt_up(-1);
is_pdl($i, i(), "csqrt_up of real -1");
my $squares="-9 -4 -1 0 1 4 9";
my $roots="3i 2i i 0 1 2 3";
is_pdl long($squares)->csqrt_up, cdouble($roots), "csqrt_up of long";
is_pdl longlong($squares)->csqrt_up, cdouble($roots), "csqrt_up of longlong";
is_pdl float($squares)->csqrt_up, cfloat($roots), "csqrt_up of float";
is_pdl double($squares)->csqrt_up, cdouble($roots), "csqrt_up of double";
is_pdl ldouble($squares)->csqrt_up, cldouble($roots), "csqrt_up of ldouble";
is_pdl cfloat($squares)->csqrt_up, cfloat($roots), "csqrt_up of cfloat";
is_pdl cdouble($squares)->csqrt_up, cdouble($roots), "csqrt_up of cdouble";
is_pdl cldouble($squares)->csqrt_up, cldouble($roots), "csqrt_up of cldouble";
is_pdl pdl('-2i')->csqrt_up, pdl('-1+i');
}

{
is_pdl(r2C(long(1)), cdouble(1), "r2C of long");
is_pdl(r2C(longlong(1)), cdouble(1), "r2C of longlong");
Expand Down

0 comments on commit 4719490

Please sign in to comment.