Skip to content

Commit

Permalink
replace zeroes() with OtherPars/RedoDimsCode - #460
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 25, 2024
1 parent eed557d commit ac6cfcb
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 80 deletions.
58 changes: 21 additions & 37 deletions Basic/Slices/slices.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1042,20 +1042,12 @@ pp_def(
'rld',
GenericTypes => [ppdefs_all],
Pars=>'indx a(n); b(n); [o]c(m);',
OtherPars=>'IV sumover_max => m',
PMCode =>pp_line_numbers(__LINE__, <<'EOD'),
sub PDL::rld {
my ($x,$y) = @_;
my ($c);
if ($#_ == 2) {
$c = $_[2];
} else {
# XXX Need to improve emulation of broadcasting in auto-generating c
my ($size) = $x->sumover->max->sclr;
my (@dims) = $x->dims;
shift @dims;
$c = $y->zeroes($size,@dims);
}
&PDL::_rld_int($x,$y,$c);
my ($c,$sm) = @_ == 3 ? ($_[2], $_[2]->dim(0)) : (PDL->null, $x->sumover->max->sclr);
PDL::_rld_int($x,$y,$c,$sm);
$c;
}
EOD
Expand Down Expand Up @@ -1217,37 +1209,32 @@ EOD
);

pp_def('rldvec',
Pars => 'indx a(N); b(M,N); [o]c(M,N)',
PMCode =>pp_line_numbers(__LINE__, <<'EOC'),
Pars => 'indx a(uniqvals); b(M,uniqvals); [o]c(M,decodedvals)',
OtherPars=>'IV sumover_max => decodedvals',
PMCode =>pp_line_numbers(__LINE__, <<'EOC'),
sub PDL::rldvec {
my ($a,$b,$c) = @_;
if (!defined($c)) {
# XXX Need to improve emulation of threading in auto-generating c
my ($rowlen) = $b->dim(0);
my ($size) = $a->sumover->max;
my (undef, @dims) = $a->dims;
$c = $b->zeroes($b->type,$rowlen,$size,@dims);
}
&PDL::_rldvec_int($a,$b,$c);
($c,my $sm) = defined($c) ? ($c,$c->dim(1)) : (PDL->null,$a->sumover->max->sclr);
PDL::_rldvec_int($a,$b,$c,$sm);
return $c;
}
EOC
Code =>pp_line_numbers(__LINE__, <<'EOC'),
PDL_Indx cn=0;
loop (N) %{
loop (uniqvals) %{
PDL_Indx i, nrows = $a();
for (i=0; i<nrows; i++) {
loop (M) %{ $c(N=>cn) = $b(); %}
loop (M) %{ $c(decodedvals=>cn) = $b(); %}
cn++;
}
%}
EOC
Doc =><<'EOD'
Doc =><<'EOD'
=for ref
Run-length decode a set of vectors, akin to a higher-order rld().
Given a vector $a() of the number of occurrences of each row, and a set $c()
Given a vector $a() of the number of occurrences of each row, and a set $b()
of row-vectors each of length $M, run-length decode to $c().
Can be used together with clump() to run-length decode "values" of arbitrary dimensions.
Expand Down Expand Up @@ -1295,29 +1282,26 @@ EOD
);

pp_def('rldseq',
Pars => 'indx a(N); b(N); [o]c(M)',
PMCode =>pp_line_numbers(__LINE__, <<'EOC'),
Pars => 'indx a(N); b(N); [o]c(M)',
OtherPars=>'IV sumover_max => M',
PMCode =>pp_line_numbers(__LINE__, <<'EOC'),
sub PDL::rldseq {
my ($a,$b,$c) = @_;
if (!defined($c)) {
my $size = $a->sumover->max;
my (undef, @dims) = $a->dims;
$c = $b->zeroes($b->type,$size,@dims);
}
&PDL::_rldseq_int($a,$b,$c);
($c,my $sm) = defined($c) ? ($c,$c->dim(1)) : (PDL->null,$a->sumover->max->sclr);
PDL::_rldseq_int($a,$b,$c,$sm);
return $c;
}
EOC
Code =>pp_line_numbers(__LINE__, <<'EOC'),
size_t mi=0;
PDL_Indx mi=0;
loop (N) %{
size_t len = $a(), li;
PDL_Indx len = $a(), li;
for (li=0; li < len; ++li, ++mi) {
$c(M=>mi) = $b() + li;
}
%}
EOC
Doc =><<'EOD'
Doc =><<'EOD'
=for ref
Run-length decode a subsequence vector.
Expand Down Expand Up @@ -1364,7 +1348,7 @@ sub rleND {
my @vdimsN = $data->dims;
##-- construct output pdls
my $counts = $#_ >= 0 ? $_[0] : zeroes(long, $vdimsN[$#vdimsN]);
my $counts = $#_ >= 0 ? $_[0] : PDL->null;
my $elts = $#_ >= 1 ? $_[1] : zeroes($data->type, @vdimsN);
##-- guts: call rlevec()
Expand Down
23 changes: 5 additions & 18 deletions Libtmp/Compression/compression.pd
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,13 @@ sub PDL::rice_compress {
) {
die("rice_compress: input needs to have type byte, short, ushort, or long, not ".($in->type)."\n");
}
# output buffer starts the same size; truncate at the end.
my ($out) = zeroes($in);
# lengths go here
my ($len) = zeroes(long, $in->slice("(0)")->dims);
PDL::_rice_compress_int( $in, $out, $len, $blocksize );
my $l = $len->max;
PDL::_rice_compress_int( $in, my $out=PDL->null, my $len=PDL->null, $blocksize );
my $l = $len->max->sclr;
$out = $out->slice("0:".($l-1))->sever;
return wantarray ? ($out, $in->dim(0), $blocksize, $len) : $out;
}
EOD
RedoDimsCode => '$SIZE(m) = $SIZE(n);', # output same size; truncate after
Code => <<'EOD',
int len;
char *err = rcomp( $P(in),
Expand All @@ -174,7 +171,8 @@ pp_def(
"rice_expand",
HandleBad=>0,
Pars=>'in(n); [o]out(m);',
OtherPars=>'int blocksize',
OtherPars=>'IV dim0 => m; int blocksize',
OtherParsDefaults=>{ blocksize=>32 },
GenericTypes=>['B','S','US','L'],
Doc=><<'EOD',
=for ref
Expand All @@ -185,17 +183,6 @@ Unsquishes a PDL that has been squished by rice_compress.
($out, $len, $blocksize, $dim0) = $pdl->rice_compress;
$copy = $out->rice_expand($dim0, $blocksize);
EOD
PMCode => <<'EOD',
sub PDL::rice_expand {
my $squished = shift;
my $dim0 =shift;
my $blocksize = shift || 32;
# Allocate output array
my $out = zeroes( $squished->slice("(0),*$dim0") );
PDL::_rice_expand_int( $squished, $out, $blocksize );
return $out;
}
EOD
Code=><<'EOD',
char *err = rdecomp( (unsigned char *)($P(in)),
Expand Down
8 changes: 4 additions & 4 deletions Libtmp/Filter/Linear.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use strict;
sub new($$) {
my($type,$npoints,$sigma) = @_;
my $cent = int($npoints/2);
my $x = ((PDL->zeroes($npoints )->xvals) - $cent)->float;
my $x = PDL->zeroes(float, $npoints )->xvals - $cent;
my $y = exp(-($x**2)/(2*$sigma**2));
# Normalize to unit total
$y /= sum($y);
Expand All @@ -77,8 +77,8 @@ use strict;
sub new($$) {
my($type,$deg,$nleft,$nright) = @_;
my $npoints = $nright + $nleft + 1;
my $x = ((PDL->zeroes($npoints )->xvals) - $nleft)->float;
my $mat1 = ((PDL->zeroes($npoints,$deg+1)->xvals))->float;
my $x = PDL->zeroes(float, $npoints )->xvals - $nleft;
my $mat1 = PDL->zeroes(float, $npoints,$deg+1)->xvals;
for(0..$deg-1) {
(my $tmp = $mat1->slice(":,($_)")) .= ($x ** $_);
}
Expand All @@ -88,4 +88,4 @@ sub new($$) {
Point => $nleft});
}


1;
20 changes: 10 additions & 10 deletions Libtmp/Slatec/Gaussian.pm
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ sub new {
}
my $ndims1 = ($#{$ndims==0} ? $ndims : [1]);
bless {
Mu => (PDL->zeroes (@$ndims1,@$nfuncs)->double),
ICV => (PDL->zeroes (@$ndims1,@$ndims1,@$nfuncs)->double),
CV => (PDL->zeroes (@$ndims1,@$ndims1,@$nfuncs)->double),
lnPrefactor=> (PDL->zeroes(@$nfuncs)->double),
EigVec => (PDL->zeroes (@$ndims1,@$ndims1,@$nfuncs)->double),
EigVal => (PDL->zeroes (@$ndims1,@$nfuncs)->double),
Mu => PDL->zeroes(PDL::double, @$ndims1,@$nfuncs),
ICV => PDL->zeroes(PDL::double, @$ndims1,@$ndims1,@$nfuncs),
CV => PDL->zeroes(PDL::double, @$ndims1,@$ndims1,@$nfuncs),
lnPrefactor=> PDL->zeroes(PDL::double, @$nfuncs),
EigVec => PDL->zeroes(PDL::double, @$ndims1,@$ndims1,@$nfuncs),
EigVal => PDL->zeroes(PDL::double, @$ndims1,@$nfuncs),
NDims => $ndims,
NFuncs => $nfuncs,
},$type;
Expand Down Expand Up @@ -193,9 +193,9 @@ sub upd_icovariance {
sub _eigs {
my($this,$mat) = @_;
my $tmpvec = $this->{EigVec}->float;
my $fvone = (PDL->zeroes(@{$this->{NDims}}))->float;
my $fvtwo = (PDL->zeroes(@{$this->{NDims}}))->float;
my $ierr = (PDL->zeroes(@{$this->{NFuncs}}))->long;
my $fvone = PDL->zeroes(PDL::float, @{$this->{NDims}});
my $fvtwo = PDL->zeroes(PDL::float, @{$this->{NDims}});
my $ierr = PDL->zeroes(PDL::long, @{$this->{NFuncs}});
my $tmp = $mat->float; # Copy, since is destroyed.
my $tmpval = $this->{EigVal}->float;

Expand Down Expand Up @@ -242,7 +242,7 @@ sub calc_value ($$$) {
sub calc_lnvalue ($$$) {
my($this,$xorig,$p) = @_;
my $x = $xorig;
my $muxed = (PDL->zeroes(@{$this->{NDims}},@{$p->{Dims}}))->double;
my $muxed = PDL->zeroes(PDL::double, @{$this->{NDims}},@{$p->{Dims}});

# print "MUXED1: $muxed\n";

Expand Down
6 changes: 3 additions & 3 deletions Libtmp/Slatec/LinPred.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ sub _mk_mat {

my $auc = $this->{AutoCor};

my $autocov = PDL::float PDL->zeroes($nl*2,$nl*2);
my $autocov = PDL->zeroes(PDL::float, $nl*2,$nl*2);
$this->{AutoCov} = $autocov;

my $sal = $this->{SymAutoCor}->px->lags(0,1,$this->{NLags})->px;
Expand All @@ -130,7 +130,7 @@ sub _mk_mat {

my $autocinv = inv($autocov);
# print "$autocinv,$auc,$n\n"; $auc->slice("$n:-1");
$this->{AutoSliceUsed} = PDL->zeroes(2*$nl)->float;
$this->{AutoSliceUsed} = PDL->zeroes(PDL::float, 2*$nl);

($tmp = $this->{AutoSliceUsed}->slice("0:$nl1"))
.= $auc->slice(($n+$nl-1).":$n");
Expand Down Expand Up @@ -264,7 +264,7 @@ sub new ($$) {
$this->{AutoCor} = $auc;
my $n = $this->{NTotLags};
$this->{SymAutoCor} =
(PDL->zeroes($n * 2 - 1)->float);
PDL->zeroes(PDL::float, $n * 2 - 1);
my $tmp;
($tmp = $this->{SymAutoCor}->slice("0:".($n-2))) .=
$auc->slice("-1:1");
Expand Down
2 changes: 1 addition & 1 deletion Libtmp/Slatec/slatec.pd
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ compute the determinant of an invertible matrix
=for example
$mat = zeroes(5,5); $mat->diagonal(0,1) .= 1; # unity matrix
$mat = identity(5); # unity matrix
$det = detslatec $mat;
Usage:
Expand Down
4 changes: 2 additions & 2 deletions Libtmp/Transform/Cartography/Cartography.pm
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ sub graticule {
$yp->flat->append($ym->flat)
)->mv(1,0);
} else {
our $pp = (zeroes($xp)-1); $pp->slice("(-1)") .= 0;
our $pm = (zeroes($xm)-1); $pm->slice("(-1)") .= 0;
our $pp = zeroes($xp)-1; $pp->slice("(-1)") .= 0;
our $pm = zeroes($xm)-1; $pm->slice("(-1)") .= 0;

if(wantarray) {
return ( pdl( $xp->flat->append($xm->flat),
Expand Down
8 changes: 3 additions & 5 deletions Libtmp/Transform/transform.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,7 @@ sub map {
croak("Whups -- got a nonlinear t_fits transformation. Can't deal with it.");
}
my $inv_sc_mat = zeroes($nd,$nd);
$inv_sc_mat->diagonal(0,1) .= $scale;
my $inv_sc_mat = PDL::MatrixOps::stretcher($scale);
my $mat = $f_in->{params}->{matrix} x $inv_sc_mat;
print "scale is $scale; mat is $mat\n" if($PDL::Transform::debug);
Expand Down Expand Up @@ -2456,7 +2455,7 @@ sub t_lookup {
my $maxvals = $p->{table}->clump($me->{idim})->maximum;
# Scale so that the range runs from 0 through the top pixel in the table
my $scale = ( pdl( $itable->dims )->slice("0:-2")-1 ) /
my $scale = ( $itable->shape->slice("0:-2")-1 ) /
(($maxvals - $minvals)+ (($maxvals-$minvals) == 0));
my $offset = - ($minvals * $scale);
Expand Down Expand Up @@ -3843,8 +3842,7 @@ sub t_spherical {
$me->{odim}=3;
$me->{params}->{origin} = _opt($o,['o','origin','Origin']);
$me->{params}->{origin} = PDL->zeroes(3)
unless defined($me->{params}->{origin});
$me->{params}->{origin} //= PDL->zeroes(3);
$me->{params}->{origin} = PDL->pdl($me->{params}->{origin});
$me->{params}->{deg} = _opt($o,['d','degrees','Degrees']);
Expand Down

0 comments on commit ac6cfcb

Please sign in to comment.