#include #include #include #include using namespace std; //Findine matrix's minors void GetMinor(vector > A, vector > &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 > A, int size) { if (size == 1) { return A[0][0]; } else { double det = 0; vector > Minor(size - 1, vector (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 > A, vector 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 > A(N, vector (N)); vector 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 x_prev(N, 0.0); while (true) { //current values vector 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"); }