Reputation:
I have wroted a LU writed in c++ and I need add same code to php i did it it's woarks but in php i have diffrent results. Main calculate in c++ are:
for (int i=0;i<m;i++)
{
for (int j=0;j<m;j++)
{
cout << tab1[i][j]<<"\t";
}
cout <<endl;
}
for (int i=0;i<m;i++)
{
for (int j=0;j<m;j++)
{
if (i==j)
{
tab2[i][j]=1;
tab3[i][j]=0;
}
else
{
tab3[i][j]=0;
tab2[i][j]=0;
}
}
}
//glowne dzielenie tego co mamy do rozlozenia
for (int i=0;i<m;i++)
{
for (int j=0;j<m;j++)
{
for (int k=0;k<m;k++)
{
pom+=tab2[i][k]*tab3[k][j];
pomoc+=tab2[j][k]*tab3[k][i];
}
if (i<=j)
tab3[i][j]=tab1[i][j]-pom;
if (j>=i)
tab2[j][i]=(tab1[j][i]-pomoc)/tab3[i][i];
pom=0;
pomoc=0;
}
}
and in php it looks:
$w= sizeof($this->input['macierz_1']);
$pom=0; $pomoc=0;
for ($i=1;$i<=$w;$i++)
{
for ($j=1;$j<=$w;$j++)
{
if ($i==$j)
{
$tab2[$i][$j]=1;
$tab3[$i][$j]=0;
}
else
{
$tab3[$i][$j]=0;
$tab2[$i][$j]=0;
}
}
}
//glowne dzielenie tego co mamy do rozlozenia
for ($i=1;$i<=$w;$i++)
{
for ($j=1;$j<=$w;$j++)
{
for ($k=1;$k<=$w;$k++)
{
$pom+=$tab2[$i][$k]*$tab3[$k][$j];
$pomoc+=$tab2[$j][$k]*$tab3[$k][$i];
}
if ($i<=$j)
$tab3[$i][$j]=($this->input['macierz_1'][$i][$j])-$pom;
if ($j>=$i)
$tab2[$j][$i]=(($this->input['macierz_1'][$i][$j])-$pomoc)/$tab3[$i][$i];
$pom=0;
$pomoc=0;
}
}
echo '<b>Macierz wynikowa L:<br> </b>';
$this->formularz($w,$w,'macierz_L',$tab2);
echo '<b>Macierz wynikowa U: <br> </b>';
$this->formularz($w,$w,'macierz_U',$tab3);
}
I know that i started arry in php form 1 not 0 and in all algorym its ok. Can any one tell me where i maked mastake or give algorytm LU in php that it woarks?
Upvotes: 0
Views: 211
Reputation: 45122
Does this example help? http://au.php.net/manual/en/function.array.php#49054
michael dot bommarito at gmail dot com
15-Jan-2005 03:14
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;
}
Upvotes: 0
Reputation:
Upvotes: 0
Reputation: 29468
At first glance, your PHP code looks correctly for me.
You have to be a bit more clearly, otherwise people are having a hard time to help you.
Upvotes: 1