Created
January 24, 2017 13:16
-
-
Save NotFounds/fb1d24c95f8363cfb429efaf51234ec5 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
#include "Matrix.h" | |
Matrix::Matrix() | |
{ | |
row = -1; | |
col = -1; | |
_matrix = NULL; | |
} | |
Matrix::Matrix(int _row, int _col) | |
{ | |
if (_row <= 0 || _col <= 0) throw "ArgumentException"; | |
resize(_row, _col); | |
} | |
Matrix::Matrix(int _row, int _col, double * args) | |
{ | |
if (_row <= 0 || _col <= 0) throw "ArgumentException"; | |
resize(_row, _col); | |
for (int i = 0; i < _row; ++i) | |
{ | |
for (int j = 0; j < _col; ++j) | |
{ | |
_matrix[i * col + j] = args[i * col + j]; | |
} | |
} | |
} | |
bool Matrix::resize(int _row, int _col) | |
{ | |
if (_row <= 0 || _col <= 0) throw "ArgumentException"; | |
if (_row == row && _col == col) return true; | |
row = _row; | |
col = _col; | |
if (_matrix != NULL) | |
delete[] _matrix; | |
_matrix = new double[row * col]; | |
for (int i = 0; i < row; ++i) | |
{ | |
for (int j = 0; j < col; ++j) | |
{ | |
_matrix[i * col + j] = 0; | |
} | |
} | |
return true; | |
} | |
double Matrix::operator()(int i, int j) const | |
{ | |
if (i < 0 || i >= row) throw "IndexOutOfRangeException"; | |
if (j < 0 || j >= col) throw "IndexOutOfRangeException"; | |
return this->_matrix[i * col + j]; | |
} | |
double * Matrix::operator[](int i) | |
{ | |
if (i < 0 || i >= row) throw "IndexOutOfRangeException"; | |
return &this->_matrix[i * col]; | |
} | |
bool Matrix::operator==(const Matrix _mat) const | |
{ | |
if (_mat.row != this->row) return false; | |
if (_mat.col != this->col) return false; | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
if (_mat(i, j) != (*this)(i, j)) | |
return false; | |
} | |
} | |
return true; | |
} | |
bool Matrix::operator!=(const Matrix _mat) const | |
{ | |
return !(_mat == (*this)); | |
} | |
Matrix & Matrix::operator+=(const Matrix & _mat) | |
{ | |
if (_mat.row != this->row) throw "ArgumentException"; | |
if (_mat.col != this->col) throw "ArgumentException"; | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
(*this)[i][j] += _mat(i, j); | |
} | |
} | |
return (*this); | |
} | |
Matrix & Matrix::operator-=(const Matrix & _mat) | |
{ | |
if (_mat.row != this->row) throw "ArgumentException"; | |
if (_mat.col != this->col) throw "ArgumentException"; | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
(*this)[i][j] -= _mat(i, j); | |
} | |
} | |
return (*this); | |
} | |
void Matrix::random(double min, double max) | |
{ | |
std::random_device rd; | |
std::mt19937 mt(rd()); | |
std::uniform_real_distribution<double> range(min, max); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
(*this)[i][j] = range(mt); | |
} | |
} | |
} | |
void Matrix::random(double min, double max, int seed) | |
{ | |
std::mt19937 mt(seed); | |
std::uniform_real_distribution<double> range(min, max); | |
for (int i = 0; i < this->row; ++i) | |
{ | |
for (int j = 0; j < this->col; ++j) | |
{ | |
(*this)[i][j] = range(mt); | |
} | |
} | |
} | |
Matrix::~Matrix() | |
{ | |
try | |
{ | |
row = -1; | |
col = -1; | |
//if (this->_matrix != NULL) | |
// delete[] this->_matrix; | |
//this->_matrix = NULL; | |
} | |
catch (char *ex) | |
{ | |
std::cerr << ex << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment