矩阵模板(尚未完善)
来源:互联网 发布:htc m9 v版网络设置 编辑:程序博客网 时间:2024/06/05 14:31
参考别人的写的矩阵的模板
目前暂时只实现赋值,加,减, 乘, 转置;
/************************************矩阵模板~~=+-*************************************/# include<iostream>using namespace std;template<typename T>class Matrix{ private: int col; int row; T ** element; public: Matrix(); Matrix(const Matrix &); Matrix(const int, const int); ~Matrix(); void setMatrix(int , int); Matrix<T> operator = (const Matrix<T> &); Matrix<T> operator + (const Matrix<T> &); Matrix<T> operator - (const Matrix<T> &); Matrix<T> operator * (const Matrix<T> &); Matrix<T> transpose(); void setM(); void print();
Matrix<T> quickpow(int n);};template<typename T>Matrix<T>::Matrix(){ row = 0; col = 0; element = NULL;}template<typename T>Matrix<T>::Matrix(const Matrix & temp){ row = temp.row; col = temp.col; element = new T*[row]; for(int k=0; k<row; k++) element[k] = new T[col]; for(int i=0; i<row; i++) for(int j=0; j<col; j++) element[i][j] = temp.element[i][j];}template<typename T>Matrix<T>::Matrix(const int m_row, const int m_col){ row = col =0; element = NULL; setMatrix(m_row, m_col);}template<typename T>void Matrix<T>::setMatrix(int m_row, int m_col){ for(int y=0; y<row; y++) delete [] element[y]; delete []element; row = m_row; col = m_col; element = new T*[row]; for(int i=0; i<row; i++) element[i] = new T[col]; for(int temprow=0; temprow<row; temprow++) for(int tempcol=0; tempcol<col; tempcol++) element[temprow][tempcol]=0;}template<typename T>void Matrix<T>::setM( ){ for(int i=0; i<row; i++) for(int j=0; j<col; j++) cin>>element[i][j];}template<typename T>Matrix<T>::~Matrix(){ for(int y=0; y<row; y++) delete [] element[y]; delete [] element;}template<typename T>Matrix<T> Matrix<T>::operator = (const Matrix<T> & right){ for(int p=0; p<row; p++) delete []element[p]; delete []element; row = right.row; col = right.col; element = new T * [row]; for(int i=0; i<row; i++) element[i]=new T[col]; for(int i=0; i<row; i++) for(int j=0; j<col; j++) element[i][j] = right.element[i][j]; return *this;}template<typename T>Matrix<T> Matrix<T>::operator + (const Matrix<T> & right){ if(row!=right.row || col !=right.col) throw string("error"); Matrix<T> result(row, col); for(int i=0; i<row; i++) for(int j=0; j<col; j++) result.element[i][j] = element[i][j] + right.element[i][j]; return result;}template<typename T>Matrix<T> Matrix<T>::operator - (const Matrix<T> & right){ if(row!=right.row || col !=right.col) throw string("error"); Matrix<T> result(row, col); for(int i=0; i<row; i++) for(int j=0; j<col; j++) result.element[i][j] = element[i][j] - right.element[i][j]; return result;}template<typename T>Matrix<T> Matrix<T>::operator * (const Matrix<T> & right){ if(col != right.row) throw string("error"); Matrix<T> result(row, right.col); for(int i=0; i<row; i++) for(int j=0; j<right.col; j++) { result.element[i][j]=0; for(int k=0; k<col; k++) result.element[i][j]+=element[i][k]*right.element[k][j]; } return result;}template<typename T>Matrix<T> Matrix<T>::transpose(){ Matrix<T> result(col, row); for(int i=0; i<result.row; i++) for(int j=0; j<result.col; j++) result.element[i][j] = element[j][i]; return result;}
template<typename T>Matrix<T> Matrix<T>::quickpow(int n){ Matrix<T> a(*this); Matrix<T> result(row, col); for(int i=0; i<result.row; i++) for(int j=0; j<result.col; j++) if(i==j) result.element[i][j]=1; while(n) { if(n%2==1){ result= a*result; } n=n>>1; a = a*a; } return result;}template<typename T>void Matrix<T>::print(){ for(int i=0; i<row; i++){ for(int j=0; j<col; j++) cout<<element[i][j]<<" "; cout<<endl; }}int main(){ Matrix<int> a(3, 3); a.setM(); Matrix<int> b(3, 4); b.setM(); b = a*b; b.print(); return 0;}
0 0
- 矩阵模板(尚未完善)
- 函数模板的完善
- 矩阵模板
- 矩阵模板
- 矩阵模板
- 【矩阵模板】
- 矩阵模板
- django模板所有tdk完善
- JS-WORD完善结合之模板化
- 类成员模板注意事项(待完善)
- 有关矩阵的模板【模板】
- 矩阵类模板
- 矩阵乘法的模板
- 最大子矩阵模板
- 【矩阵运算+模板】
- 矩阵类模板
- 矩阵乘法 贴个新模板
- 矩阵模板之一
- B树、B+树、B*树全解
- java初基1
- XMLHttpRequest对象
- POJ 2125 最小割最大流
- A20 Gate
- 矩阵模板(尚未完善)
- 多线程编程示例(基于tcp_socket)
- 编程注意事项
- 使用jQuery实现图片滑动窗
- iPhone摄像头拍照后图像旋转
- SQL Server 存储过程
- 小牛在线
- 非递归的二叉树遍历
- JAVA学习第1篇:J2EE,J2SE,J2ME,JDK,SDK,JRE,JVM区别