//CMatrix.cpp
#include<stdlib.h>
#include<math.h>
#include "matrix.h"
CMatrix::CMatrix(int row,int col)
{
CMatrix::row=row;
CMatrix::col=col;
elem=new double[row*col];
for(int i=0;i<row*col;i++)
elem[i]=0.0;
}
void CMatrix::set(int row,int col)
{
CMatrix::row=row;
CMatrix::col=col;
elem=new double[row*col];
for(int i=0;i<row*col;i++)
elem[i]=0.0;
}
CMatrix::CMatrix(const CMatrix& m):row(m.row),col(m.col)
{
elem=new double[m.row*m.col];
for(int i=0;i<=row*col;i++)
elem[i]=m.elem[i];
}
CMatrix& CMatrix::operator =(const CMatrix& m)
{
row=m.row,col=m.col;
if (this!=&m)
{
delete[] elem;
elem=new double[row*col];
for(int i=0;i<=row*col;i++)
elem[i]=m.elem[i];
}
return *this;
}
double& CMatrix::operator()(int x, int y)
{
return elem[col*(x-1)+y-1];
}
double CMatrix::operator()(int x, int y) const
{
return elem[col*(x-1)+y-1];
}
double CMatrix::getmod()
{
if(col!=1){
cerr<<"不是列向量(不能求模) \n";
exit(1);
}
double temp=0;
for (int i=1;i<=row ;i++)
temp+=operator()(i,1)*operator()(i,1);
double mod=sqrt(temp);
return mod;
}
double CMatrix::mult(const CMatrix& b)
{
if (col!=b.row)
{
cerr<<"the two CMatrix can't be multiplied \n";
exit(1);
}
double temp=0;
for(int i=1;i<=col;i++)
temp+=operator()(1,i)*b(i,1);
return temp;
}
CMatrix CMatrix::invert()
{
CMatrix temp(col,row);
for(int i=1;i<=row;i++)
{
for(int j=1;j<=col;j++)
{
temp(j,i)=operator()(i,j);
}
}
return temp;
}
CMatrix operator +(const CMatrix& a, const CMatrix& b)
{
if (a.col!=b.col || a.row!=b.row)
{
cerr<<"the tow CMatrix can't be added \n";
exit(1);
}
CMatrix temp(a.row,a.col);
for (int i=1; i<=a.row; i++)
{
for (int j=1; j<=a.col; j++)
{
temp(i,j)=a(i,j)+b(i,j);
}
}
return temp;
}
CMatrix operator -(const CMatrix& a, const CMatrix& b)
{
return a+(-1.0*b);
}
CMatrix operator *(const CMatrix& a, const CMatrix& b)
{
if (a.col!=b.row)
{
cerr<<"the two CMatrix can't be multiplied \n";
exit(1);
}
CMatrix temp(a.row,b.col);
for(int i=1;i<=a.row;i++)
{
for(int j=1;j<=b.col;j++)
{
for (int n=1;n<=a.row;n++)
{
temp(i,j)+=a(i,n)*b(n,j);
}
}
}
return temp;
}
CMatrix operator * (double a,const CMatrix& b)
{
CMatrix temp(b.row ,b.col);
for(int i=1;i<=b.row;i++)
{
for(int j=1;j<=b.col;j++)
{
temp(i,j)=a*b(i,j);
}
}
return temp;
}
CMatrix operator/(const CMatrix& b,double a)
{
return (1/a)*b;
}
ostream& operator<<(ostream& out, const CMatrix& a)
{
for(int i=1;i<=a.row;i++)
{
for (int j=1;j<=a.col;j++)
{
out<<a(i,j)<<" ";
}
cout<<endl;
}
return out;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DFP.rar (7个子文件)
differ.cpp 538B
differ.h 236B
dfp.h 830B
matrix.cpp 3KB
matrix.h 1KB
dfp.cpp 2KB
run.cpp 601B
共 7 条
- 1
资源评论
刘良运
- 粉丝: 66
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功