Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <array>
- #include <assert.h>
- #include <iostream>
- template <class T, unsigned int M, unsigned int N> class Matrix
- {
- protected:
- // std::array<std::array<T, N>, M> matrix;
- T matrix[M][N];
- public:
- Matrix() = default;
- void constructList(std::initializer_list<std::initializer_list<T>> list)
- {
- if (M == 0)
- return;
- if (list.size() > M)
- throw std::out_of_range{"Too many rows."};
- int i = 0;
- for (auto &row : list) {
- if (row.size() > N)
- throw std::out_of_range{"Too many cols."};
- int j = 0;
- for (auto &ele : row)
- matrix[i][j++] = ele;
- i++;
- }
- }
- Matrix(std::initializer_list<std::initializer_list<T>> list)
- {
- constructList(list);
- }
- bool isSquare() const
- {
- if (M == N)
- return true;
- }
- void fill(T (*f)(int, int))
- {
- for (int i = 0; i < M; i++)
- for (int j = 0; j < N; j++)
- matrix[i][j] = f(i, j);
- }
- template <unsigned _N, unsigned _P>
- // TODO -> usar algoritmo otimizado
- Matrix<T, M, _P> *mul(const Matrix<T, _N, _P> &mat) const
- {
- if (N != _N)
- throw;
- Matrix<T, M, _P> *out = new Matrix<T, M, _P>();
- for (int i = 0; i < M; i++)
- for (int j = 0; j < _P; j++) {
- (*out)[i][j] = 0;
- for (int k = 0; k < N; k++)
- (*out)[i][j] += matrix[i][k] * mat.get(k, j);
- }
- return out;
- }
- Matrix<T, M, N> *getTranspose() const
- {
- auto *out = new Matrix<T, N, M>;
- T t[N][M] = (T[N][M])(*out);
- for (int i = 0; i < M; i++)
- for (int j = 0; j < N; j++)
- t[j][i] = matrix[i][j];
- return out;
- }
- // std::array<T, N>
- T *operator[](const unsigned int i)
- {
- return matrix[i];
- }
- T get(const unsigned int i, const unsigned int j) const
- {
- return matrix[i][j];
- }
- Matrix &operator+=(const Matrix<T, M, N> &mat)
- {
- for (int i = 0; i < M; i++)
- for (int j = 0; j < N; j++)
- matrix[i][j] += mat.get(i, j);
- return *this;
- }
- Matrix &operator-=(const Matrix<T, M, N> &mat)
- {
- for (int i = 0; i < M; i++)
- for (int j = 0; j < N; j++)
- matrix[i][j] -= mat.get(i, j);
- return *this;
- }
- Matrix &operator*(const T scalar)
- {
- for (int i = 0; i < M; i++)
- for (int j = 0; i < N; j++)
- matrix[i][j] *= scalar;
- return *this;
- }
- Matrix &operator/(const T scalar)
- {
- for (int i = 0; i < M; i++)
- for (int j = 0; i < N; j++)
- matrix[i][j] /= scalar;
- return *this;
- }
- friend std::ostream &operator<<(std::ostream &out, const Matrix &mat)
- {
- out << '[' << std::endl;
- for (int i = 0; i < M; i++) {
- out << '[';
- if (N > 0)
- out << mat.get(i, 0);
- for (int j = 1; j < M; j++)
- out << ", " << mat.get(i, j);
- out << ']' << std::endl;
- }
- out << ']' << std::endl;
- return out;
- }
- };
- template <class T, int N> class SquareMatrix : public Matrix<T, N, N>
- {
- public:
- SquareMatrix() = default;
- SquareMatrix(std::initializer_list<std::initializer_list<T>> list)
- {
- this->constructList(list);
- }
- bool isSquare() const
- {
- return true;
- }
- // WARNING: verificicar corretude
- void transpose()
- {
- for (int i = 0; i < N; i++)
- for (int j = 0; j < i; j++) {
- T swap = this->matrix[i][j];
- this->matrix[i][j] = this->matrix[j][i];
- this->matrix[j][i] = swap;
- }
- }
- // TODO -> Implement
- void invert();
- SquareMatrix *getInverse();
- T getDeterminant();
- };
- template <class T> class Matrix2 : public SquareMatrix<T, 2>
- {
- public:
- Matrix2() = default;
- Matrix2(std::initializer_list<std::initializer_list<T>> list)
- {
- this->constructList(list);
- }
- };
- template <class T> class Matrix3 : public SquareMatrix<T, 3>
- {
- public:
- Matrix3() = default;
- Matrix3(std::initializer_list<std::initializer_list<T>> list)
- {
- this->constructList(list);
- }
- };
- template <class T> class Matrix4 : public SquareMatrix<T, 4>
- {
- public:
- Matrix4() = default;
- Matrix4(std::initializer_list<std::initializer_list<T>> list)
- {
- this->constructList(list);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement