Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bitset>
- #include <fstream>
- #include <iomanip>
- #include <iostream>
- #include <map>
- #include <cmath>
- using namespace std;
- map<pair<int, int>, long double> calculatedMinors;
- long double calculateDeterminant(long double** matrix, int matrixSize, pair<int, int> minorMask);
- long double calculateMinor(long double** matrix, int matrixSize, int row, int column, pair<int, int> minorMask);
- long double** createMatrix(int rowNumber, int columnNumber)
- {
- auto** matrix = new long double* [rowNumber];
- for (int i = 0; i < rowNumber; i++)
- matrix[i] = new long double[columnNumber];
- for (int i = 0; i < rowNumber; i++)
- {
- for (int j = 0; j < columnNumber; j++)
- matrix[i][j] = 0;
- }
- return matrix;
- }
- void deleteMatrix(long double** matrix, int sizeMatrix)
- {
- for (int i = 0; i < sizeMatrix; i++)
- delete[] matrix[i];
- delete matrix;
- }
- void createNewMatrixOnBaseWithoutRowAndCol(long double** matrix, long double** newMatrix, int matrixSize, int row, int column)
- {
- bool skipRow = false;
- for (int i = 0; i < matrixSize - 1; i++)
- {
- bool skipColumn = false;
- if (row == i)
- skipRow = true;
- for (int j = 0; j < matrixSize - 1; j++)
- {
- if (column == j)
- skipColumn = true;
- newMatrix[i][j] = matrix[i + (skipRow == true ? 1 : 0)][j + (skipColumn == true ? 1 : 0)];
- }
- }
- }
- long double calculateDeterminant(long double** matrix, int matrixSize, pair<int, int> minorMask)
- {
- static int initialSize = 0;
- if (initialSize == 0)
- initialSize = matrixSize;
- long double ans = 0;
- if (matrixSize == 1)
- return matrix[0][0];
- if (matrixSize == 2)
- return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
- for (int i = 0; i < matrixSize; i++)
- {
- ans += matrix[0][i] * calculateMinor(matrix, matrixSize, 0, i, minorMask);
- }
- if (matrixSize != initialSize)
- deleteMatrix(matrix, matrixSize);
- return ans;
- }
- long double calculateMinor(long double** matrix, int matrixSize, int row, int column, pair<int, int> minorMask)
- {
- minorMask = { minorMask.first ^ (1 << (matrixSize - 1 - row)), minorMask.second ^ (1 << (matrixSize - 1 - column)) };
- cout << bitset<8>(minorMask.first) << " " << bitset<8>(minorMask.second) << endl;
- if (calculatedMinors[minorMask] < 0.00000001)
- {
- long double** newMatrix = createMatrix(matrixSize - 1, matrixSize - 1);
- createNewMatrixOnBaseWithoutRowAndCol(matrix, newMatrix, matrixSize, row, column);
- calculatedMinors[minorMask] = pow(-1, row + column) * calculateDeterminant(newMatrix, matrixSize - 1, minorMask);
- }
- return calculatedMinors[minorMask];
- }
- void calculateInverseMatrix(long double** matrix, int matrixSize, long double** inverseMatrix)
- {
- pair<int, int> minorMask = { (1 << (matrixSize + 1)) - 1,(1 << (matrixSize + 1)) - 1 };
- long double determinant = calculateDeterminant(matrix, matrixSize, minorMask);
- for (int i = 0; i < matrixSize; i++)
- {
- for (int j = 0; j < matrixSize; j++)
- {
- inverseMatrix[j][i] = calculateMinor(matrix, matrixSize, i, j, minorMask) / determinant;
- }
- }
- }
- long double** multiplyMatrices(long double** a, int a_rowNumber, int a_columnNumber, long double** b, int b_rowNumber, int b_columnNumber)
- {
- if (a_columnNumber != b_rowNumber)
- exit(-1);
- auto multiplicationMatrix = createMatrix(a_rowNumber, b_columnNumber);
- for (int r_a = 0; r_a < a_rowNumber; r_a++)
- {
- for (int c_b = 0; c_b < b_columnNumber; c_b++)
- {
- long double result = 0.0;
- for (int c_a = 0, r_b = 0; c_a < a_columnNumber && r_b < b_rowNumber; c_a++, r_b++)
- {
- result += a[r_a][c_a] * b[r_b][c_b];
- }
- multiplicationMatrix[r_a][c_b] = result;
- }
- }
- return multiplicationMatrix;
- }
- long double** transposeMatrix(long double** a, int a_rowNumber, int a_columnNumber)
- {
- long double** result = createMatrix(a_columnNumber, a_rowNumber);
- for (int i = 0; i < a_rowNumber; i++)
- {
- for (int j = 0; j < a_columnNumber; j++)
- {
- result[j][i] = a[i][j];
- }
- }
- /*for (int i = 0; i < a_columnNumber; i++)
- {
- for (int j = 0; j < a_rowNumber; j++)
- {
- cout << result[i][j] << " ";
- }
- cout << endl;
- }*/
- return result;
- }
- int main()
- {
- //cout << calculatedMinors[{0,0}] << endl;
- ifstream in("input.txt");
- ofstream out("output.txt");
- out << fixed << setprecision(2);
- int n, m;
- in >> n >> m;
- n++;
- auto A = createMatrix(m, n);
- auto b = createMatrix(m, 1);
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j <= n; j++)
- {
- if (j == 0)
- A[i][0] = 1.0;
- else
- {
- if (j != n)
- in >> A[i][j];
- else in >> b[i][0];
- }
- }
- }
- out << "A:\n";
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- out << A[i][j] << " ";
- out << "\n";
- }
- out << "\nb:\n";
- for (int i = 0; i < m; i++)
- out << b[i][0] << "\n";
- long double** A_T = transposeMatrix(A, m, n);
- long double** A_T_A = multiplyMatrices(A_T, n, m, A, m, n);
- out << "\nA_T*A:\n";
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- out << A_T_A[i][j] << " ";
- out << "\n";
- }
- long double** inverse = createMatrix(n, n);
- calculateInverseMatrix(A_T_A, n, inverse);
- out << "\n(A_T*A)_-1:\n";
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- out << inverse[i][j] << " ";
- out << "\n";
- }
- long double** proj = multiplyMatrices(inverse, n, n, A_T, n, m);
- out << "\n(A_T*A)_-1*A_T:\n";
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- out << proj[i][j] << " ";
- out << "\n";
- }
- long double** x = multiplyMatrices(proj, n, m, b, m, 1);
- out << "\nx:\n";
- for (int i = 0; i < n; i++)
- out << x[i][0] << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement