矩阵类
来源:互联网 发布:淘宝网2017女装冬装 编辑:程序博客网 时间:2024/05/17 16:46
将矩阵matrix用一个一维数组element储存(涉及了很多运算符重载的方法)
#include"expection.h"using namespace std;template <class T>class matrix{ friend ostream& operator<<(ostream &, const matrix<T>&);public: matrix(int theRows = 0, int theColumns = 0); //构造函数 matrix(const matrix<T>&); //复制构造函数 ~matrix(){ delete[] element; } int rows() const { return theRows; } int columns() const { return theColumns; } T &operator()(int i, int j) const; matrix<T>& operator=(const matrix<T>&); matrix<T> operator+() const; //单目运算符 matrix<T> operator+(const matrix<T>&) const;//双目运算符 matrix<T> operator-() const;//单目运算符 matrix<T> operator-(const matrix<T>&) const;//双目 matrix<T> operator*(const matrix<T>&) const; matrix<T>& operator+=(const T&);private: int theRows;//行数 int theColumns;//列数 T *element;//数组};template<class T>matrix<T>::matrix(int theRows , int theColumns){//构造函数 if (theColumns < 0 || theRows < 0) { throw illegalParameterValue("Rows and columns must be >=0"); } if ((theColumns == 0 || theRows == 0) && (theColumns != 0 || theRows != 0)) { throw illegalParameterValue("Either both or neither rows and columns shoud be zero"); } //创建矩阵 this->theColumns = theColumns; this->theRows = theRows; element = new T[theColumns*theRows];}template<class T>matrix<T>::matrix(const matrix<T>&M){//复制构造函数 //创建矩阵 theColumns = M.theColumns; theRows = M.theRows; element = new T[theColumns*theRows]; copy(M.element, M.element + theColumns*theRows, element);}template<class T>//重载赋值操作=matrix<T>& matrix<T>::operator=(const matrix<T>& M){//赋值 (*this)=M if (this != &M) {//不能复制自己 delete[] element; theColumns = M.theColumns; theRows = M.theRows; element = new T[theColumns*theRows]; copy(M.element, M.element + theColumns*theRows, element); } return *this;}template<class T>//括号运算符重载,用左右括号来表示矩阵的索引a(i,j)T& matrix<T>::operator()(int i, int j) const{ if (i<1 || i>theRows || j<1 || j>theColumns) { throw matrixIndexOutOfBounds(); } return element[(i - 1)*theColumns + j - 1];}template<class T>//加号运算符重载matrix<T> matrix<T>::operator+(const matrix<T>&M) const{//返回矩阵w=*this+m if (theColumns != M.theColumns || theRows != M.theRows) { throw matrixSizeMismatch(); } //生成相加后的矩阵 matrix<T> w(theColumns, theRows); for (int i = 0; i < theColumns*theRows; i++) { w.element[i] = this->element[i] + M.element[i]; } return w;}template<class T>matrix<T> matrix<T>::operator-(const matrix<T>&M) const{//返回矩阵w=*this+m if (theColumns != M.theColumns || theRows != M.theRows) { throw matrixSizeMismatch(); } //生成相加后的矩阵 matrix<T> w(theColumns, theRows); for (int i = 0; i < theColumns*theRows; i++) { w.element[i] = this->element[i] - M.element[i]; } return w;}template<class T>//乘号运算符重载matrix<T> matrix<T>::operator*(const matrix<T>&M) const{//矩阵乘法 返回结果矩阵w=(*this) * M matrix<T> w(theRows, theColumns);; //结果矩阵 int ct = 0, cm = 0, cw = 0; for (int i = 1; i <= theRows; i++) { for (int j = 1; j <= theColumns; j++) { T sum = element[ct] * M.element[cm]; for (int k = 2; k <= theColumns; k++) { ct++;//i行的下一项 cm += theColumns;//j列的下一项 sum += element[ct] * M.element[cm]; } w.element[cw++] = sum; //定位到该行起点与该列的下一列 cm = j; ct -= theColumns - 1; } ct += theColumns; cm = 0; } return w;}template<class T>matrix<T> matrix<T>:: operator+() const{ matrix<T> w(theRows, theColumns); for (int i = 0; i < theColumns*theRows; i++) { w.element[i] = +element[i]; } return w;}template<class T>matrix<T> matrix<T>:: operator-() const{ matrix<T> w(theRows, theColumns); for (int i = 0; i < theColumns*theRows; i++) { w.element[i] = -element[i]; } return w;}template<class T>matrix<T>& matrix<T>::operator+=(const T&x){ for (int i = 0; i < theColumns*theRows; i++) { element[i] += x; } return *this;}//template<class T>ostream &operator <<(ostream& out, const matrix<int>& M){ int k = 0; for (int i = 0; i < M.theRows; i++) { for (int j = 0; j < M.theColumns; j++) { out << M.element[k++]<<" "; } out << endl; } return out;}
0 0
- 矩阵类
- 矩阵类
- 矩阵类
- 矩阵类
- 矩阵类
- 矩阵类
- 矩阵类
- 矩阵类模板
- C#矩阵类
- java 作业 矩阵类
- 矩阵类,二维数组
- 3x3矩阵类
- 矩阵类 Matrix
- C++简单矩阵类
- 矩阵类C++实现
- 矩阵类模板
- C#矩阵类运算
- 矩阵类备份
- SpringBoot学习笔记(六):配置拦截器,控制登录跳转
- 这个MySQL入门概念总结得好
- unity中的灯光烘焙问题lightMap
- SQL将用户表中已存在的数据所有姓名(汉字)转换为拼音首字母
- [一周一译]脸书正在吞并世界
- 矩阵类
- Android 获取电池信息
- POJ2187Beauty Contest
- Android打包动态配置签名
- [Hybris]Hybris6.3环境搭建
- python3实战|python3爬取豆瓣top250备份到数据库
- 初学MyBatis
- getIntent().getExtras().getString()为null的问题
- HTTP和SOAP完全就是两个不同的协议