-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGauss_Methods.cpp
98 lines (89 loc) · 2.05 KB
/
Gauss_Methods.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <random>
#include "Gauss_Methods.h"
void methods::GenerateRandom(unsigned long raws, unsigned columns, double **X)
{
std::random_device rt;
std::mt19937 generator(rt());
std::uniform_int_distribution<int> ur(MIN, MAX);
for (unsigned long i = 0; i < raws; i++)
{
for (unsigned long j = 0; j < columns; j++)
{
X[i][j] = ur(generator);
}
}
}
void methods::Show(unsigned long raws, unsigned long columns, double **X)
{
for (unsigned long i = 0; i < raws; i++)
{
for (unsigned long j = 0; j < columns; j++)
{
std::cout << X[i][j] << " ";
}
std::cout << "\n";
}
}
void methods::exchangeraws(double **X, unsigned long fraw, unsigned long sraw, unsigned long size)
{
double temp;
for (unsigned long i = 0; i < size; i++)
{
temp = X[fraw][i];
X[fraw][i] = X[sraw][i];
X[sraw][i] = temp;
}
}
double * methods::GaussMethod(unsigned long size, double ** X, double ** Y)
{
double * result = new double[size];
double max;
//const double eps = 0.0000001;
unsigned long index;
for (unsigned long i = 0; i < size; i++)
{
max = abs(X[i][i]);
index = i;
for (unsigned long j = i + 1; j < size; j++)
{
if (abs(X[j][i] > max))
{
max = abs(X[j][i]);
index = j;
}
}
if (max < eps)
{
std::cout << "There is a 0 - vector here! Can't get result!\n";
return 0;
}
methods::exchangeraws(X, i, index, size);
double temp = Y[0][i];
Y[0][i] = Y[0][index];
Y[0][index] = temp;
for (unsigned long j = i; j < size; j++)
{
double temp = X[j][i];
if (abs(temp) < eps)
continue;
for (unsigned long k = 0; k < size; k++)
X[j][k] = X[j][k] / temp;
Y[0][j] = Y[0][j] / temp;
if (j == i)
continue;
for (unsigned long k = 0; k < size; k++)
X[j][k] = X[j][k] - X[i][k];
Y[0][j] = Y[0][j] - Y[0][i];
}
}
for (long i = size - 1; i >= 0; i--)
{
result[i] = Y[0][i];
for (long j = 0; j < i; j++)
{
Y[0][j] = Y[0][j] - X[j][i] * result[i];
}
}
return result;
}