Advertisement
Gerson_Linky

matrix.hpp - v.3

Jul 30th, 2023
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 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.     Matrix(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 &el : row)
  29.                 matrix[i][j++] = el;
  30.             i++;
  31.         }
  32.     }
  33.  
  34.     Matrix(const T arr[M][N])
  35.     {
  36.         T *m = *this->matrix;
  37.         for (const T *p = *arr; p < *(arr + M - 1) + N; ++p)
  38.             *m++ = *p;
  39.     }
  40.  
  41.     bool isSquare() const
  42.     {
  43.         if (M == N)
  44.             return true;
  45.     }
  46.  
  47.     void fill(T (*f)(int, int))
  48.     {
  49.         for (int i = 0; i < M; i++)
  50.             for (int j = 0; j < N; j++)
  51.                 matrix[i][j] = f(i, j);
  52.     }
  53.  
  54.     template <unsigned _N, unsigned _P>
  55.  
  56.     // TODO -> usar algoritmo otimizado
  57.     Matrix<T, M, _P> *mul(const Matrix<T, _N, _P> &mat) const
  58.     {
  59.         if (N != _N)
  60.             throw;
  61.  
  62.         Matrix<T, M, _P> *out = new Matrix<T, M, _P>();
  63.  
  64.         for (int i = 0; i < M; i++)
  65.             for (int j = 0; j < _P; j++) {
  66.                 (*out)[i][j] = 0;
  67.                 for (int k = 0; k < N; k++)
  68.                     (*out)[i][j] += matrix[i][k] * mat.get(k, j);
  69.             }
  70.  
  71.         return out;
  72.     }
  73.  
  74.     // WARNING: verificicar corretude
  75.  
  76.     void transpose()
  77.     {
  78.         for (int i = 0; i < N; i++)
  79.             for (int j = 0; j < i; j++) {
  80.                 T swap = this->matrix[i][j];
  81.                 this->matrix[i][j] = this->matrix[j][i];
  82.                 this->matrix[j][i] = swap;
  83.             }
  84.     }
  85.  
  86.     Matrix<T, M, N> *getTranspose() const
  87.     {
  88.         auto *out = new Matrix<T, N, M>;
  89.         T t[N][M] = (T[N][M])(*out);
  90.  
  91.         for (int i = 0; i < M; i++)
  92.             for (int j = 0; j < N; j++)
  93.                 t[j][i] = matrix[i][j];
  94.  
  95.         return out;
  96.     }
  97.  
  98.     // std::array<T, N>
  99.     T *operator[](const unsigned int i)
  100.     {
  101.         return matrix[i];
  102.     }
  103.  
  104.     T get(const unsigned int i, const unsigned int j) const
  105.     {
  106.         return matrix[i][j];
  107.     }
  108.  
  109.     Matrix &operator+=(const Matrix<T, M, N> &mat)
  110.     {
  111.         for (int i = 0; i < M; i++)
  112.             for (int j = 0; j < N; j++)
  113.                 matrix[i][j] += mat.get(i, j);
  114.         return *this;
  115.     }
  116.  
  117.     Matrix &operator-=(const Matrix<T, M, N> &mat)
  118.     {
  119.         for (int i = 0; i < M; i++)
  120.             for (int j = 0; j < N; j++)
  121.                 matrix[i][j] -= mat.get(i, j);
  122.         return *this;
  123.     }
  124.  
  125.     Matrix &operator*(const T scalar)
  126.     {
  127.         for (int i = 0; i < M; i++)
  128.             for (int j = 0; i < N; j++)
  129.                 matrix[i][j] *= scalar;
  130.         return *this;
  131.     }
  132.  
  133.     Matrix &operator/(const T scalar)
  134.     {
  135.         for (int i = 0; i < M; i++)
  136.             for (int j = 0; i < N; j++)
  137.                 matrix[i][j] /= scalar;
  138.         return *this;
  139.     }
  140.  
  141.     friend std::ostream &operator<<(std::ostream &out, const Matrix &mat)
  142.     {
  143.         out << '[' << std::endl;
  144.         for (int i = 0; i < M; i++) {
  145.             out << '[';
  146.             if (N > 0)
  147.                 out << mat.get(i, 0);
  148.             for (int j = 1; j < M; j++)
  149.                 out << ", " << mat.get(i, j);
  150.             out << ']' << std::endl;
  151.         }
  152.         out << ']' << std::endl;
  153.  
  154.         return out;
  155.     }
  156. };
  157.  
  158. template <class T, int N> class SquareMatrix : public Matrix<T, N, N>
  159. {
  160. public:
  161.     using Matrix<T, N, N>::Matrix;
  162.  
  163.     bool isSquare() const
  164.     {
  165.         return true;
  166.     }
  167.  
  168.     // TODO -> Implement
  169.     void invert();
  170.     SquareMatrix *getInverse();
  171.     T getDeterminant();
  172. };
  173.  
  174. template <class T> using Matrix2 = SquareMatrix<T, 2>;
  175. template <class T> using Matrix3 = SquareMatrix<T, 3>;
  176. template <class T> using Matrix4 = SquareMatrix<T, 4>;
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement