Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.26 KB | None | 0 0
  1. #include "aghMatrix.h"
  2. #include <cmath>
  3.  
  4. // Parameter Constructor
  5. template <typename T>
  6. AGHMatrix<T>::AGHMatrix(const std::vector<std::vector<T>>& mat) {
  7.     matrix.resize(mat.size());
  8.     for (unsigned i = 0; i < mat.size(); i++) {
  9.         matrix[i].resize(mat[i].size());
  10.         for (unsigned j = 0; j < mat[i].size(); j++) {
  11.             matrix[i][j] = mat[i][j];
  12.         }
  13.     }
  14.     rows = matrix.size();
  15.     cols = matrix[0].size();
  16. }
  17.  
  18. // Parameter Constructor
  19. template <typename T>
  20. AGHMatrix<T>::AGHMatrix(unsigned _rows, unsigned _cols, const T& _initial) {
  21.     matrix.resize(_rows);
  22.     for (unsigned i = 0; i < matrix.size(); i++) {
  23.         matrix[i].resize(_cols, _initial);
  24.     }
  25.     rows = _rows;
  26.     cols = _cols;
  27. }
  28.  
  29. // Copy Constructor
  30. template <typename T>
  31. AGHMatrix<T>::AGHMatrix(const AGHMatrix<T>& rhs) {
  32.     matrix = rhs.matrix;
  33.     rows = rhs.get_rows();
  34.     cols = rhs.get_cols();
  35. }
  36.  
  37. // Get the number of rows of the matrix
  38. template <typename T>
  39. unsigned AGHMatrix<T>::get_rows() const {
  40.     return this->rows;
  41. }
  42.  
  43. // Get the number of columns of the matrix
  44. template <typename T>
  45. unsigned AGHMatrix<T>::get_cols() const {
  46.     return this->cols;
  47. }
  48.  
  49. // Assignment Operator
  50. template <typename T>
  51. AGHMatrix<T>& AGHMatrix<T>::operator=(const AGHMatrix<T>& rhs) {
  52.     if (&rhs == this) return *this;
  53.  
  54.     unsigned new_rows = rhs.get_rows();
  55.     unsigned new_cols = rhs.get_cols();
  56.  
  57.     matrix.resize(new_rows);
  58.     for (unsigned i = 0; i < matrix.size(); i++) {
  59.         matrix[i].resize(new_cols);
  60.     }
  61.  
  62.     for (unsigned i = 0; i < new_rows; i++) {
  63.         for (unsigned j = 0; j < new_cols; j++) {
  64.             matrix[i][j] = rhs(i, j);
  65.         }
  66.     }
  67.     rows = new_rows;
  68.     cols = new_cols;
  69.  
  70.     return *this;
  71. }
  72.  
  73. // Access the individual elements
  74. template <typename T>
  75. T& AGHMatrix<T>::operator()(const unsigned& row, const unsigned& col) {
  76.     return this->matrix[row][col];
  77. }
  78.  
  79. // Access the individual elements (const)
  80. template <typename T>
  81. const T& AGHMatrix<T>::operator()(const unsigned& row,
  82.                                   const unsigned& col) const {
  83.     return this->matrix[row][col];
  84. }
  85.  
  86. // Addition of two matrices
  87. template <typename T>
  88. AGHMatrix<T> AGHMatrix<T>::operator+(const AGHMatrix<T>& rhs) {
  89.     // Task 1 - implement addition of two matrices
  90.     AGHMatrix<T> new_matrix = *this;
  91.  
  92.     for (int y = 0; y < this->rows; y++) {
  93.         for (int x = 0; x < this->cols; x++) {
  94.             new_matrix.matrix[y][x] += rhs(y, x);
  95.         }
  96.     }
  97.     return new_matrix;
  98. }
  99.  
  100. template <typename T>
  101. AGHMatrix<T> AGHMatrix<T>::operator-(const AGHMatrix<T>& rhs) {
  102.     // Task 1 - implement addition of two matrices
  103.     AGHMatrix<T> new_matrix = *this;
  104.  
  105.     for (int y = 0; y < this->rows; y++) {
  106.         for (int x = 0; x < this->cols; x++) {
  107.             new_matrix.matrix[y][x] -= rhs(y, x);
  108.         }
  109.     }
  110.     return new_matrix;
  111. }
  112.  
  113. // Left multiplication of this matrix and another
  114. template <typename T>
  115. AGHMatrix<T> AGHMatrix<T>::operator*(const AGHMatrix<T>& rhs) {
  116.     // Task 1 - implement multiplication of two matrices
  117.  
  118.     AGHMatrix<T>* new_matrix = new AGHMatrix(this->rows, rhs.cols, 0);
  119.  
  120.     for (int x = 0; x < rhs.cols; x++) {
  121.         for (int y = 0; y < this->rows; y++) {
  122.             for (int i = 0; i < this->rows; i++) {
  123.                 new_matrix->matrix[y][x] +=
  124.                     this->matrix[y][i] * rhs.matrix[i][x];
  125.             }
  126.         }
  127.     }
  128.  
  129.     return *new_matrix;
  130. }
  131.  
  132. // Printing matrix
  133. template <typename T>
  134. std::ostream& operator<<(std::ostream& stream, const AGHMatrix<T>& matrix) {
  135.     for (int i = 0; i < matrix.get_rows(); i++) {
  136.         for (int j = 0; j < matrix.get_cols(); j++) {
  137.             stream << matrix(i, j) << ", ";
  138.         }
  139.         stream << std::endl;
  140.     }
  141.     stream << std::endl;
  142.  
  143.     return stream;
  144. }
  145.  
  146. template <typename T>
  147. bool AGHMatrix<T>::is_symmetric() const {
  148.     if (this->cols != this->rows) return false;
  149.  
  150.     for (int y = 0; y < this->rows; y++) {
  151.         for (int x = 0; x < this->cols; x++) {
  152.             if (this->matrix[y][x] != this->matrix[x][y]) return false;
  153.         }
  154.     }
  155.  
  156.     return true;
  157. }
  158.  
  159. template <typename T>
  160. AGHMatrix<T> AGHMatrix<T>::without_row_and_column(int row, int col) {
  161.     AGHMatrix<T> new_matrix = *this;
  162.  
  163.     new_matrix.rows--;
  164.     new_matrix.cols--;
  165.  
  166.     new_matrix.matrix.erase(new_matrix.matrix.begin() + row);
  167.  
  168.     for (int y = 0; y < new_matrix.rows; y++) {
  169.         new_matrix.matrix[y].erase(new_matrix.matrix[y].begin() + col);
  170.     }
  171.  
  172.     return new_matrix;
  173. }
  174.  
  175. template <typename T>
  176. T AGHMatrix<T>::determinant() {
  177.     if (this->cols == 1) return this->matrix[0][0];
  178.  
  179.     T retval = 0;
  180.     int coeff = 1;
  181.  
  182.     for (int x = 0; x < this->cols; x++) {
  183.         T det = this->without_row_and_column(0, x).determinant();
  184.         retval += coeff * this->matrix[0][x] * det;
  185.         coeff *= -1;
  186.     }
  187.  
  188.     return retval;
  189. }
  190.  
  191. template <typename T>
  192. AGHMatrix<T> AGHMatrix<T>::transpose() {
  193.     AGHMatrix<T> retval = AGHMatrix(this->cols, this->rows, 0);
  194.  
  195.     for (int y = 0; y < this->rows; y++) {
  196.         for (int x = 0; x < this->cols; x++) {
  197.             retval.matrix[x][y] = this->matrix[y][x];
  198.         }
  199.     }
  200.  
  201.     return retval;
  202. }
  203.  
  204. template <typename T>
  205. std::pair<AGHMatrix<T>, AGHMatrix<T>> AGHMatrix<T>::LU() {
  206.     AGHMatrix<T> U = *this;
  207.     AGHMatrix<T> L = AGHMatrix<T>(this->rows, this->cols, 0);
  208.     for (int y = 0; y < this->rows; y++) {
  209.         L(y, y) = 1;
  210.     }
  211.  
  212.     for (int x = 0; x < this->cols; x++) {
  213.         for (int i = x + 1; i < this->cols; i++) {
  214.             T l = -U(i, x) / U(x, x);
  215.  
  216.             for (int j = x; j < this->cols; j++) {
  217.                 U(i, j) += l * U(x, j);
  218.             }
  219.  
  220.             L(i, x) = -l;
  221.         }
  222.     }
  223.  
  224.     return std::make_pair(L, U);
  225. }
  226.  
  227. template <typename T>
  228. AGHMatrix<T> AGHMatrix<T>::cholesky() {
  229.     AGHMatrix<T> L = AGHMatrix<T>(this->rows, this->cols, 0);
  230.  
  231.     for (int y = 0; y < this->rows; y++) {
  232.         for (int x = 0; x <= y; x++) {
  233.             T acc = this->matrix[y][x];
  234.             if (y == x) {
  235.                 for (int i = 0; i < x; i++) {
  236.                     acc -= pow(L(x, i), 2);
  237.                 }
  238.                 L(x, x) = sqrt(acc);
  239.             } else {
  240.                 for (int i = 0; i < x; i++) {
  241.                     acc -= L(x, i) * L(y, i);
  242.                 }
  243.                 L(y, x) = acc / L(x, x);
  244.             }
  245.         }
  246.     }
  247.  
  248.     return L;
  249. }
  250.  
  251. template <typename T>
  252. AGHMatrix<T> AGHMatrix<T>::gauss(AGHMatrix<T> b) {
  253.     AGHMatrix<T> A = *this;
  254.  
  255.     for (int x = 0; x < A.cols; x++) {
  256.         for (int y = x; y < A.rows; y++) {
  257.             if (A.matrix[y][x] == 0) continue;
  258.  
  259.             T f = A.matrix[y][x];
  260.             for (int i = x; i < A.cols; i++) {
  261.                 A.matrix[y][i] /= f;
  262.             }
  263.             b.matrix[y][0] /= f;
  264.  
  265.             for (int i = y + 1; i < A.rows; i++) {
  266.                 T c = A.matrix[i][x];
  267.                 for (int j = x; j < A.cols; j++) {
  268.                     A.matrix[i][j] -= c * A.matrix[y][j];
  269.                 }
  270.                 b.matrix[i][0] -= c * b.matrix[y][0];
  271.             }
  272.         }
  273.     }
  274.  
  275.     for (int x = A.cols - 1; x >= 0; x--) {
  276.         for (int y = x; y >= 0; y--) {
  277.             if (A.matrix[y][x] == 0) continue;
  278.  
  279.             for (int i = y - 1; i >= 0; i--) {
  280.                 b.matrix[i][0] -= A.matrix[i][x] * b.matrix[y][0];
  281.                 A.matrix[i][x] = 0;
  282.             }
  283.         }
  284.     }
  285.  
  286.     return b;
  287. }
  288.  
  289. template <typename T>
  290. T AGHMatrix<T>::length() {
  291.     T retval = 0;
  292.     for (int y = 0; y < this->rows; y++) {
  293.         retval += pow(this->matrix[y][0], 2);
  294.     }
  295.     return retval;
  296. }
  297.  
  298. template <typename T>
  299. AGHMatrix<T> AGHMatrix<T>::jacobi(AGHMatrix<T> b) {
  300.     AGHMatrix<T> A = *this;
  301.  
  302.     AGHMatrix<T> x = AGHMatrix<T>(A.rows, 1, 1);
  303.     AGHMatrix<T> prev_x = AGHMatrix<T>(A.rows, 1, 0);
  304.  
  305.     while (std::abs((x - prev_x).length()) > 0.00001) {
  306.         prev_x = x;
  307.  
  308.         for (int y = 0; y < A.rows; y++) {
  309.             x(y, 0) = b(y, 0);
  310.             for (int j = 0; j < A.cols; j++) {
  311.                 if (j == y) continue;
  312.                 x(y, 0) -= A(y, j) * prev_x(j, 0);
  313.             }
  314.             x(y, 0) /= A(y, y);
  315.         }
  316.     }
  317.  
  318.     return x;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement