📄 matrix1.txt
字号:
武汉白云黄鹤站∶精华区武汉白云黄鹤站∶精华区
发信人: sweeping (simon), 信区: Algorithm WWW-POST
标 题: 一些矩阵运算源码1
发信站: 武汉白云黄鹤站 (Thu Nov 8 17:38:01 2001) , 转信
///////////////////////////////
// CMatrix.h
// Jacob eigenvalue and eigenvector
// QR eigenvalue
#define MATRIXEPSILON 0.000001
// QR eigenvalue
// Seidel iterations
#define MATRIXITERATION 60
//
class CSymmetricalMatrix;
class CMatrix
{
protected: // for subclass to access
int m_rows; // rows
int m_cols; // columns
double *m_a; // elements
public:
CMatrix() { m_rows=0; m_cols=0; m_a=NULL; }
CMatrix(int rows, int cols);
~CMatrix();
BOOL Resize(int rows, int cols);
int GetRows();
int GetColumns();
void SetElement(int row, int col, double e);
double GetElement(int row, int col);
void SetAllElements(double e);
double *GetBuffer(); // return m_a: begin address
// return a copy of a matrix
CMatrix *Copy();
// add pB to current, and return the result matrix
CMatrix *AddMatrix(CMatrix *pB);
// subtract pB from current, and return the result matrix
CMatrix *SubMatrix(CMatrix *pB);
// multiply pB with current, and return the result matrix
CMatrix *Multiply(CMatrix *pB);
// return the transpose of current matrix
CMatrix *Transpose();
// return At*A, A: current matrix
CSymmetricalMatrix *LeftTransposeMultiply();
// return A*At, A: current matrix
CSymmetricalMatrix *RightTransposeMultiply();
BOOL WriteToFile(char *filename);
BOOL ReadFromFile(char *filename);
};
class CSquareMatrix : public CMatrix
{
public:
CSquareMatrix() : CMatrix() {};
CSquareMatrix(int rows) : CMatrix(rows, rows) {};
~CSquareMatrix() {};
BOOL Resize(int rows) { return CMatrix::Resize(rows, rows); };
void SetToIdentity();
double Determinantal();
// calculate the inverse matrix of a nxn matrix
// the original matrix will be destroyed
// and become the inverse matrix
// if the inverse does not exist, return FALSE
BOOL Inverse();
// calculate the LU decomposition of matrix
// the results are put into pL and pU
// pL and pU are allocated outside of this method
// the original matrix will be destroyed
// if can not decompose, return FALSE
BOOL LUDecompose(CSquareMatrix *pL, CSquareMatrix *pU);
// calculate the QR decomposition of matrix
// the results are put into pQ and original matrix
// pQ is allocated outside of this method
// the original matrix will be destroyed
// if can not decompose, return FALSE
BOOL QRDecompose(CSquareMatrix *pQ);
// transform matrix into H matrix
// the original matrix will be destroyed
// if can not transform, return FALSE
BOOL Hessenberg();
// calculate the eigenvalues of nxn real matrix
// the result eigenvalues are in the returned nx2 matrix
// the first column is the real part of eigenvalues
// the second column is the imaginary part of eigenvalues
// for a eigenvalue lambda,
// solve the equation (A-lambda*I)X=0 to get the corresponding eigenvector
// original matrix is destroyed!
// if can not get the eigenvalues, return FALSE
CMatrix *QREigenvalue();
};
class CSymmetricalMatrix : public CSquareMatrix
{
// calculate the eigenvalue and eigenvictor of
// nnxnn symmetric real matrix aa
// the result eigenvictors are ss,
// the result eigenvalues are in the diagonal of aa
void jacobian(double *aa, double *ss, int nn);
public:
CSymmetricalMatrix() : CSquareMatrix() {};
CSymmetricalMatrix(int rows) : CSquareMatrix(rows) {};
~CSymmetricalMatrix() {};
// calculate the eigenvalue and eigenvictor of
// nxn symmetric real matrix
// the result eigenvectors are in returned matrix,
// the result eigenvalues are in the diagonal of original matrix
// original matrix is destroyed!
// if original matrix is not symmetrical,
// we will use the upper-right to fill out down-left to make it symmetrical
CSymmetricalMatrix *Jacob();
};
class CEquationMatrix : public CMatrix
{
public:
CEquationMatrix() : CMatrix() {};
CEquationMatrix(int rows) : CMatrix(rows, rows+1) {};
~CEquationMatrix() {};
BOOL Resize(int rows) { return CMatrix::Resize(rows, rows+1); };
// solve nx(n+1) equations using Gaussian method
// the matrix ia n rows and n+1 columns
// the first n columns are matrix A
// the last column is the constant vector B
// after solving, matrix will be destroied,
// the solution will be put into the last column
// if can not solve the equations, FALSE will be returned
BOOL Gauss();
// solve nx(n+1) equations using Jordan's method
// the matrix ia n rows and n+1 columns
// the first n columns are matrix A
// the last column is the constant vector B
// after solving, matrix will be destroied,
// the solution will be put into the last column
// if can not solve the equations, FALSE will be returned
BOOL Jordan();
// solve nx(n+1) equations using Seidel's method
// the matrix ia n rows and n+1 columns
// the first n columns are matrix A
// the last column is the constant vector B
// after solving, matrix will be destroied,
// the solution will be put into the last column
// if can not solve the equations, FALSE will be returned
BOOL Seidel();
};
--
※ 来源:.武汉白云黄鹤站WWW bbs.whnet.edu.cn. [FROM: 211.101.187.186]
华中地区网络中心
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -