Skip to content

Commit

Permalink
error if Ops::carg(real), or Ops::czip complex inputs - #511
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 6, 2025
1 parent 4977f0d commit b6ae6fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- partly restoring pre-2.096 xform type-selection: if xform given no typed outputs, and non-available (greater than last-given type) typed inputs, use last-given (#511, https://github.com/moocow-the-bovine/PDL-CCS/issues/18)
- fix Math::polyroots with native-complex input and supplied null output
- add Pars type-spec "!real" which makes it an error to supply real values (#511)
- now an error to call Ops::carg on a real value, or Ops::czip on complex inputs (#511)

2.098 2025-01-03
- fix Windows build problems
Expand Down
18 changes: 9 additions & 9 deletions lib/PDL/Ops.pd
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ ufunc('exp','exp',1,'the exponential function',GenericTypes => [@$C, @$F]);
ufunc('log','log',1,'the natural logarithm',GenericTypes => [@$C, @$F], Exception => '$a() <= 0');

# no export these because clash with Test::Deep (re) or internal (_*abs)
cfunc('re', 'creal', 1, 'Returns the real part of a complex number.',
cfunc('re', 'creal', 1, 0, 'Returns the real part of a complex number.',
'$complexv() = $b() + I * cimag($complexv());'
);
cfunc('im', 'cimag', 1, 'Returns the imaginary part of a complex number.',
cfunc('im', 'cimag', 1, 0, 'Returns the imaginary part of a complex number.',
'$complexv() = creal($complexv()) + I * $b();'
);
cfunc('_cabs', 'fabs', 1, 'Returns the absolute (length) of a complex number.', undef,
cfunc('_cabs', 'fabs', 1, 0, 'Returns the absolute (length) of a complex number.', undef,
PMFunc=>'',
);
my $rabs_code = '
Expand Down Expand Up @@ -476,14 +476,14 @@ PDL_IF_BAD(if (anybad) $PDLSTATESETBAD(b);,)
# special functions for complex data types that don't work well with
# the ufunc/bifunc logic
sub cfunc {
my ($name, $func, $make_real, $doc, $backcode, %extra) = @_;
my ($name, $func, $make_real, $force_complex, $doc, $backcode, %extra) = @_;
my $codestr = pp_line_numbers(__LINE__-1,"\$b() = $func(\$complexv());");
pp_def($name,
GenericTypes=>$C,
Pars => 'complexv(); '.($make_real ? 'real' : '').' [o]b()',
Pars => ($force_complex ? '!real ' : '').'complexv(); '.($make_real ? 'real' : '').' [o]b()',
HandleBad => 1,
NoBadifNaN => 1,
($make_real ? () : (Inplace => 1)),
(($make_real || $force_complex) ? () : (Inplace => 1)),
Code => pp_line_numbers(__LINE__-1, qq{
PDL_IF_BAD(if ( \$ISBAD(complexv()) ) \$SETBAD(b()); else,)
$codestr
Expand All @@ -502,11 +502,11 @@ PDL_IF_BAD(if ( \$ISBAD(complexv()) ) \$SETBAD(b()); else,)
);
}

cfunc('carg', 'carg', 1, 'Returns the polar angle of a complex number.', undef);
cfunc('conj', 'conj', 0, 'complex conjugate.', undef);
cfunc('carg', 'carg', 1, 1, 'Returns the polar angle of a complex number.', undef);
cfunc('conj', 'conj', 0, 0, 'complex conjugate.', undef);

pp_def('czip',
Pars => 'r(); i(); complex [o]c()',
Pars => '!complex r(); !complex i(); complex [o]c()',
Doc => <<'EOF',
convert real, imaginary to native complex, (sort of) like LISP zip
function. Will add the C<r> ndarray to "i" times the C<i> ndarray. Only
Expand Down
10 changes: 10 additions & 0 deletions t/nat_complex.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ ok !$x->type->real, 'complex type not real';
ok double->real, 'real type is real';
ok !$x->sumover->type->real, 'sumover type=complex';

for (qw(conj re im)) {
eval {double(5)->$_};
is $@, '', "NO error if give real data to $_";
}
for (qw(carg)) {
eval {double(5)->$_};
like $@, qr/must be complex/, "error if give real data to $_";
}
eval {czip(cdouble(5),1)};
like $@, qr/must be real/, "error if give complex data to czip";
$x = cdouble(2,3);
$x-=i2C(3);
is type($x), 'cdouble', 'type promotion ndarray - i';
Expand Down

0 comments on commit b6ae6fd

Please sign in to comment.