📄 matrix.h
字号:
} m_nMatStatus = N_MAPPED; } // deallocates a matrix' elements from the free store template < class T > void CWTMatrix<T>::Deallocate() { // has to take account of the state the matrix is in switch (m_nMatStatus) { case N_NOTALLOCATED: // nothing needs to be deallocated break; case N_MAPPED: // delete the array of row pointers#ifdef CC_CWTMTX_ASSUME_BASIC_TYPES free(m_rgrow);#else delete [] m_rgrow;#endif break; case N_ALLOCATED: // delete the contents of the matrix completely#ifdef CC_CWTMTX_ASSUME_BASIC_TYPES // Deallocate space for row pointers and the rows themselves // using ANSI C free(3) function. free(m_rgrow);#else // Deallocate space for row pointers and the rows themselves // using ANSI C++ delete() operator. delete [] *m_rgrow; delete [] m_rgrow;#endif break; }; // bring this matrix in initialized state Initialize(); } template < class T > CWTMatrix<T> CWTMatrix<T>::operator +(const CWTMatrix<T> &mat) const { // copy this and add argument return CWTMatrix( *this ) += mat; } template < class T > CWTMatrix<T> CWTMatrix<T>::operator -(const CWTMatrix<T> &mat) const { // copy this and subtract argument return CWTMatrix( *this ) -= mat; } template < class T > CWTMatrix<T> CWTMatrix<T>::operator -() const { return (*this)*static_cast<const T &>(CWTZero<T>() - CWTUnity<T>()); } template < class T > CWTMatrix<T> CWTMatrix<T>::operator *(const T &value) const { // copy this and multiply by argument return CWTMatrix( *this ) *= value; } template < class T > CWTMatrix<T> CWTMatrix<T>::operator *(const CWTMatrix<T> &mat) const { // create result matrix CWTMatrix matResult( m_crow , mat.m_ccol ); // store (*this)*mat in result matrix matResult.StoreProduct( *this , mat ); return matResult; } // assignment operator template < class T > CWTMatrix<T> & CWTMatrix<T>::operator =(const CWTMatrix<T> &mat) { if (m_nMatStatus == N_NOTALLOCATED) { // if this is not allocated, we'll allocate it on the fly Dimension(mat.m_crow, mat.m_ccol); } // Copy the values from mat to this, excess values in mat are // ignored. If mat has too few elements, garbage will be copied // into remaining elements of this for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] = mat.m_rgrow[irow][icol]; } } return *this; } template < class T > CWTMatrix<T> & CWTMatrix<T>::operator +=(const CWTMatrix<T> &mat) { for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] += mat.m_rgrow[irow][icol]; } } return *this; } template < class T > CWTMatrix<T> & CWTMatrix<T>::operator -=(const CWTMatrix<T> &mat) { for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] -= mat.m_rgrow[irow][icol]; } } return *this; } template < class T > CWTMatrix<T> & CWTMatrix<T>::operator *=(const T &value) { for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] *= value; } } return *this; } template < class T > int CWTMatrix<T>::operator ==(const CWTMatrix<T> &mat) const { if ((m_crow == mat.m_crow) && (m_ccol == mat.m_ccol)) { for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { if (m_rgrow[irow][icol] != mat.m_rgrow[irow][icol]) { return 0; } } } return 1; } else { return 0; } } // stores mat1 + mat2 in this template < class T > void CWTMatrix<T>::StoreSum(const CWTMatrix<T> &mat1, const CWTMatrix<T> &mat2) { // NOTE: it is assumed that this has correct dimensions, // i.e. CWMatrix(mat1.m_crow, mat2.m_ccol) for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] = mat1.m_rgrow[irow][icol] + mat2.m_rgrow[irow][icol]; } } } // stores mat1*mat2 in this template < class T > void CWTMatrix<T>::StoreProduct(const CWTMatrix<T> &mat1, const CWTMatrix<T> &mat2) { // NOTE: it is assumed that this has correct dimensions, // i.e. CWMatrix(mat1.m_crow, mat2.m_ccol) for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] = CWTZero<T>(); for (unsigned icol2 = 0; icol2 < mat1.m_ccol; ++icol2) { m_rgrow[irow][icol] += mat1.m_rgrow[irow][icol2]*mat2.m_rgrow[icol2][icol]; } } } } // mat should fit in this template < class T > void CWTMatrix<T>::StoreAtPosition(unsigned irowStart, unsigned icolStart, const CWTMatrix<T> &mat) { for (unsigned irow = 0; irow < mat.m_crow; ++irow) { for (unsigned icol = 0; icol < mat.m_ccol; ++icol) { m_rgrow[irow + irowStart][icol + icolStart] = mat.m_rgrow[irow][icol]; } } } // stores transpose of mat in this template < class T > void CWTMatrix<T>::StoreTranspose(const CWTMatrix<T> &mat) { // NOTE: it is assumed that this has correct dimensions, // i.e. CWMatrix(mat.m_ccol, mat.m_crow) for (unsigned irow = 0; irow < m_crow; ++irow) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] = mat.m_rgrow[icol][irow]; } } } template < class T > inline void CWTMatrix<T>::Fill(const T &elemFill) { unsigned iEnd = m_crow*m_ccol; for (unsigned i = 0; i < iEnd; ++i) { (*m_rgrow)[i] = elemFill; } } template < class T > void CWTMatrix<T>::InterchangeRows(unsigned irow1, unsigned irow2) { T* rowSav = m_rgrow[irow1]; // switch row pointers m_rgrow[irow1] = m_rgrow[irow2]; m_rgrow[irow2] = rowSav; } template < class T > void CWTMatrix<T>::MultiplyRow(unsigned irow, const T &value) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irow][icol] *= value; } } template < class T > void CWTMatrix<T>::AddRowToRow(unsigned irowSrc, unsigned irowDest, const T &value) { for (unsigned icol = 0; icol < m_ccol; ++icol) { m_rgrow[irowDest][icol] += m_rgrow[irowSrc][icol]*value; } } // // template functions designed work with the base matrix class. // template < class T > inline CWTMatrix<T> operator *(const T &value, const CWTMatrix<T> &mat) { return mat*value; } template < class T > CWTMatrix<T> transpose(const CWTMatrix<T> &mat) { CWTMatrix<T> matTranspose( mat.GetCols(), mat.GetRows() ); matTranspose.StoreTranspose(mat); return matTranspose; } template < class T > ostream & operator <<(ostream &os, const CWTMatrix<T>& mtx) { os << "[" ; for (unsigned i = 0; i < mtx.GetRows(); i++) { if (i != 0) { os << ";"; } for (unsigned j = 0; j < mtx.GetCols(); j++) { os << " " << mtx[i][j]; } } os << " ]"; return os; }}#endif // IG_MATRIX_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -