📄 cmatrix.h
字号:
// cMatrix.h: interface for the cMatrix class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CMATRIX_H__0E51C477_3BB8_4632_AC00_A36672E70AD0__INCLUDED_)
#define AFX_CMATRIX_H__0E51C477_3BB8_4632_AC00_A36672E70AD0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class T> class cMatrix
{
public:
cMatrix()
{
int m_Row = 0;
int m_Col = 0;
m_pElements = NULL;
};
virtual ~cMatrix()
{
delete [] m_pElements;
};
public:
int m_Row;
int m_Col;
T* m_pElements;
public:
void Create(int Row,int Col)
{
m_Row = Row;
m_Col = Col;
m_pElements = new T[Row*Col];
int i,j,Index;
Index = 0;
for(i = 0;i < Row;i++)
for(j = 0;j < Col;j++,Index++)
m_pElements[Index] = (rand()-RAND_MAX/2.0f)/RAND_MAX;
}
public:
cMatrix* MultiLeft(cMatrix & Matrix)
{
cMatrix<float> * m_Mat3;
m_Mat3=new(cMatrix<float>);
int i,j,Index;
if(m_Col!=Matrix.m_Row)
AfxMessageBox("两个矩阵不能相乘!");
else
{
m_Mat3->Create(Matrix.m_Row,m_Col);
for(i=0;i<m_Row;i++)
for(j=0;j<Matrix.m_Col;j++)
{
m_Mat3->m_pElements[i*j]=0;
for(Index=0;Index<m_Row;Index++)
{
m_Mat3->m_pElements[i*j]+=m_pElements[i*j]*Matrix.m_pElements[Index*j];
}
}
}
return m_Mat3;
}
cMatrix* MultiRight(cMatrix & Matrix)
{
cMatrix<float> * m_Mat3;
m_Mat3=new(cMatrix<float>);
int i,j,Index=0;
if(m_Col!=Matrix.m_Row)
AfxMessageBox("两个矩阵不能相乘!");
else
{
m_Mat3->Create(Matrix.m_Row,m_Col);
for(i=0;i<m_Row;i++)
for(j=0;j<Matrix.m_Col;j++)
{
m_Mat3->m_pElements[i*j]=0;
for(Index=0;Index<m_Row;Index++)
{
m_Mat3->m_pElements[i*j]+=m_pElements[i*j]*Matrix.m_pElements[Index*j];
}
}
}
return m_Mat3;
}
cMatrix* Plus(cMatrix & Matrix)
{
cMatrix<float> * m_Mat3;
m_Mat3=new(cMatrix<float>);
int i,j,Index=0;
if((Matrix.m_Col!=m_Col)||(Matrix.m_Row!=m_Row))
AfxMessageBox("两个矩阵为不同类型,不能相加!");
else
{
m_Mat3->Create(m_Row,m_Col);
for(i=0;i<m_Col;i++)
for(j=0;j<m_Row;j++,Index++)
{
m_Mat3->m_pElements[Index]=0;
m_Mat3->m_pElements[Index]=Matrix.m_pElements[Index]+m_pElements[Index];
}
}
return m_Mat3;
}
cMatrix* Minus(cMatrix & Matrix)
{
cMatrix<float> * m_Mat3;
m_Mat3=new(cMatrix<float>);
int i,j,Index=0;
if((Matrix.m_Col!=m_Col)||(Matrix.m_Row!=m_Row))
AfxMessageBox("两个矩阵为不同类型,不能相减!");
else
{
m_Mat3->Create(m_Row,m_Col);
for(i=0;i<m_Col;i++)
for(j=0;j<m_Row;j++,Index++)
{
m_Mat3->m_pElements[Index]=0;
m_Mat3->m_pElements[Index]=m_pElements[Index]-Matrix.m_pElements[Index];
}
}
return m_Mat3;
}
cMatrix* DotPlus(T Value,int row,int col)
{
if((row<=m_Row)&&(col<=m_Col))
{
m_pElements[m_Col*row+col]=Value;
}
else
AfxMessageBox("赋值错误!");
return this;
}
cMatrix* Transpos()
{
cMatrix<float> * m_Mat3;
m_Mat3=new(cMatrix<float>);
int i,j;
m_Mat3->Create(m_Col,m_Row);
for(i=0;i<m_Row;i++)
{
for(j=0;j<m_Col;j++)
{
m_Mat3->m_pElements[m_Mat3->m_Col*j+i]=m_pElements[m_Col*i+j];
}
}
return m_Mat3;
}
};
#endif // !defined(AFX_CMATRIX_H__0E51C477_3BB8_4632_AC00_A36672E70AD0__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -