期末了,好久没上了,传一个最近写的矩阵类的原型(目前只有乘法,求行列式以及高斯全主元消元)待完善
2014-12-23 23:09
471 查看
/************************************************************************ > File Name: Matrix.h > Author:keson > Mail:keson@bupt.edu.cn > Created Time: 2014年12月22日 星期一 20时08分49秒 ************************************************************************/ #ifndef _MATRIX_H #define _MATRIX_H #include<iostream> #include<fstream> #include<vector> #include<cmath> #include<sstream> #include<algorithm> #include<iomanip> using namespace std; class Matrix { public: Matrix(int i,int j):rowSize(i),colSize(j) { mat=vector<vector<double>>(i,vector<double>(j)); } Matrix(istream &in); void matPrint(); int getRowNum() const{return rowSize;} int getColNum() const{return colSize;} void changeRow(int ri,int rj); void changeCol(int ci,int cj); void gaussEliminate(); double gaussDet(); vector<vector<double>>& getMat() { return mat; } double getValue(int i,int j) { return mat[i-1][j-1]; } vector<double> getRow(int i); vector<double> getCol(int j); Matrix matMulti(const Matrix &mat2); private: vector<vector<double>> mat; int rowSize; int colSize; }; double Matrix::gaussDet() { int ret=1; int index=0; for(auto v:mat) { ret*=v[index++]; } return ret; } Matrix::Matrix(istream &in) { string line; double word; vector<double> vec; while (getline(in,line)) { istringstream record(line); while(record>>word) vec.push_back(word); mat.push_back(vec); colSize=vec.size(); vec.clear(); } rowSize=mat.size(); } void Matrix::changeRow(int ri,int rj) { std::swap(mat[ri],mat[rj]); } void Matrix::changeCol(int ci,int cj) { for(auto &v:mat) std::swap(v[ci],v[cj]); } void Matrix::gaussEliminate() { int i,j,k,rs,cs; double tmp,d; for(k=0;k<=rowSize-2;++k) { d=0.0; for(i=k;i<=rowSize-1;++i) { for(j=k;j<=colSize-1;++j) { if(fabs(mat[i][j])>d) { d=mat[i][j]; rs=i; cs=j; } } } if(k!=rs) changeRow(k,rs); if(k!=cs) changeCol(k,cs); d=mat[k][k]; for(i=k+1;i<=rowSize-1;++i) { double tmp=mat[i][k]/mat[k][k]; for(j=k;j<=colSize-1;++j) { mat[i][j]=mat[i][j]-tmp*mat[k][j]; if (fabs(mat[i][j])<=1e-10) mat[i][j]=0; } } } } /** * vector<double> getRow(int i) * return the i row */ vector<double> Matrix::getRow(int i) { return mat[i-1]; } /** * vector<double> getCol(int j) */ vector<double> Matrix::getCol(int j) { vector<double> col; for(auto c:mat) col.push_back(c[j-1]); return col; } /** * Matrix multiplication * matrix1=m*n matrix2=n*k matrix3=m*k */ Matrix Matrix::matMulti(const Matrix &matrix2) { if(getColNum()!=matrix2.getRowNum()) { cout<<"The col of mat1 is not equal the row of the mat2"<<endl; } int m=getRowNum(); int n=getColNum(); int k=matrix2.getColNum(); Matrix matrix3(m,k); for(int i=0;i<m;++i) for (int j=0;j<k;++j) for(int l=0;l<n;++l) matrix3.mat[i][j]+=mat[i][l]*matrix2.mat[l][j]; return matrix3; } /** * print the mat; */ void Matrix::matPrint() { for(auto c:mat) { for(auto w:c) { cout<<setiosflags(ios::fixed); cout<<setw(10)<<setprecision(3)<<w; } cout<<endl; } } #endif
相关文章推荐
- [AC自动机 概率DP 矩阵乘法||高斯消元] BZOJ 1444 [Jsoi2009]有趣的游戏
- 矩阵算法 高斯消元 行列式 矩阵的秩
- 【CF446D】DZY Loves Games 高斯消元+矩阵乘法
- 矩阵乘+高斯消元专题小结【矩阵乘法】
- 系统原型结构描述(概述)最近要设计一个框架,大家一起讨论下如何实现?
- 【C语言】有一个3*4的矩阵,要求编一个程序求出其中值最大的那个元素的值, 以及其所在的行号和列号。
- POJ 1222 EXTENDED LIGHTS OUT 关灯游戏-高斯消元(列主元)
- 写的一个小的矩阵乘法
- 高斯消元模板(列主元)
- pku 1222 EXTENDED LIGHTS OUT(构造矩阵+高斯消元)
- pku 1830 开关问题(构造矩阵+高斯消元)
- C++ 求一个矩阵的行列式的值(星星笔记)
- TextView图文混排图片被截断的问题以及Android省略号只有一个点的问题
- 使用函数指针,完成一个sort()函数,能对任何类型的数组元素进行排序: 回调函数 以及 memcpy ()原型实现
- 一个矩阵乘法的问题
- poj 3744 矩阵 高斯消元
- 一个简单的MATLAB读写文本文件程序--稀疏矩阵向量乘法
- HDU 4305 Lightning (矩阵行列式求生成树个数+高斯逆元求行列式模板)
- 最近在利用DSP做一个项目,好久没来自己的空间了,后续有时间会发一些自己的学习经验,呵呵!
- 科普:矩阵乘法是怎么做的?怎么判断两个矩阵是否可以相乘?不使用行列式