📄 cmatsym.cpp
字号:
#include "stdafx.h"
#include <iostream.h>
#include "CMat.h"
#include "CMatSym.h"
#include "eigen.h"
CMatrixSym::CMatrixSym()
{
li = 0;
col = 0;
bTranspose = FALSE;
pMem = NULL;
pData = NULL;
}
CMatrixSym::CMatrixSym(long l)
{
li = l;
col = l;
bTranspose = FALSE;
pData = NULL;
pMem = NULL;
Allocate();
}
CMatrixSym::~CMatrixSym()
{
if (pMem != NULL)
{
delete pMem;
pMem = NULL;
}
}
/***********************************************************************
* Get the element at (l,c) of the matrix if bTranspose == FALSE, and of the
* transposed matrix if bTranspose == TRUE
***********************************************************************/
inline valtype CMatrixSym::GetAt(long l, long c)
{
long t;
#ifdef CHECK_BOUNDARY
if (l <= 0 || c <= 0 || l > li || c > col)
Fail(NULL);
#endif
if (c < l)
{
t = c;
c = l;
l = t;
}
return pData[(l-1L)*li - ((l-1L)*l)/2L + c-1L];
}
/***********************************************************************
* Obvious. Becareful to bTranspose
***********************************************************************/
inline void CMatrixSym::SetAt(long l, long c, valtype value)
{
long t;
#ifdef CHECK_BOUNDARY
if (l <= 0 || c <= 0 || l > li || c > col)
Fail(NULL);
#endif
if (c < l)
{
t = c;
c = l;
l = t;
}
pData[(l-1L)*li - ((l-1L)*l)/2L + c-1L] = value;
}
/***********************************************************************
* Allocate (or reallocate) the memory needed by pData.
* Call the CMemory's Allocate fct
***********************************************************************/
void CMatrixSym::Allocate()
{
if (pMem == NULL)
{ /*Allocate memory*/
pMem = new CMemory();
pMem->Allocate((li+1L)*li*sizeof(valtype)/2L);
}
else
{ /*Reallocate memory*/
if (bLocked == TRUE)
{
Unlock();
}
pMem->Reallocate((li+1L)*li*sizeof(valtype)/2L);
}
}
long CMatrixSym::GetCol()
{
return li;
}
long CMatrixSym::GetLi()
{
return li;
}
/***********************************************************************
* Puts in Q the eigenvectors and in ev the eigenvalues of the matrix.
* This matrix is a real symmetric li*li matrix.
* Q must be a pointer ot a square li*li matrix, and
* ev a pointer to a vector of li elements
***********************************************************************/
int CMatrixSym::Eigen(CMatrix *Q, CVect *ev)
{
CVect *offdiag;
long r = GetLi();
offdiag = new CVect(r);
Q->Lock();
ev->Lock();
offdiag->Lock();
Put(Q);
tred2(Q, r, ev, offdiag);
tqli(ev, offdiag, r, Q);
ev->Unlock();
offdiag->Unlock();
Q->Unlock();
return 0;
}
/***********************************************************************
* Puts all the elements of this matrix into the pointer to a CMatrix object
***********************************************************************/
int CMatrixSym::Put(CMatrix *Q)
{
long r, x, y;
r = GetLi();
Lock();
Q->Lock();
for (y = 1; y <= r; y++)
{
for (x = 1; x <= r; x++)
{
Q->SetAt(y, x, GetAt(y, x));
}
}
Q->Unlock();
Unlock();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -