diffabs return normtype entity, elementwise abs() implemented

This commit is contained in:
Jiri Pittner 2021-11-13 19:00:46 +01:00
parent f5d1a13a18
commit b1fea2f1c2
3 changed files with 44 additions and 15 deletions

23
mat.h
View File

@ -409,7 +409,8 @@ public:
#endif #endif
} }
NRMat diffabs(const NRMat &rhs) const; //difference of absolute values NRMat<typename LA_traits<T>::normtype> abs() const; //elementwise absolute values
NRMat<typename LA_traits<T>::normtype> diffabs(const NRMat &rhs) const; //difference of absolute values
}; };
}//namespace }//namespace
@ -1505,19 +1506,25 @@ NRMat<T> & NRMat<T>::operator-=(const NRMat<T> &rhs) {
/*difference of absolute values*/ /*difference of absolute values*/
template <typename T> template <typename T>
NRMat<T> NRMat<T>::diffabs(const NRMat<T> &rhs) const { NRMat<typename LA_traits<T>::normtype> NRMat<T>::diffabs(const NRMat<T> &rhs) const {
#ifdef DEBUG #ifdef DEBUG
if (nn != rhs.nn ||mm!=rhs.mm) laerror("incompatible dimensions"); if (nn != rhs.nn ||mm!=rhs.mm) laerror("incompatible dimensions");
#endif #endif
NOT_GPU(*this); NOT_GPU(*this);
NOT_GPU(rhs); NOT_GPU(rhs);
NRMat<T> r(nn,mm); NRMat<typename LA_traits<T>::normtype> r(nn,mm);
#ifdef MATPTR for(size_t i=0; i< nn; i++) for(size_t j=0; j< mm; j++) r(i,j) = MYABS((*this)(i,j)) - MYABS(rhs(i,j));
for(size_t i=0; i< (size_t)nn*mm; i++) r.v[0][i] = MYABS(v[0][i]) - MYABS(rhs.v[0][i]); return r;
#else }
for(size_t i=0; i<(size_t) nn*mm; i++) r.v[i] = MYABS(v[i]) - MYABS(rhs.v[i]);
#endif /*elementwise absolute values*/
template <typename T>
NRMat<typename LA_traits<T>::normtype> NRMat<T>::abs() const {
NOT_GPU(*this);
NRMat<typename LA_traits<T>::normtype> r(nn,mm);
for(size_t i=0; i< nn; i++) for(size_t j=0; j< mm; j++) r(i,j) = MYABS((*this)(i,j));
return r; return r;
} }

20
smat.h
View File

@ -186,7 +186,8 @@ public:
#endif #endif
} }
NRSMat diffabs(const NRSMat &rhs) const; //difference of absolute values NRSMat<typename LA_traits<T>::normtype> diffabs(const NRSMat &rhs) const; //difference of absolute values
NRSMat<typename LA_traits<T>::normtype> abs() const; //elementwise absolute values
}; };
}//namespace }//namespace
@ -395,18 +396,29 @@ inline NRSMat<T> & NRSMat<T>::operator-=(const T &a) {
/*difference of absolute values*/ /*difference of absolute values*/
template <typename T> template <typename T>
NRSMat<T> NRSMat<T>::diffabs(const NRSMat<T> &rhs) const { NRSMat<typename LA_traits<T>::normtype> NRSMat<T>::diffabs(const NRSMat<T> &rhs) const {
#ifdef DEBUG #ifdef DEBUG
if (nn != rhs.nn) laerror("incompatible dimensions"); if (nn != rhs.nn) laerror("incompatible dimensions");
#endif #endif
NOT_GPU(*this); NOT_GPU(*this);
NOT_GPU(rhs); NOT_GPU(rhs);
NRSMat<T> r(nn); NRSMat<typename LA_traits<T>::normtype> r(nn);
for(int i=0; i<NN2; ++i) r.v[i] = MYABS(v[i]) - MYABS(rhs.v[i]); for(int i=0; i<nn; ++i) for(int j=0; j<=i; ++j) r(i,j) = MYABS((*this)(i,j)) - MYABS(rhs(i,j));
return r; return r;
} }
/*elementwise absolute values*/
template <typename T>
NRSMat<typename LA_traits<T>::normtype> NRSMat<T>::abs() const {
NOT_GPU(*this);
NRSMat<typename LA_traits<T>::normtype> r(nn);
for(int i=0; i<nn; ++i) for(int j=0; j<=i; ++j) r(i,j) = MYABS((*this)(i,j));
return r;
}
/***************************************************************************//** /***************************************************************************//**
* add up this real symmetric matrix with given symmetric matrix * add up this real symmetric matrix with given symmetric matrix

16
vec.h
View File

@ -426,7 +426,8 @@ public:
} }
NRVec diffabs(const NRVec &rhs) const; //difference of absolute values NRVec<typename LA_traits<T>::normtype> diffabs(const NRVec &rhs) const; //difference of absolute values
NRVec<typename LA_traits<T>::normtype> abs() const; //element-wise absolute values
}; };
@ -834,18 +835,27 @@ inline NRVec<T> & NRVec<T>::operator-=(const NRVec<T> &rhs) {
* @return reference to the modified vector * @return reference to the modified vector
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
NRVec<T> NRVec<T>::diffabs(const NRVec<T> &rhs) const { NRVec<typename LA_traits<T>::normtype> NRVec<T>::diffabs(const NRVec<T> &rhs) const {
#ifdef DEBUG #ifdef DEBUG
if (nn != rhs.nn) laerror("incompatible dimensions"); if (nn != rhs.nn) laerror("incompatible dimensions");
#endif #endif
NOT_GPU(*this); NOT_GPU(*this);
NOT_GPU(rhs); NOT_GPU(rhs);
NRVec<T> r(nn); NRVec<typename LA_traits<T>::normtype> r(nn);
for(int i=0; i<nn; ++i) r[i] = MYABS(v[i]) - MYABS(rhs.v[i]); for(int i=0; i<nn; ++i) r[i] = MYABS(v[i]) - MYABS(rhs.v[i]);
return r; return r;
} }
template <typename T>
NRVec<typename LA_traits<T>::normtype> NRVec<T>::abs() const {
NOT_GPU(*this);
NRVec<typename LA_traits<T>::normtype> r(nn);
for(int i=0; i<nn; ++i) r[i] = MYABS(v[i]);
return r;
}
/***************************************************************************//** /***************************************************************************//**
* multiply this general vector \f$\vec{x}\f$ by scalar value \f$\lambda\f$ * multiply this general vector \f$\vec{x}\f$ by scalar value \f$\lambda\f$