package math.matrix; import java.io.Serializable; /** * LU Decomposition. *

* For an m-by-n matrix A with m ≥ n, the LU decomposition is an m-by-n * unit lower triangular matrix L, an n-by-n upper triangular matrix U, * and a permutation vector piv of length m so that A(piv,:) = L*U. * If m < n, then L is m-by-m and U is m-by-n. *

* The LU decompostion with pivoting always exists, even if the matrix is * singular, so the constructor will never fail. The primary use of the * LU decomposition is in the solution of square systems of simultaneous * linear equations. This will fail if isNonsingular() returns false. */ public class LUDecomposition implements Serializable { /** * UID */ private static final long serialVersionUID = -3852210156107961377L; /** * Array for internal storage of decomposition. * @serial internal array storage */ private double[][] LU; /** * @serial row dimension */ private int m; /** * @serial column dimension */ private int n; /** * @serial pivot sign */ private int pivsign; /** * Internal storage of pivot vector. * @serial pivot vector. */ private int[] piv; /** * LU Decomposition * Structure to access L, U and piv. * @param A Rectangular matrix */ public LUDecomposition(Matrix A){ int i, j, k; // Use a "left-looking", dot-product, Crout/Doolittle algorithm. LU = A.getCopy(); m = A.getM(); n = A.getN(); piv = new int[m]; for(i=0; i Math.abs(LUcolj[p])) p = i; if(p!=j){ for(k=0; k This constructor computes L and U with the "daxpy"-based elimination algorithm used in LINPACK and MATLAB. In Java, we suspect the dot-product, Crout algorithm will be faster. We have temporarily included this constructor until timing experiments confirm this suspicion.

Structure to access L, U and piv. @param A Rectangular matrix @param linpackflag Use Gaussian elimination. Actual value ignored. */ public LUDecomposition(Matrix A, int linpackflag){ int i, j, k; // Initialize. LU = A.getCopy(); m = A.getM(); n = A.getN(); piv = new int[m]; for(i=0; i Math.abs(LU[p][k])) p = i; // Exchange if necessary. if(p != k) { for(j=0; jj) L[i][j] = LU[i][j]; else if(i==j) L[i][j] = 1.0; else L[i][j] = 0.0; } } return X; } /** * Return upper triangular factor * @return U */ public Matrix getU(){ int i, j; Matrix X = new Matrix(n,n); double[][] U = X.getD(); for(i=0; i=0; k--) for(j=0; j