Sei sulla pagina 1di 3

#include<iostream>

#include<iomanip>
#include<math.h>
#include<cstdio>
#include <stdlib.h>
using namespace std;
int main()
{
double a[10][10], l[10][10], q[10][10], b[10][10], c[10][10],w[10][10], r[10][10],
mul[10][10], s[10][10], lo[10][10], temp[10], temp1[10];
int p , i, j, k ;
float max, max1, mult, d;

a[1][1] = 1; a[1][2] = 2; a[1][3] = 4;


a[2][1] = 4; a[2][2] = 6; a[2][3] = 5;
a[3][1] = 2; a[3][2] = 2; a[3][3] = 7;

for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
r[i][j] = a[i][j] ;

for (i =1; i<=3; i++)


{
for (j =1; j<=3; j++)
{
if (i==j)
{
q[i][j] = 1;
}
else
{
q[i][j] = 0;
}
std::cout<<q[i][j]<<" ";
}
std::cout<<"\n";
}

std::cout<<"Matrix A is"<<"\n";
for (i = 1; i<=3; i++)
{
for (j = 1; j<=3; j++)
{
std::cout<<a[i][j]<<" ";
}
std::cout<<"\n";
}
for (i = 1; i<=3; i++)
{
max = a[i][i];
p = i;
for (int k = i+1; k<=3; k++)
{
if(fabs(max)<fabs(a[k][i]))
{
max = a[k][i];
p = k;
}
}
for(j = 1; j <=3; j++)
{
temp[j] = a[p][j];
a[p][j] = a[i][j];
a[i][j] = temp[j];

temp1[j] = q[p][j];
q[p][j] = q[i][j];
q[i][j] = temp1[j];
}

for(j=i+1;j<=3;j++)
{
mult = a[j][i]/a[i][i];
for(k=1;k<=3;k++)
a[j][k] -= mult*a[i][k];
}
}
std::cout<<"\n";
std::cout<<"Matrix U is"<<"\n";
for ( i = 1; i<=3; i++)
{
for ( j = 1; j<=3; j++)
{
std::cout<<a[i][j]<<" ";
}
std::cout<<"\n";
}

for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
s[i][j] = a[i][j] ;

for(i=1;i<=3;i++)
for(j=3+1;j<=3*2;j++)
{ if(i+3==j)
{
s[i][j] = 1 ;
}
else
{
s[i][j] = 0;
}
}

std::cout<<"\n";

std::cout<<"Matrix P is"<<"\n";

for ( i = 1; i<=3; i++)


{
for ( j = 1; j<=3; j++)
{
std::cout<<q[i][j]<<" ";
}
std::cout<<"\n";
}
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3 * 2; j++)
if (j != i)
{
d = s[j][i] / s[i][i];
for (k = 1; k <= 3 * 2; k++)
s[j][k] -= s[i][k] * d;
}
}
/************** reducing to unit matrix *************/
for (i = 1; i <= 3; i++)
{
d = s[i][i];
for (j = 1; j <= 3 * 2; j++)
s[i][j] = s[i][j] / d;
}
std::cout<<"\n";

for (i = 1; i <=3; i++)


{
for (j = 1; j <=3; j++)
{
c[i][j] = 0;
for (k = 1; k <=3; k++)
{
c[i][j] += r[i][k] * s[k][j+3];
}
}
}

for (i = 1; i <=3; i++)


{
for (j = 1; j <=3; j++)
{
w[i][j] = 0;
for (k = 1; k <=3; k++)
{
w[i][j] += q[i][k] * c[k][j];
}
}
}

std::cout<<"Matrix L is"<<"\n";
for (i = 1; i <=3; i++)
{
for (j = 1; j <=3; j++)
{
std::cout<<w[i][j]<<" ";
}
std::cout<<"\n";
}

return (0);
}