⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 矩阵类.txt

📁 1.内存管理一律根据实际需要的大小在堆中动态分配内存。 2.边界检查。如果数组下标超越了数组大小界限
💻 TXT
📖 第 1 页 / 共 2 页
字号:

#ifndef _MATRIX_H_
#define _MATRIX_H_
/*
File Name:  Matrix.h
Lib  Name:  JGMath2D.lib
Description:  For the 2D translation
Created Author:Bookworm_zju@zjuem.edu.cn
Created Data:  04/22/02
*/
class CJGArray
{
public:
    CJGArray(UINT size);
    CJGArray();
    double& operator[](UINT x) const;
    virtual ~CJGArray();
private:
    double* m_buf;
};
class CMatrix
{
public:
    //构造函数
    CMatrix();
    CMatrix(UINT mSize,UINT nSize);
    CMatrix(const CMatrix& src);
    virtual ~CMatrix();
public:
    CJGArray& CMatrix::operator [](UINT x) const;
    //赋值操作
    CMatrix& operator = (const CMatrix& src);
    void UNIT_OP();
    void NULL_OP();
    UINT GetM()const{return m;};
    UINT GetN()const{return n;};
    //矩阵行列式
    double Min(void);
    double Max(void);
    double Det(void);
    //正负号操作
    CMatrix operator +();
    CMatrix operator -();
    //组合符号操作
    CMatrix& operator +=(const CMatrix& m);
    CMatrix& operator -=(const CMatrix& m);
    CMatrix& operator *=(const CMatrix& m);
    CMatrix& operator *=(const double& c);
    CMatrix& operator /=(const double& c);
    CMatrix& operator ^=(const  UINT& pow);
    //在输出窗口列出其值
#ifdef _DEBUG
    void OutPut() const;
#endif
public:
    int CMatrix::Pivot( UINT row);
protected:
    CJGArray** m_buf;
    UINT m;
    UINT n;
};
//逻辑操作
BOOL operator ==(const CMatrix& A, const CMatrix& B);
BOOL operator !=(const CMatrix& A, const CMatrix& B);
//矩阵运算
CMatrix operator * (const CMatrix& A,const CMatrix& B);
CMatrix operator * (double k , const CMatrix& B);
CMatrix operator * (const CMatrix& B , double k);
CMatrix operator /(const CMatrix& A, const CMatrix& B);
CMatrix operator /(const CMatrix& B, const double& k);
CMatrix operator /(const double& k, const CMatrix& B);
CMatrix operator + (const CMatrix& A,const CMatrix& B);
CMatrix operator - (const CMatrix& A,const CMatrix& B);
CMatrix operator ~(const CMatrix& M);
CMatrix operator !(const CMatrix& M);
CMatrix operator ^(const CMatrix& M, const  UINT& pow);
#endif
///cpp文件
#i nclude "stdafx.h"
#i nclude "math.h"
#i nclude "matrix.h"
//--------------------------------------------------------------------------
-------------------------
CJGArray::CJGArray():m_buf(NULL)
{
}
//--------------------------------------------------------------------------
-------------------------
CJGArray::~CJGArray()
{
    delete []m_buf;
    m_buf = NULL;
}
//--------------------------------------------------------------------------
-------------------------
CJGArray::CJGArray(UINT size)
{
    m_buf = new double[size];
    for( UINT i=0; i<size; i++)
        m_buf[i] = 0.0;
}
//--------------------------------------------------------------------------
-------------------------
double& CJGArray::operator [](UINT x) const
{
    return m_buf[x];
}
//--------------------------------------------------------------------------
-------------------------
CMatrix::CMatrix(UINT msize, UINT nsize)
{
    if( msize<=0 || nsize<=0 )
    {
        ASSERT(FALSE);
        return;
    }
    m_buf = new CJGArray* [msize];
    for(UINT i=0; i<msize; i++)
        m_buf[i] = new CJGArray(nsize);

    m = msize;
    n = nsize;
}
//--------------------------------------------------------------------------
-------------------------
CJGArray& CMatrix::operator [](UINT x)  const
{
    return *m_buf[x];
}
//--------------------------------------------------------------------------
-------------------------
CMatrix::CMatrix():m_buf(NULL)
{
}
//--------------------------------------------------------------------------
-------------------------
CMatrix::CMatrix(const CMatrix& src)
{
    m = src.m;
    n = src.n;
    m_buf = new CJGArray* [m];
    for(UINT i=0; i<m; i++)
        m_buf[i] = new CJGArray(n);
    for( i=0; i<m; i++)
        for(UINT j=0; j<n; j++)
        {
            (*this)[i][j] = src[i][j];
        }

}
//--------------------------------------------------------------------------
-------------------------
CMatrix::~CMatrix()
{
    for(UINT i=0; i<m; i++)
       delete m_buf[i];
    delete []m_buf;
    m_buf = NULL;
}
//--------------------------------------------------------------------------
-------------------------
CMatrix& CMatrix::operator = (const CMatrix& src)
{
    if( this == &src )//It is me!
        return (*this);
    if( m!= src.m || n != src.n )
    {
        ASSERT(FALSE);
        return (*this);
    }
    for(UINT i=0; i<m; i++)
    {
        for( UINT j=0; j<n; j++)
        {
            (*this)[i][j] = src[i][j];
        }
    }
    return (*this);
}
//--------------------------------------------------------------------------
-------------------------
void CMatrix::UNIT_OP()
{
    for(UINT i=0; i<m; i++)
    {
        for(UINT j=0; j<n; j++)
        {
            if( i==j )
                (*this)[i][j] = 1.0;
            else
                (*this)[i][j] = 0.0;
        }
    }
}
//--------------------------------------------------------------------------
-------------------------
void CMatrix::NULL_OP()
{
    for(UINT i=0; i<m; i++)
    {
        for( UINT j=0; j<n; j++)
        {
            (*this)[i][j] = 0.0;
        }
    }
}
//--------------------------------------------------------------------------
-------------------------
//private partial pivoting method
int CMatrix::Pivot( UINT m)
{
    UINT k = (m);
    double t = 0;
    double amax = -1;
    for ( UINT i=m; i<m; i++)
    {
        if ((t=fabs((*this)[i][m]))>amax && (t!=0.0))
        {
            amax = t;
            k = i;
        }
    }
    if ((*this)[k][m] == double(0))
    {
        return -1;
    }
    if (k != UINT(m))
    {
        CJGArray &mptr = (*this)[k];
        ( (*this)[k] ) =  ( (*this)[m] );
        (*this)[m] = mptr;
        return k;
    }
    return 0;
}
//正负号操作
//--------------------------------------------------------------------------
-------------------------
CMatrix CMatrix::operator +()
{
    return (*this);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix CMatrix::operator -()
{
    CMatrix T(m, n);

    for ( UINT i=0; i<m; i++)
        {
            for( UINT j=0; j<n; j++)
            {
                T[i][j] = -(*this)[i][j];
            }
        }

    return T;
}
//组合符号操作
//--------------------------------------------------------------------------
-------------------------
CMatrix& CMatrix::operator +=(const CMatrix& M)
{
    if (m!=M.m || n!=M.n)
    {
        ASSERT(FALSE);
        return (*this);
    }

    for ( UINT i=0; i<M.m; i++)
    {
        for ( UINT j=0; j<M.n; j++)
        {
            (*this)[i][j] += M[i][j];
        }
    }
    return (*this);
}
//--------------------------------------------------------------------------
-------------------------
CMatrix& CMatrix::operator -=(const CMatrix& M)
{
    if (m!=M.m || n!=M.n)
    {
        ASSERT(FALSE);
        return (*this);
    }

    for ( UINT i=0; i< M.m; i++)
    {
        for ( UINT j=0; j<M.n; j++)
        {
            (*this)[i][j] -= (*this)[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 CMatrix &M )
{
    if (m!=M.m || n!=M.n)
    {
        ASSERT(FALSE);
        return (*this);
    }
    ASSERT(m == M.m || n == M.n);
    for ( UINT i=0; i<M.m; i++)
    {
        for ( UINT j=0; j<M.n; j++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -