Advertisement
Gerson_Linky

matrix.hpp - v.1

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