Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. //Developed by Nikita Mikaev
  2. #ifndef MY_MATRIX
  3. #define MY_MATRIX
  4. #include <iostream>
  5. #include <cassert>
  6. using namespace std;
  7.  
  8.  
  9. template <class T>
  10. class Matrix{
  11.     private:
  12.         T **data;
  13.         int rows,cols;
  14.         void null_init();
  15.     public:
  16.         Matrix(int rows_=0,int cols_=0);
  17.         Matrix(const Matrix<T> &from);
  18.         ~Matrix();
  19.         int Rows() const{return rows;};
  20.         int Cols() const{return cols;};
  21.         T Get(int row,int col) const;
  22.         void Set(int row,int col, T value);
  23.         Matrix<T> operator+(const Matrix<T> &arg);
  24.         Matrix<T>& operator=(const Matrix<T> &arg);
  25.         Matrix<T> operator*(const Matrix<T> &arg);
  26.         //template<class T,typename T2> Matrix<T> operator*(const Matrix<T> &arg, T2 value);
  27.         template<class A, typename T2> friend Matrix<T> operator*(T2 value, const Matrix<T> &arg);
  28.         template<class A> friend ostream& operator<<(ostream &out,const Matrix<T> &arg);
  29.         Matrix<T> Transp();
  30. };
  31.  
  32. template <class T>
  33. Matrix<T> Matrix<T>::Transp(){
  34.     Matrix<T> temp(Cols(),Rows());
  35.     for(int j=0;j<Cols();j++){
  36.         for(int i=0;i<Rows();i++)
  37.             temp.Set(j,i,Get(i,j));
  38.     }
  39.     return temp;
  40. }
  41.  
  42. template <class T>
  43. Matrix<T>::Matrix(int rows_,int cols_){
  44.     assert(rows_>=0 and cols_>=0);
  45.     rows=rows_;
  46.     cols=cols_;
  47.     data=new T*[rows];
  48.     for(int i=0;i<rows;i++)
  49.         data[i]=new T[cols];
  50.     null_init();
  51. }
  52.  
  53. template <class T>
  54. Matrix<T>::Matrix(const Matrix &from){
  55.     rows=from.Rows();
  56.     cols=from.Cols();
  57.     data=new T*[rows];
  58.     for(int i=0;i<rows;i++)
  59.         data[i]=new T[cols];
  60.     for(int i=0;i<rows;i++){
  61.         for(int j=0;j<cols;j++)
  62.             Set(i,j,from.Get(i,j));
  63.     }
  64. }
  65.  
  66. template <class T>
  67. Matrix<T>::~Matrix(){
  68.     for(int i=0;i<cols;i++)
  69.         delete[] data[i];
  70.     delete[] data;
  71. }
  72.  
  73. template <class T>
  74. T Matrix<T>::Get(int row,int col) const{
  75.     assert(row>=0 and row<rows and col>=0 and col<cols);
  76.     return data[row][col];
  77. }
  78.  
  79. template <class T>
  80. void Matrix<T>::Set(int row, int col, T value){
  81.     assert(row>=0 and row<rows and col>=0 and col<cols);
  82.     data[row][col]=value;
  83. }
  84.  
  85. template <class T>
  86. Matrix<T> Matrix<T>::operator+(const Matrix<T> &arg){
  87.     assert(rows==arg.Rows() and cols==arg.Cols());
  88.     Matrix<T> temp(rows,cols);
  89.     for(int i=0;i<rows;i++){
  90.         for(int j=0;j<cols;j++)
  91.             temp.Set(i,j,Get(i,j)+arg.Get(i,j));
  92.     }
  93.     return temp;
  94. }
  95.  
  96. template <class T>
  97. Matrix<T>& Matrix<T>::operator=(const Matrix<T> &arg){
  98.     rows=arg.Rows();
  99.     cols=arg.Cols();
  100.     for(int i=0;i<rows;i++){
  101.         for(int j=0;j<cols;j++)
  102.             data[i][j]=arg.Get(i,j);
  103.     }
  104.     return *this;
  105. }
  106.  
  107. template <class T>
  108. Matrix<T> Matrix<T>::operator*(const Matrix<T> &arg){
  109.     assert(Cols()==arg.Rows());
  110.     int minimal=min(min(Rows(),Cols()),min(arg.Rows(),arg.Cols()));
  111.     Matrix<T> temp(Rows(),arg.Cols());
  112.     for(int i=0;i<temp.Rows();i++){
  113.         for(int j=0;j<temp.Cols();j++){
  114.             for(int s=0;s<Cols();s++){
  115.                 int sum=temp.Get(i,j)+Get(i,s)*arg.Get(s,j);
  116.                 temp.Set(i,j,sum);
  117.             }
  118.         }
  119.     }
  120.     return temp;
  121. }
  122.  
  123. template<class T> ostream& operator<<(ostream &out,const Matrix<T> &arg){
  124.     for(int i=0;i<arg.Rows();i++){
  125.         for(int j=0;j<arg.Cols();j++)
  126.             out<<arg.Get(i,j)<<" ";
  127.         cout << endl;
  128.     }
  129.     return out;
  130. }
  131.  
  132. template<class T>
  133. void Matrix<T>::null_init(){
  134.  
  135.     for(int i=0;i<Rows();i++){
  136.         for(int j=0;j<Cols();j++)
  137.             Set(i,j,0);
  138.     }
  139. }
  140.  
  141. /*template<class T, typename T2> Matrix<T> operator*(const Matrix<T> &arg, T2 value){
  142.     Matrix<T> temp(arg);
  143.     for(int i=0;i<temp.Rows();i++){
  144.         for(int j=0;j<temp.Cols();j++)
  145.             temp.Set(i,j,temp.Get(i,j)*value);
  146.     }
  147.     return temp;
  148. }*/
  149.  
  150. template<class A, typename T> Matrix<A> operator*(T value, const Matrix<A> &arg){
  151.     Matrix<A> temp(arg);
  152.     for(int i=0;i<temp.Rows();i++){
  153.         for(int j=0;j<temp.Cols();j++)
  154.             temp.Set(i,j,temp.Get(i,j)*value);
  155.     }
  156.     return temp;
  157. }
  158. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement