// Matrix.cpp: implementation of the CMatrix class.
//
//////////////////////////////////////////////////////////////////////
#include "Matrix.h"
#include "iostream.h"
//#include "stdlib.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//using namespace std;
CMatrix::CMatrix()
{
m_iLine=m_iColumn=0;
m_pNumber = new float;
}
CMatrix::~CMatrix()
{
if(m_iLine>0||m_iColumn>0)
delete []m_pNumber;
}
CMatrix::CMatrix(const CMatrix& m)
{
// if(m_iLine>0||m_iColumn>0)
// delete []m_pNumber;
m_iLine = m.m_iLine;
m_iColumn = m.m_iColumn;
m_pNumber = new float[m_iLine*m_iColumn];
for(int i=0;i<m_iLine*m_iColumn;i++)
m_pNumber[i] = m.m_pNumber[i];
}
CMatrix::CMatrix(int lines, int columns)
{
if(m_iLine>0||m_iColumn>0)
delete []m_pNumber;
m_iLine = lines;
m_iColumn = columns;
m_pNumber = new float[m_iColumn*m_iLine];
}
float CMatrix::det()
{
int aaaaa=0;
if(m_iLine!=m_iColumn)
{
// AfxMessageBox("行列式行、列数不一致,不能进行求行列式值!",MB_OK|MB_ICONERROR);
return 0;
}
float temp =0;
if(m_iLine==1)
{
return m_pNumber[0];
}
else if(m_iLine==2)
{
return m_pNumber[0]*m_pNumber[3]-m_pNumber[1]*m_pNumber[2];
}
else if(m_iLine==3)
{
for(int i=0;i<m_iLine;i++)
{
float positivev=1.0f;
float negativev =1.0f;
int tempk = i;
int templ = i;
for(int j=0;j<m_iLine;j++)
{
if(tempk==m_iLine)
tempk = 0;
if(templ == -1)
templ = m_iLine-1;
positivev *= m_pNumber[tempk*m_iColumn+j];
negativev *= m_pNumber[templ*m_iColumn+j];
templ--;
tempk++;
}
temp +=(positivev-negativev);
}
return temp;
}
else
{
CMatrix tempm(*this);
//行列式性质:行列式中某行(列)各元素乘以同一数,然后加到另一行(列)对应的元素上,
//行列式的值不变
float temxk[100];
int count=0;
for(int j=0;j<m_iColumn;j++)
for(int i=1;i<m_iLine;i++)
{
if(j>=i) continue;
float xk = -tempm.r(i,j)/tempm.r(j,j);
temxk[count] = xk;
count++;
tempm.r(i,j)=0;
for(int k=j+1;k<m_iColumn;k++)
tempm.r(i,k) += xk*tempm.r(j,k);
}
// tempm.Print();
temp = 1;
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iColumn;j++)
{
if(i==j) temp*=tempm.r(i,j);
}
return temp;
}
}
float& CMatrix::r(int i, int j)
{
return m_pNumber[i*m_iColumn+j];
}
float CMatrix::AlgebraicComplement(int line, int column)
{
if(m_iLine!=m_iColumn)
{
// AfxMessageBox("行列式行、列数不一致,不能进行求行列式值!",MB_OK|MB_ICONERROR);
return 0;
}
CMatrix tempm(m_iLine-1,m_iLine-1);
for(int i=0;i<m_iLine-1;i++)
for(int j=0;j<m_iLine-1;j++)
{
if(i>=line&&j<column)
tempm.r(i,j) = m_pNumber[(i+1)*m_iLine+j];
else if(i>=line&&j>=column)
tempm.r(i,j) = m_pNumber[(i+1)*m_iLine+j+1];
else if(i<line&&j>=column)
tempm.r(i,j) = m_pNumber[i*m_iColumn+j+1];
else
tempm.r(i,j) = m_pNumber[i*m_iColumn+j];
}
return tempm.det();
}
CMatrix CMatrix::Inverse()
{
CMatrix returnm;
if(m_iLine!=m_iColumn)
{
// AfxMessageBox("行列式行、列数不一致,不能进行求行列式值!",MB_OK|MB_ICONERROR);
return returnm;
}
float detthis = det();
returnm.SetLinesAndColumns(m_iLine,m_iLine);
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iLine;j++)
{
if((i+j)%2==1)
returnm.r(i,j)=-1.0f*AlgebraicComplement(j,i)/detthis;
else
returnm.r(i,j)=AlgebraicComplement(j,i)/detthis;
}
return returnm;
}
CMatrix CMatrix::operator *(CMatrix m)
{
CMatrix temp;
if(m_iColumn!=m.m_iLine)
return temp;
temp.SetLinesAndColumns(m_iLine,m.m_iColumn);
temp.zeros();
for(int i=0;i<m_iLine;i++)
for(int j=0;j<temp.m_iColumn;j++)
{
for(int k=0;k<m_iColumn;k++)
temp.r(i,j) += m_pNumber[i*m_iColumn+k]*m.r(k,j);
}
return temp;
}
CMatrix CMatrix::operator +(CMatrix m)
{
if(m_iColumn!=m.m_iColumn&&m_iLine!=m.m_iLine)
return *this;
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iColumn;j++)
{
m_pNumber[i*j] += m.r(i,j);
}
return *this;
}
CMatrix CMatrix::operator -(CMatrix m)
{
if(m_iColumn!=m.m_iColumn&&m_iLine!=m.m_iLine)
return *this;
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iColumn;j++)
{
m_pNumber[i*j] -= m.r(i,j);
}
return *this;
}
const CMatrix CMatrix::operator =(CMatrix m)
{
if(m_iColumn!=m.m_iColumn&&m_iLine!=m.m_iLine)
return *this;
SetLinesAndColumns(m_iLine,m_iColumn);
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m.m_iColumn;j++)
{
m_pNumber[i*m_iColumn+j] = m.r(i,j);
}
return *this;
}
void CMatrix::eye()
{
if(m_iLine!=m_iColumn)
{
return ;
}
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iLine;j++)
{
if(i==j)
m_pNumber[i*m_iColumn+j] = 1;
else
m_pNumber[i*m_iColumn+j] = 0;
}
}
void CMatrix::zeros()
{
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iColumn;j++)
{
m_pNumber[i*m_iColumn+j] = 0;
}
}
void CMatrix::SetLinesAndColumns(int line, int column)
{
if(m_iLine>0||m_iColumn>0)
delete []m_pNumber;
m_pNumber = new float[line*column];
m_iLine = line;
m_iColumn = column;
}
CMatrix CMatrix::transp()
{
CMatrix tempm;
tempm.SetLinesAndColumns(m_iColumn,m_iLine);
for(int i=0;i<m_iLine;i++)
for(int j=0;j<m_iColumn;j++)
tempm.r(j,i) = m_pNumber[i*m_iColumn+j];
int aaaaa=0;
return tempm;
}
void CMatrix::Print()
{
for(int i=0;i<m_iLine;i++)
{
for(int j=0;j<m_iColumn;j++)
{
cout<<m_pNumber[i*m_iColumn+j]<<'\t';
}
cout<<endl;
}
cout<<endl;
}