Sei sulla pagina 1di 6

#include<iostream>

#include<iomanip>
#include<cmath>

using namespace std;

// Displaying the Augmented Matrix


void disp_matrix(double *M, int r, int c)
{
cout<<endl;
for(int ir=0; ir<r; ir++)
{
for(int ic=0; ic<c; ic++)
{
cout<<setw(10)<<*M;
M=M+1;
}
cout<<endl;
}
cout<<endl;
}

void enter_matrix(double *M, int r, int c)


{
for(int ir=0; ir<r; ir++)
{
for(int ic=0; ic<c; ic++)
{
if(ic != c-1)
{
cout<<"M["<<ir<<"]["<<ic<<"] = ";
cin>>*M;
}
M=M+1;
}
cout<<endl;
}
cout<<endl;
}

void enter_B(double *M, int r, int c)


{
for(int i=0; i<r; i++)
{
cout<<"Enter B["<<i<<"] = ";
cin>>*(M + c-1 + i*c);
}
}

void row_transformation(double *M, int r, int c, double


conditioning_parameter)
{
int f=0;
for(int ip=0; ip<r; ip++) // index of pivot
{
if(*(M + ip*c + ip) != 0)
{
double p = *(M + ip*c + ip);

for(int ir=ip+1; ir<r; ir++)


{
double fact = *(M + ir*c + ip)/p;

for(int ic=0; ic<c; ic++)


{
*(M + ir*c + ic) += 0 - fact*(*(M + ip*c + ic));
}
}
}
else
{
for(int check = ip+1; check<r; check++ )
{
if(*(M + check*c + ip) != 0)
{
f=1; // flag variable - to see if there is any
// nonzero pivot in the column

for(int j = 0; j<c; j++) // swapping


{
double temp1 = *(M + c*ip + j);
double temp2 = *(M + c*check + j);

*(M + c*ip + j) = temp2;


*(M + c*check + j) = temp1;
}
break;
}
}
if(f==1) //do pivoting after swapping only if there
exists
//one non zero element in that column
{
double p = *(M + ip*c + ip);

for(int ir=ip+1; ir<r; ir++)


{
double fact = *(M + ir*c + ip)/p;

for(int ic=0; ic<c; ic++)


{
*(M + ir*c + ic) += 0.0 - fact*(*(M + ip*c + ic));
}
}
}
}
cout<<"After performing the #"<<ip<<" iteration"<<endl;
disp_matrix(M, r, c);

if((*(M + ip*c + ip) != 0) && (*(M + ip*c + ip) != 1) &&


(fabs(*(M + ip*c + ip)) >= conditioning_parameter))
{
double a = *(M + ip*c + ip);

for(int j=0; j<c; j++)


{
*(M + ip*c + j) = *(M + ip*c + j)/a;
}
cout<<"After dividing by pivot"<<endl;

disp_matrix(M, r, c);
cout<<endl;
}

for(int ir=0; ir<r; ir++)


{
for(int ic=0; ic<c; ic++)
{
if(fabs(*(M + ir*c + ic)) <= conditioning_parameter)
*(M + ir*c + ic)=0;
}
}
}
}

void Gaussian_elimination(double *M, int r, int c, float *x)


{
if((*(M + (r-1)*c + (c-2)) == 0) && (*(M + (r-1)*c + (c-1)) != 0))
{
cout<<"Solution does not exist"<<endl;
}
else
{
int flag = 0;

//To check if the entire row is zero


for(int i=0; i<c; i++)
{
if(*(M + (r-1)*c + i) != 0)
{
flag = 1;
break;
}
}

if(flag != 1)
{
cout<<"Infinite solutions exist "<<endl;
}
else
{
float sum=0;
for(int i = r-1; i>=0; i--)
{
sum = *(M + i*c + (c-1));
for (int j = r-1-i; j<c; j++)
{
sum -= *(M + i*c + j) * (*(x+j));
}
*(x + i) = sum/(*(M + i*c + i));
}

for(int i = 0; i<r; i++)


{
cout<<x[i]<<endl;
}
}
}
}

int main()
{
int r, c;
double conditioning_parameter;

cout<<"Please enter the conditioning paramter, for safer


computations"<<endl;
cin>>conditioning_parameter;

cout<<"Enter the number of rows of the matrix"<<endl;


cin>>r;

cout<<"Enter the number of column of the matrix"<<endl;


cin>>c;

c=c+1; /*To accept the Augmented matrix of AX=B*/

double M[r][c];

cout<<"Enter the matrix"<<endl<<endl;


enter_matrix(*M, r, c);

cout<<"Enter B"<<endl<<endl;
enter_B(*M, r, c);

disp_matrix(*M, r, c);

cout<<"Performing row operations"<<endl;


row_transformation(*M, r, c, conditioning_parameter);

float x[r];
cout<<"Gaussian Elimination"<<endl;

Gaussian_elimination(*M, r, c, x);
return 0;
}

/*
cout<<"After performing row operation"<<endl;

//disp_matrix_EM(r,c,&e);
e++;
cout<<endl;

//convert_identity(r,c);

if((M[ip][ip]!=0)&&(M[ip][ip]!=1)&&(M[ip][ip]>=0.00001))
{
float a=M[ip][ip];
for(int j=0; j<c; j++)
{
M[ip][j]=M[ip][j]/a;
}
cout<<"After dividing by pivot"<<endl;

//disp_matrix_EM(r,c,&e);

cout<<endl;
e++;
}

//conditioning the matrix


for(int ir=0; ir<r; ir++)
{
for(int ic=0; ic<c; ic++)
{
if(M[ir][ic]<=0.00001)
M[ir][ic]=0;
}
cout<<endl;
}
//convert_identity(r,c);

if(ip>=1) // only make column elements above pivot zero if its not on the
first row
{
for(int ir=ip-1; ir>=0;ir--) // subtracting column elements above pivot to
be zero
{
float fact = M[ir][ip];

for(int ic=0; ic<c; ic++)


{
M[ir][ic] = M[ir][ic] - fact*M[ip][ic];
}
}

cout<<"After making elements above pivot to be zero"<<endl;


disp_matrix_EM(r,c,&e);
e++;
}
//convert_identity(r,c);

*/

Potrebbero piacerti anche