forked from Aigo-Oggi/mp-2023-pmi2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gauss.cpp
46 lines (39 loc) · 1.11 KB
/
Gauss.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
#include <iostream>
#include "vecmat.h"
#include "gauss.h"
#include <ctime>
// Реализация методов класса Gauss
Gauss::Gauss(Matrix* A, Vector* x, Vector* b) : A(A), x(x), b(b), status(0) { M = *A; bold = *b; }
void Gauss::Solve() {
for (int i = 0; i < A->Rows(); i++) {
double p = (*A)(i, i);
if (p == 0) {
status = -1;
return;
}
for (int j = i + 1; j < A->Rows(); j++) {
double r = (*A)(j, i) / p;
for (int k = 0; k < A->Cols(); k++) {
(*A)(j, k) -= r * (*A)(i, k);
}
(*b)(j) -= r * (*b)(i);
}
}
for (int i = A->Rows() - 1; i >= 0; i--) {
double sum = 0;
for (int j = i + 1; j < A->Rows(); j++) {
sum += (*A)(i, j) * (*x)(j);
}
(*x)(i) = ((*b)(i) - sum) / (*A)(i, i);
}
}
double Gauss::Check() {
if (status != 0) {
return -1;
}
//Matrix M = *A;
Vector e = M * (*x) - bold;
double a = e.Norm();
double y = bold.Norm();
return a / y;
}