matrix.hpp 996 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef MATRIX_HPP
  2. #define MATRIX_HPP
  3. #include "vector.hpp"
  4. class Matrix{
  5. public:
  6. size_t nr,nc;
  7. double* data;
  8. Matrix();
  9. Matrix(size_t nr,size_t nc);
  10. ~Matrix();
  11. void resize(size_t nr,size_t nc);
  12. double& get(size_t i);
  13. double get(size_t i) const;
  14. double& get(size_t i,size_t j);
  15. double get(size_t i,size_t j) const;
  16. void view() const;
  17. };
  18. inline
  19. Matrix::Matrix(){
  20. nr=0;
  21. nc=0;
  22. data=nullptr;
  23. }
  24. inline
  25. Matrix::Matrix(size_t _nr,size_t _nc){
  26. assert(_nr>0);
  27. assert(_nc>0);
  28. nr=_nr;
  29. nc=_nc;
  30. data=new double[nr*nc];
  31. }
  32. inline
  33. Matrix::~Matrix(){
  34. if(data!=nullptr) delete[] data;
  35. }
  36. inline
  37. double& Matrix::get(size_t i,size_t j){
  38. assert(i<nr);
  39. assert(j<nc);
  40. return data[i*nc+j];
  41. }
  42. inline
  43. double Matrix::get(size_t i,size_t j) const{
  44. assert(i<nr);
  45. assert(j<nc);
  46. return data[i*nc+j];
  47. }
  48. inline
  49. double& Matrix::get(size_t i){
  50. assert(i<nr*nc);
  51. return data[i];
  52. }
  53. inline double
  54. Matrix::get(size_t i) const{
  55. assert(i<nr*nc);
  56. return data[i];
  57. }
  58. #endif