PDL::Math has floor
, ceil
, and rint
. All these functions work in place.
Therefore, something like the following should work:
#!/usr/bin/env perl
use warnings;
use strict;
use PDL;
my $pdl = 'PDL'->new(
[ 1, 1.3, 1.9, 2, 2.1, 2.7 ],
[ -1, -1.3, -1.9, -2, -2.1, -2.7 ]
);
print $pdl;
floor(inplace $pdl->where($pdl >= 0));
ceil (inplace $pdl->where($pdl < 0));
print $pdl;
Output:
[
[ 1 1.3 1.9 2 2.1 2.7]
[ -1 -1.3 -1.9 -2 -2.1 -2.7]
]
[
[ 1 1 1 2 2 2]
[-1 -1 -1 -2 -2 -2]
]
PS: @choroba's answer seems to run about 20% faster in the following benchmark with non-threaded perl
5.24 on an ancient MacBook Pro:
#!/usr/bin/env perl
use warnings;
use strict;
use constant N_ELEMS => $ARGV[0] || 100_000;
use Dumbbench;
use PDL;
sub one_scan {
my $pdl = 100 * grandom(N_ELEMS);
$pdl = floor(abs($pdl)) * ($pdl <=> 0);
return;
}
sub two_scans {
my $pdl = 100 * grandom(N_ELEMS);
floor(inplace $pdl->where($pdl >= 0));
ceil (inplace $pdl->where($pdl < 0));
return;
}
sub baseline {
my $pdl = 100 * grandom(N_ELEMS);
return;
}
my $bench = Dumbbench->new;
$bench->add_instances(
Dumbbench::Instance::PerlSub->new(code => \&baseline, name => 'Baseline'),
Dumbbench::Instance::PerlSub->new(code => \&one_scan, name => 'One Scan'),
Dumbbench::Instance::PerlSub->new(code => \&two_scans, name => 'Two Scans'),
);
$bench->run;
$bench->report;