The following is a matrix class
1 namespacekunkun_matrix{2 Const intMOD =1000000007;3template<classE>4 classmatrix{5 Public:6 Matrix (size_t _m,size_t _n);7Matrix (Constmatrix©);8Matrixoperator*(ConstMatrix &b);9e*operator[] (size_t i);Tenmatrix&operator=(Constmatrix©); OneMatrixoperator^(size_t e); AMatrixoperator+(Constmatrix& b)Const; -Matrixoperator-(Constmatrix& b)Const; -Matrix Transpose ()Const; the~Matrix (); - voidOutput ()Const; - Private: - size_t M,n; +EMat; - }; + Atemplate<classE> at voidMatrix<e>::output ()Const{ - for(size_t i=0; i<m;i++){ - for(size_t j=0; j<n;j++){ -cout << Mat[i*n+j] <<" "; - } -cout <<Endl; in } - } to +template<classE> -Matrix<e>:: Matrix (size_t _m,size_t _n): M (_m), n (_n) { theMat =Newe[m*n]; *Memset (Mat,0,sizeof(E) *m*n); $ }Panax Notoginseng -template<classE> theE* Matrix<e>::operator[] (size_t i) { + returnmat+i*N; A } the +template<classE> -matrix<e>& Matrix<e>::operator=(Constmatrix©) { $m =copy.m; $n =COPY.N; -Mat =Newe[m*n]; -memcpy (Mat,copy.mat,sizeof(E) *m*n); the return* This; - }Wuyi thetemplate<classE> -Matrix<e>::matrix (Constmatrix©): M (copy.m), n (COPY.N) { WuMat =Newe[m*n]; -memcpy (Mat,copy.mat,sizeof(E) *m*n); About } $ -template<classE> -Matrix<e> Matrix<e>::operator*(ConstMatrix &b) { - if(n!=b.m) { A ThrowRuntime_error ("This both matrix cannot be multiplied!"); + } theMatrix<e>Res (M,B.N); - for(size_t i=0; i<m;i++){ $ for(size_t k=0; k<b.m;k++)if(mat[i*n+K]) { the for(size_t j=0; j<b.n;j++)if(b.mat[k*b.n+J]) { theRES[I][J] + = mat[i*n+k]*b.mat[k*b.n+j]; theRES[I][J]%=MOD; the } - } in } the returnRes; the } About thetemplate<classE> thematrix<e>::~Matrix () { the Delete[] mat; + } - thetemplate<classE>BayiMatrix<e> Matrix<e>::operator^(size_t e) { the if(m!=N) { the ThrowRuntime_error ("This matrix was not a square matrix!"); - } - Matrix Res (m,m); theMatrix X (* This); the for(size_t i=0; i<m;i++) Res[i][i] =1; the for(; e; e>>=1 ) { the if(e&1) Res = res *x; -x = x *x; the } the returnRes; the }94 thetemplate<classE> theMatrix<e> Matrix<e>::operator+(Constmatrix& b)Const{ the if(n!=b.n| | m!=b.m) {98 ThrowRuntime_error ("This both matrix is not in same size!"); About } -Matrix<e>Res (m,n);101 for(size_t i=0; i<m*n;i++){102Res.mat[i] = Mat[i] +B.mat[i];103 }104 returnRes; the }106 107template<classE>108Matrix<e> Matrix<e>::operator-(Constmatrix& b)Const{109 if(n!=b.n| | m!=b.m) { the ThrowRuntime_error ("This both matrix is not in same size!");111 } theMatrix<e>Res (m,n);113 for(size_t i=0; i<m*n;i++){ theRes.mat[i] = mat[i]-B.mat[i]; the } the returnRes;117 }118 119template<classE> -Matrix<e> Matrix<e>::transpose ()Const{121 Matrix Res (n,m);122 for(intI=0; i<m;i++){123 for(intj=0; j<n;j++){124Res.mat[j*n+i] = mat[i*n+j]; the }126 }127 returnRes; - }129}
Matrix Class (Basic)