Created
January 24, 2017 13:17
-
-
Save NotFounds/fa96e4250f09f181b96b85276516e30e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <random> | |
class Matrix | |
{ | |
private: | |
double* _matrix; | |
public: | |
int row; | |
int col; | |
Matrix(); | |
Matrix(int _row, int _col); | |
Matrix(int _row, int _col, double* args); | |
bool resize(int _row, int _col); | |
double operator()(int i, int j) const; | |
double* operator[](int i); | |
bool operator==(const Matrix _mat)const; | |
bool operator!=(const Matrix _mat)const; | |
Matrix& operator+=(const Matrix& _mat); | |
Matrix& operator-=(const Matrix& _mat); | |
void random(double min, double max); | |
void random(double min, double max, int seed); | |
~Matrix(); | |
Matrix& Matrix::operator=(const Matrix _mat) | |
{ | |
resize(_mat.row, _mat.col); | |
for (int i = 0; i < _mat.row; ++i) | |
{ | |
for (int j = 0; j < _mat.col; ++j) | |
{ | |
_matrix[i * col + j] = _mat(i, j); | |
} | |
} | |
return (*this); | |
} | |
inline Matrix Matrix::operator+(const Matrix& _mat) const | |
{ | |
if (_mat.row != this->row) throw "ArgumentException"; | |
if (_mat.col != this->col) throw "ArgumentException"; | |
Matrix ret(this->row, this->col); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
ret[i][j] = (*this)(i, j) + _mat(i, j); | |
} | |
} | |
return ret; | |
} | |
inline Matrix Matrix::operator-(const Matrix& _mat) const | |
{ | |
if (_mat.row != this->row) throw "ArgumentException"; | |
if (_mat.col != this->col) throw "ArgumentException"; | |
Matrix ret(this->row, this->col); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
ret[i][j] = (*this)(i, j) - _mat(i, j); | |
} | |
} | |
return ret; | |
} | |
inline Matrix Matrix::operator*(const Matrix& _mat) const | |
{ | |
if (this->col != _mat.row) throw "ArgumentException"; | |
Matrix ret(this->row, _mat.col); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < _mat.col; ++j) | |
{ | |
for (int k = 0; k < this->col; ++k) | |
{ | |
ret[i][j] += (*this)(i, k) * _mat(k, j); | |
} | |
} | |
} | |
return ret; | |
} | |
inline Matrix Matrix::operator*(const double x) const | |
{ | |
Matrix ret(this->row, this->col); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
ret[i][j] = (*this)(i, j) * x; | |
} | |
} | |
return ret; | |
} | |
inline Matrix Matrix::operator/(const double x) const | |
{ | |
Matrix ret(this->row, this->col); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
ret[i][j] = (*this)(i, j) / x; | |
} | |
} | |
return ret; | |
} | |
inline Matrix Matrix::transpose() const | |
{ | |
Matrix ret(this->col, this->row); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
ret[j][i] = (*this)(i, j); | |
} | |
} | |
return ret; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment