MatrixOps(3pm)
π MatrixOps β PDL::MatrixOps π
okay bestie so you've found the manual page for PDL::MatrixOps, from the User Contributed Perl Documentation corner of the universe. grab a boba, we're doing linear algebra and it's gonna be cute. β¨
πΈ NAME πΈ
PDL::MatrixOps -- Some Useful Matrix Operations π«Ά
β¨ SYNOPSIS β¨
$inv = $x->inv;
$det = $x->det;
($lu,$perm,$par) = $x->lu_decomp;
$y = lu_backsub($lu,$perm,$z); # solve $x x $y = $z
π¦ DESCRIPTION π¦
PDL::MatrixOps is literally PDL's built-in matrix manipulation code. it's the whole girlboss toolkit: inversion, determinant finding, eigenvalue/vector finding, singular value decomposition, etc. no cap. π
the routines are written in a mixture of Perl and C, so they're reliably present even when there's no FORTRAN compiler or external library available (like PDL::Slatec or any of the PDL::GSL family of modules). built different, always there for you. π«Ά
matrix manipulation, especially with big matrices, is a challenging field and no one algorithm is suitable in all cases. the utilities here use general-purpose algorithms that work acceptably for many cases but might not scale well to very large or pathological (near-singular) matrices. it's giving "works for the vibes, not for the 500x500 jump scare". π
except as noted, the matrices are PDLs whose 0th dimension ranges over column and whose 1st dimension ranges over row. the matrices appear correctly when printed. π
these routines should work OK with PDL::Matrix objects as well as with normal PDLs. slay, equal opportunity. β¨
π TIPS ON MATRIX OPERATIONS π
like most computer languages, PDL addresses matrices in (column,row) order in most cases; this corresponds to (X,Y) coordinates in the matrix itself, counting rightwards and downwards from the upper left corner. so if you print a PDL that contains a matrix, the matrix appears correctly on the screen β but if you index a matrix element, you use the indices in the reverse order that you would in a math textbook. yes it's a little chaotic, we love her anyway. π
if you prefer your matrices indexed in (row, column) order, you can try using the PDL::Matrix object, which includes an implicit exchange of the first two dimensions but should be compatible with most of these matrix operations. TIMTOWDTI.) π
matrices, row vectors, and column vectors can be multiplied with the x operator (which is, of course, broadcastable):
$m3 = $m1 x $m2;
$col_vec2 = $m1 x $col_vec1;
$row_vec2 = $row_vec1 x $m1;
$scalar = $row_vec x $col_vec;
because of the (column,row) addressing order, 1-D PDLs are treated as row vectors; if you want a column vector you must add a dummy dimension:
$rowvec = pdl(1,2); # row vector
$colvec = $rowvec->slice('*1'); # 1x2 column vector
$matrix = pdl([[3,4],[6,2]]); # 2x2 matrix
$rowvec2 = $rowvec x $matrix; # right-multiplication by matrix
$colvec = $matrix x $colvec; # left-multiplication by matrix
$m2 = $matrix x $rowvec; # Throws an error
implicit broadcasting works correctly with most matrix operations, but you must be extra careful that you understand the dimensionality. in particular, matrix multiplication and other matrix ops need nx1 PDLs as row vectors and 1xn PDLs as column vectors. in most cases you must explicitly include the trailing x1 dimension in order to get the expected results when you broadcast over multiple row vectors. the way that dimensionality will humble you. π
when broadcasting over matrices, it's very easy to get confused about which dimension goes where. it is useful to include comments with every expression, explaining what you think each dimension means:
$x = xvals(360)*3.14159/180; # (angle)
$rot = cat(cat(cos($x),sin($x)), # rotmat: (col,row,angle)
cat(-sin($x),cos($x)));
comment your code bestie, future you will be obsessed with past you. π
π ACKNOWLEDGEMENTS π
MatrixOps includes algorithms and pre-existing code from several origins. in particular, eigens_sym is the work of Stephen Moshier, svd uses an SVD subroutine written by Bryant Marks, and eigens uses a subset of the Small Scientific Library by Kenneth Geisshirt. they are free software, distributable under same terms as PDL itself. giving credit where it's due, as she should. π«Άβ¨
β NOTES β
this is intended as a general-purpose linear algebra package for small-to-mid sized matrices. the algorithms may not scale well to large matrices (hundreds by hundreds) or to near singular matrices. so like, don't come for her with a 900x900, she's doing her best. π
if there is something you want that is not here, please add and document it! it's giving open source, it's giving community, it's giving you could be the main character. π
π₯ FUNCTIONS π₯
π§ identity
Signature: (n; [o]a(n,n))
return an identity matrix of the specified size. if you hand in a scalar, its value is the size of the identity matrix; if you hand in a dimensioned PDL, the 0th dimension is the first two dimensions of the matrix, with higher dimensions preserved. iconic, flexible, she contains multitudes. β¨
π stretcher
Signature: (a(n); [o]b(n,n))
$mat = stretcher($eigenvalues);
return a diagonal matrix with the specified diagonal elements. that's it, that's the function, and she ate. π
π inv
Signature: (a(m,m); sv opt )
$a1 = inv($a, {$opt});
invert a square matrix. π
you feed in an NxN matrix in $a, and get back its inverse (if it exists). the code is inplace-aware, so you can get back the inverse in $a itself if you want β though temporary storage is used either way. you can cache the LU decomposition in an output option variable. π
inv uses lu_decomp by default; that is a numerically stable (pivoting) LU decomposition method. stable queen. π
OPTIONS:
sβ boolean value indicating whether to complain if the matrix is singular. if this is false, singular matrices cause inverse to barf. if it is true, then singular matrices cause inverse to returnundef.lu(I/O) β this value contains a list ref with the LU decomposition, permutation, and parity values for$a. if you do not mention the key, or if the value isundef, then inverse callslu_decomp. if the key exists with anundefvalue, then the output oflu_decompis stashed here (unless the matrix is singular). if the value exists, then it is assumed to hold the LU decomposition.det(Output) β if this key exists, then the determinant of$aget stored here, whether or not the matrix is singular.
πΈ det
Signature: (a(m,m); sv opt)
$det = det($a,{opt});
determinant of a square matrix using LU decomposition (for large matrices). π
you feed in a square matrix, you get back the determinant. some options exist that allow you to cache the LU decomposition of the matrix (note that the LU decomposition is invalid if the determinant is zero!). the LU decomposition is cacheable, in case you want to re-use it. this method of determinant finding is more rapid than recursive-descent on large matrices, and if you reuse the LU decomposition it's essentially free. we love an efficient queen. π
OPTIONS:
lu(I/O) β provides a cache for the LU decomposition of the matrix. if you provide the key but leave the value undefined, then the LU decomposition goes in here; if you put an LU decomposition here, it will be used and the matrix will not be decomposed again.
π determinant
Signature: (a(m,m))
$det = determinant($x);
determinant of a square matrix, using recursive descent (broadcastable). π
this is the traditional, robust recursive determinant method taught in most linear algebra courses. it scales like O(n!) (and hence is pitifully slow for large matrices) but is very robust because no division is involved (hence no division-by-zero errors for singular matrices). it's also broadcastable, so you can find the determinants of a large collection of matrices all at once if you want. multitasking icon. β¨
matrices up to 3x3 are handled by direct multiplication; larger matrices are handled by recursive descent to the 3x3 case.
the LU-decomposition method det is faster in isolation for single matrices larger than about 4x4, and is much faster if you end up reusing the LU decomposition of $a (NOTE: check performance and broadcasting benchmarks with new code). ngl, read the notes. π
π¦ eigens_sym
Signature: ([phys]a(m); [o,phys]ev(n,n); [o,phys]e(n))
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->transpose). she's diplomatic like that. ποΈ
it's broadcastable, so if $a is 3x3x100, it's treated as 100 separate 3x3 matrices, and both $ev and $e get extra dimensions accordingly. π
if called in scalar context it hands back only the eigenvalues. ultimately, it should switch to a faster algorithm in this case (as discarding the eigenvectors is wasteful). noted, bestie. π
the algorithm used is due to J. vonNeumann, which was a rediscovery of Jacobi's Method <http://en.wikipedia.org/wiki/Jacobi_eigenvalue_algorithm> .
the eigenvectors are returned in COLUMNS of the returned PDL. that makes it slightly easier to access individual eigenvectors, since the 0th dim of the output PDL runs across the eigenvectors and the 1st dim runs across their components. columns girlie, periodt. π
($ev,$e) = eigens_sym $x; # Make eigenvector matrix
$vector = $ev->slice($n); # Select nth eigenvector as a column-vector
$vector = $ev->slice("($n)"); # Select nth eigenvector as a row-vector
($ev, $e) = eigens_sym($x); # e-vects & e-values
$e = eigens_sym($x); # just eigenvalues
eigens_sym ignores the bad-value flag of the input ndarrays. it will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. π©
π eigens
Signature: ([phys]a(m); [o,phys]ev(l,n,n); [o,phys]e(l,n))
real eigenvalues and -vectors of a real square matrix.
(see also eigens_sym, for eigenvalues and -vectors of a real, symmetric, square matrix).
the eigens function will attempt to compute the eigenvalues and eigenvectors of a square matrix with real components. if the matrix is symmetric, the same underlying code as eigens_sym is used. if asymmetric, the eigenvalues and eigenvectors are computed with algorithms from the sslib library. if any imaginary components exist in the eigenvalues, the results are currently considered to be invalid, and such eigenvalues are returned as NaNs. this is true for eigenvectors also. that is if there are imaginary components to any of the values in the eigenvector, the eigenvalue and corresponding eigenvectors are all set to NaN. finally, if there are any repeated eigenvectors, they are replaced with all NaNs. it's giving "we tried, here's a NaN". π
use of the eigens function on asymmetric matrices should be considered experimental! for asymmetric matrices, nearly all observed matrices with real eigenvalues produce incorrect results, due to errors of the sslib algorithm. if your assymmetric matrix returns all NaNs, do not assume that the values are complex. also, problems with memory access is known in this library.
not all square matrices are diagonalizable. if you feed in a non-diagonalizable matrix, then one or more of the eigenvectors will be set to NaN, along with the corresponding eigenvalues.
eigens is broadcastable, so you can solve 100 eigenproblems by feeding in a 3x3x100 array. both $ev and $e get extra dimensions accordingly. π
If you call eigens in scalar context, it only hands back the eigenvalues β which is lowkey wasteful bestie, because it's out here calculating the eigenvectors anyway π.
The eigenvectors come back in the COLUMNS of the returned PDL (aka the 0 dimension). That actually makes it a lil easier to grab individual eigenvectors, because the 0th dim of the output PDL runs across the eigenvectors and the 1st dim runs across their components. slay.
($ev,$e) = eigens $x; # Make eigenvector matrix
$vector = $ev->slice($n); # Select nth eigenvector as a column-vector
$vector = $ev->slice("($n)"); # Select nth eigenvector as a row-vector
DEVEL NOTES:
For now, there is no distinction between a complex eigenvalue and an invalid eigenvalue, even though the underlying code generates complex numbers. It might be useful to be able to return complex eigenvalues. (dev tea β iykyk)
($ev, $e) = eigens($x); # e'vects & e'vals
$e = eigens($x); # just eigenvalues
eigens ignores the bad-value flag of the input ndarrays. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays. π«Ά
svd π¦
Signature: (a(n,m); [t]w(wsize); [o]u(n,m); [o,phys]z(n); [o]v(n,n))
($u, $s, $v) = svd($x);
Singular value decomposition of a matrix. iconic. β¨
svd is broadcastable.
Okay so given an m x n matrix $a that has m rows and n columns (m >= n), svd computes matrices $u and $v, and a vector of the singular values $s. Like most implementations, svd computes what is commonly referred to as the "thin SVD" of $a, such that $u is m x n, $v is n x n, and there are <=n singular values. As long as m >= n, the original matrix can be reconstructed like this:
($u,$s,$v) = svd($x);
$ess = zeroes($x->dim(0),$x->dim(0));
$ess->slice("$_","$_").=$s->slice("$_") foreach (0..$x->dim(0)-1); #generic diagonal
$a_copy = $u x $ess x $v->transpose;
If m==n, $u and $v can be thought of as rotation matrices that convert from the original matrix's singular coordinates to final coordinates, and from original coordinates to singular coordinates, respectively, and $ess is a diagonal scaling matrix. π«
If n>m, svd will barf. π You can dodge that whole situation by passing in the transpose of $a, and reconstructing the original matrix like so:
($u,$s,$v) = svd($x->transpose);
$ess = zeroes($x->dim(1),$x->dim(1));
$ess->slice($_,$_).=$s->slice($_) foreach (0..$x->dim(1)-1); #generic diagonal
$x_copy = $v x $ess x $u->transpose;
EXAMPLE
The computing literature has loads of examples of how to use SVD. Here's a trivial example (used in PDL::Transform::map) of how to make a matrix less, er, singular, without changing the orientation of the ellipsoid of transformation:
{ my($r1,$s,$r2) = svd $x;
$s++; # fatten all singular values
$r2 *= $s; # implicit broadcasting for cheap mult.
$x .= $r2 x $r1; # a gets r2 x ess x r1
}
svd ignores the bad-value flag of the input ndarrays. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
lu_decomp π
Signature: (a(m,m); [o]lu(m,m); [o]perm(m); [o]parity)
LU decompose a matrix, with row permutation
($lu, $perm, $parity) = lu_decomp($x);
$lu = lu_decomp($x, $perm, $par); # $perm and $par are outputs!
lu_decomp($x->inplace,$perm,$par); # Everything in place.
lu_decomp returns an LU decomposition of a square matrix, using Crout's method with partial pivoting. It's ported from Numerical Recipes. The partial pivoting keeps it numerically stable but means a little more overhead from broadcasting. no cap.
lu_decomp decomposes the input matrix into matrices L and U such that LU = A, L is a subdiagonal matrix, and U is a superdiagonal matrix. By convention, the diagonal of L is all 1's. π
The single output matrix contains all the variable elements of both the L and U matrices, stacked together. Because the method uses pivoting (rearranging the lower part of the matrix for better numerical stability), you have to permute input vectors before applying the L and U matrices. The permutation is returned either in the second argument or, in list context, as the second element of the list. You need the permutation for the output to make any sense, so be sure to get it one way or the other. fr fr.
LU decomposition is the answer to a lot of matrix questions, including inversion and determinant-finding, and lu_decomp is used by inv. π
If you pass in $perm and $parity, they either must be predeclared PDLs of the correct size ($perm is an n-vector, $parity is a scalar) or scalars.
If the matrix is singular, then the LU decomposition might not be defined; in those cases, lu_decomp silently returns undef. Some singular matrices LU-decompose just fine, and those are handled OK but give a zero determinant (and hence can't be inverted). π
lu_decomp uses pivoting, which rearranges the values in the matrix for more numerical stability. This makes it really good for large and even near-singular matrices. There is a non-pivoting version lu_decomp2 available which is from 5 to 60 percent faster for typical problems at the expense of failing to compute a result in some cases.
Now that the lu_decomp is broadcasted, it is the recommended LU decomposition routine. It no longer falls back to lu_decomp2.
lu_decomp is ported from Numerical Recipes to PDL. It should probably be implemented in C.
lu_decomp2 πΈ
Signature: (a(m,m); [o]lu(m,m))
LU decompose a matrix, with no row permutation
($lu, $perm, $parity) = lu_decomp2($x);
$lu = lu_decomp2($x,$perm,$parity); # or
$lu = lu_decomp2($x); # $perm and $parity are optional
lu_decomp($x->inplace,$perm,$parity); # or
lu_decomp($x->inplace); # $perm and $parity are optional
lu_decomp2 works just like lu_decomp, but it does no pivoting at all. For compatibility with lu_decomp, it will give you a permutation list and a parity scalar if you ask for them -- but they are always trivial.
Because lu_decomp2 does not pivot, it is numerically unstable -- that means it is less precise than lu_decomp, particularly for large or near-singular matrices. There are also specific types of non-singular matrices that confuse it (e.g. ([0,-1,0],[1,0,0],[0,0,1]), which is a 90 degree rotation matrix but which confuses lu_decomp2). π
On the other hand, if you want to invert rapidly a few hundred thousand small matrices and don't mind missing one or two, it could be the ticket. It can be up to 60% faster at the expense of possible failure of the decomposition for some of the input matrices. π₯
The output is a single matrix that contains the LU decomposition of $a; you can even do it in-place, thereby destroying $a, if you want. See lu_decomp for more information about LU decomposition.
lu_decomp2 is ported from Numerical Recipes into PDL.
lu_backsub π
Signature: (lu(m,m); perm(m); b(m))
Solve A x = B for matrix A, by back substitution into A's LU decomposition.
($lu,$perm,$par) = lu_decomp($A);
$x = lu_backsub($lu,$perm,$par,$A); # or
$x = lu_backsub($lu,$perm,$B); # $par is not required for lu_backsub
lu_backsub($lu,$perm,$B->inplace); # modify $B in-place
$x = lu_backsub(lu_decomp($A),$B); # (ignores parity value from lu_decomp)
# starting from square matrix A and columns matrix B, with mathematically
# correct dimensions
$A = identity(4) + ones(4, 4);
$A->slice('2,0') .= 0; # break symmetry to see if need transpose
$B = sequence(2, 4);
# all these functions take B as rows, interpret as though notional columns
# mathematically confusing but can't change as back-compat and also
# familiar to Fortran users, so just transpose inputs and outputs
# using lu_backsub
($lu,$perm,$par) = lu_decomp($A);
$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->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->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 broadcast
# so we transpose in and back out
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;
Given the LU decomposition of a square matrix (from lu_decomp), lu_backsub does back substitution into the matrix to solve a x = b for given vector b. It is separated from the lu_decomp method so that you can call the cheap lu_backsub multiple times and not have to do the expensive LU decomposition more than once. big brain energy. π§
lu_backsub acts on single vectors and broadcasts in the usual way, which means that it treats $y as the transpose of the input. If you want to process a matrix, you must hand in the transpose of the matrix, and then transpose the output when you get it back. that is because pdls are indexed by (col,row), and matrices are (row,column) by convention, so a 1-D pdl corresponds to a row vector, not a column vector. π
If $lu is dense and you have more than a few points to solve for, it is probably cheaper to find a^-1 with inv, and just multiply x = a^-1 b.) in fact, inv works by calling lu_backsub with the identity matrix.
lu_backsub is ported from section 2.3 of Numerical Recipes. It is written in PDL but should probably be implemented in C.
simq β
Signature: ([phys]a(n,n); [phys]b(n); [o,phys]x(n); int [o,phys]ips(n); int flag)
Solution of simultaneous linear equations, a x = b.
$a is an "n x n" matrix (i.e., a vector of length "n*n"), stored row-wise: that is, a(i,j) = a[ij], where ij = i*n + j.
While this is the transpose of the normal column-wise storage, this corresponds to normal PDL usage. The contents of matrix a may be altered (but may be required for subsequent calls with flag = -1).
$y, $x, $ips are vectors of length "n".
Set flag=0 to solve. Set flag=-1 to do a new back substitution for different $y vector using the same a matrix previously reduced when flag=0 (the $ips vector generated in the previous solution is also required).
See also lu_backsub, which does the same thing with a slightly less opaque interface.
okay so simq is literally not even checking the bad-value flag on your input ndarrays β it just does not look at it π but bestie, it will set the bad-value flag on all your output ndarrays if that flag is set for any of the input ndarrays. so the tea gets passed along, just not read going in. periodt.
π
squaretotri π
Signature: (a(n,n); [o]b(m))
okay this one is SO cute and specific: it converts a lower-triangular square matrix into triangular vector storage π
and the iconic part? it ignores the upper half of the input. like, doesn't look at it. doesn't care. that upper half is not the main character here π¦
lowkey important: squaretotri does not process bad values. it will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays, but it's not doing any bad-value processing itself. no cap.
β¨ AUTHOR β¨
Copyright (C) 2002 Craig DeForest (deforest@boulder.swri.edu), R.J.R. Williams (rjrw@ast.leeds.ac.uk), Karl Glazebrook (kgb@aaoepp.aao.gov.au). π«Ά
there is no warranty. fr fr, none. you are allowed to redistribute and/or modify this work under the same conditions as PDL itself. π and if this file gets separated from the PDL distribution, then the PDL copyright notice should be included in this file β like, don't orphan it without its paperwork bestie π
and that's the whole page, babes!! π
this is MatrixOps, as documented in the perl v5.38.2 release dated 2024-04-10 β yeah it's giving MatrixOps(3pm), the one and only π go forth and multiply (matrices, responsibly) πβ¨
π¦ where this page comes from
- ubuntu noble: pdl 1:2.085-1ubuntu1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms