logplus/BaseFun/include/Matrix1.h
2025-10-29 17:23:30 +08:00

217 lines
6.9 KiB
C++

//////////////////////////////////////////////////////////////////////
// Matrix.h
//
// 操作矩阵的类 CMatrix1 的声明接口
//
// 编制, 2002/8
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MATRIX_H__ACEC32EA_5254_4C23_A8BD_12F9220EF43A__INCLUDED_)
#define AFX_MATRIX_H__ACEC32EA_5254_4C23_A8BD_12F9220EF43A__INCLUDED_
#include "BaseFun.h"
#include "CStringType.h"
#if !defined(_BITSET_)
# include <bitset>
#endif // !defined(_BITSET_)
#ifndef BOOL
#define BOOL bool
#endif
#ifndef TRUE
#define TRUE true
#endif
//////////////////////////////////////////////////////////////////////////////////////
//
//(-- class CTokenizer1
//
class BASEFUN_EXPORT CTokenizer1
{
public:
CTokenizer1(const CString& cs, const CString& csDelim) : m_cs(cs), m_nCurPos(0)
{
SetDelimiters(csDelim);
}
void SetDelimiters(const CString& csDelim)
{
for(int i = 0; i < csDelim.GetLength(); ++i){
char * c=csDelim.GetString();
m_sDelimiter.set(static_cast<BYTE>(c[i]));
}
}
BOOL Next(CString& cs)
{
cs.Empty();
while(m_nCurPos < m_cs.GetLength() && m_sDelimiter[static_cast<BYTE>(m_cs[m_nCurPos])])
++m_nCurPos;
if(m_nCurPos >= m_cs.GetLength())
return FALSE;
int nStartPos = m_nCurPos;
while(m_nCurPos < m_cs.GetLength() && !m_sDelimiter[static_cast<BYTE>(m_cs[m_nCurPos])])
++m_nCurPos;
cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos);
return TRUE;
}
CString Tail() const
{
int nCurPos = m_nCurPos;
char *ch=m_cs.GetString();
while(nCurPos < m_cs.GetLength()) {
if(!m_sDelimiter[static_cast<BYTE>(ch[nCurPos])]) break;
++nCurPos;
}
CString csResult;
if(nCurPos < m_cs.GetLength())
csResult = m_cs.Mid(nCurPos);
return csResult;
}
private:
CString m_cs;
std::bitset<256> m_sDelimiter;
int m_nCurPos;
};
//
//--) // class CTokenizer1
//
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//
//(-- class CMatrix1
//
class BASEFUN_EXPORT CMatrix1
{
//
// 公有接口函数
//
public:
//
// 构造与析构
//
CMatrix1(); // 基础构造函数
CMatrix1(int nRows, int nCols); // 指定行列构造函数
CMatrix1(int nRows, int nCols, float value[]); // 指定数据构造函数
CMatrix1(int nSize); // 方阵构造函数
CMatrix1(int nSize, float value[]); // 指定数据方阵构造函数
CMatrix1(const CMatrix1& other); // 拷贝构造函数
BOOL Init(int nRows, int nCols); // 初始化矩阵
BOOL MakeUnitMatrix(int nSize); // 将方阵初始化为单位矩阵
virtual ~CMatrix1(); // 析构函数
//
// 输入与显示
//
// 将字符串转换为矩阵数据
BOOL FromString(CString s, const CString& sDelim = " ", BOOL bLineBreak = TRUE);
// 将矩阵转换为字符串
CString ToString(const CString& sDelim = " ", BOOL bLineBreak = TRUE) const;
// 将矩阵的指定行转换为字符串
CString RowToString(int nRow, const CString& sDelim = " ") const;
// 将矩阵的指定列转换为字符串
CString ColToString(int nCol, const CString& sDelim = " ") const;
//
// 元素与值操作
//
BOOL SetElement(int nRow, int nCol, float value); // 设置指定元素的值
float GetElement(int nRow, int nCol) const; // 获取指定元素的值
void SetData(float value[]); // 设置矩阵的值
int GetNumColumns() const; // 获取矩阵的列数
int GetNumRows() const; // 获取矩阵的行数
int GetRowVector(int nRow, float* pVector) const; // 获取矩阵的指定行矩阵
int GetColVector(int nCol, float* pVector) const; // 获取矩阵的指定列矩阵
float* GetData() const; // 获取矩阵的值
//
// 数学操作
//
CMatrix1& operator=(const CMatrix1& other);
BOOL operator==(const CMatrix1& other) const;
BOOL operator!=(const CMatrix1& other) const;
CMatrix1 operator+(const CMatrix1& other) const;
CMatrix1 operator-(const CMatrix1& other) const;
CMatrix1 operator*(float value) const;
CMatrix1 operator*(const CMatrix1& other) const;
// 复矩阵乘法
BOOL CMul(const CMatrix1& AR, const CMatrix1& AI, const CMatrix1& BR, const CMatrix1& BI, CMatrix1& CR, CMatrix1& CI) const;
// 矩阵的转置
CMatrix1 Transpose() const;
//
// 算法
//
// 实矩阵求逆的全选主元高斯-约当法
BOOL InvertGaussJordan();
// 复矩阵求逆的全选主元高斯-约当法
BOOL InvertGaussJordan(CMatrix1& mtxImag);
// 对称正定矩阵的求逆
BOOL InvertSsgj();
// 托伯利兹矩阵求逆的埃兰特方法
BOOL InvertTrench();
// 求行列式值的全选主元高斯消去法
float DetGauss();
// 求矩阵秩的全选主元高斯消去法
int RankGauss();
// 对称正定矩阵的乔里斯基分解与行列式的求值
BOOL DetCholesky(float* dblDet);
// 矩阵的三角分解
BOOL SplitLU(CMatrix1& mtxL, CMatrix1& mtxU);
// 一般实矩阵的QR分解
BOOL SplitQR(CMatrix1& mtxQ);
// 一般实矩阵的奇异值分解
BOOL SplitUV(CMatrix1& mtxU, CMatrix1& mtxV, float eps = 0.000001);
// 求广义逆的奇异值分解法
BOOL GInvertUV(CMatrix1& mtxAP, CMatrix1& mtxU, CMatrix1& mtxV, float eps = 0.000001);
// 约化对称矩阵为对称三对角阵的豪斯荷尔德变换法
BOOL MakeSymTri(CMatrix1& mtxQ, CMatrix1& mtxT, float dblB[], float dblC[]);
// 实对称三对角阵的全部特征值与特征向量的计算
BOOL SymTriEigenv(float dblB[], float dblC[], CMatrix1& mtxQ, int nMaxIt = 60, float eps = 0.000001);
// 约化一般实矩阵为赫申伯格矩阵的初等相似变换法
void MakeHberg();
// 求赫申伯格矩阵全部特征值的QR方法
BOOL HBergEigenv(float dblU[], float dblV[], int nMaxIt = 60, float eps = 0.000001);
// 求实对称矩阵特征值与特征向量的雅可比法
BOOL JacobiEigenv(float dblEigenValue[], CMatrix1& mtxEigenVector, int nMaxIt = 60, float eps = 0.000001);
// 求实对称矩阵特征值与特征向量的雅可比过关法
BOOL JacobiEigenv2(float dblEigenValue[], CMatrix1& mtxEigenVector, float eps = 0.000001);
//
// 保护性数据成员
//
protected:
int m_nNumColumns; // 矩阵列数
int m_nNumRows; // 矩阵行数
float* m_pData; // 矩阵数据缓冲区
//
// 内部函数
//
private:
void ppp(float a[], float e[], float s[], float v[], int m, int n);
void sss(float fg[2], float cs[2]);
};
//
//--) // class CMatrix1
//
//////////////////////////////////////////////////////////////////////////////////////
#endif // !defined(AFX_MATRIX_H__ACEC32EA_5254_4C23_A8BD_12F9220EF43A__INCLUDED_)