note 49054 added to function.array
| From: | michael dot bommarito at gmail dot com | Date: | Sat, 15 Jan 2005 15:14:15 +0000 |
| Subject: | note 49054 added to function.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83323@lists.php.net to get a copy of this message | ||
Just in case anyone else was looking for some help writing an LU decomposition function, here's
a simple example.
N.B. All arrays are assumed to begin with index 1, not 0. This is not hard to change, but make sure
you specify array(1=>...), not just array(...).
Furthermore, this function is optimized to only consider variable elements of the matrices. As $L
will be a lower triangular matrix, there is no need to compute the elements of either the diagonal
or the upper triangle; likewise with $U.
This function also does not check to verify that the input matrix is non-singular.
/*
* LU Decomposition
* @param $A initial matrix (1...m x 1...n)
* @param $L lower triangular matrix, passed by reference
* @param $U upper triangular matrix, passed by reference
*/
function LUDecompose($A, &$L, &$U) {
$m = sizeof($A);
$n = sizeof($A[1]);
for ( $i = 1; $i <= $m; $i++ ) {
$U[$i][$i] = $A[$i][$i];
for ( $j = $i + 1; $j <= $m; $j++ ) {
$L[$j][$i] = $A[$j][$i] / $U[$i][$i];
$U[$i][$j] = $A[$i][$j];
}
for ( $j = $i + 1; $j <= $m; $j++ ) {
for ( $k = $i + 1; $k <= $m; $k++ ) {
$A[$j][$k] = $A[$j][$k] - ($L[$j][$i] * $U[$i][$k]);
}
}
}
return;
}
----
Manual Page -- http://www.php.net/manual/en/function.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49054
Delete -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49054&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49054&report=yes
Search -- http://master.php.net/manage/user-notes.php