Advertisement
habur331

Untitled

Mar 27th, 2022
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include <bitset>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <map>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. map<pair<int, int>, long double> calculatedMinors;
  11.  
  12. long double calculateDeterminant(long double** matrix, int matrixSize, pair<int, int> minorMask);
  13.  
  14. long double calculateMinor(long double** matrix, int matrixSize, int row, int column, pair<int, int> minorMask);
  15.  
  16. long double** createMatrix(int rowNumber, int columnNumber)
  17. {
  18.     auto** matrix = new long double* [rowNumber];
  19.  
  20.     for (int i = 0; i < rowNumber; i++)
  21.         matrix[i] = new long double[columnNumber];
  22.  
  23.     for (int i = 0; i < rowNumber; i++)
  24.     {
  25.         for (int j = 0; j < columnNumber; j++)
  26.             matrix[i][j] = 0;
  27.     }
  28.  
  29.     return matrix;
  30. }
  31.  
  32. void deleteMatrix(long double** matrix, int sizeMatrix)
  33. {
  34.     for (int i = 0; i < sizeMatrix; i++)
  35.         delete[] matrix[i];
  36.  
  37.     delete matrix;
  38. }
  39.  
  40. void createNewMatrixOnBaseWithoutRowAndCol(long double** matrix, long double** newMatrix, int matrixSize, int row, int column)
  41. {
  42.     bool skipRow = false;
  43.  
  44.     for (int i = 0; i < matrixSize - 1; i++)
  45.     {
  46.         bool skipColumn = false;
  47.  
  48.         if (row == i)
  49.             skipRow = true;
  50.  
  51.         for (int j = 0; j < matrixSize - 1; j++)
  52.         {
  53.             if (column == j)
  54.                 skipColumn = true;
  55.  
  56.             newMatrix[i][j] = matrix[i + (skipRow == true ? 1 : 0)][j + (skipColumn == true ? 1 : 0)];
  57.         }
  58.     }
  59. }
  60.  
  61. long double calculateDeterminant(long double** matrix, int matrixSize, pair<int, int> minorMask)
  62. {
  63.     static int initialSize = 0;
  64.     if (initialSize == 0)
  65.         initialSize = matrixSize;
  66.  
  67.     long double ans = 0;
  68.  
  69.     if (matrixSize == 1)
  70.         return matrix[0][0];
  71.     if (matrixSize == 2)
  72.         return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  73.  
  74.     for (int i = 0; i < matrixSize; i++)
  75.     {
  76.         ans += matrix[0][i] * calculateMinor(matrix, matrixSize, 0, i, minorMask);
  77.     }
  78.  
  79.     if (matrixSize != initialSize)
  80.         deleteMatrix(matrix, matrixSize);
  81.  
  82.     return ans;
  83. }
  84.  
  85. long double calculateMinor(long double** matrix, int matrixSize, int row, int column, pair<int, int> minorMask)
  86. {
  87.     minorMask = { minorMask.first ^ (1 << (matrixSize - 1 - row)), minorMask.second ^ (1 << (matrixSize - 1 - column)) };
  88.     cout << bitset<8>(minorMask.first) << " " << bitset<8>(minorMask.second) << endl;
  89.     if (calculatedMinors[minorMask] < 0.00000001)
  90.     {
  91.         long double** newMatrix = createMatrix(matrixSize - 1, matrixSize - 1);
  92.         createNewMatrixOnBaseWithoutRowAndCol(matrix, newMatrix, matrixSize, row, column);
  93.         calculatedMinors[minorMask] = pow(-1, row + column) * calculateDeterminant(newMatrix, matrixSize - 1, minorMask);
  94.     }
  95.  
  96.     return calculatedMinors[minorMask];
  97. }
  98.  
  99. void calculateInverseMatrix(long double** matrix, int matrixSize, long double** inverseMatrix)
  100. {
  101.     pair<int, int> minorMask = { (1 << (matrixSize + 1)) - 1,(1 << (matrixSize + 1)) - 1 };
  102.     long double determinant = calculateDeterminant(matrix, matrixSize, minorMask);
  103.  
  104.     for (int i = 0; i < matrixSize; i++)
  105.     {
  106.         for (int j = 0; j < matrixSize; j++)
  107.         {
  108.             inverseMatrix[j][i] = calculateMinor(matrix, matrixSize, i, j, minorMask) / determinant;
  109.         }
  110.     }
  111. }
  112.  
  113. long double** multiplyMatrices(long double** a, int a_rowNumber, int a_columnNumber, long double** b, int b_rowNumber, int b_columnNumber)
  114. {
  115.     if (a_columnNumber != b_rowNumber)
  116.         exit(-1);
  117.  
  118.     auto multiplicationMatrix = createMatrix(a_rowNumber, b_columnNumber);
  119.  
  120.     for (int r_a = 0; r_a < a_rowNumber; r_a++)
  121.     {
  122.         for (int c_b = 0; c_b < b_columnNumber; c_b++)
  123.         {
  124.             long double result = 0.0;
  125.  
  126.             for (int c_a = 0, r_b = 0; c_a < a_columnNumber && r_b < b_rowNumber; c_a++, r_b++)
  127.             {
  128.                 result += a[r_a][c_a] * b[r_b][c_b];
  129.             }
  130.  
  131.             multiplicationMatrix[r_a][c_b] = result;
  132.         }
  133.     }
  134.  
  135.     return multiplicationMatrix;
  136. }
  137.  
  138. long double** transposeMatrix(long double** a, int a_rowNumber, int a_columnNumber)
  139. {
  140.     long double** result = createMatrix(a_columnNumber, a_rowNumber);
  141.  
  142.     for (int i = 0; i < a_rowNumber; i++)
  143.     {
  144.         for (int j = 0; j < a_columnNumber; j++)
  145.         {
  146.             result[j][i] = a[i][j];
  147.         }
  148.     }
  149.  
  150.     /*for (int i = 0; i < a_columnNumber; i++)
  151.     {
  152.         for (int j = 0; j < a_rowNumber; j++)
  153.         {
  154.             cout << result[i][j] << " ";
  155.         }
  156.         cout << endl;
  157.     }*/
  158.  
  159.  
  160.     return result;
  161. }
  162.  
  163. int main()
  164. {
  165.     //cout << calculatedMinors[{0,0}] << endl;
  166.  
  167.  
  168.     ifstream in("input.txt");
  169.     ofstream out("output.txt");
  170.     out << fixed << setprecision(2);
  171.  
  172.     int n, m;
  173.     in >> n >> m;
  174.     n++;
  175.  
  176.     auto A = createMatrix(m, n);
  177.     auto b = createMatrix(m, 1);
  178.  
  179.     for (int i = 0; i < m; i++)
  180.     {
  181.         for (int j = 0; j <= n; j++)
  182.         {
  183.             if (j == 0)
  184.                 A[i][0] = 1.0;
  185.             else
  186.             {
  187.                 if (j != n)
  188.                     in >> A[i][j];
  189.                 else in >> b[i][0];
  190.             }
  191.         }
  192.     }
  193.  
  194.     out << "A:\n";
  195.     for (int i = 0; i < m; i++)
  196.     {
  197.         for (int j = 0; j < n; j++)
  198.             out << A[i][j] << " ";
  199.         out << "\n";
  200.     }
  201.  
  202.     out << "\nb:\n";
  203.     for (int i = 0; i < m; i++)
  204.         out << b[i][0] << "\n";
  205.  
  206.     long double** A_T = transposeMatrix(A, m, n);
  207.  
  208.     long double** A_T_A = multiplyMatrices(A_T, n, m, A, m, n);
  209.     out << "\nA_T*A:\n";
  210.     for (int i = 0; i < n; i++)
  211.     {
  212.         for (int j = 0; j < n; j++)
  213.             out << A_T_A[i][j] << " ";
  214.         out << "\n";
  215.     }
  216.  
  217.     long double** inverse = createMatrix(n, n);
  218.     calculateInverseMatrix(A_T_A, n, inverse);
  219.  
  220.     out << "\n(A_T*A)_-1:\n";
  221.     for (int i = 0; i < n; i++)
  222.     {
  223.         for (int j = 0; j < n; j++)
  224.             out << inverse[i][j] << " ";
  225.         out << "\n";
  226.     }
  227.  
  228.     long double** proj = multiplyMatrices(inverse, n, n, A_T, n, m);
  229.     out << "\n(A_T*A)_-1*A_T:\n";
  230.     for (int i = 0; i < n; i++)
  231.     {
  232.         for (int j = 0; j < m; j++)
  233.             out << proj[i][j] << " ";
  234.         out << "\n";
  235.     }
  236.  
  237.     long double** x = multiplyMatrices(proj, n, m, b, m, 1);
  238.     out << "\nx:\n";
  239.     for (int i = 0; i < n; i++)
  240.         out << x[i][0] << "\n";
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement