implemented subvector/submatrix with individual index selection

This commit is contained in:
Jiri Pittner 2023-07-26 21:18:57 +02:00
parent 85a514a50e
commit 51a26337c6
6 changed files with 204 additions and 0 deletions

45
mat.cc
View File

@ -763,6 +763,30 @@ const NRMat<T> NRMat<T>::submatrix(const int fromrow, const int torow, const int
return r; return r;
} }
template <typename T>
const NRMat<T> NRMat<T>::submatrix(const NRVec<int> &rows, const NRVec<int> &cols) const
{
NOT_GPU(*this);
const int n = rows.size();
const int m = cols.size();
NRMat<T> r(n,m);
for(int i=0; i<n; ++i)
{
int ii=rows[i];
if(ii<0||ii>=nn) laerror("bad row index in submatrix");
for(int j=0; j<m; ++j)
{
int jj=cols[j];
if(jj<0||jj>=mm) laerror("bad col index in submatrix");
r(i,j) = (*this)(ii,jj);
}
}
return r;
}
/***************************************************************************//** /***************************************************************************//**
* places given matrix as submatrix at given position * places given matrix as submatrix at given position
* @param[in] fromrow row-coordinate of top left corner * @param[in] fromrow row-coordinate of top left corner
@ -799,6 +823,27 @@ void NRMat<T>::storesubmatrix(const int fromrow, const int fromcol, const NRMat
} }
} }
template <typename T>
void NRMat<T>::storesubmatrix(const NRVec<int> &rows, const NRVec<int> &cols, const NRMat &rhs)
{
NOT_GPU(*this);
const int n = rows.size();
const int m = cols.size();
if(rhs.nrows()!=n || rhs.ncols()!=m) laerror("incompatible dimensions in storesubmatrix");
for(int i=0; i<n; ++i)
{
int ii=rows[i];
if(ii<0||ii>=nn) laerror("bad row index in storesubmatrix");
for(int j=0; j<m; ++j)
{
int jj=cols[j];
if(jj<0||jj>=mm) laerror("bad col index in storesubmatrix");
(*this)(ii,jj) = rhs(i,j);
}
}
}
/***************************************************************************//** /***************************************************************************//**
* compute matrix transposition for a principal leading minor * compute matrix transposition for a principal leading minor
* @param[in] _n order of the leading minor * @param[in] _n order of the leading minor

2
mat.h
View File

@ -326,9 +326,11 @@ public:
//! extract specified submatrix //! extract specified submatrix
const NRMat submatrix(const int fromrow, const int torow, const int fromcol, const int tocol) const; const NRMat submatrix(const int fromrow, const int torow, const int fromcol, const int tocol) const;
const NRMat submatrix(const NRVec<int> &rows, const NRVec<int> &cols) const;
//! store given matrix at given position into the current matrix //! store given matrix at given position into the current matrix
void storesubmatrix(const int fromrow, const int fromcol, const NRMat &rhs); void storesubmatrix(const int fromrow, const int fromcol, const NRMat &rhs);
void storesubmatrix(const NRVec<int> &rows, const NRVec<int> &cols, const NRMat &rhs);
//! perform the \b gemm operation //! perform the \b gemm operation
void gemm(const T &beta, const NRMat &a, const char transa, const NRMat &b, const char transb, const T &alpha); void gemm(const T &beta, const NRMat &a, const char transa, const NRMat &b, const char transb, const T &alpha);

112
smat.cc
View File

@ -727,6 +727,118 @@ NRSMat<std::complex<double> >::NRSMat(const NRSMat<double> &rhs, bool imagpart):
#endif #endif
} }
/***************************************************************************//**
* extract block submatrix
* @param[in] from starting position
* @param[in] to final position
* @return extracted block submatrix
******************************************************************************/
template <typename T>
const NRSMat<T> NRSMat<T>::submatrix(const int from, const int to) const
{
#ifdef DEBUG
if(from<0 || from>=nn|| to<0 || to>=nn || from>to){
laerror("invalid submatrix specification");
}
#endif
NOT_GPU(*this);
const int n = to - from + 1;
NRSMat<T> r(n);
for(int i=0; i<n; ++i)
{
int ii= i+from;
if(ii<0||ii>=nn) laerror("bad index in submatrix");
for(int j=0; j<=i; ++j)
{
int jj= j+from;
r(i,j) = (*this)(ii,jj);
}
}
return r;
}
template <typename T>
const NRSMat<T> NRSMat<T>::submatrix(const NRVec<int> &selection) const
{
NOT_GPU(*this);
const int n = selection.size();
NRSMat<T> r(n);
for(int i=0; i<n; ++i)
{
int ii=selection[i];
if(ii<0||ii>=nn) laerror("bad index in submatrix");
for(int j=0; j<=i; ++j)
{
int jj=selection[j];
r(i,j) = (*this)(ii,jj);
}
}
return r;
}
/***************************************************************************//**
* places given matrix as submatrix at given position
* @param[in] fromrow row-coordinate of top left corner
* @param[in] fromcol col-coordinate of top left corner
* @param[in] rhs input matrix
******************************************************************************/
template <typename T>
void NRSMat<T>::storesubmatrix(const int from, const NRSMat &rhs)
{
#ifdef DEBUG
if(from<0 || from>=nn){
laerror("invalid submatrix specification");
}
#endif
NOT_GPU(*this);
const int n = rhs.nrows();
for(int i=0; i<n; ++i)
{
int ii= i+from;
if(ii<0||ii>=nn) laerror("bad index in storesubmatrix");
for(int j=0; j<=i; ++j)
{
int jj= j+from;
(*this)(ii,jj) = rhs(i,j);
}
}
}
template <typename T>
void NRSMat<T>::storesubmatrix(const NRVec<int> &selection, const NRSMat &rhs)
{
NOT_GPU(*this);
const int n = selection.size();
if(rhs.size()!=n) laerror("size mismatch in storesubmatrix");
for(int i=0; i<n; ++i)
{
int ii=selection[i];
if(ii<0||ii>=nn) laerror("bad index in storesubmatrix");
for(int j=0; j<=i; ++j)
{
int jj=selection[j];
(*this)(ii,jj) = rhs(i,j);
}
}
}
/***************************************************************************//** /***************************************************************************//**
* forced instantization in the corresponding object file * forced instantization in the corresponding object file
******************************************************************************/ ******************************************************************************/

9
smat.h
View File

@ -167,6 +167,15 @@ public:
void resize(const int n); void resize(const int n);
void dealloc(void) {resize(0);} void dealloc(void) {resize(0);}
//! extract specified submatrix
const NRSMat submatrix(const int from, const int to) const;
const NRSMat submatrix(const NRVec<int> &selection) const;
//! store given matrix at given position into the current matrix
void storesubmatrix(const int from, const NRSMat &rhs);
void storesubmatrix(const NRVec<int> &selection, const NRSMat &rhs);
inline operator T*(); inline operator T*();
inline operator const T*() const; inline operator const T*() const;
void fprintf(FILE *f, const char *format, const int modulo) const; void fprintf(FILE *f, const char *format, const int modulo) const;

34
vec.cc
View File

@ -837,6 +837,24 @@ const NRVec<T> NRVec<T>::subvector(const int from, const int to) const
return r; return r;
} }
template <typename T>
const NRVec<T> NRVec<T>::subvector(const NRVec<int> &selection) const
{
NOT_GPU(*this);
const int n = selection.size();
NRVec<T> r(n);
for(int i=0; i<n; ++i)
{
int ii=selection[i];
if(ii<0||ii>=nn) laerror("bad row index in subvector");
r[i] = (*this)[ii];
}
return r;
}
/***************************************************************************//** /***************************************************************************//**
* places given vector as subvector at given position * places given vector as subvector at given position
* @param[in] from coordinate * @param[in] from coordinate
@ -866,6 +884,22 @@ void NRVec<T>::storesubvector(const int from, const NRVec &rhs)
} }
template <typename T>
void NRVec<T>::storesubvector(const NRVec<int> &selection, const NRVec &rhs)
{
NOT_GPU(*this);
const int n = selection.size();
if(n!=rhs.size()) laerror("size mismatch in storesubvector");
for(int i=0; i<n; ++i)
{
int ii=selection[i];
if(ii<0||ii>=nn) laerror("bad index in storesubvector");
(*this)[ii] = rhs[i];
}
}
/***************************************************************************//** /***************************************************************************//**
* forced instantization in the corespoding object file * forced instantization in the corespoding object file
******************************************************************************/ ******************************************************************************/

2
vec.h
View File

@ -192,9 +192,11 @@ public:
//! extract specified subvector //! extract specified subvector
const NRVec subvector(const int from, const int to) const; const NRVec subvector(const int from, const int to) const;
const NRVec subvector(const NRVec<int> &selection) const;
//! store given vector at given position into the current vector //! store given vector at given position into the current vector
void storesubvector(const int from, const NRVec &rhs); void storesubvector(const int from, const NRVec &rhs);
void storesubvector(const NRVec<int> &selection, const NRVec &rhs);
//! relational operators //! relational operators
const bool operator!=(const NRVec &rhs) const const bool operator!=(const NRVec &rhs) const