📄 矩阵类.txt
字号:
{
(*this)[i][j] += M[i][j];
}
}
return (*this);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix& CMatrix::operator /=(const double &c)
{
for ( UINT i=0; i<m; i++)
{
for ( UINT j=0; j<n; j++)
{
(*this)[i][j] /= c;
}
}
return (*this);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix& CMatrix::operator ^=(const UINT &pow)
{
for( UINT i=2; i<=pow; i++)
{
*this = (*this) * (*this);
}
return (*this);
}
//--------------------------------------------------------------------------
-------------------------
//矩阵行列式
double CMatrix::Det()
{
UINT i = 0;
UINT j = 0;
UINT k = 0;
double piv = 0;
double detVal = double(1);
ASSERT(m == n);
CMatrix T(*this);
for (k=0; k<m; k++)
{
UINT indx = T.Pivot(k);
if (indx == -1)
{
return 0;
}
if (indx != 0)
{
detVal = -detVal;
}
detVal = detVal * T[k][k];
for (i=k+1; i<m; i++)
{
piv = T[i][k] / T[k][k];
for (j=k+1; j<m; j++)
{
T[i][j] -= piv * T[k][j];
}
}
}
return detVal;
}
//--------------------------------------------------------------------------
-------------------------
//矩阵极值
double CMatrix::Max(void)
{
double t = -1e308;
for ( UINT i=0; i<m; i++)
{
for ( UINT j=0; j<n; j++)
{
t = max(t, (*this)[i][j]);
}
}
return t;
}
//--------------------------------------------------------------------------
-------------------------
double CMatrix::Min(void)
{
double t = 1e308;
for ( UINT i=0; i<m; i++)
{
for ( UINT j=0; j<n; j++)
{
t = min(t, (*this)[i][j]);
}
}
return t;
}
//--------------------------------------------------------------------------
----------------------
#ifdef _DEBUG
void CMatrix::OutPut() const
{
UINT i,j;
for( i=0; i<GetM(); i++)
{
for( j=0; j<GetN(); j++)
{
double b;
b = (*this)[i][j];
TRACE("M[%d][%d]=%f ",i,j,b);
}
}
TRACE("\n");
}
#endif
//以下为友员操作
//--------------------------------------------------------------------------
-------------------------
CMatrix operator * (const CMatrix &A,const CMatrix &B)
{
UINT i,j,k;
CMatrix M( A.GetM(), B.GetN());
if ( A.GetN() != B.GetM() )
{
ASSERT(FALSE);
return M;
}
for( i=0; i<A.GetM(); i++)
{
for( j=0; j<B.GetN(); j++)
{
M[i][j] = 0.0;
for( k=0; k< A.GetN(); k++)
{
M[i][j] += A[i][k] * B[k][j];
}
}
}
return M;
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator * (double k , const CMatrix& B)
{
CMatrix M(B.GetM(),B.GetN());
for( UINT i=0; i<B.GetM(); i++)
{
for( UINT j=0; j<B.GetN(); j++)
{
M[i][j] = k*B[i][j];
}
}
return M;
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator * (const CMatrix& B,double k)
{
return (k*B);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator + (const CMatrix& A,const CMatrix& B)
{
CMatrix M(A.GetM(),A.GetN());
if( (A.GetM() != B.GetM()) || (A.GetN() != B.GetN()) )
{
ASSERT(FALSE);
return M;
}
for( UINT i=0; i<A.GetM(); i++)
{
for(UINT j=0; j<A.GetN(); j++)
{
M[i][j] = A[i][j]+B[i][j];
}
}
return M;
}
//--------------------------------------------------------------------------
----------------
CMatrix operator - (const CMatrix& A,const CMatrix& B)
{
CMatrix M(A.GetM(),A.GetN());
if( (A.GetM() != B.GetM()) || (A.GetN() != B.GetN()) )
{
ASSERT(FALSE);
return M;
}
for( UINT i=0; i<A.GetM(); i++)
{
for(UINT j=0; j<A.GetN(); j++)
{
M[i][j] = A[i][j] - B[i][j];
}
}
return M;
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator /(const CMatrix& m1, const CMatrix& m2)
{
return(m1 * (!m2));
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator /(const double& no, const CMatrix& m)
{
return((!m) * no);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator /(const CMatrix& m, const double& no)
{
return(m * (1/no));
}
//--------------------------------------------------------------------------
-------------------------
//求转置矩阵
CMatrix operator ~(const CMatrix& M)
{
CMatrix T(M.GetN(), M.GetM());
for (UINT i=0; i<M.GetM(); i++)
{
for (UINT j=0; j<M.GetN(); j++)
{
T[j][i] = M[i][j];
}
}
return T;
}
//--------------------------------------------------------------------------
-------------------------
//求逆矩阵
CMatrix operator !(const CMatrix& m)
{
UINT i = 0;
UINT j = 0;
UINT k = 0;
double a1 = 0;
double a2 = 0;
CMatrix M(m);
CMatrix T(M.GetM(), M.GetN());
T.UNIT_OP();
if( M.GetM() != M.GetN() )
{
ASSERT(FALSE);
return T;
}
for (k=0; k<M.GetM(); k++)
{
UINT indx = T.Pivot(k);
if (indx == -1)
{
AfxMessageBox("CMatrix::operator:inversion of a singular CMatrix!");
}
if(indx != 0)
{
CJGArray& mptr = T[k];
T[k] = T[indx];
T[indx] = mptr;
}
a1 = M[k][k];
for (j=0; j<M.GetM(); j++)
{
M[k][j] /= a1;
T[k][j] /= a1;
}
for (i=0; i<M.GetM(); i++)
{
if (i != k)
{
a2 = M[i][k];
for (j=0; j<M.GetM(); j++)
{
M[i][j] -= a2 * M[k][j];
T[i][j] -= a2 * T[k][j];
}
}
}
}
return T;
}
//--------------------------------------------------------------------------
-------------------------
CMatrix operator ^(const CMatrix& M,const UINT pow)
{
CMatrix T(M);
for ( UINT i=2; i<=pow; i++)
{
T = T * T;
}
return T;
}
//--------------------------------------------------------------------------
---------------------------
//逻辑操作
BOOL operator ==(const CMatrix &m1, const CMatrix &m2)
{
BOOL retVal = false;
if (m1.GetM()!=m2.GetM() || m1.GetN()!=m2.GetN())
return retVal;
for (UINT i=0; i<m1.GetM(); i++)
for (UINT j=0; j<m1.GetN(); j++)
if (m1[i][j] != m2[i][j])
return retVal;
return true;
}
//--------------------------------------------------------------------------
---------------
BOOL operator !=(const CMatrix &m1, const CMatrix &m2)
{
return((m1 == m2) ? false : true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -