Skip to content

Commit

Permalink
add Math::c{log,acos,asin,acosh,atanh} - fix #527
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 23, 2025
1 parent 4719490 commit 5ef1fe2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- 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
- add Math::c{log,acos,asin,acosh,atanh} (#527) - thanks @wlmb for suggestion

2.098 2025-01-03
- fix Windows build problems
Expand Down
52 changes: 25 additions & 27 deletions lib/PDL/Math.pd
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,25 @@ 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.
sub cequiv {
my ($func, $ref) = @_;
pp_def("c$func",
GenericTypes => $AF,
Pars => 'i(); complex [o] o()',
Doc => <<EOF,
=for ref\n
Takes real or complex data, returns the complex C<$func>.\n
Added in 2.099.
EOF
Code => q{
$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp=$i();
tmp=csqrt(tmp);
$o() = tmp;
},
);
Code => pp_line_numbers(__LINE__, <<EOF),
\$TFDEGCH(PDL_CFloat,PDL_CDouble,PDL_CLDouble,PDL_CFloat,PDL_CDouble,PDL_CLDouble) tmp = \$i();
tmp = c$func(tmp);
\$o() = tmp;
EOF
);
}

cequiv($_) for qw(sqrt log acos asin acosh atanh);

pp_def('csqrt_up',
GenericTypes => $AF,
Expand All @@ -512,23 +517,16 @@ 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;
},
Code => pp_line_numbers(__LINE__, <<'EOF'),
$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;
EOF
);

pp_addpm({At=>'Bot'},<<'EOD');
=head1 BUGS
Hasn't been tested on all platforms to ensure Cephes
versions are picked up automatically and used correctly.
=head1 AUTHOR
Copyright (C) R.J.R. Williams 1997 ([email protected]), Karl Glazebrook
Expand Down
6 changes: 6 additions & 0 deletions t/math.t
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ is_pdl erfi(erf(0.5)), pdl(0.5), "erfi (both ways)";
is_pdl pdl('-2i')->csqrt, pdl('1-i');
}

is_pdl cacosh(-1), pdl('3.141592i');
is_pdl clog(-1), pdl('3.141592i');
is_pdl cacos(-2), pdl('3.141592-1.316957i');
is_pdl casin(-2), pdl('-1.570796+1.316957i');
is_pdl tanh(catanh(-2)), cdouble(-2); # Cygwin catanh returns with negative Im

{ # csqrt_up
my $pi=4*atan2(1,1);
my $eiO = exp(i()*sequence(8)*$pi/4);
Expand Down

0 comments on commit 5ef1fe2

Please sign in to comment.