Implement scalar multiples and division by a scalar as a binary-expression with a constant expression.

This slightly complexifies the type of the expressions and implies that we now have to distinguish between scalar*expr and expr*scalar to catch scalar-multiple expression (e.g., see BlasUtil.h), but this brings several advantages:
- it makes it clear on each side the scalar is applied,
- it clearly reflects that we are dealing with a binary-expression,
- the complexity of the type is hidden through macros defined at the end of Macros.h,
- distinguishing between "scalar op expr" and "expr op scalar" is important to support non commutative fields (like quaternions)
- "scalar op expr" is now fully equivalent to "ConstantExpr(scalar) op expr"
- scalar_multiple_op, scalar_quotient1_op and scalar_quotient2_op are not used anymore in officially supported modules (still used in Tensor)
This commit is contained in:
Gael Guennebaud
2016-06-14 11:26:57 +02:00
parent 39781dc1e2
commit 64fcfd314f
12 changed files with 146 additions and 99 deletions

View File

@@ -1,3 +1,4 @@
/** \returns an expression of the coefficient wise product of \c *this and \a other
*
* \sa MatrixBase::cwiseProduct

View File

@@ -45,3 +45,33 @@ binaryExpr(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other, const Cu
return CwiseBinaryOp<CustomBinaryOp, const Derived, const OtherDerived>(derived(), other.derived(), func);
}
#ifndef EIGEN_PARSED_BY_DOXYGEN
EIGEN_MAKE_SCALAR_BINARY_OP(operator*,product);
#else
/** \returns an expression of \c *this scaled by the scalar factor \a scalar
*
* \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
*/
template<typename T>
const CwiseBinaryOp<internal::scalar_product_op<Scalar,T>,Derived,Constant<Scalar> > operator*(const T& scalar) const;
/** \returns an expression of \c *this scaled by the scalar factor \a scalar
*
* \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
*/
template<typename T> friend
const CwiseBinaryOp<internal::scalar_product_op<T,Scalar>,Constant<Scalar>,Derived> operator*(const T& scalar, const StorageBaseType& expr);
#endif
#ifndef EIGEN_PARSED_BY_DOXYGEN
EIGEN_MAKE_SCALAR_BINARY_OP_ONTHERIGHT(operator/,quotient);
#else
/** \returns an expression of \c *this divided by the scalar value \a scalar
*
* \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
*/
template<typename T>
const CwiseBinaryOp<internal::scalar_quotient_op<Scalar,T>,Derived,Constant<Scalar> > operator/(const T& scalar) const;
#endif

View File

@@ -12,11 +12,6 @@
#ifndef EIGEN_PARSED_BY_DOXYGEN
/** \internal Represents a scalar multiple of an expression */
typedef CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Derived> ScalarMultipleReturnType;
/** \internal Represents a quotient of an expression by a scalar*/
typedef CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived> ScalarQuotient1ReturnType;
/** \internal the return type of conjugate() */
typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
const CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, const Derived>,
@@ -38,7 +33,6 @@ typedef CwiseUnaryOp<internal::scalar_imag_op<Scalar>, const Derived> ImagReturn
typedef CwiseUnaryView<internal::scalar_imag_ref_op<Scalar>, Derived> NonConstImagReturnType;
typedef CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const Derived> NegativeReturnType;
//typedef CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived>
#endif // not EIGEN_PARSED_BY_DOXYGEN
@@ -49,71 +43,6 @@ inline const NegativeReturnType
operator-() const { return NegativeReturnType(derived()); }
/** \returns an expression of \c *this scaled by the scalar factor \a scalar */
EIGEN_DEVICE_FUNC
inline const ScalarMultipleReturnType
operator*(const Scalar& scalar) const
{
return ScalarMultipleReturnType(derived(), internal::scalar_multiple_op<Scalar>(scalar));
}
#ifdef EIGEN_PARSED_BY_DOXYGEN
const ScalarMultipleReturnType operator*(const RealScalar& scalar) const;
#endif
/** \returns an expression of \c *this divided by the scalar value \a scalar */
EIGEN_DEVICE_FUNC
inline const ScalarQuotient1ReturnType
operator/(const Scalar& scalar) const
{
return ScalarQuotient1ReturnType(derived(), internal::scalar_quotient1_op<Scalar>(scalar));
}
/** Overloaded for efficiently multipling with compatible scalar types */
template <typename T>
EIGEN_DEVICE_FUNC inline
typename internal::enable_if<ScalarBinaryOpTraits<Scalar,T>::Defined,
const CwiseUnaryOp<internal::bind2nd_op<internal::scalar_product_op<Scalar,T> >, const Derived> >::type
operator*(const T& scalar) const
{
#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
#endif
typedef internal::bind2nd_op<internal::scalar_product_op<Scalar,T> > op;
return CwiseUnaryOp<op, const Derived>(derived(), op(scalar) );
}
EIGEN_DEVICE_FUNC
inline friend const ScalarMultipleReturnType
operator*(const Scalar& scalar, const StorageBaseType& matrix)
{ return matrix*scalar; }
template <typename T>
EIGEN_DEVICE_FUNC inline friend
typename internal::enable_if<ScalarBinaryOpTraits<T,Scalar>::Defined,
const CwiseUnaryOp<internal::bind1st_op<internal::scalar_product_op<T,Scalar> >, const Derived> >::type
operator*(const T& scalar, const StorageBaseType& matrix)
{
#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
#endif
typedef internal::bind1st_op<internal::scalar_product_op<T,Scalar> > op;
return CwiseUnaryOp<op, const Derived>(matrix.derived(), op(scalar) );
}
template <typename T>
EIGEN_DEVICE_FUNC inline
typename internal::enable_if<ScalarBinaryOpTraits<Scalar,T>::Defined,
const CwiseUnaryOp<internal::bind2nd_op<internal::scalar_quotient_op<Scalar,T> >, const Derived> >::type
operator/(const T& scalar) const
{
#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN
#endif
typedef internal::bind2nd_op<internal::scalar_quotient_op<Scalar,T> > op;
return CwiseUnaryOp<op, const Derived>(derived(), op(scalar) );
}
template<class NewType> struct CastXpr { typedef typename internal::cast_return_type<Derived,const CwiseUnaryOp<internal::scalar_cast_op<Scalar, NewType>, const Derived> >::type Type; };
/** \returns an expression of *this with the \a Scalar type casted to