include iostream include vector include cmath using namespace std Find

  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
#include <iostream>
#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 n = 2;
double eps = 0.0001;
bool converge(vector<double> a, vector<double> b)
{
double norm = 0;
for (int i = 0; i < n; i++)
{
norm += (a[i] - b[i])*(a[i] - b[i]);
}
if (sqrt(norm) >= eps)
return false;
return true;
}
void 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 << "1. Cramer. 2.Relax: ";
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:
{
double omega;
cout << "Enter omega: ";
cin >> omega;
cout << endl;
int k=0;
//previous values
vector <double> x_prev(N);
vector <double> x_curr(N,0.0);
do
{
k++;
printf("%d. ", k);
for (int i = 0; i < N; i++)
x_prev[i] = x_curr[i];
for (int i = 0; i < N; i++)
{
double var = 0;
for (int j = 0; j < i; j++)
var = var + (A[i][j] * x_curr[j]);
for (int j = i + 1; j < N; j++)
var = var + (A[i][j] * x_prev[j]);
x_curr[i] = (1 - omega) * x_prev[i] + omega / A[i][i] * (B[i] - var);
printf("%.8f \t", x_prev[i]);
}
cout << endl;
} while (!converge(x_curr, x_prev));
cout << k << endl;
}
}
cout << "\n";
system("pause");
}