Advertisement
Guest User

Matrix in c++

a guest
Jan 20th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #ifndef _MATRIX_H_INCLUDED
  2. #define _MATRIX_H_INCLUDED
  3.  
  4. #include <cstdio>
  5. #include <cstddef>
  6. #include <cstdlib>
  7. #include <algorithm>
  8.  
  9. class Matrix {
  10. public:
  11. Matrix(std::size_t r, std::size_t c);
  12. ~Matrix();
  13. Matrix(const Matrix& m);
  14.  
  15. std::size_t get_rows();
  16. std::size_t get_cols();
  17. void set(std::size_t i, std::size_t j, int val);
  18. int get(std::size_t i, std::size_t j);
  19. void print(FILE *f);
  20.  
  21. Matrix& operator=(const Matrix& m);
  22. int* operator[](std::size_t i);
  23. const int* operator[](std::size_t i) const;
  24.  
  25. Matrix& operator+=(const Matrix& m);
  26. Matrix& operator-=(const Matrix& m);
  27. Matrix& operator*=(const Matrix& m);
  28.  
  29. bool operator==(const Matrix& m);
  30. bool operator!=(const Matrix& m);
  31. private:
  32. int** new_data(std::size_t r, std::size_t c);
  33. void del_data();
  34.  
  35. std::size_t _rows;
  36. std::size_t _cols;
  37. int **_data;
  38. };
  39.  
  40. Matrix operator+(Matrix, const Matrix& right);
  41. Matrix operator-(Matrix, const Matrix& right);
  42. Matrix operator*(Matrix, const Matrix& right);
  43.  
  44. #endif
  45.  
  46. //================================CPP FILE========================================
  47.  
  48. #include "matrix.h"
  49.  
  50. int** Matrix::new_data(std::size_t r, std::size_t c){
  51. int *tmp = new int [r * c];
  52. std::fill(tmp, tmp + r * c, 0);
  53. int **data = new int *[r];
  54. for (std::size_t i = 0; i < r; i++)
  55. data[i] = tmp + i * c;
  56. return data;
  57. }
  58.  
  59. void Matrix::del_data(){
  60. delete[] _data[0];
  61. delete[] _data;
  62. }
  63.  
  64. Matrix::Matrix(std::size_t r, std::size_t c) {
  65. _data = new_data(r, c);
  66. _rows = r;
  67. _cols = c;
  68. }
  69.  
  70. Matrix::~Matrix(){
  71. del_data();
  72. }
  73.  
  74. Matrix::Matrix(const Matrix& m){
  75. _rows = m._rows;
  76. _cols = m._cols;
  77. _data = new_data(_rows, _cols);
  78. for (std::size_t i = 0; i < _rows; i++)
  79. for (std::size_t j = 0; j < _cols; j++)
  80. _data[i][j] = m._data[i][j];
  81. }
  82.  
  83. int* Matrix::operator[](std::size_t i){
  84. return _data[i];
  85. }
  86.  
  87. const int* Matrix::operator[](std::size_t i) const{
  88. return _data[i];
  89. }
  90.  
  91. std::size_t Matrix::get_rows() { return _rows; }
  92. std::size_t Matrix::get_cols() { return _cols; }
  93.  
  94. void Matrix::set(std::size_t i, std::size_t j, int val) {
  95. _data[i][j] = val;
  96. }
  97.  
  98. int Matrix::get(std::size_t i, std::size_t j) {
  99. return _data[i][j];
  100. }
  101.  
  102. void Matrix::print(FILE* f) {
  103. for (std::size_t i = 0; i<_rows; i++){
  104. for (std::size_t j = 0; j<_cols; j++)
  105. fprintf(f, "%d ", _data[i][j]);
  106. fprintf(f, "\n");
  107. }
  108. }
  109.  
  110. Matrix& Matrix::operator=(const Matrix& m){
  111. if (this == &m) return *this;
  112. Matrix c = m;
  113. _cols = c._cols;
  114. _rows = c._rows;
  115. std::swap(_data, c._data);
  116. return *this;
  117. }
  118.  
  119. bool Matrix::operator==(const Matrix& m){
  120. if (_cols != m._cols || _rows != m._rows)
  121. return false;
  122. for (std::size_t i=0; i<_rows; i++)
  123. for (std::size_t j=0; j<_cols; j++)
  124. if (_data[i][j] != m._data[i][j])
  125. return false;
  126. return true;
  127. }
  128.  
  129. bool Matrix::operator!=(const Matrix& m) {
  130. return !(*this == m);
  131. }
  132.  
  133. Matrix& Matrix::operator+=(const Matrix& m) {
  134. for (std::size_t i=0; i<_rows; i++)
  135. for (std::size_t j=0; j<_cols; j++)
  136. _data[i][j] += m._data[i][j];
  137. return *this;
  138. }
  139.  
  140. Matrix& Matrix::operator-=(const Matrix& m) {
  141. for (std::size_t i=0; i<_rows; i++)
  142. for (std::size_t j=0; j<_cols; j++)
  143. _data[i][j] -= m._data[i][j];
  144. return *this;
  145. }
  146.  
  147. Matrix& Matrix::operator*=(const Matrix& m) {
  148. Matrix c(_rows, m._cols);
  149. int sum = 0;
  150. for (std::size_t i=0; i<_rows; i++)
  151. for (std::size_t j=0; j<m._cols; j++){
  152. for (std::size_t k=0; k<_cols; k++)
  153. sum += _data[i][k] * m._data[k][j];
  154. c.set(i, j, sum);
  155. sum = 0;
  156. }
  157. *this = c;
  158. return *this;
  159. }
  160.  
  161. Matrix operator+(Matrix left, const Matrix& right){
  162. return left += right;
  163. }
  164.  
  165. Matrix operator-(Matrix left, const Matrix& right){
  166. return left -= right;
  167. }
  168.  
  169. Matrix operator*(Matrix left, const Matrix& right){
  170. return left *= right;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement