include iostream include stdio include vector include cmath using name

  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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>
using namespace std;
//Findine matrix's minors
void GetMinor(vector <vector<double>> A, vector <vector<double>> &B, int x, int y, int size)
{
int _x = 0;
int _y;
for (int i = 0; i < size; ++i)
{
if (i != x)
{
_y = 0;
for (int j = 0; j < size; ++j)
{
if (j != y)
{
B[_x][_y] = A[i][j];
_y++;
}
}
_x++;
}
}
}
//Finding determinant of matrix (size x size)
double FindDeterminant(vector <vector<double>> A, int size)
{
if (size == 1)
{
return A[0][0];
}
else
{
double det = 0;
vector <vector<double>> Minor(size - 1, vector <double>(size - 1));
for (int i = 0; i < size; ++i)
{
GetMinor(A, Minor, 0, i, size);
det += pow(-1, i) * A[0][i] * FindDeterminant(Minor, size - 1);
}
return det;
}
}
double InsertFreeCoeff(vector <vector<double>> A, vector<double> B, int size, int j)
{
for (int i = 0; i < size; ++i)
{
A[i][j] = B[i];
}
double det = FindDeterminant(A, size);
return det;
}
int main()
{
int N = 0;
cout << "Enter size matrix: ";
cin >> N;
vector <vector<double> > A(N, vector <double>(N));
vector <double> B(N);
cout << "Enter coefficients a[i][j]: " << endl;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << "a[" << i + 1 << "][" << j + 1 << "] = ";
cin >> A[i][j];
}
cout << "\n";
}
cout << "\n";
cout << "Enter free coefficients b[i]: " << endl;
for (int i = 0; i < N; ++i)
{
cout << "b[" << i + 1 << "] = ";
cin >> B[i];
}
cout << "\n";
//CAAAAASE
int key;
cout << "Enter key: ";
cin >> key;
switch (key)
{
case 1:
{
cout << "Linear system equation:" << endl;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
cout << A[i][j] << " * X" << j + 1 << "\t";
cout << " = " << B[i] << "\n";
}
cout << endl;
double det = FindDeterminant(A, N);
if (det == 0)
{
cout << ">>>No roots!<<<" << endl;
}
else
{
cout << "det_A: " << det << endl;
cout << "\n";
for (int i = 0; i < N; ++i)
{
cout << "det_" << i << " = " << InsertFreeCoeff(A, B, N, i) << "\n";
}
cout << endl;
cout << "Roots: " << endl;
for (int i = 0; i < N; ++i)
{
cout << "X" << i + 1 << " = " << InsertFreeCoeff(A, B, N, i) / det << "\n";
}
}
break;
}
case 2:
{
int omega;
cout << "Enter omega: ";
cin >> omega;
cout << endl;
for (int i = 0; i < N; ++i)
{
A[i].push_back(B[i]);
}
double eps;
cout << "Enter epsilon: ";
cin >> eps;
//previous values
vector <double> x_prev(N, 0.0);
while (true)
{
//current values
vector <double> x_curr(N);
for (int i = 0; i < N; ++i)
{
x_curr[i] = A[i][N]; // N+1?
for (int j = 0; j < N; ++j)
{
if (j < i)
{
x_curr[i] = omega*(-A[i][j] * x_curr[j] + x_curr[i]);
}
if (j > i)
{
x_curr[i] = omega*(-A[i][j] * x_prev[j] + x_prev[i]);
}
}
x_curr[i] /= A[i][i];
}
double err = 0.0;
for (int i = 0; i < N; ++i)
{
err += abs(x_curr[i] - x_prev[i]);
}
if (err < eps)
{
break;
}
x_prev = x_curr;
}
for (int i = 0; i < N; ++i)
{
cout << x_prev[i];
}
}
}
cout << "\n";
system("pause");
}