📄 matlib.h
字号:
CMatlibVector<T>::CMatlibVector(const CMatlibVector<T>& vecI) :
iVectorLength(vecI.GetSize()), pData(NULL), eVType(VTY_CONST)
{
if (iVectorLength > 0)
{
/* Allocate data block for vector */
pData = new T[iVectorLength];
/* Copy vector */
for (int i = 0; i < iVectorLength; i++)
pData[i] = vecI[i];
}
}
template<class T>
void CMatlibVector<T>::Init(const int iIniLen, const T tIniVal)
{
iVectorLength = iIniLen;
/* Allocate data block for vector */
if (iVectorLength > 0)
{
if (pData != NULL)
delete[] pData;
pData = new T[iVectorLength];
/* Init with init value */
for (int i = 0; i < iVectorLength; i++)
pData[i] = tIniVal;
}
}
template<class T> inline
CMatlibVector<T> CMatlibVector<T>::operator()(const int iFrom,
const int iTo) const
{
/* This is also capable of "wrap around" blocks (if the value in "iFrom" is
larger then the "iTo" value) */
int i;
const int iStartVal = iFrom - 1;
if (iFrom > iTo)
{
/* Wrap around case */
CMatlibVector<T> vecRet(iVectorLength - iStartVal + iTo, VTY_TEMP);
int iCurPos = 0;
for (i = iStartVal; i < iVectorLength; i++)
vecRet[iCurPos++] = operator[](i);
for (i = 0; i < iTo; i++)
vecRet[iCurPos++] = operator[](i);
return vecRet;
}
else
{
CMatlibVector<T> vecRet(iTo - iStartVal, VTY_TEMP);
for (i = iStartVal; i < iTo; i++)
vecRet[i - iStartVal] = operator[](i);
return vecRet;
}
}
template<class T> inline
CMatlibVector<T> CMatlibVector<T>::operator()(const int iFrom,
const int iStep,
const int iTo) const
{
CMatlibVector<T> vecRet(abs(iTo - iFrom) / abs(iStep) + 1, VTY_TEMP);
int iOutPos = 0;
int i;
if (iFrom > iTo)
{
const int iEnd = iTo - 2;
for (i = iFrom - 1; i > iEnd; i += iStep)
{
vecRet[iOutPos] = operator[](i);
iOutPos++;
}
}
else
{
for (i = iFrom - 1; i < iTo; i += iStep)
{
vecRet[iOutPos] = operator[](i);
iOutPos++;
}
}
return vecRet;
}
template<class T> inline
CMatlibVector<T>& CMatlibVector<T>::PutIn(const int iFrom,
const int iTo,
CMatlibVector<T>& vecI)
{
const int iStart = iFrom - 1;
const int iEnd = iTo - iStart;
for (int i = 0; i < iEnd; i++)
operator[](i + iStart) = vecI[i];
return *this;
}
template<class T> inline
CMatlibVector<T>& CMatlibVector<T>::Merge(const CMatlibVector<T>& vecA, T& tB)
{
const int iSizeA = vecA.GetSize();
for (int i = 0; i < iSizeA; i++)
operator[](i) = vecA[i];
operator[](iSizeA) = tB;
return *this;
}
template<class T> inline
CMatlibVector<T>& CMatlibVector<T>::Merge(const CMatlibVector<T>& vecA,
const CMatlibVector<T>& vecB)
{
int i;
const int iSizeA = vecA.GetSize();
const int iSizeB = vecB.GetSize();
/* Put first vector */
for (i = 0; i < iSizeA; i++)
operator[](i) = vecA[i];
/* Put second vector behind the first one, both
together must have length of *this */
for (i = 0; i < iSizeB; i++)
operator[](i + iSizeA) = vecB[i];
return *this;
}
template<class T> inline
CMatlibVector<T>& CMatlibVector<T>::Merge(const CMatlibVector<T>& vecA,
const CMatlibVector<T>& vecB,
const CMatlibVector<T>& vecC)
{
int i;
const int iSizeA = vecA.GetSize();
const int iSizeB = vecB.GetSize();
const int iSizeC = vecC.GetSize();
const int iSizeAB = iSizeA + iSizeB;
/* Put first vector */
for (i = 0; i < iSizeA; i++)
operator[](i) = vecA[i];
/* Put second vector behind the first one */
for (i = 0; i < iSizeB; i++)
operator[](i + iSizeA) = vecB[i];
/* Put third vector behind previous put vectors */
for (i = 0; i < iSizeC; i++)
operator[](i + iSizeAB) = vecC[i];
return *this;
}
/******************************************************************************/
/* CMatlibMatrix class ********************************************************/
/******************************************************************************/
/*
We define: Matrix[row][column]
*/
template<class T>
class CMatlibMatrix
{
public:
/* Construction, Destruction -------------------------------------------- */
CMatlibMatrix() : iRowSize(0), ppData(NULL), eVType(VTY_CONST) {}
CMatlibMatrix(const int iNRowLen, const int iNColLen,
const EVecTy eNTy = VTY_CONST) :
iRowSize(0), ppData(NULL), eVType(eNTy) {Init(iNRowLen, iNColLen);}
CMatlibMatrix(const int iNRowLen, const int iNColLen, const T tIniVal) :
iRowSize(0), ppData(NULL), eVType(VTY_CONST)
{Init(iNRowLen, iNColLen, tIniVal);}
CMatlibMatrix(const CMatlibMatrix<T>& matI);
virtual ~CMatlibMatrix() {if (ppData != NULL) delete[] ppData;}
void Init(const int iNRowLen, const int iNColLen, const T tIniVal = 0);
inline int GetRowSize() const {return iRowSize;}
inline int GetColSize() const
{if (iRowSize > 0) return ppData[0].GetSize(); else return 0;}
/* Operator[] (Regular indices!!!) */
inline CMatlibVector<T>& operator[](int const iPos) const
{_TESTRNGRM(iPos); return ppData[iPos];}
inline CMatlibVector<T>& operator[](int const iPos)
{_TESTRNGWM(iPos); return ppData[iPos];} // For use as l value
/* Operator() */
inline CMatlibVector<T>& operator()(int const iPos) const
{_TESTRNGRM(iPos - 1); return ppData[iPos - 1];}
inline CMatlibVector<T>& operator()(int const iPos)
{_TESTRNGWM(iPos - 1); return ppData[iPos - 1];} // For use as l value
CMatlibMatrix<T> operator()(const int iRowFrom, const int iRowTo,
const int iColFrom, const int iColTo) const;
/* operator= */
inline CMatlibMatrix<T>& operator=(const CMatlibMatrix<CReal>& matI)
{_TESTSIZEM(matI.GetRowSize()); _MATOPCL(= matI[i]);}
inline CMatlibMatrix<CComplex>& operator=(const CMatlibMatrix<CComplex>& matI)
{_TESTSIZEM(matI.GetRowSize()); _MATOPCL(= matI[i]);}
/* operator*= */
inline CMatlibMatrix<T>& operator*=(const CReal& rI)
{_MATOPCL(*= rI);}
inline CMatlibMatrix<CComplex>& operator*=(const CComplex& cI)
{_MATOPCL(*= cI);}
/* operator/= */
inline CMatlibMatrix<T>& operator/=(const CReal& rI)
{_MATOPCL(/= rI);}
inline CMatlibMatrix<CComplex>& operator/=(const CComplex& cI)
{_MATOPCL(/= cI);}
protected:
EVecTy eVType;
int iRowSize;
CMatlibVector<T>* ppData;
};
/* Help functions *************************************************************/
/* operator+ */
inline CMatlibMatrix<CComplex> // cm, cm
operator+(const CMatlibMatrix<CComplex>& cmA, const CMatlibMatrix<CComplex>& cmB)
{
const int iRowSizeA = cmA.GetRowSize();
const int iColSizeA = cmA.GetColSize();
CMatlibMatrix<CComplex> matRet(iRowSizeA, iColSizeA, VTY_TEMP);
for (int j = 0; j < iRowSizeA; j++)
for (int i = 0; i < iColSizeA; i++)
matRet[j][i] = cmA[j][i] + cmB[j][i];
return matRet;
}
/* operator- */
inline CMatlibMatrix<CComplex> // cm, cm
operator-(const CMatlibMatrix<CComplex>& cmA, const CMatlibMatrix<CComplex>& cmB)
{
const int iRowSizeA = cmA.GetRowSize();
const int iColSizeA = cmA.GetColSize();
CMatlibMatrix<CComplex> matRet(iRowSizeA, iColSizeA, VTY_TEMP);
for (int j = 0; j < iRowSizeA; j++)
for (int i = 0; i < iColSizeA; i++)
matRet[j][i] = cmA[j][i] - cmB[j][i];
return matRet;
}
/* operator* */
inline CMatlibVector<CComplex> // cm, cv
operator*(const CMatlibMatrix<CComplex>& cmA, const CMatlibVector<CComplex>& cvB)
{
const int iRowSizeA = cmA.GetRowSize();
const int iSizeB = cvB.GetSize();
CMatlibVector<CComplex> vecRet(iSizeB, VTY_TEMP);
for (int j = 0; j < iRowSizeA; j++)
{
vecRet[j] = (CReal) 0.0;
for (int i = 0; i < iSizeB; i++)
vecRet[j] += cmA[j][i] * cvB[i];
}
return vecRet;
}
/* operator* */
inline CMatlibMatrix<CComplex> // cm, cm
operator*(const CMatlibMatrix<CComplex>& cmA, const CMatlibMatrix<CComplex>& cmB)
{
const int iRowSizeA = cmA.GetRowSize();
const int iRowSizeB = cmB.GetRowSize();
const int iColSizeB = cmB.GetColSize();
CMatlibMatrix<CComplex> matRet(iRowSizeA, iColSizeB, VTY_TEMP);
for (int k = 0; k < iColSizeB; k++)
{
for (int j = 0; j < iRowSizeA; j++)
{
matRet[j][k] = (CReal) 0.0;
for (int i = 0; i < iRowSizeB; i++)
matRet[j][k] += cmA[j][i] * cmB[i][k];
}
}
return matRet;
}
/* operator* */
inline CMatlibMatrix<CComplex> // c, cm
operator*(const CComplex& cA, const CMatlibMatrix<CComplex>& cmB)
{
const int iRowSizeB = cmB.GetRowSize();
const int iColSizeB = cmB.GetColSize();
CMatlibMatrix<CComplex> matRet(iRowSizeB, iColSizeB, VTY_TEMP);
for (int k = 0; k < iColSizeB; k++)
{
for (int j = 0; j < iRowSizeB; j++)
matRet[j][k] = cA * cmB[j][k];
}
return matRet;
}
/* Implementation **************************************************************
(the implementation of template classes must be in the header file!) */
template<class T>
CMatlibMatrix<T>::CMatlibMatrix(const CMatlibMatrix<T>& matI) :
iRowSize(matI.GetRowSize()), ppData(NULL), eVType(VTY_CONST)
{
if (iRowSize > 0)
{
/* Allocate data block for vector */
ppData = new CMatlibVector<T>[iRowSize];
/* Init column vectors and copy */
for (int i = 0; i < iRowSize; i++)
{
ppData[i].Init(matI.GetColSize());
/* Copy entire vector */
ppData[i] = matI[i];
}
}
}
template<class T>
void CMatlibMatrix<T>::Init(const int iNRowLen, const int iNColLen, const T tIniVal)
{
iRowSize = iNRowLen;
/* Allocate data block for vector */
if (iRowSize > 0)
{
if (ppData != NULL)
delete[] ppData;
ppData = new CMatlibVector<T>[iRowSize];
/* Init column vectors and set to init value */
for (int i = 0; i < iRowSize; i++)
ppData[i].Init(iNColLen, tIniVal);
}
}
template<class T> inline
CMatlibMatrix<T> CMatlibMatrix<T>::operator()(const int iRowFrom, const int iRowTo,
const int iColFrom, const int iColTo) const
{
const int iStartRow = iRowFrom - 1;
const int iStartCol = iColFrom - 1;
CMatlibMatrix<T> matRet(iRowTo - iStartRow, iColTo - iStartCol, VTY_TEMP);
for (int j = iStartRow; j < iRowTo; j++)
for (int i = iStartCol; i < iColTo; i++)
matRet[j - iStartRow][i - iStartCol] = operator[](j)[i];
return matRet;
}
/* Include toolboxes after all type definitions */
#include "MatlibStdToolbox.h"
#include "MatlibSigProToolbox.h"
#endif /* _MATLIB_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -