Advertisement
Gerson_Linky

matrix.hpp - v.2

Jul 30th, 2023 (edited)
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | Source Code | 0 0
  1. #pragma once
  2. #include <array>
  3. #include <assert.h>
  4. #include <iostream>
  5.  
  6. template <class T, unsigned int M, unsigned int N> class Matrix
  7. {
  8. protected:
  9.     // std::array<std::array<T, N>, M> matrix;
  10.     T matrix[M][N];
  11.  
  12. public:
  13.     Matrix() = default;
  14.  
  15.     void constructList(std::initializer_list<std::initializer_list<T>> list)
  16.     {
  17.         if (M == 0)
  18.             return;
  19.         if (list.size() > M)
  20.             throw std::out_of_range{"Too many rows."};
  21.  
  22.         int i = 0;
  23.         for (auto &row : list) {
  24.             if (row.size() > N)
  25.                 throw std::out_of_range{"Too many cols."};
  26.  
  27.             int j = 0;
  28.             for (auto &ele : row)
  29.                 matrix[i][j++] = ele;
  30.             i++;
  31.         }
  32.     }
  33.  
  34.     Matrix(std::initializer_list<std::initializer_list<T>> list)
  35.     {
  36.         constructList(list);
  37.     }
  38.  
  39.     bool isSquare() const
  40.     {
  41.         if (M == N)
  42.             return true;
  43.     }
  44.  
  45.     void fill(T (*f)(int, int))
  46.     {
  47.         for (int i = 0; i < M; i++)
  48.             for (int j = 0; j < N; j++)
  49.                 matrix[i][j] = f(i, j);
  50.     }
  51.  
  52.     template <unsigned _N, unsigned _P>
  53.  
  54.     // TODO -> usar algoritmo otimizado
  55.     Matrix<T, M, _P> *mul(const Matrix<T, _N, _P> &mat) const
  56.     {
  57.         if (N != _N)
  58.             throw;
  59.  
  60.         Matrix<T, M, _P> *out = new Matrix<T, M, _P>();
  61.  
  62.         for (int i = 0; i < M; i++)
  63.             for (int j = 0; j < _P; j++) {
  64.                 (*out)[i][j] = 0;
  65.                 for (int k = 0; k < N; k++)
  66.                     (*out)[i][j] += matrix[i][k] * mat.get(k, j);
  67.             }
  68.  
  69.         return out;
  70.     }
  71.  
  72.     Matrix<T, M, N> *getTranspose() const
  73.     {
  74.         auto *out = new Matrix<T, N, M>;
  75.         T t[N][M] = (T[N][M])(*out);
  76.  
  77.         for (int i = 0; i < M; i++)
  78.             for (int j = 0; j < N; j++)
  79.                 t[j][i] = matrix[i][j];
  80.  
  81.         return out;
  82.     }
  83.  
  84.     // std::array<T, N>
  85.     T *operator[](const unsigned int i)
  86.     {
  87.         return matrix[i];
  88.     }
  89.  
  90.     T get(const unsigned int i, const unsigned int j) const
  91.     {
  92.         return matrix[i][j];
  93.     }
  94.  
  95.     Matrix &operator+=(const Matrix<T, M, N> &mat)
  96.     {
  97.         for (int i = 0; i < M; i++)
  98.             for (int j = 0; j < N; j++)
  99.                 matrix[i][j] += mat.get(i, j);
  100.         return *this;
  101.     }
  102.  
  103.     Matrix &operator-=(const Matrix<T, M, N> &mat)
  104.     {
  105.         for (int i = 0; i < M; i++)
  106.             for (int j = 0; j < N; j++)
  107.                 matrix[i][j] -= mat.get(i, j);
  108.         return *this;
  109.     }
  110.  
  111.     Matrix &operator*(const T scalar)
  112.     {
  113.         for (int i = 0; i < M; i++)
  114.             for (int j = 0; i < N; j++)
  115.                 matrix[i][j] *= scalar;
  116.         return *this;
  117.     }
  118.  
  119.     Matrix &operator/(const T scalar)
  120.     {
  121.         for (int i = 0; i < M; i++)
  122.             for (int j = 0; i < N; j++)
  123.                 matrix[i][j] /= scalar;
  124.         return *this;
  125.     }
  126.  
  127.     friend std::ostream &operator<<(std::ostream &out, const Matrix &mat)
  128.     {
  129.         out << '[' << std::endl;
  130.         for (int i = 0; i < M; i++) {
  131.             out << '[';
  132.             if (N > 0)
  133.                 out << mat.get(i, 0);
  134.             for (int j = 1; j < M; j++)
  135.                 out << ", " << mat.get(i, j);
  136.             out << ']' << std::endl;
  137.         }
  138.         out << ']' << std::endl;
  139.  
  140.         return out;
  141.     }
  142. };
  143.  
  144. template <class T, int N> class SquareMatrix : public Matrix<T, N, N>
  145. {
  146. public:
  147.     SquareMatrix() = default;
  148.  
  149.     SquareMatrix(std::initializer_list<std::initializer_list<T>> list)
  150.     {
  151.         this->constructList(list);
  152.     }
  153.  
  154.     bool isSquare() const
  155.     {
  156.         return true;
  157.     }
  158.  
  159.     // WARNING: verificicar corretude
  160.     void transpose()
  161.     {
  162.         for (int i = 0; i < N; i++)
  163.             for (int j = 0; j < i; j++) {
  164.                 T swap = this->matrix[i][j];
  165.                 this->matrix[i][j] = this->matrix[j][i];
  166.                 this->matrix[j][i] = swap;
  167.             }
  168.     }
  169.  
  170.     // TODO -> Implement
  171.     void invert();
  172.     SquareMatrix *getInverse();
  173.     T getDeterminant();
  174. };
  175.  
  176. template <class T> class Matrix2 : public SquareMatrix<T, 2>
  177. {
  178. public:
  179.     Matrix2() = default;
  180.  
  181.     Matrix2(std::initializer_list<std::initializer_list<T>> list)
  182.     {
  183.         this->constructList(list);
  184.     }
  185. };
  186.  
  187. template <class T> class Matrix3 : public SquareMatrix<T, 3>
  188. {
  189. public:
  190.     Matrix3() = default;
  191.  
  192.     Matrix3(std::initializer_list<std::initializer_list<T>> list)
  193.     {
  194.         this->constructList(list);
  195.     }
  196. };
  197.  
  198. template <class T> class Matrix4 : public SquareMatrix<T, 4>
  199. {
  200. public:
  201.     Matrix4() = default;
  202.  
  203.     Matrix4(std::initializer_list<std::initializer_list<T>> list)
  204.     {
  205.         this->constructList(list);
  206.     }
  207. };
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement