Created
January 19, 2021 19:00
-
-
Save M1nified/a885d668b4c6b3b28f4ae78a0275f5ad to your computer and use it in GitHub Desktop.
Gaus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0.02 0.01 0 0 0.02 | |
1 2 1 0 1 | |
0 1 2 1 4 | |
0 0 100 200 800 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// aproksymacja.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
// | |
#include <iostream> | |
// #include <mkl_lapack.h> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
#include <math.h> | |
using namespace std; | |
const int MAX_SIZE = 1000; | |
int equations_count = 0, col_count = 0; | |
double equations[MAX_SIZE][MAX_SIZE] = { 0 }; | |
double x[MAX_SIZE] = { 0 }; | |
void print_equations(){ | |
for(int i=0;i<equations_count; i++){ | |
for(int j=0;j<col_count; j++){ | |
cout << equations[i][j]<< "\t"; | |
} | |
cout << endl; | |
} | |
cout<< endl; | |
} | |
int find_index_of_max_in_col(int col, int first_row){ | |
int max_val = abs(equations[first_row][col]); | |
int index_of_max = first_row; | |
for(int i = first_row+1; i<equations_count; i++){ | |
if(abs(equations[i][col]) > max_val){ | |
max_val = abs(equations[i][col]); | |
index_of_max = i; | |
} | |
} | |
return index_of_max; | |
} | |
void swap_rows(int row_a, int row_b){ | |
double tmp = 0; | |
for(int j=0;j<col_count;j++){ | |
tmp = equations[row_a][j]; | |
equations[row_a][j] = equations[row_b][j]; | |
equations[row_b][j] = tmp; | |
} | |
} | |
void pivot(int row, int col){ | |
int index_of_max = find_index_of_max_in_col(col, row); | |
if(index_of_max != row){ | |
swap_rows(index_of_max, row); | |
} | |
} | |
bool is_zero(int row, int col){ | |
return equations[row][col] == 0; | |
} | |
void eliminate(int row, int col){ | |
for(int i = row+1; i < equations_count; i++){ | |
double curr_multiplier = equations[i][col] / equations[row][col]; | |
for(int j = col; j<col_count; j++){ | |
equations[i][j] -= curr_multiplier * equations[row][j]; | |
} | |
} | |
} | |
void purge(){ | |
for(int i=0;i<equations_count;i++){ | |
print_equations(); | |
pivot(i,i); | |
if(is_zero(i,i)) { | |
return; | |
} | |
eliminate(i,i); | |
print_equations(); | |
} | |
} | |
double calc_x(){ | |
x[equations_count] = 1; | |
for(int i=equations_count-1; i>=0; i--){ | |
double b_i = 1.0 * equations[i][col_count-1]; | |
cout << "bi" << b_i<<endl; | |
double counter = b_i; | |
for(int j=col_count-2; j>i; j--){ | |
counter -= x[j+1]*equations[i][j]; | |
cout << "c" << counter<<endl; | |
} | |
// counter -= equations[i][i]; | |
x[i] = counter / equations[i][i]; | |
cout << x[i] <<endl; | |
cout << endl<< endl; | |
} | |
} | |
int main() | |
{ | |
string line; | |
ifstream file("data.txt"); | |
double suma = 0; | |
if (file.is_open()) | |
{ | |
double elem; | |
while (getline(file, line)) | |
{ | |
istringstream ss(line); | |
int j = 0; | |
while(ss >> elem){ | |
equations[equations_count][j++] = elem; | |
} | |
col_count = max(col_count, j); | |
equations_count++; | |
} | |
} | |
purge(); | |
calc_x(); | |
for(int i=0;i<equations_count; i++){ | |
cout << "x_" << i << "=" << x[i] << endl; | |
} | |
return 0; | |
// // approx(3); | |
// // Results | |
// cout << "Liczba wezlow: " << args_count << endl | |
// << endl; | |
// for (int i = 0; i < args_count; i++) | |
// { | |
// cout << "x" << i << "=" << xtab[i] << "\t"; | |
// cout << "f(" << xtab[i] << ")=" << ytab[i] << endl; | |
// } | |
// return 0; | |
} | |
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu | |
// Debug program: F5 or Debug > Start Debugging menu | |
// Tips for Getting Started: | |
// 1. Use the Solution Explorer window to add/manage files | |
// 2. Use the Team Explorer window to connect to source control | |
// 3. Use the Output window to see build output and other messages | |
// 4. Use the Error List window to view errors | |
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project | |
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file | |
/* | |
1 1 | |
3 2 | |
4 4 | |
6 4 | |
8 5 | |
9 7 | |
11 8 | |
14 9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment