Source for file LUDecomposition.php
Documentation is available at LUDecomposition.php
* 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.
* @author Bartosz Matosiuk
* @author Michael Bommarito
const MatrixSingularException = "Can only perform operation on singular matrix.";
const MatrixSquareException = "Mismatched Row dimension";
* Internal storage of pivot vector.
* LU Decomposition constructor.
* @param $A Rectangular matrix
* @return Structure to access L, U and piv.
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
$this->LU = $A->getArray();
$this->m = $A->getRowDimension();
$this->n = $A->getColumnDimension();
for ($i = 0; $i < $this->m; ++ $i) {
$LUrowi = $LUcolj = array();
for ($j = 0; $j < $this->n; ++ $j) {
// Make a copy of the j-th column to localize references.
for ($i = 0; $i < $this->m; ++ $i) {
$LUcolj[$i] = &$this->LU[$i][$j];
// Apply previous transformations.
for ($i = 0; $i < $this->m; ++ $i) {
// Most of the time is spent in the following dot product.
for ($k = 0; $k < $kmax; ++ $k) {
$s += $LUrowi[$k] * $LUcolj[$k];
$LUrowi[$j] = $LUcolj[$i] -= $s;
// Find pivot and exchange if necessary.
for ($i = $j+ 1; $i < $this->m; ++ $i) {
if (abs($LUcolj[$i]) > abs($LUcolj[$p])) {
for ($k = 0; $k < $this->n; ++ $k) {
$this->LU[$p][$k] = $this->LU[$j][$k];
$this->piv[$p] = $this->piv[$j];
$this->pivsign = $this->pivsign * - 1;
if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) {
for ($i = $j+ 1; $i < $this->m; ++ $i) {
$this->LU[$i][$j] /= $this->LU[$j][$j];
} // function __construct()
* Get lower triangular factor.
* @return array Lower triangular factor
for ($i = 0; $i < $this->m; ++ $i) {
for ($j = 0; $j < $this->n; ++ $j) {
$L[$i][$j] = $this->LU[$i][$j];
* Get upper triangular factor.
* @return array Upper triangular factor
for ($i = 0; $i < $this->n; ++ $i) {
for ($j = 0; $j < $this->n; ++ $j) {
$U[$i][$j] = $this->LU[$i][$j];
* Return pivot permutation vector.
* @return array Pivot vector
} // function getDoublePivot()
* Is the matrix nonsingular?
* @return true if U, and hence A, is nonsingular.
for ($j = 0; $j < $this->n; ++ $j) {
if ($this->LU[$j][$j] == 0) {
} // function isNonsingular()
* @return array d matrix deterninat
if ($this->m == $this->n) {
for ($j = 0; $j < $this->n; ++ $j) {
* @param $B A Matrix with as many rows as A and any number of columns.
* @return X so that L*U*X = B(piv,:)
* @exception IllegalArgumentException Matrix row dimensions must agree.
* @exception RuntimeException Matrix is singular.
public function solve($B) {
if ($B->getRowDimension() == $this->m) {
// Copy right hand side with pivoting
$nx = $B->getColumnDimension();
$X = $B->getMatrix($this->piv, 0, $nx- 1);
for ($k = 0; $k < $this->n; ++ $k) {
for ($i = $k+ 1; $i < $this->n; ++ $i) {
for ($j = 0; $j < $nx; ++ $j) {
$X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
for ($k = $this->n- 1; $k >= 0; -- $k) {
for ($j = 0; $j < $nx; ++ $j) {
$X->A[$k][$j] /= $this->LU[$k][$k];
for ($i = 0; $i < $k; ++ $i) {
for ($j = 0; $j < $nx; ++ $j) {
$X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
throw new Exception(self::MatrixSingularException);
throw new Exception(self::MatrixSquareException);
} // class PHPExcel_Shared_JAMA_LUDecomposition
|