.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "MatrixOps 3pm" .TH MatrixOps 3pm "2023-06-17" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" PDL::MatrixOps \-\- Some Useful Matrix Operations .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& $inv = $x\->inv; \& \& $det = $x\->det; \& \& ($lu,$perm,$par) = $x\->lu_decomp; \& $y = lu_backsub($lu,$perm,$z); # solve $x x $y = $z .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" PDL::MatrixOps is \s-1PDL\s0's built-in matrix manipulation code. It contains utilities for many common matrix operations: inversion, determinant finding, eigenvalue/vector finding, singular value decomposition, etc. PDL::MatrixOps routines are written in a mixture of Perl and C, so that they are reliably present even when there is no \&\s-1FORTRAN\s0 compiler or external library available (e.g. PDL::Slatec or any of the \s-1PDL::GSL\s0 family of modules). .PP Matrix manipulation, particularly with large 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. .PP 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. .PP These routines should work \s-1OK\s0 with PDL::Matrix objects as well as with normal PDLs. .SH "TIPS ON MATRIX OPERATIONS" .IX Header "TIPS ON MATRIX OPERATIONS" Like most computer languages, \s-1PDL\s0 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. This means that if you print a \s-1PDL\s0 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. 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. \s-1TIMTOWDTI.\s0) .PP Matrices, row vectors, and column vectors can be multiplied with the 'x' operator (which is, of course, broadcastable): .PP .Vb 4 \& $m3 = $m1 x $m2; \& $col_vec2 = $m1 x $col_vec1; \& $row_vec2 = $row_vec1 x $m1; \& $scalar = $row_vec x $col_vec; .Ve .PP 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: .PP .Vb 6 \& $rowvec = pdl(1,2); # row vector \& $colvec = $rowvec\->slice(\*(Aq*1\*(Aq); # 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 .Ve .PP 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. .PP 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: .PP .Vb 3 \& $x = xvals(360)*3.14159/180; # (angle) \& $rot = cat(cat(cos($x),sin($x)), # rotmat: (col,row,angle) \& cat(\-sin($x),cos($x))); .Ve .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" MatrixOps includes algorithms and pre-existing code from several origins. In particular, \f(CW\*(C`eigens_sym\*(C'\fR is the work of Stephen Moshier, \&\f(CW\*(C`svd\*(C'\fR uses an \s-1SVD\s0 subroutine written by Bryant Marks, and \f(CW\*(C`eigens\*(C'\fR uses a subset of the Small Scientific Library by Kenneth Geisshirt. They are free software, distributable under same terms as \s-1PDL\s0 itself. .SH "NOTES" .IX Header "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. .PP If there is something you want that is not here, please add and document it! .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "identity" .IX Subsection "identity" .Vb 1 \& Signature: (n; [o]a(n,n)) .Ve .PP 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 \s-1PDL,\s0 the 0th dimension is the first two dimensions of the matrix, with higher dimensions preserved. .SS "stretcher" .IX Subsection "stretcher" .Vb 1 \& Signature: (a(n); [o]b(n,n)) .Ve .PP .Vb 1 \& $mat = stretcher($eigenvalues); .Ve .PP Return a diagonal matrix with the specified diagonal elements .SS "inv" .IX Subsection "inv" .Vb 1 \& Signature: (a(m,m); sv opt ) .Ve .PP .Vb 1 \& $a1 = inv($a, {$opt}); .Ve .PP Invert a square matrix. .PP You feed in an NxN matrix in \f(CW$a\fR, and get back its inverse (if it exists). The code is inplace-aware, so you can get back the inverse in \f(CW$a\fR itself if you want \*(-- though temporary storage is used either way. You can cache the \s-1LU\s0 decomposition in an output option variable. .PP \&\f(CW\*(C`inv\*(C'\fR uses \f(CW\*(C`lu_decomp\*(C'\fR by default; that is a numerically stable (pivoting) \s-1LU\s0 decomposition method. .PP \&\s-1OPTIONS:\s0 .IP "\(bu" 3 s .Sp 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 return undef. .IP "\(bu" 3 lu (I/O) .Sp This value contains a list ref with the \s-1LU\s0 decomposition, permutation, and parity values for \f(CW$a\fR. If you do not mention the key, or if the value is undef, then inverse calls \f(CW\*(C`lu_decomp\*(C'\fR. If the key exists with an undef value, then the output of \f(CW\*(C`lu_decomp\*(C'\fR is stashed here (unless the matrix is singular). If the value exists, then it is assumed to hold the \s-1LU\s0 decomposition. .IP "\(bu" 3 det (Output) .Sp If this key exists, then the determinant of \f(CW$a\fR get stored here, whether or not the matrix is singular. .SS "det" .IX Subsection "det" .Vb 1 \& Signature: (a(m,m); sv opt) .Ve .PP .Vb 1 \& $det = det($a,{opt}); .Ve .PP Determinant of a square matrix using \s-1LU\s0 decomposition (for large matrices) .PP You feed in a square matrix, you get back the determinant. Some options exist that allow you to cache the \s-1LU\s0 decomposition of the matrix (note that the \s-1LU\s0 decomposition is invalid if the determinant is zero!). The \s-1LU\s0 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 \s-1LU\s0 decomposition it's essentially free. .PP \&\s-1OPTIONS:\s0 .IP "\(bu" 3 lu (I/O) .Sp Provides a cache for the \s-1LU\s0 decomposition of the matrix. If you provide the key but leave the value undefined, then the \s-1LU\s0 decomposition goes in here; if you put an \s-1LU\s0 decomposition here, it will be used and the matrix will not be decomposed again. .SS "determinant" .IX Subsection "determinant" .Vb 1 \& Signature: (a(m,m)) .Ve .PP .Vb 1 \& $det = determinant($x); .Ve .PP Determinant of a square matrix, using recursive descent (broadcastable). .PP This is the traditional, robust recursive determinant method taught in most linear algebra courses. It scales like \f(CW\*(C`O(n!)\*(C'\fR (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. .PP Matrices up to 3x3 are handled by direct multiplication; larger matrices are handled by recursive descent to the 3x3 case. .PP The LU-decomposition method \*(L"det\*(R" is faster in isolation for single matrices larger than about 4x4, and is much faster if you end up reusing the \s-1LU\s0 decomposition of \f(CW$a\fR (\s-1NOTE:\s0 check performance and broadcasting benchmarks with new code). .SS "eigens_sym" .IX Subsection "eigens_sym" .Vb 1 \& Signature: ([phys]a(m); [o,phys]ev(n,n); [o,phys]e(n)) .Ve .PP 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). .PP It's broadcastable, so if \f(CW$a\fR is 3x3x100, it's treated as 100 separate 3x3 matrices, and both \f(CW$ev\fR and \f(CW$e\fR get extra dimensions accordingly. .PP 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). .PP The algorithm used is due to J. vonNeumann, which was a rediscovery of Jacobi's Method . .PP The eigenvectors are returned in \s-1COLUMNS\s0 of the returned \s-1PDL.\s0 That makes it slightly easier to access individual eigenvectors, since the 0th dim of the output \s-1PDL\s0 runs across the eigenvectors and the 1st dim runs across their components. .PP .Vb 3 \& ($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 .Ve .PP .Vb 2 \& ($ev, $e) = eigens_sym($x); # e\-vects & e\-values \& $e = eigens_sym($x); # just eigenvalues .Ve .PP 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. .SS "eigens" .IX Subsection "eigens" .Vb 1 \& Signature: ([phys]a(m); [o,phys]ev(l,n,n); [o,phys]e(l,n)) .Ve .PP Real eigenvalues and \-vectors of a real square matrix. .PP (See also \*(L"eigens_sym\*(R", for eigenvalues and \-vectors of a real, symmetric, square matrix). .PP 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 \*(L"eigens_sym\*(R" 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 \*(L"NaN\*(R"s. 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 \*(L"NaN\*(R". Finally, if there are any repeated eigenvectors, they are replaced with all \*(L"NaN\*(R"s. .PP 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. .PP 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. .PP \&\f(CW\*(C`eigens\*(C'\fR is broadcastable, so you can solve 100 eigenproblems by feeding in a 3x3x100 array. Both \f(CW$ev\fR and \f(CW$e\fR get extra dimensions accordingly. .PP If called in scalar context \f(CW\*(C`eigens\*(C'\fR hands back only the eigenvalues. This is somewhat wasteful, as it calculates the eigenvectors anyway. .PP The eigenvectors are returned in \s-1COLUMNS\s0 of the returned \s-1PDL\s0 (ie the the 0 dimension). That makes it slightly easier to access individual eigenvectors, since the 0th dim of the output \s-1PDL\s0 runs across the eigenvectors and the 1st dim runs across their components. .PP .Vb 3 \& ($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 .Ve .PP \&\s-1DEVEL NOTES:\s0 .PP For now, there is no distinction between a complex eigenvalue and an invalid eigenvalue, although the underlying code generates complex numbers. It might be useful to be able to return complex eigenvalues. .PP .Vb 2 \& ($ev, $e) = eigens($x); # e\*(Aqvects & e\*(Aqvals \& $e = eigens($x); # just eigenvalues .Ve .PP 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. .SS "svd" .IX Subsection "svd" .Vb 1 \& Signature: (a(n,m); [t]w(wsize); [o]u(n,m); [o,phys]z(n); [o]v(n,n)) .Ve .PP .Vb 1 \& ($u, $s, $v) = svd($x); .Ve .PP Singular value decomposition of a matrix. .PP \&\f(CW\*(C`svd\*(C'\fR is broadcastable. .PP Given an m x n matrix \f(CW$a\fR that has m rows and n columns (m >= n), \&\f(CW\*(C`svd\*(C'\fR computes matrices \f(CW$u\fR and \f(CW$v\fR, and a vector of the singular values \f(CW$s\fR. Like most implementations, \f(CW\*(C`svd\*(C'\fR computes what is commonly referred to as the \*(L"thin \s-1SVD\*(R"\s0 of \f(CW$a\fR, such that \f(CW$u\fR is m x n, \f(CW$v\fR is n x n, and there are <=n singular values. As long as m >= n, the original matrix can be reconstructed as follows: .PP .Vb 4 \& ($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; .Ve .PP If m==n, \f(CW$u\fR and \f(CW$v\fR 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 \f(CW$ess\fR is a diagonal scaling matrix. .PP If n>m, \f(CW\*(C`svd\*(C'\fR will barf. This can be avoided by passing in the transpose of \f(CW$a\fR, and reconstructing the original matrix like so: .PP .Vb 4 \& ($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; .Ve .PP \&\s-1EXAMPLE\s0 .PP The computing literature has loads of examples of how to use \s-1SVD.\s0 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: .PP .Vb 5 \& { 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 \& } .Ve .PP 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. .SS "lu_decomp" .IX Subsection "lu_decomp" .Vb 1 \& Signature: (a(m,m); [o]lu(m,m); [o]perm(m); [o]parity) .Ve .PP \&\s-1LU\s0 decompose a matrix, with row permutation .PP .Vb 1 \& ($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. .Ve .PP \&\f(CW\*(C`lu_decomp\*(C'\fR returns an \s-1LU\s0 decomposition of a square matrix, using Crout's method with partial pivoting. It's ported from \fINumerical Recipes\fR. The partial pivoting keeps it numerically stable but means a little more overhead from broadcasting. .PP \&\f(CW\*(C`lu_decomp\*(C'\fR decomposes the input matrix into matrices L and U such that \s-1LU\s0 = A, L is a subdiagonal matrix, and U is a superdiagonal matrix. By convention, the diagonal of L is all 1's. .PP 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. .PP \&\s-1LU\s0 decomposition is the answer to a lot of matrix questions, including inversion and determinant-finding, and \f(CW\*(C`lu_decomp\*(C'\fR is used by \*(L"inv\*(R". .PP If you pass in \f(CW$perm\fR and \f(CW$parity\fR, they either must be predeclared PDLs of the correct size ($perm is an n\-vector, \&\f(CW$parity\fR is a scalar) or scalars. .PP If the matrix is singular, then the \s-1LU\s0 decomposition might not be defined; in those cases, \f(CW\*(C`lu_decomp\*(C'\fR silently returns undef. Some singular matrices LU-decompose just fine, and those are handled \s-1OK\s0 but give a zero determinant (and hence can't be inverted). .PP \&\f(CW\*(C`lu_decomp\*(C'\fR 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 \f(CW\*(C`lu_decomp2\*(C'\fR available which is from 5 to 60 percent faster for typical problems at the expense of failing to compute a result in some cases. .PP Now that the \f(CW\*(C`lu_decomp\*(C'\fR is broadcasted, it is the recommended \&\s-1LU\s0 decomposition routine. It no longer falls back to \f(CW\*(C`lu_decomp2\*(C'\fR. .PP \&\f(CW\*(C`lu_decomp\*(C'\fR is ported from \fINumerical Recipes\fR to \s-1PDL.\s0 It should probably be implemented in C. .SS "lu_decomp2" .IX Subsection "lu_decomp2" .Vb 1 \& Signature: (a(m,m); [o]lu(m,m)) .Ve .PP \&\s-1LU\s0 decompose a matrix, with no row permutation .PP .Vb 1 \& ($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 .Ve .PP \&\f(CW\*(C`lu_decomp2\*(C'\fR works just like \*(L"lu_decomp\*(R", but it does \fBno\fR pivoting at all. For compatibility with \*(L"lu_decomp\*(R", it will give you a permutation list and a parity scalar if you ask for them \*(-- but they are always trivial. .PP Because \f(CW\*(C`lu_decomp2\*(C'\fR does not pivot, it is numerically \fBunstable\fR \*(-- that means it is less precise than \*(L"lu_decomp\*(R", 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 \f(CW\*(C`lu_decomp2\*(C'\fR). .PP 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. .PP The output is a single matrix that contains the \s-1LU\s0 decomposition of \f(CW$a\fR; you can even do it in-place, thereby destroying \f(CW$a\fR, if you want. See \&\*(L"lu_decomp\*(R" for more information about \s-1LU\s0 decomposition. .PP \&\f(CW\*(C`lu_decomp2\*(C'\fR is ported from \fINumerical Recipes\fR into \s-1PDL.\s0 .SS "lu_backsub" .IX Subsection "lu_backsub" .Vb 1 \& Signature: (lu(m,m); perm(m); b(m)) .Ve .PP Solve A x = B for matrix A, by back substitution into A's \s-1LU\s0 decomposition. .PP .Vb 1 \& ($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(\*(Aq2,0\*(Aq) .= 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\*(Aqt 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\*(Aqs 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; .Ve .PP Given the \s-1LU\s0 decomposition of a square matrix (from \*(L"lu_decomp\*(R"), \&\f(CW\*(C`lu_backsub\*(C'\fR does back substitution into the matrix to solve \&\f(CW\*(C`a x = b\*(C'\fR for given vector \f(CW\*(C`b\*(C'\fR. It is separated from the \&\f(CW\*(C`lu_decomp\*(C'\fR method so that you can call the cheap \f(CW\*(C`lu_backsub\*(C'\fR multiple times and not have to do the expensive \s-1LU\s0 decomposition more than once. .PP \&\f(CW\*(C`lu_backsub\*(C'\fR acts on single vectors and broadcasts in the usual way, which means that it treats \f(CW$y\fR as the \fItranspose\fR of the input. If you want to process a matrix, you must hand in the \fItranspose\fR 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. .PP If \f(CW$lu\fR is dense and you have more than a few points to solve for, it is probably cheaper to find \f(CW\*(C`a^\-1\*(C'\fR with \&\*(L"inv\*(R", and just multiply \f(CW\*(C`x = a^\-1 b\*(C'\fR.) in fact, \&\*(L"inv\*(R" works by calling \f(CW\*(C`lu_backsub\*(C'\fR with the identity matrix. .PP \&\f(CW\*(C`lu_backsub\*(C'\fR is ported from section 2.3 of \fINumerical Recipes\fR. It is written in \s-1PDL\s0 but should probably be implemented in C. .SS "simq" .IX Subsection "simq" .Vb 1 \& Signature: ([phys]a(n,n); [phys]b(n); [o,phys]x(n); int [o,phys]ips(n); int flag) .Ve .PP Solution of simultaneous linear equations, \f(CW\*(C`a x = b\*(C'\fR. .PP \&\f(CW$a\fR is an \f(CW\*(C`n x n\*(C'\fR matrix (i.e., a vector of length \f(CW\*(C`n*n\*(C'\fR), stored row-wise: that is, \f(CW\*(C`a(i,j) = a[ij]\*(C'\fR, where \f(CW\*(C`ij = i*n + j\*(C'\fR. .PP While this is the transpose of the normal column-wise storage, this corresponds to normal \s-1PDL\s0 usage. The contents of matrix a may be altered (but may be required for subsequent calls with flag = \-1). .PP \&\f(CW$y\fR, \f(CW$x\fR, \f(CW$ips\fR are vectors of length \f(CW\*(C`n\*(C'\fR. .PP Set \f(CW\*(C`flag=0\*(C'\fR to solve. Set \f(CW\*(C`flag=\-1\*(C'\fR to do a new back substitution for different \f(CW$y\fR vector using the same a matrix previously reduced when \&\f(CW\*(C`flag=0\*(C'\fR (the \f(CW$ips\fR vector generated in the previous solution is also required). .PP See also \*(L"lu_backsub\*(R", which does the same thing with a slightly less opaque interface. .PP simq 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. .SS "squaretotri" .IX Subsection "squaretotri" .Vb 1 \& Signature: (a(n,n); [o]b(m)) .Ve .PP Convert a lower-triangular square matrix to triangular vector storage. Ignores upper half of input. .PP 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. .SH "AUTHOR" .IX Header "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. You are allowed to redistribute and/or modify this work under the same conditions as \s-1PDL\s0 itself. If this file is separated from the \s-1PDL\s0 distribution, then the \&\s-1PDL\s0 copyright notice should be included in this file.