-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab_05.cpp
213 lines (187 loc) · 6.17 KB
/
Lab_05.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <cmath>
using namespace std;
const int MatrixSize = 4;
const double eps = 0.001;
void displayMatrix(string msg, double const arrayA[][MatrixSize], double const arrayB[])
{
cout << msg << endl;
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
cout << arrayA[i][j] << "\t";
}
cout << arrayB[i] << endl;
}
}
void copyMatrix(double const stablearray[][MatrixSize], double tmpArray[][MatrixSize])
{
for (int i = 0; i < MatrixSize; i++)
{
for (int j = 0; j < MatrixSize; j++)
{
tmpArray[i][j] = stablearray[i][j];
}
}
}
void copyArray(double const initArray[], double tempArray[]) {
for (int i = 0; i < MatrixSize; i++) tempArray[i] = initArray[i];
}
bool isConvergent(double const matrix[][MatrixSize]) {
double sum = 0, sum2 = 0;
// check 1
for (int i = 0; i < MatrixSize; i++) {
sum = 0;
for (int j = 0; j < MatrixSize; j++) {
sum += fabs(matrix[i][j] / matrix[j][i]);
}
if (sum < 1) return true;
}
// check 2
for (int j = 0; j < MatrixSize; j++) {
sum = 0;
for (int i = 0; i < MatrixSize; i++) {
sum += fabs(matrix[i][j] / matrix[j][i]);
}
if (sum < 1) return true;
}
// check 3
for (int i = 0; i < MatrixSize; i++) {
sum = 0, sum2 = 0;
for (int j = 0; j < MatrixSize; j++) {
sum += pow(matrix[i][j] / matrix[i][i], 2);
sum2 += pow(matrix[j][i] / matrix[j][j], 2);
}
if (sum < 1 && sum2 < 1) return true;
}
// check from example
double maxi = 0, maxj = 0;
for(int i = 0; i < MatrixSize; i++){
sum = 0, sum2 = 0;
for(int j = 0; j < MatrixSize; j++){
sum += fabs(matrix[i][j]);
sum2 += fabs(matrix[j][i]);
}
maxi = max(maxi, sum);
maxj = max(maxj, sum2);
}
if(maxi < 1 || maxj < 1) return true;
// check from wiki
for (int i = 0; i < MatrixSize; i++) {
sum = 0;
for (int j = 0; j < MatrixSize; j++) {
if (i == j) continue;
sum += fabs(matrix[i][j]);
}
if (matrix[i][i] <= sum) break;
if (i == MatrixSize - 1) return true;
}
return false;
}
void Jacobi(double const arrayA[][MatrixSize], double const arrayB[]) {
cout << "----------------------------------------------------------------\n"
<< "\t\t\tJacobi method" << endl;
double tempA[MatrixSize][MatrixSize], tempB[MatrixSize];
for (int i = 0; i < MatrixSize; i++) {
tempB[i] = arrayB[i] / arrayA[i][i];
for (int j = 0; j < MatrixSize; j++) {
tempA[i][j] = - arrayA[i][j] / arrayA[i][i];
if (i == j) tempA[i][j] = 0;
}
}
displayMatrix("Matrix after Jacobi: ", tempA, tempB);
cout << "Is matrix convergent? " << isConvergent(tempA) << endl;
double results[MatrixSize], tempResults[MatrixSize];
double zeroArr[MatrixSize] = {0};
double oneArr[MatrixSize] = {1, 1, 1, 1};
copyArray(tempB, results);
copyArray(tempB, tempResults);
double sum = 0;
int iterCount = 0;
while (true) {
for (int i = 0; i < MatrixSize; i++) {
sum = 0;
for (int j = 0; j < MatrixSize; j++) {
if (j == i) continue;
sum += tempA[i][j] * tempResults[j];
}
results[i] = tempB[i] + sum;
}
sum = 0;
for (int i = 0; i < MatrixSize; i++) {
sum += results[i] - tempResults[i];
}
if (fabs(sum) < eps) break;
copyArray(results, tempResults);
iterCount++;
}
for (int i = 0; i < MatrixSize; i++) cout << "Result x" << i + 1 << ": " << results[i] << endl;
cout << "Iteration count: " << iterCount << endl;
cout << "----------------------------------------------------------------" << endl;
}
void Seidel(double const arrayA[][MatrixSize], double const arrayB[]) {
cout << "----------------------------------------------------------------\n"
<< "\t\t\tGauss-Seidel method" << endl;
double tempA[MatrixSize][MatrixSize], tempB[MatrixSize];
for (int i = 0; i < MatrixSize; i++) {
tempB[i] = arrayB[i] / arrayA[i][i];
for (int j = 0; j < MatrixSize; j++) {
tempA[i][j] = - arrayA[i][j] / arrayA[i][i];
if (i == j) tempA[i][j] = 0;
}
}
displayMatrix("Matrix after Jacobi: ", tempA, tempB);
cout << "Is matrix convergent? " << isConvergent(tempA) << endl;
double results[MatrixSize], tempResults[MatrixSize];
double zeroArr[MatrixSize] = {0};
double oneArr[MatrixSize] = {1, 1, 1, 1};
copyArray(oneArr, results);
copyArray(oneArr, tempResults);
double sum = 0;
int iterCount = 0;
while (true) {
for (int i = 0; i < MatrixSize; i++) {
sum = 0;
for (int j = 0; j < i; j++) {
sum += tempA[i][j] * results[j];
}
for (int j = i+1; j < MatrixSize; j++) {
sum += tempA[i][j] * tempResults[j];
}
results[i] = tempB[i] + sum;
}
sum = 0;
for (int i = 0; i < MatrixSize; i++) {
sum += results[i] - tempResults[i];
}
if (fabs(sum) < eps) break;
copyArray(results, tempResults);
iterCount++;
}
for (int i = 0; i < MatrixSize; i++) cout << "Result x" << i + 1 << ": " << results[i] << endl;
cout << "Iteration count: " << iterCount << endl;
cout << "----------------------------------------------------------------" << endl;
}
int main() {
double arrayA[MatrixSize][MatrixSize];
double arrayB[MatrixSize];
exit(7);
char way;
cout << "i - input, f - file" << endl;
cin >> way;
if (way == 'f') {
freopen("file5.txt", "r", stdin);
}
for(int i = 0; i < MatrixSize; i++) {
for (int j = 0; j < MatrixSize; j++) {
cin >> arrayA[i][j];
}
}
for (int i = 0; i < MatrixSize; i++) {
cin >> arrayB[i];
}
displayMatrix("Matrix : ", arrayA, arrayB);
Jacobi(arrayA, arrayB);
Seidel(arrayA, arrayB);
}