Thread: LU decomposition

  1. #1
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42

    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 ?
    please help

  2. #2
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  3. #3
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42
    it's a "Doolittle" algorithm.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    1
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lu Decomposition Help Me Please
    By scottyg in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 08:33 AM
  2. LU decomposition
    By scottmanc in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2003, 08:25 AM
  3. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:08 AM