- actually fixed gauss this fucking time

This commit is contained in:
Patrice Matz 2017-10-31 16:12:58 +01:00
parent 1b07a149c5
commit f2737825fc
3 changed files with 57 additions and 46 deletions

View File

@ -39,6 +39,6 @@ void cylinder()
void lgs()
{
LGS lgs;
lgs.data_entry();
cout << lgs.gauss() << endl;
//lgs.data_entry();
lgs.gauss();
}

View File

@ -1,6 +1,8 @@
#include "lgs.h"
#include "time.h"
#include <iostream>
#include<iostream>
#include<iomanip>
using namespace std;
void LGS::data_entry()
@ -36,6 +38,7 @@ void LGS::data_entry()
void LGS::creat_array()
{
x = new long double[variables];
GA = new long double*[variables];
for (int i = 0; i < variables; ++i)
GA[i] = new long double[variables+1];
@ -83,50 +86,57 @@ void LGS::print_exp()
}
}
float LGS::gauss()
{
double t1 = clock();
int i, j, k;
data_entry();
for (i = 0; i<variables; i++) //Pivotisation
for (k = i + 1; k<variables; k++)
if (abs(GA[i][i])<abs(GA[k][i]))
for (j = 0; j <= variables; j++)
{
long double temp = GA[i][j];
GA[i][j] = GA[k][j];
GA[k][j] = temp;
}
cout << endl << "Matrix nach Pivot: " << endl;
print();
for (i = 0; i<variables - 1; i++) //gauss elimination
for (k = i + 1; k<variables; k++)
{
long double t = GA[k][i] / GA[i][i];
for (j = 0; j <= variables; j++)
GA[k][j] = GA[k][j] - t*GA[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
}
cout << endl << "Matrix nach Gausselimination: " << endl;
print();
for (i = variables - 1; i >= 0; i--) //back-substitution
{ //x is an array whose values correspond to the values of x,y,z..
x[i] = GA[i][variables]; //make the variable to be calculated equal to the rhs of the last equation
for (j = i + 1; j<variables; j++)
if (j != i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
x[i] = x[i] - GA[i][j] * x[j];
x[i] = x[i] / GA[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
}
cout << endl << "Loesung: " << endl;
for (i = 0; i<variables; i++){
int var1 = i + 65;
cout << (char)var1 << 1 << " = " << x[i] << endl; // Print the values of x, y,z,....
}
cout << endl;
return (float)((clock() - t1) / CLOCKS_PER_SEC);
}
float LGS::cramer()
{
double t1 = clock();
return (float)((clock() - t1) / CLOCKS_PER_SEC);
}
float LGS::gauss()
{
double t1 = clock();
gauss_down();
gauss_up();
return (float)((clock() - t1) / CLOCKS_PER_SEC);
}
void LGS::gauss_down( )
{
for(int stage = 1; stage < variables ; stage++)
{
long double div = (GA[stage][stage-1] / GA[stage-1][stage-1]);
for (int i = 0; i < variables-1 && stage+i < variables; i++)
{
for (int n = 0; n <= variables; n++)
{
GA[stage + i][n] = GA[stage + i][n] - (GA[stage - 1][n] * div);
}
}
print();
cout << endl;
}
}
void LGS::gauss_up()
{
}
}

View File

@ -2,6 +2,7 @@
class LGS{
public:
long double** GA;
long double* x;
int variables = 0;
@ -11,15 +12,15 @@ public:
for (int i = 0; i < variables; ++i)
delete[] GA[i];
delete[] GA;
delete[] x;
};
float gauss();
float cramer();
void data_entry();
void creat_array();
void print();
void print_ln(int);
void print_exp();
void gauss_down();
void gauss_up();
float gauss();
};