/usr/share/php/JAMA/LUDecomposition.php is in php-jama 0~2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | <?php
/**
* @package JAMA
*
* 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 Paul Meagher
* @author Bartosz Matosiuk
* @author Michael Bommarito
* @version 1.1
* @license PHP v3.0
*/
class LUDecomposition {
/**
* Decomposition storage
* @var array
*/
var $LU = array();
/**
* Row dimension.
* @var int
*/
var $m;
/**
* Column dimension.
* @var int
*/
var $n;
/**
* Pivot sign.
* @var int
*/
var $pivsign;
/**
* Internal storage of pivot vector.
* @var array
*/
var $piv = array();
/**
* LU Decomposition constructor.
* @param $A Rectangular matrix
* @return Structure to access L, U and piv.
*/
function LUDecomposition ($A) {
if( is_a($A, 'Matrix') ) {
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
$this->LU = $A->getArrayCopy();
$this->m = $A->getRowDimension();
$this->n = $A->getColumnDimension();
for ($i = 0; $i < $this->m; $i++)
$this->piv[$i] = $i;
$this->pivsign = 1;
$LUrowi = array();
$LUcolj = array();
// Outer loop.
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++) {
$LUrowi = $this->LU[$i];
// Most of the time is spent in the following dot product.
$kmax = min($i,$j);
$s = 0.0;
for ($k = 0; $k < $kmax; $k++)
$s += $LUrowi[$k]*$LUcolj[$k];
$LUrowi[$j] = $LUcolj[$i] -= $s;
}
// Find pivot and exchange if necessary.
$p = $j;
for ($i = $j+1; $i < $this->m; $i++) {
if (abs($LUcolj[$i]) > abs($LUcolj[$p]))
$p = $i;
}
if ($p != $j) {
for ($k = 0; $k < $this->n; $k++) {
$t = $this->LU[$p][$k];
$this->LU[$p][$k] = $this->LU[$j][$k];
$this->LU[$j][$k] = $t;
}
$k = $this->piv[$p];
$this->piv[$p] = $this->piv[$j];
$this->piv[$j] = $k;
$this->pivsign = $this->pivsign * -1;
}
// Compute multipliers.
if ( ($j < $this->m) AND ($this->LU[$j][$j] != 0.0) ) {
for ($i = $j+1; $i < $this->m; $i++)
$this->LU[$i][$j] /= $this->LU[$j][$j];
}
}
} else {
trigger_error(ArgumentTypeException, ERROR);
}
}
/**
* Get lower triangular factor.
* @return array Lower triangular factor
*/
function getL () {
for ($i = 0; $i < $this->m; $i++) {
for ($j = 0; $j < $this->n; $j++) {
if ($i > $j)
$L[$i][$j] = $this->LU[$i][$j];
else if($i == $j)
$L[$i][$j] = 1.0;
else
$L[$i][$j] = 0.0;
}
}
return new Matrix($L);
}
/**
* Get upper triangular factor.
* @return array Upper triangular factor
*/
function getU () {
for ($i = 0; $i < $this->n; $i++) {
for ($j = 0; $j < $this->n; $j++) {
if ($i <= $j)
$U[$i][$j] = $this->LU[$i][$j];
else
$U[$i][$j] = 0.0;
}
}
return new Matrix($U);
}
/**
* Return pivot permutation vector.
* @return array Pivot vector
*/
function getPivot () {
return $this->piv;
}
/**
* Alias for getPivot
* @see getPivot
*/
function getDoublePivot () {
return $this->getPivot();
}
/**
* Is the matrix nonsingular?
* @return true if U, and hence A, is nonsingular.
*/
function isNonsingular () {
for ($j = 0; $j < $this->n; $j++) {
if ($this->LU[$j][$j] == 0)
return false;
}
return true;
}
/**
* Count determinants
* @return array d matrix deterninat
*/
function det() {
if ($this->m == $this->n) {
$d = $this->pivsign;
for ($j = 0; $j < $this->n; $j++)
$d *= $this->LU[$j][$j];
return $d;
} else {
trigger_error(MatrixDimensionException, ERROR);
}
}
/**
* Solve A*X = B
* @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.
*/
function solve($B) {
if ($B->getRowDimension() == $this->m) {
if ($this->isNonsingular()) {
// Copy right hand side with pivoting
$nx = $B->getColumnDimension();
$X = $B->getMatrix($this->piv, 0, $nx-1);
// Solve L*Y = B(piv,:)
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];
// Solve U*X = Y;
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];
}
return $X;
} else {
trigger_error(MatrixSingularException, ERROR);
}
} else {
trigger_error(MatrixSquareException, ERROR);
}
}
}
?>
|