user466534
user466534

Reputation:

matrix decomposition algorithm

I have the following algorithm which works very strangly, for clarity here is the code

#include <iostream>

using namespace std;

#define maxn 1000
#define n 3

double sum=0;
double sum1=0;
double a[maxn][maxn];
double l[maxn][maxn];
double u[maxn][maxn];

void read(){
    for(int i=1;i=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
}

void decomposition(){
    for(int i=1;i<=n;i++)
        l[i][1]=a[i][1];

    for(int j=1;j<=n;j++)
        u[1][j]=a[1][j]/l[1][1];

    for(int j=2;j<=n;j++){
        for(int i=j;i<=n;i++){
            for(int k=1;k<j;k++){
                sum+=l[i][k]*u[k][j];
            }

            l[i][j]=a[i][j]-sum;
        }

        u[j][j]=1;
        for(int i=j+1;i<=n;i++){
            for(int k=1;k<j;k++){
                sum1+=l[j][k]*u[k][i];
            }
            u[j][i]=(a[j][i]-sum1)/l[j][j];
        }
    }
}

void print(){
    cout<<"   L matrix "<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<l[i][j]<<"  ";
        }
        cout<<endl;
    }
    cout<<endl;
    cout<<" U matrix "<<endl;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<u[i][j]<<"  ";
        }
        cout<<endl;
    }
}

int main(){
    cout<<"enter the matrix "<<endl;
    read();
    cout<<endl;
    decomposition();
    cout<<"print twwo matrix  "<<endl;
    print();

    return 0;
}

but when I enter matrix, for example I want to decompose this matrix

 3 -1 2
 1 2 3
 2 -2 -1

program does not show output,just requires again to enter some input, I can't see in my code here it is required to enter more matrix or data, so what is problem?

Upvotes: 0

Views: 1674

Answers (2)

BenC
BenC

Reputation: 8976

Try with this:

void read(){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }
}

It should read the user matrix properly.

Upvotes: 3

Hasturkun
Hasturkun

Reputation: 36412

Not sure if this is the problem, but in read (which is not a good name for a function, btw) you have:

for(int i=1;i=n;i++){

This assigns i to be n, and should be i==n or more likely i<=n, it is also always true, so execution never stops.

Upvotes: 4

Related Questions