LU decomposition

• 03-30-2009
khdani
LU decomposition
Hello,
I've written this code for LU decomposition
Code:

```        for(int k=0;k<size;k++) {                 l[k][k]=1;                 for(int j=k;j<size;j++) {                         long double sum=0;                         for(int s=0;s<k-1;s++) {                                 sum+= l[k][s]*u[s][j];                         }                         u[k][j]=a[k][j]-sum;                 }                 for(int i=k+1;i<size;i++) {                         long double sum=0;                         for(int s=0;s<k-1;s++) {                                 sum+=l[i][s]*u[s][k];                         }                         l[i][k]=(a[i][k]-sum)/u[k][k];                 }         }```
where 'a' is array with values of matrix A, and 'l' and 'u' should contain
the result of A=LU accordingly.
but the code doesn't give the correct results, and i don't understand why ?
• 03-30-2009
Snafuist
It's 2:40am over here, so I might be wrong, but your code snippet doesn't look like LU decomposition. L and U are usually computed simultaneously. Furthermore, the line "u[k][j]=a[k][j]-sum;" looks terribly wrong.

Do you know how LU decomposition works? Can you tell us?

Besides, you will notice that every element of the main diagonal of L is always 1, so you don't need to save them. Instead, you can use a single array LU[][] and use the upper left to store U and the lower right to store L without its main diagonal.

Greets,
Philip
• 03-30-2009
khdani
it's a "Doolittle" algorithm.
• 10-15-2010
Sara Brazilian
Hi! I found the wrong in this program. Is very simple!!
You write:
for(int s=0;s<k-1;s++) {

the correct is:
for(int s=0;s<=k-1;s++) {

ok???
thank you and congretulations for the program....

kisses...