Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <random>
  4.  
  5. class Matrix
  6. {
  7. private:
  8. double* _matrix;
  9. public:
  10. int row;
  11. int col;
  12. Matrix();
  13. Matrix(int _row, int _col);
  14. Matrix(int _row, int _col, double* args);
  15. bool resize(int _row, int _col);
  16. double operator()(int i, int j) const;
  17. double* operator[](int i);
  18. bool operator==(const Matrix _mat)const;
  19. bool operator!=(const Matrix _mat)const;
  20. Matrix& operator+=(const Matrix& _mat);
  21. Matrix& operator-=(const Matrix& _mat);
  22. void random(double min, double max);
  23. void random(double min, double max, int seed);
  24. ~Matrix();
  25. Matrix& Matrix::operator=(const Matrix _mat)
  26. {
  27. resize(_mat.row, _mat.col);
  28. for (int i = 0; i < _mat.row; ++i)
  29. {
  30. for (int j = 0; j < _mat.col; ++j)
  31. {
  32. _matrix[i * col + j] = _mat(i, j);
  33. }
  34. }
  35. return (*this);
  36. }
  37. inline Matrix Matrix::operator+(const Matrix& _mat) const
  38. {
  39. if (_mat.row != this->row) throw "ArgumentException";
  40. if (_mat.col != this->col) throw "ArgumentException";
  41. Matrix ret(this->row, this->col);
  42. for (int i = 0; i < this->row; ++i)
  43. {
  44. for (int j = 0; j < this->col; ++j)
  45. {
  46. ret[i][j] = (*this)(i, j) + _mat(i, j);
  47. }
  48. }
  49. return ret;
  50. }
  51. inline Matrix Matrix::operator-(const Matrix& _mat) const
  52. {
  53. if (_mat.row != this->row) throw "ArgumentException";
  54. if (_mat.col != this->col) throw "ArgumentException";
  55. Matrix ret(this->row, this->col);
  56. for (int i = 0; i < this->row; ++i)
  57. {
  58. for (int j = 0; j < this->col; ++j)
  59. {
  60. ret[i][j] = (*this)(i, j) - _mat(i, j);
  61. }
  62. }
  63. return ret;
  64. }
  65.  
  66. inline Matrix Matrix::operator*(const Matrix& _mat) const
  67. {
  68. if (this->col != _mat.row) throw "ArgumentException";
  69. Matrix ret(this->row, _mat.col);
  70. for (int i = 0; i < this->row; ++i)
  71. {
  72. for (int j = 0; j < _mat.col; ++j)
  73. {
  74. for (int k = 0; k < this->col; ++k)
  75. {
  76. ret[i][j] += (*this)(i, k) * _mat(k, j);
  77. }
  78. }
  79. }
  80. return ret;
  81. }
  82.  
  83. inline Matrix Matrix::operator*(const double x) const
  84. {
  85. Matrix ret(this->row, this->col);
  86. for (int i = 0; i < this->row; ++i)
  87. {
  88. for (int j = 0; j < this->col; ++j)
  89. {
  90. ret[i][j] = (*this)(i, j) * x;
  91. }
  92. }
  93. return ret;
  94. }
  95.  
  96.  
  97. inline Matrix Matrix::operator/(const double x) const
  98. {
  99. Matrix ret(this->row, this->col);
  100. for (int i = 0; i < this->row; ++i)
  101. {
  102. for (int j = 0; j < this->col; ++j)
  103. {
  104. ret[i][j] = (*this)(i, j) / x;
  105. }
  106. }
  107. return ret;
  108. }
  109.  
  110. inline Matrix Matrix::transpose() const
  111. {
  112. Matrix ret(this->col, this->row);
  113. for (int i = 0; i < this->row; ++i)
  114. {
  115. for (int j = 0; j < this->col; ++j)
  116. {
  117. ret[j][i] = (*this)(i, j);
  118. }
  119. }
  120. return ret;
  121. }
  122. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement