implemented diffabs() useful for checks of results up to a sign

This commit is contained in:
2021-06-30 14:54:35 +02:00
parent 9d0249cdc4
commit cf86493a6f
5 changed files with 121 additions and 52 deletions

35
vec.h
View File

@@ -33,6 +33,20 @@ template <typename T> void lawritemat(FILE *file, const T *a, int r, int c,
template <typename T> class NRPerm;
template <typename T> class CyclePerm;
/***************************************************************************//**
* auxiliary macro to avoid compilation errors for some types
******************************************************************************/
template <typename T>
inline typename LA_traits<T>::normtype MYABS(const T &x) {return abs(x);}
template <> inline unsigned char MYABS(const unsigned char &x) {return x;}
template <> inline unsigned short MYABS(const unsigned short &x) {return x;}
template <> inline unsigned int MYABS(const unsigned int &x) {return x;}
template <> inline unsigned long MYABS(const unsigned long &x) {return x;}
template <> inline unsigned long long MYABS(const unsigned long long &x) {return x;}
/***************************************************************************//**
* static constants used in several cblas-routines
******************************************************************************/
@@ -394,6 +408,8 @@ public:
#endif
}
NRVec diffabs(const NRVec &rhs) const; //difference of absolute values
};
@@ -759,6 +775,25 @@ inline NRVec<T> & NRVec<T>::operator-=(const NRVec<T> &rhs) {
return *this;
}
/***************************************************************************//**
* difference of elements of two vectors in absolute values
* \f[\vec{z}_i = \vec{x}_i-\vec{y}_i\f]
* @param[in] rhs vector \f$\vec{y}\f$
* @return reference to the modified vector
******************************************************************************/
template <typename T>
NRVec<T> NRVec<T>::diffabs(const NRVec<T> &rhs) const {
#ifdef DEBUG
if (nn != rhs.nn) laerror("incompatible dimensions");
#endif
NOT_GPU(*this);
NOT_GPU(rhs);
NRVec<T> r(nn);
for(int i=0; i<nn; ++i) r[i] = MYABS(v[i]) - MYABS(rhs.v[i]);
return r;
}
/***************************************************************************//**
* multiply this general vector \f$\vec{x}\f$ by scalar value \f$\lambda\f$