-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussSerial.cpp
133 lines (111 loc) · 3.14 KB
/
gaussSerial.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* File: gaussSerial.cpp
* Author: zhakov
*
* Created on 4 Декабрь 2012 г., 23:51
*/
#include "gaussSerial.h"
#include "math.h"
#include "stdio.h"
#include "matrixHelpers.h"
gaussSerial::gaussSerial(int size) {
mSize = size;
pSerialPivotIter = new int[size];
pSerialPivotPos = new int[size];
//Заполняем -1, чтобы было понятно, что в эти строки мы ещё не заходили
for (int i = 0; i < size; i++) {
pSerialPivotIter[i] = -1;
}
}
gaussSerial::gaussSerial(const gaussSerial& orig) {
}
gaussSerial::~gaussSerial() {
}
int gaussSerial::resultCalculation(double** pMatrix, double* pVector, double* pResult) {
// Gaussian elimination
serialGaussianElimination(pMatrix, pVector);
// Back substitution
serialBackSubstitution(pMatrix, pVector, pResult);
return 0;
}
/**
*
* @param pMatrix
* @param Iter
* @return
*/
int gaussSerial::findPivotRow(double** pMatrix, int Iter) {
int PivotRow = -1; // The index of the pivot row
double MaxValue = 0; // The value of the pivot element
int i; // Loop variable
// Choose the row, that stores the maximum element
for (i = 0; i < mSize; i++) {
if ((pSerialPivotIter[i] == -1) && (fabs(pMatrix[i][Iter]) > MaxValue)) {
PivotRow = i;
MaxValue = fabs(pMatrix[i][Iter]);
}
}
return PivotRow;
}
/**
*
* @param pMatrix
* @param pVector
* @return
*/
int gaussSerial::serialColumnElimination(double** pMatrix, double* pVector, int Pivot, int Iter) {
double PivotValue, PivotFactor;
PivotValue = pMatrix[Pivot][Iter];
for (int i = 0; i < mSize; i++) {
if (pSerialPivotIter[i] == -1) {
PivotFactor = pMatrix[i][Iter] / PivotValue;
for (int j = Iter; j < mSize; j++) {
pMatrix[i][j] -= PivotFactor * pMatrix[Pivot][j];
}
pVector[i] -= PivotFactor * pVector[Pivot];
}
}
return 0;
}
/**
*
* @param pMatrix
* @param pVector
* @return
*/
int gaussSerial::serialGaussianElimination(double** pMatrix, double* pVector) {
int Iter;
// The Number of the iteration of the gaussian
// elimination
int PivotRow;
// The Number of the current pivot row
for (Iter = 0; Iter < mSize; Iter++) {
// Finding the pivot row
PivotRow = findPivotRow(pMatrix, Iter);
pSerialPivotPos[Iter] = PivotRow;
pSerialPivotIter[PivotRow] = Iter;
serialColumnElimination(pMatrix, pVector, PivotRow, Iter);
}
return 0;
}
/**
* Обратный ход метода Гаусса
*
* @param pMatrix
* @param pVector
* @param pResult
* @return
*/
int gaussSerial::serialBackSubstitution(double** pMatrix, double* pVector, double* pResult) {
int RowIndex, Row;
for (int i = mSize - 1; i >= 0; i--) {
RowIndex = pSerialPivotPos[i];
pResult[i] = pVector[RowIndex] / pMatrix[RowIndex][i];
for (int j = 0; j < i; j++) {
Row = pSerialPivotPos[j];
pVector[j] -= pMatrix[Row][i] * pResult[i];
pMatrix[Row][i] = 0;
}
}
return 0;
}