Skip to content

Commit

Permalink
use transpose everywhere appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed May 31, 2021
1 parent 1e02c53 commit 26e238e
Show file tree
Hide file tree
Showing 37 changed files with 86 additions and 90 deletions.
4 changes: 2 additions & 2 deletions Basic/Bad/bad.pd
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ it is possible to perform this calculation over I<any> dimension.
=for example
$spectrum = nbadover $image->xchg(0,1)
$spectrum = nbadover $image->transpose
=for bad
Expand Down Expand Up @@ -551,7 +551,7 @@ I<any> dimension.
=for example
$spectrum = ngoodover $image->xchg(0,1)
$spectrum = ngoodover $image->transpose
=for bad
Expand Down
2 changes: 1 addition & 1 deletion Basic/Complex/complex.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ sub sum {
sub sumover{
my $m = shift;
PDL::Ufunc::sumover($m->xchg(0,1));
PDL::Ufunc::sumover($m->transpose);
}
*PDL::Complex::Csumover=\&sumover; # define through alias
Expand Down
2 changes: 1 addition & 1 deletion Basic/Core/pdlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ void pdl_make_physical(pdl *it) {
* called for this ndarray and results in associated memory leaks!
* On the other hand, if I comment out !(it->state & PDL_ALLOCATED)
* then we get errors for cases like
* $in = $lut->xchg(0,1)->index($im->dummy(0));
* $in = $lut->transpose->index($im->dummy(0));
* $in .= pdl -5;
* Currently ugly fix: detect in initthreadstruct that it has been called before
* and free all pdl_thread related memory before reallocating
Expand Down
8 changes: 2 additions & 6 deletions Basic/Matrix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ sub string {
my ($me,@a) = shift;
return $me->SUPER::string(@a) unless($me->ndims > 0);
$me = $me->dummy(1,1) unless($me->ndims > 1);
$me->xchg(0,1)->SUPER::string(@a);
$me->transpose->SUPER::string(@a);
}


Expand All @@ -132,11 +132,7 @@ constructs an object of class PDL::Matrix which is an ndarray child class.

sub pdl {
my $class = shift;
my $pdl = $class->SUPER::pdl(@_);
if($pdl->ndims > 0) {
$pdl = $pdl->dummy(1,1) unless $pdl->ndims > 1;
$pdl = $pdl->xchg(0,1);
}
my $pdl = $class->SUPER::pdl(@_)->transpose;
bless $pdl, ref $class || $class;
}

Expand Down
28 changes: 14 additions & 14 deletions Basic/MatrixOps/matrixops.pd
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ sub inv {
my $idenA = $x->zeros;
$idenA->diagonal(0,1) .= 1;
my $out = lu_backsub($lu,$perm,$par,$idenA)->xchg(0,1)->sever;
my $out = lu_backsub($lu,$perm,$par,$idenA)->transpose->sever;
return $out
unless($x->is_inplace);
Expand Down Expand Up @@ -457,7 +457,7 @@ pp_def("eigens_sym",
barf "Need real square matrix for eigens_sym"
if $#d < 1 or $d[0] != $d[1];
my ($n) = $d[0];
my ($sym) = 0.5*($x + $x->mv(0,1));
my ($sym) = 0.5*($x + $x->transpose);
my ($err) = PDL::max(abs($sym));
barf "Need symmetric component non-zero for eigens_sym"
if $err == 0;
Expand All @@ -479,7 +479,7 @@ pp_def("eigens_sym",
&PDL::_eigens_sym_int($lt, $ev, $e);
return $ev->xchg(0,1), $e
return $ev->transpose, $e
if(wantarray);
$e; #just eigenvalues
}
Expand All @@ -489,7 +489,7 @@ pp_def("eigens_sym",
Eigenvalues and -vectors of a symmetric square matrix. If passed
an asymmetric matrix, the routine will warn and symmetrize it, by taking
the average value. That is, it will solve for 0.5*($a+$a->mv(0,1)).
the average value. That is, it will solve for 0.5*($a+$a->transpose).
It\'s threadable, so if C<$a> is 3x3x100, it\'s treated as 100 separate 3x3
matrices, and both C<$ev> and C<$e> get extra dimensions accordingly.
Expand Down Expand Up @@ -618,7 +618,7 @@ pp_def("eigens",
my $n = $d[0];
barf "Need real square matrix for eigens"
if $#d < 1 or $d[0] != $d[1];
my $deviation = PDL::max(abs($x - $x->mv(0,1)))/PDL::max(abs($x));
my $deviation = PDL::max(abs($x - $x->transpose))/PDL::max(abs($x));
if ( $deviation <= 1e-5 ) {
#taken from eigens_sym code
Expand All @@ -631,7 +631,7 @@ pp_def("eigens",
&PDL::_eigens_sym_int($lt, $ev, $e);
return $ev->xchg(0,1), $e if wantarray;
return $ev->transpose, $e if wantarray;
return $e; #just eigenvalues
}
else {
Expand All @@ -652,7 +652,7 @@ pp_def("eigens",
&PDL::_eigens_int($x->clump(0,1), $ev, $e);
return $ev->index(0)->xchg(0,1)->sever, $e->index(0)->sever
return $ev->index(0)->transpose->sever, $e->index(0)->sever
if(wantarray);
return $e->index(0)->sever; #just eigenvalues
}
Expand Down Expand Up @@ -1158,28 +1158,28 @@ Solve A x = B for matrix A, by back substitution into A's LU decomposition.
# using lu_backsub
($lu,$perm,$par) = lu_decomp($A);
$x = lu_backsub($lu,$perm,$par, $B->xchg(0,1))->xchg(0,1);
$x = lu_backsub($lu,$perm,$par, $B->transpose)->transpose;
# or with Slatec LINPACK
use PDL::Slatec;
gefa($lu=$A->copy, $ipiv=null, $info=null);
# 1 = do transpose because Fortran's idea of rows vs columns
gesl($lu, $ipiv, $x=$B->xchg(0,1)->copy, 1);
$x = $x->inplace->xchg(0,1);
gesl($lu, $ipiv, $x=$B->transpose->copy, 1);
$x = $x->inplace->transpose;
# or with LAPACK
use PDL::LinearAlgebra::Real;
getrf($lu=$A->copy, $ipiv=null, $info=null);
getrs($lu, 1, $x=$B->xchg(0,1)->copy, $ipiv, $info=null); # again, need transpose
$x=$x->inplace->xchg(0,1);
getrs($lu, 1, $x=$B->transpose->copy, $ipiv, $info=null); # again, need transpose
$x=$x->inplace->transpose;
# or with GSL
use PDL::GSL::LINALG;
LU_decomp(my $lu=$A->copy, my $p=null, my $signum=null);
# $B and $x, first dim is because GSL treats as vector, higher dims thread
# so we transpose in and back out
LU_solve($lu, $p, $B->xchg(0,1), my $x=null);
$x=$x->inplace->xchg(0,1);
LU_solve($lu, $p, $B->transpose, my $x=null);
$x=$x->inplace->transpose;
# proof of the pudding is in the eating:
print $A x $x;
Expand Down
4 changes: 2 additions & 2 deletions Basic/Pod/PP.pod
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ of arrays 'in-place'. It will thread automatically - e.g. if
a 2D array is given it will be called repeatedly for each
1D row (again check L<PDL::Indexing> for the details of threading).
And then b() will be a 1D array of sums of each row.
We could call it with $x->xchg(0,1) to sum the columns instead.
We could call it with $x->transpose to sum the columns instead.
And Dataflow tracing etc. will be available.

You can see PP saves the programmer from writing a lot of
Expand Down Expand Up @@ -815,7 +815,7 @@ do? From its signature you see that it takes a 2D matrix with equal numbers
of columns and rows and outputs a matrix of the same size. From a given
input matrix $a it computes a symmetric output matrix $c (symmetric in
the matrix sense that A^T = A where ^T means matrix transpose, or in PDL
parlance $c == $c->xchg(0,1)). It does this by using only the values
parlance $c == $c->transpose). It does this by using only the values
on and below the diagonal of $a. In the output matrix $c all values on
and below the diagonal are the same as those in $a while those above the
diagonal are a mirror image of those below the diagonal (above and below
Expand Down
6 changes: 3 additions & 3 deletions Basic/SourceFilter/NiceSlice.pm
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ PDL::NiceSlice - toward a nicer slicing syntax for PDL
$x(1:4) .= 2; # concise syntax for ranges
print $y((0),1:$end); # use variables in the slice expression
$x->xchg(0,1)->(($pos-1)) .= 0; # default method syntax
$x->transpose->(($pos-1)) .= 0; # default method syntax
$idx = long 1, 7, 3, 0; # an ndarray of indices
$x(-3:2:2,$idx) += 3; # mix explicit indexing and ranges
Expand Down Expand Up @@ -673,7 +673,7 @@ The second syntax that will be recognized is what I called the
I<default method> syntax. It is the method arrow C<-E<gt>> directly
followed by an open parenthesis, e.g.
$x->xchg(0,1)->(($pos)) .= 0;
$x->transpose->(($pos)) .= 0;
Note that this conflicts with the use of normal code references, since you
can write in plain Perl
Expand Down Expand Up @@ -704,7 +704,7 @@ The first syntax C<$x(args)> doesn't work with chained method calls. E.g.
won't work. It can I<only> be used directly following a valid perl variable
name. Instead, use the I<default method> syntax in such cases:
$x->xchg(0,1)->(0);
$x->transpose->(0);
Similarly, if you have a list of ndarrays C<@pdls>:
Expand Down
8 changes: 4 additions & 4 deletions Basic/Ufunc/ufunc.pd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ I<any> dimension.
=for example
\$spectrum = $op \$image->xchg(0,1)
\$spectrum = $op \$image->transpose
$extras
Expand Down Expand Up @@ -96,7 +96,7 @@ is the first element of the parameter.
=for example
\$spectrum = $op \$image->xchg(0,1)
\$spectrum = $op \$image->transpose
$extras
Expand Down Expand Up @@ -840,7 +840,7 @@ I<any> dimension.
=for example
$spectrum = pctover $image->xchg(0,1), $p
$spectrum = pctover $image->transpose, $p
=cut
Expand Down Expand Up @@ -884,7 +884,7 @@ I<any> dimension.
=for example
$spectrum = oddpctover $image->xchg(0,1), $p
$spectrum = oddpctover $image->transpose, $p
=cut
Expand Down
2 changes: 1 addition & 1 deletion Demos/General.pm
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ act q|
|;

act q|
output $y->xchg(0,1);
output $y->transpose;
output $y->minimum,"\n"; # over first dim.
output $y->min,"\n";
|;
Expand Down
2 changes: 1 addition & 1 deletion Demos/TriD/test5.p
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $g->bind_default("pts");

$y = PDL->zeroes(3,30,30);
axisvalues($b->slice("(0)"));
axisvalues($b->slice("(1)")->xchg(0,1));
axisvalues($b->slice("(1)")->transpose);

$y /= 30;

Expand Down
4 changes: 2 additions & 2 deletions Demos/TriDGallery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ $i=$cz-$cx-$cy;$q=$i*$n;points3d[$y*sin$q,$r*cos$q,$g*sin$q],[$r,$g,$y]}
actnw q~
# Fractal mountain range [Tuomas Lukka]
use PDL;use PDL::Image2D;use PDL::Graphics::TriD; keeptwiddling3d(); $k=ones(5,5) / 25;
$x=5;$y=ones(1,1)/2;for(1..7){$c=$y->dummy(0,2)->clump(2)->xchg(0,1)->
dummy(0,2)->clump(2)->xchg(0,1)->copy;$c+=$x*$c->random;$x/=3;
$x=5;$y=ones(1,1)/2;for(1..7){$c=$y->dummy(0,2)->clump(2)->transpose->
dummy(0,2)->clump(2)->transpose->copy;$c+=$x*$c->random;$x/=3;
$y=conv2d($c,$k); imag3d[$y],{Lines => 0}; }
~;

Expand Down
4 changes: 2 additions & 2 deletions Graphics/TriD/Rout/rout.pd
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ sub PDL::Graphics::TriD::Contours::contour_segments {
$this->{ContourSegCnt}[$i] = $pcnt;
$pcnt=$pcnt+1;
$this->{Points} = $this->{Points}->append($segs->slice(":,0:$ncnt,($i)")->xchg(0,1));
$this->{Points} = $this->{Points}->append($segs->slice(":,0:$ncnt,($i)")->transpose);
}
$this->{Points} = $this->{Points}->xchg(0,1);
$this->{Points} = $this->{Points}->transpose;
}
EOD
Expand Down
10 changes: 5 additions & 5 deletions Graphics/TriD/TriD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ PDL::Graphics::TriD -- PDL 3D interface
# Generate a somewhat interesting sequence of points:
$t = sequence(100)/10;
$x = sin($t); $y = cos($t), $z = $t;
$coords = cat($x, $y, $z)->xchg(0,1);
$coords = cat($x, $y, $z)->transpose;
my $red = cos(2*$t); my $green = sin($t); my $blue = $t;
$colors = cat($red, $green, $blue)->xchg(0,1);
$colors = cat($red, $green, $blue)->transpose;
# After each graph, let the user rotate and
# wait for him to press 'q', then make new graph
Expand All @@ -23,9 +23,9 @@ PDL::Graphics::TriD -- PDL 3D interface
$surf1 = (rvals(100, 100) / 50)**2 + sin(xvals(100, 100) / 10);
$surf2 = sqrt(rvals(zeroes(50,50))/2);
$x = sin($surface); $y = cos($surface), $z = $surface;
$coords = cat($x, $y, $z)->xchg(0,1);
$coords = cat($x, $y, $z)->transpose;
$red = cos(2*$surface); $green = sin($surface); $blue = $surface;
$colors = cat($red, $green, $blue)->xchg(0,1);
$colors = cat($red, $green, $blue)->transpose;
imagrgb([$red,$green,$blue]); # 2-d ndarrays
lattice3d([$surf1]);
points3d([$x,$y,$z]);
Expand Down Expand Up @@ -225,7 +225,7 @@ meaningful surface (unless you're into fractals, perhaps).
# the data to look like
# ((x1, y1, z1), (x2, y2, z2), ...)
# which is why we have to do the exchange:
$coords = cat($x, $y, $z)->xchg(0,1);
$coords = cat($x, $y, $z)->transpose;
print "again, with a different coordinate syntax (press q when you're done twiddling)\n";
line3d $coords;
Expand Down
4 changes: 2 additions & 2 deletions Graphics/TriD/TriD/Contours.pm
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ sub addlabels{

}
if($lp->nelem>0){
$self->{Points} = $self->{Points}->xchg(0,1)
->append($lp->reshape(3,$lp->nelem/3)->xchg(0,1))->xchg(0,1);
$self->{Points} = $self->{Points}->transpose
->append($lp->reshape(3,$lp->nelem/3)->transpose)->transpose;
$self->{Labels} = [$cnt+1,$cnt+$lp->nelem/3];
$self->{LabelStrings} = $strlist;
$self->{Options}{Font}=$font;
Expand Down
4 changes: 2 additions & 2 deletions Graphics/TriD/TriD/MathGraph.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sub step {
$this->{Velo} *=
((0.96*50/(50+sqrt(($this->{Velo}**2)->sumover->dummy(0)))))**$tst;
$c += $tst * 0.05 * $this->{Velo};
(my $tmp = $c->xchg(0,1)->index($this->{FInd}->dummy(0)))
(my $tmp = $c->transpose->index($this->{FInd}->dummy(0)))
.= $this->{FCoord}
if (defined $this->{FInd});
print "C: $c\n" if $verbose;
Expand Down Expand Up @@ -155,7 +155,7 @@ sub step {
$this->{Velo} *=
((0.92*50/(50+sqrt(($this->{Velo}**2)->sumover->dummy(0)))))**$tst;
$c += $tst * 0.05 * $this->{Velo};
(my $tmp = $c->xchg(0,1)->index($this->{FInd}->dummy(0)))
(my $tmp = $c->transpose->index($this->{FInd}->dummy(0)))
.= $this->{FCoord}
if (defined $this->{FInd});
print "C: $c\n" if $verbose;
Expand Down
2 changes: 1 addition & 1 deletion Graphics/TriD/TriD/Mesh.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sub new {
Data => $data,
};
PDL::Primitive::axisvalues($this->{Vertices}->slice('(0),:,:'));
PDL::Primitive::axisvalues($this->{Vertices}->slice('(1),:,:')->xchg(0,1));
PDL::Primitive::axisvalues($this->{Vertices}->slice('(1),:,:')->transpose);
PDL::Ops::assgn($this->{Data},$this->{Vertices}->slice('(2),:,:'));
bless $this,$type;
}
Expand Down
2 changes: 1 addition & 1 deletion Graphics/TriD/VRML/VRML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ sub to_text {
lines($this->{Points},$cols,$seq,
\$vtxt,\$ctxt,\$vidx,tabs($level+1));
lines($this->{Points}->xchg(1,2),$cols->xchg(1,2),
$seq->xchg(0,1),undef,\$ctxt,\$vidx,
$seq->transpose,undef,\$ctxt,\$vidx,
tabs($level+1)) if $this->{IsLattice};
$useidx = 1;
} elsif ($this->{Title} eq 'IndexedFaceSet') {
Expand Down
6 changes: 3 additions & 3 deletions IO/FITS/FITS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ sub _rfits_bintable ($$$$) {
}

$tmpcol->{data}->hdrcpy(1);
my $td = $tmpcol->{data}->xchg(0,1);
my $td = $tmpcol->{data}->transpose;
$tbl->{$tmpcol->{name}} = $td->reshape($td->dim(0),@tdims);
} else {
print STDERR "rfits: WARNING: invalid TDIM$i field in binary table. Ignoring.\n";
Expand All @@ -1264,7 +1264,7 @@ sub _rfits_bintable ($$$$) {
$tbl->{$tmpcol->{name}} =
( ( $tmpcol->{data}->dim(0) == 1 )
? $tmpcol->{data}->slice("(0)")
: $tmpcol->{data}->xchg(0,1)
: $tmpcol->{data}->transpose
);
}
}
Expand Down Expand Up @@ -1485,7 +1485,7 @@ sub _rfits_unpack_zimage($$$) {
if($tbl->{UNCOMPRESSED_DATA}->dim(1) != $tilesize) {
die "rfits: tile size is $tilesize, but uncompressed data rows have size ".$tbl->{UNCOMPRESSED_DATA}->dim(1)."\n";
}
$tiles->dice_axis(1,$patchup) .= $tbl->{UNCOMPRESSED_DATA}->dice_axis(0,$patchup)->xchg(0,1);
$tiles->dice_axis(1,$patchup) .= $tbl->{UNCOMPRESSED_DATA}->dice_axis(0,$patchup)->transpose;
}

##########
Expand Down
2 changes: 1 addition & 1 deletion IO/Misc/misc.pd
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ if ( $options->{VERBOSE} ) {
# fix 2D pdls to match what wcols generates
foreach my $col (@ret) {
next if ref($col) eq "ARRAY";
$col = $col->mv(0,1) if $col->ndims == 2;
$col = $col->transpose if $col->ndims == 2;
}
wantarray ? return(@ret) : return $ret[0];
Expand Down

0 comments on commit 26e238e

Please sign in to comment.