您的位置:首页 > 其它

期末了,好久没上了,传一个最近写的矩阵类的原型(目前只有乘法,求行列式以及高斯全主元消元)待完善

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: