Skip to content

Commit

Permalink
error to specify Inplace between differently-typed Pars - #511
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Dec 28, 2024
1 parent 2905c81 commit 53e2da5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
- xforms now select datatype from outputs only if can (#511)
- xforms now give error if supply output with trans_parent needing converting (#511)
- eigens_sym now sorts output like EISPACK
- Math::isfinite no longer works inplace due to mismatch between Pars (#511)
- now an error to specify Inplace between differently-typed Pars (#511)

2.095 2024-11-03
- add PDL_GENTYPE_IS_{REAL,FLOATREAL,COMPLEX,SIGNED,UNSIGNED}_##ppsym (#502)
Expand Down
26 changes: 11 additions & 15 deletions lib/PDL/Math.pd
Original file line number Diff line number Diff line change
Expand Up @@ -307,26 +307,23 @@ elsif ($Config{cc} =~ /\bgcc/i) {
);
} # elsif: cc =~ /\bgcc/i

pp_def(
'isfinite',
Pars => 'a(); int [o]mask();',
Inplace => 1,
HandleBad => 1,
Code =>'
pp_def('isfinite',
Pars => 'a(); int [o]mask();',
HandleBad => 1,
Code => <<'EOF',
broadcastloop %{
$mask() = isfinite((double) $a()) != 0 PDL_IF_BAD(&& $ISGOOD($a()),);
$mask() = isfinite((double) $a()) != 0 PDL_IF_BAD(&& $ISGOOD($a()),);
%}
$PDLSTATESETGOOD(mask);
',
Doc =>
EOF
Doc =>
'Sets C<$mask> true if C<$a> is not a C<NaN> or C<inf> (either positive or negative). Works inplace.',
BadDoc =>
BadDoc =>
'Bad values are treated as C<NaN> or C<inf>.',
);
);

# Extra functions from cephes
pp_def(
"erfi",
pp_def("erfi",
HandleBad => 1,
NoBadifNaN => 1,
GenericTypes => $F,
Expand All @@ -339,8 +336,7 @@ pp_def(
else,) { $b() = SQRTH*ndtri((1+(double)$a())/2); }',
);

pp_def(
"ndtri",
pp_def("ndtri",
HandleBad => 1,
NoBadifNaN => 1,
GenericTypes => $F,
Expand Down
7 changes: 7 additions & 0 deletions lib/PDL/PP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,13 @@ EOD
confess "ERROR in pp_def($name): Inplace Pars $in and $out inds ".join('=',@$in_ind{qw(Name Value)})." and ".join('=',@$out_ind{qw(Name Value)})." not compatible"
if $in_ind->{Value} != $out_ind->{Value};
}
my ($in_flags, $out_flags) = map [sort grep /^FlagType/, keys %$_], $in_obj, $out_obj;
confess "ERROR in pp_def($name): Inplace args $in and $out have different type specifications '@$in_flags' vs '@$out_flags'"
if "@$in_flags" ne "@$out_flags";
confess "ERROR in pp_def($name): Inplace args $in and $out have different type specifications '@$in_obj{@$in_flags}' vs '@$out_obj{@$out_flags}"
if "@$in_obj{@$in_flags}" ne "@$out_obj{@$out_flags}";
confess "ERROR in pp_def($name): Inplace args $in and $out have different type specifications '@{[($in_obj->{Type}//'NONE')]}' vs '@{[($out_obj->{Type}//'NONE')]}'"
if ($in_obj->{Type}//'NONE') ne ($out_obj->{Type}//'NONE');
[$in, $out];
}),
PDL::PP::Rule->new(["InplaceCode"], [qw(InplaceNormalised)],
Expand Down
2 changes: 1 addition & 1 deletion lib/PDL/Primitive.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ broadcastloop %{
###########################################################

pp_def('fibonacci',
Pars => 'i(n); indx [o]x(n)',
Pars => 'i(n); [o]x(n)',
Inplace => 1,
GenericTypes => [ppdefs_all],
Doc=>'Constructor - a vector with Fibonacci\'s sequence',
Expand Down
4 changes: 2 additions & 2 deletions t/bad.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use PDL::LiteF;
use PDL::Math;
use PDL::Types qw(types);
use Test::Warn;
use Test::PDL;

# although approx() caches the tolerance value, we
# use it in every call just to document things
Expand Down Expand Up @@ -338,8 +339,7 @@ $x->setbadat(1);
$y = hist $x, 0, 6, 1;
is( PDL::Core::string($y), "[0 2 2 2 2 1]", "hist()" );

$x->inplace->isfinite;
is( PDL::Core::string($x), "[1 0 1 1 1 1 1 1 1 1]", "isfinite()" );
is_pdl $x->isfinite, long("[1 0 1 1 1 1 1 1 1 1]"), "isfinite()";

# histogram2d
$x = long(1,1,1,2,2);
Expand Down
14 changes: 14 additions & 0 deletions t/pp_croaking.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ eval { pp_def( "func", Code => ';',
) };
like $@, qr/Invalid OtherPars name/;

for (
'short a(o,c); short [o]b(o,c)',
) {
eval { pp_def( "func", Code => ';', Pars => $_, Inplace => 1) };
is $@, '', "Pars=>'$_'";
}
for (
'a(); int [o]mask();',
'r(m=2); float+ [o]p(m=2);',
) {
eval { pp_def( "func", Code => ';', Pars => $_, Inplace => 1) };
like $@, qr/have different type specifications/, "Pars=>'$_'";
}

eval { pp_def( "func", Code => ';',
Pars => "[o] a();",
Inplace => ['a'],
Expand Down
2 changes: 1 addition & 1 deletion t/primitive-misc.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ subtest glue => sub {
};

subtest 'fibonacci' => sub {
is_pdl fibonacci(15), indx('1 1 2 3 5 8 13 21 34 55 89 144 233 377 610'), 'Fibonacci sequence';
is_pdl fibonacci(15), pdl('1 1 2 3 5 8 13 21 34 55 89 144 233 377 610'), 'Fibonacci sequence';
};

subtest 'indadd' => sub {
Expand Down

0 comments on commit 53e2da5

Please sign in to comment.