1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-10 19:20:44 +01:00

MatSample: scalar operators fix

This commit is contained in:
Antonin Portelli 2014-09-18 17:15:27 +01:00
parent 674263dde7
commit 05f5223d31

View File

@ -30,9 +30,9 @@ BEGIN_NAMESPACE
/****************************************************************************** /******************************************************************************
* matrix sample class * * matrix sample class *
******************************************************************************/ ******************************************************************************/
#define SCAL_OP_RETURN(op, x) this->unaryExpr(\ #define SCAL_OP_RETURN(op, s, x) s.unaryExpr(\
std::bind(MatSample<T>::scalar##op,\ std::bind(MatSample<T>::scalar##op,\
std::placeholders::_1, x)) std::placeholders::_1, x))
template <typename T> template <typename T>
class MatSample: public Sample<Mat<T>>, public IoObject class MatSample: public Sample<Mat<T>>, public IoObject
@ -69,15 +69,6 @@ public:
// block types // block types
typedef BlockTemplate<Sample<Mat<T>>> Block; typedef BlockTemplate<Sample<Mat<T>>> Block;
typedef const BlockTemplate<const Sample<Mat<T>>> ConstBlock; typedef const BlockTemplate<const Sample<Mat<T>>> ConstBlock;
private:
static inline Mat<T> scalarMul(const Mat<T> &m, const T &x)
{
return m*x;
}
static inline Mat<T> scalarDiv(const Mat<T> &m, const T &x)
{
return m/x;
}
public: public:
// constructors // constructors
MatSample(void) = default; MatSample(void) = default;
@ -89,15 +80,23 @@ public:
// destructor // destructor
virtual ~MatSample(void) = default; virtual ~MatSample(void) = default;
// assignement operator // assignement operator
MatSample & operator=(Block &sampleBlock); MatSample<T> & operator=(Block &sampleBlock);
MatSample & operator=(Block &&sampleBlock); MatSample<T> & operator=(Block &&sampleBlock);
MatSample & operator=(ConstBlock &sampleBlock); MatSample<T> & operator=(ConstBlock &sampleBlock);
MatSample & operator=(ConstBlock &&sampleBlock); MatSample<T> & operator=(ConstBlock &&sampleBlock);
// product/division by scalar operators (not provided by Eigen) // product/division by scalar operators (not provided by Eigen)
auto operator*=(const T &x)->decltype(SCAL_OP_RETURN(Mul, x)); static inline Mat<T> scalarMul(const Mat<T> &m, const T &x)
auto operator*=(const T &&x)->decltype(SCAL_OP_RETURN(Mul, x)); {
auto operator/=(const T &x)->decltype(SCAL_OP_RETURN(Div, x)); return m*x;
auto operator/=(const T &&x)->decltype(SCAL_OP_RETURN(Div, x)); }
static inline Mat<T> scalarDiv(const Mat<T> &m, const T &x)
{
return m/x;
}
MatSample<T> & operator*=(const T &x);
MatSample<T> & operator*=(const T &&x);
MatSample<T> & operator/=(const T &x);
MatSample<T> & operator/=(const T &&x);
// block access // block access
ConstBlock block(const Index i, const Index j, const Index nRow, ConstBlock block(const Index i, const Index j, const Index nRow,
const Index nCol) const; const Index nCol) const;
@ -112,39 +111,43 @@ public:
// non-member operators // non-member operators
template <typename T> template <typename T>
inline auto operator*(MatSample<T> s, const T &x)->decltype(s *= x) inline auto operator*(MatSample<T> s, const T &x)
->decltype(SCAL_OP_RETURN(Mul, s, x))
{ {
return s *= x; return SCAL_OP_RETURN(Mul, s, x);
} }
template <typename T> template <typename T>
inline auto operator*(MatSample<T> s, const T &&x)->decltype(s *= x) inline auto operator*(MatSample<T> s, const T &&x)
->decltype(SCAL_OP_RETURN(Mul, s, x))
{ {
return s *= x; return SCAL_OP_RETURN(Mul, s, x);
} }
template <typename T> template <typename T>
inline auto operator*(const T &x, MatSample<T> s)->decltype(s *= x) inline auto operator*(const T &x, MatSample<T> s)->decltype(s*x)
{ {
return s *= x; return s*x;
} }
template <typename T> template <typename T>
inline auto operator*(const T &&x, MatSample<T> s)->decltype(s *= x) inline auto operator*(const T &&x, MatSample<T> s)->decltype(s*x)
{ {
return s *= x; return s*x;
} }
template <typename T> template <typename T>
inline auto operator/(MatSample<T> s, const T &x)->decltype(s /= x) inline auto operator/(MatSample<T> s, const T &x)
->decltype(SCAL_OP_RETURN(Div, s, x))
{ {
return s /= x; return SCAL_OP_RETURN(Div, s, x);
} }
template <typename T> template <typename T>
inline auto operator/(MatSample<T> s, const T &&x)->decltype(s /= x) inline auto operator/(MatSample<T> s, const T &&x)
->decltype(SCAL_OP_RETURN(Div, s, x))
{ {
return s /= x; return SCAL_OP_RETURN(Div, s, x);
} }
// type aliases // type aliases
@ -328,27 +331,27 @@ MatSample<T> & MatSample<T>::operator=(ConstBlock &&sampleBlock)
// product/division by scalar operators (not provided by Eigen) //////////////// // product/division by scalar operators (not provided by Eigen) ////////////////
template <typename T> template <typename T>
auto MatSample<T>::operator*=(const T &x)->decltype(SCAL_OP_RETURN(Mul, x)) MatSample<T> & MatSample<T>::operator*=(const T &x)
{ {
return SCAL_OP_RETURN(Mul, x); return *this = (*this)*x;
} }
template <typename T> template <typename T>
auto MatSample<T>::operator*=(const T &&x)->decltype(SCAL_OP_RETURN(Mul, x)) MatSample<T> & MatSample<T>::operator*=(const T &&x)
{ {
return (*this) *= x; return *this = (*this)*x;
} }
template <typename T> template <typename T>
auto MatSample<T>::operator/=(const T &x)->decltype(SCAL_OP_RETURN(Div, x)) MatSample<T> & MatSample<T>::operator/=(const T &x)
{ {
return SCAL_OP_RETURN(Div, x); return *this = (*this)/x;
} }
template <typename T> template <typename T>
auto MatSample<T>::operator/=(const T &&x)->decltype(SCAL_OP_RETURN(Div, x)) MatSample<T> & MatSample<T>::operator/=(const T &&x)
{ {
return (*this) *= x; return *this = (*this)/x;
} }
// block access //////////////////////////////////////////////////////////////// // block access ////////////////////////////////////////////////////////////////