📄 matrix.cpp
字号:
// Matrix.cpp: implementation of the CMatrix class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "单像空间后方交会.h"
#include "Matrix.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//构造函数
CMatrix::CMatrix()
{
m_nRow=0;
m_nCol=0;
}
CMatrix::CMatrix(const CMatrix &mat)
{
m_nRow=mat.m_nRow;
m_nCol=mat.m_nCol;
m_Array.SetSize(m_nRow*m_nCol);
for(int i=0;i<int(m_nRow*m_nCol);i++)
{
m_Array[i]=mat.m_Array[i];
}
}
CMatrix::CMatrix(std::vector<double>f,UINT row,UINT col)
{
m_nRow=row;
m_nCol=col;
m_Array.SetSize(m_nRow*m_nCol);
for(int i=0;i<int(m_nRow*m_nCol);i++)
{
m_Array[i]=f[i];
}
}
///////////////////////////////////////////////////////////////
//返回矩阵大小
int CMatrix::GetSize(void)
{
return m_nRow*m_nCol;
}
/////////////////////////////////////////////////////////////////
//重新设置矩阵
CMatrix CMatrix::reset(std::vector<double>f,UINT row,UINT col)
{
m_nRow=row;
m_nCol=col;
m_Array.SetSize(m_nRow*m_nCol);
for(int i=0;i<int(m_nRow*m_nCol);i++)
{
m_Array[i]=f[i];
}
return *this;
}
/////////////////////////////////////////////////////////////
//析构函数
CMatrix::~CMatrix()
{
}
/////////////////////////////////////////////////////////////
//矩阵运算
CMatrix CMatrix::operator *(const double& t)
{
CMatrix tp=*this;
for(int i=0;i<int(m_nRow*m_nCol);i++)
{
tp.m_Array[i]*=t;
}
return tp;
}
CMatrix CMatrix::operator *(const CMatrix &m)
{
CMatrix temp;
temp.m_nRow=m_nRow;
temp.m_nCol=m.m_nCol;
temp.m_Array.SetSize(temp.m_nRow*temp.m_nCol);
double total=0.0;
for(int i=0;i<int(temp.m_nRow);i++)
{
for(int j=0;j<int(temp.m_nCol);j++)
{
total=0.0;
for(int n=0;n<int(m_nCol);n++)
{
total+=m_Array[i*m_nCol+n]*m.m_Array[n*m.m_nCol+j];
}
temp.m_Array[i*temp.m_nCol+j]=total;
}
}
return temp;
}
CMatrix CMatrix::operator =(const CMatrix &mat)
{
m_nRow=mat.m_nRow;
m_nCol=mat.m_nCol;
m_Array.SetSize(m_nRow*m_nCol);
for(int i=0;i<int(m_nRow*m_nCol);i++)
{
m_Array[i]=mat.m_Array[i];
}
return *this;
}
CMatrix CMatrix::T()
{
CMatrix temp;
if(m_nCol==0||m_nRow==0) return temp;
if(m_nCol==1&&m_nRow==1) return *this;
temp.m_Array.SetSize(m_Array.GetSize());
temp.m_nCol=m_nRow;
temp.m_nRow=m_nCol;
for(int i=0;i<int(m_nRow);i++)
{
for(int j=0;j<int(m_nCol);j++)
{
temp.m_Array[j*temp.m_nCol+i]=m_Array[i*m_nCol+j];
}
}
return temp;
}
CMatrix CMatrix::RemMatrix(int x,int y)
{
std::vector<double>t;
for(int i=0;i<m_nRow;i++)
for(int j=0;j<m_nCol;j++)
{
if(i!=x && j!=y)
t.push_back(m_Array[i*m_nCol+j]);
else
continue;
}
CMatrix temp(t,m_nRow-1,m_nCol-1);
return temp;
}
///////////////////////////////////////////////////////////////////////
//求行列式的值
float CMatrix::Range()
{
//m_nRow=m_nCol
double range=0.0;
if(m_nRow==1)
return m_Array[0];
if(m_nRow==2)
return m_Array[0]*m_Array[3]-m_Array[1]*m_Array[2];
else
{
for(int i=0;i<m_nCol;i++)
{
if(i%2==0)
range+=m_Array[i]*RemMatrix(0,i).Range();
else
range-=m_Array[i]*RemMatrix(0,i).Range();
}
return range;
}
}
CMatrix CMatrix::Accompany()
{
//m_nRow=m_nCol
std::vector<double>t;
for(int i=0;i<m_nRow;i++)
for(int j=0;j<m_nCol;j++)
if((i+j)%2==0)
t.push_back(RemMatrix(i,j).Range());
else
t.push_back(-RemMatrix(i,j).Range());
CMatrix temp(t,m_nRow,m_nCol);//m_nRow=m_nCol
return temp.T();
}
CMatrix CMatrix::operator ~()
{
CMatrix temp=*this;
if(temp.Range()!=0)
temp=temp.Accompany()*(1.0/temp.Range());
else
AfxMessageBox("不能求逆!");
return temp;
}
void CMatrix::Display()
{
CString S;
for(int i=0;i<m_nRow;i++)
for(int j=0;j<m_nCol;j++)
{
S.Format("M(%d,%d)=%f",i,j,m_Array[i*m_nCol+j]);
AfxMessageBox(S);
}
S.Format("row=%d,col=%d",m_nRow,m_nCol);
AfxMessageBox(S);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -