diff options
author | 2016-06-02 22:16:37 +0200 | |
---|---|---|
committer | 2016-06-02 22:16:37 +0200 | |
commit | 2c00ac0b53a207b1bcc303d3e02ded2edb8ca021 (patch) | |
tree | 4d46b297350f496ef8de2f04e4e3c970c3da1d2a | |
parent | 8b6f53222b84d1e4f0f1e86b1d321777b58a28dc (diff) |
Implement generic scalar*expr and expr*scalar operator based on scalar_product_traits.
This is especially useful for custom scalar types, e.g., to enable float*expr<multi_prec> without conversion.
-rw-r--r-- | Eigen/src/Core/ArrayBase.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/DenseBase.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/util/XprHelper.h | 28 | ||||
-rw-r--r-- | Eigen/src/SparseCore/SparseDenseProduct.h | 18 | ||||
-rw-r--r-- | Eigen/src/SparseCore/SparseMatrixBase.h | 3 | ||||
-rw-r--r-- | Eigen/src/plugins/CommonCwiseUnaryOps.h | 44 |
7 files changed, 46 insertions, 53 deletions
diff --git a/Eigen/src/Core/ArrayBase.h b/Eigen/src/Core/ArrayBase.h index 57333af1a..3d9c37bf6 100644 --- a/Eigen/src/Core/ArrayBase.h +++ b/Eigen/src/Core/ArrayBase.h @@ -52,8 +52,6 @@ template<typename Derived> class ArrayBase typedef typename NumTraits<Scalar>::Real RealScalar; typedef DenseBase<Derived> Base; - using Base::operator*; - using Base::operator/; using Base::RowsAtCompileTime; using Base::ColsAtCompileTime; using Base::SizeAtCompileTime; diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index da80e0438..126187aa2 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -75,8 +75,6 @@ template<typename Derived> class DenseBase typedef typename NumTraits<Scalar>::Real RealScalar; typedef internal::special_scalar_op_base<Derived,Scalar,RealScalar, DenseCoeffsBase<Derived> > Base; - using Base::operator*; - using Base::operator/; using Base::derived; using Base::const_cast_derived; using Base::rows; diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 1e66b4e1b..b8b7f458f 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -80,8 +80,6 @@ template<typename Derived> class MatrixBase using Base::operator-=; using Base::operator*=; using Base::operator/=; - using Base::operator*; - using Base::operator/; typedef typename Base::CoeffReturnType CoeffReturnType; typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType; diff --git a/Eigen/src/Core/util/XprHelper.h b/Eigen/src/Core/util/XprHelper.h index 58723d860..8a7e36253 100644 --- a/Eigen/src/Core/util/XprHelper.h +++ b/Eigen/src/Core/util/XprHelper.h @@ -466,34 +466,6 @@ struct special_scalar_op_base : public BaseType template<typename Derived,typename Scalar,typename OtherScalar, typename BaseType> struct special_scalar_op_base<Derived,Scalar,OtherScalar,BaseType,true> : public BaseType { - const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, const Derived> - operator*(const OtherScalar& scalar) const - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, const Derived> - (*static_cast<const Derived*>(this), scalar_multiple2_op<Scalar,OtherScalar>(scalar)); - } - - inline friend const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, const Derived> - operator*(const OtherScalar& scalar, const Derived& matrix) - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return static_cast<const special_scalar_op_base&>(matrix).operator*(scalar); - } - - const CwiseUnaryOp<scalar_quotient2_op<Scalar,OtherScalar>, const Derived> - operator/(const OtherScalar& scalar) const - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return CwiseUnaryOp<scalar_quotient2_op<Scalar,OtherScalar>, const Derived> - (*static_cast<const Derived*>(this), scalar_quotient2_op<Scalar,OtherScalar>(scalar)); - } }; template<typename XprType, typename CastType> struct cast_return_type diff --git a/Eigen/src/SparseCore/SparseDenseProduct.h b/Eigen/src/SparseCore/SparseDenseProduct.h index c9da8a2bb..476796dd7 100644 --- a/Eigen/src/SparseCore/SparseDenseProduct.h +++ b/Eigen/src/SparseCore/SparseDenseProduct.h @@ -72,14 +72,16 @@ struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, t }; // FIXME: what is the purpose of the following specialization? Is it for the BlockedSparse format? -template<typename T1, typename T2/*, int _Options, typename _StrideType*/> -struct scalar_product_traits<T1, Ref<T2/*, _Options, _StrideType*/> > -{ - enum { - Defined = 1 - }; - typedef typename CwiseUnaryOp<scalar_multiple2_op<T1, typename T2::Scalar>, T2>::PlainObject ReturnType; -}; +// -> let's disable it for now as it is conflicting with generic scalar*matrix and matrix*scalar operators +// template<typename T1, typename T2/*, int _Options, typename _StrideType*/> +// struct scalar_product_traits<T1, Ref<T2/*, _Options, _StrideType*/> > +// { +// enum { +// Defined = 1 +// }; +// typedef typename CwiseUnaryOp<scalar_multiple2_op<T1, typename T2::Scalar>, T2>::PlainObject ReturnType; +// }; + template<typename SparseLhsType, typename DenseRhsType, typename DenseResType, typename AlphaType> struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, AlphaType, ColMajor, true> { diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index 2a90f40bf..534c56e71 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -143,8 +143,7 @@ template<typename Derived> class SparseMatrixBase { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); } typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base; - using Base::operator*; - using Base::operator/; + #endif // not EIGEN_PARSED_BY_DOXYGEN #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase diff --git a/Eigen/src/plugins/CommonCwiseUnaryOps.h b/Eigen/src/plugins/CommonCwiseUnaryOps.h index 050bce03c..67ec601b9 100644 --- a/Eigen/src/plugins/CommonCwiseUnaryOps.h +++ b/Eigen/src/plugins/CommonCwiseUnaryOps.h @@ -70,12 +70,18 @@ operator/(const Scalar& scalar) const return ScalarQuotient1ReturnType(derived(), internal::scalar_quotient1_op<Scalar>(scalar)); } -/** Overloaded for efficient real matrix times complex scalar value */ -EIGEN_DEVICE_FUNC -inline const ScalarComplexMultipleReturnType -operator*(const std::complex<Scalar>& scalar) const +/** Overloaded for efficiently multipling with compatible scalar types */ +template <typename T> +EIGEN_DEVICE_FUNC inline +typename internal::enable_if<internal::scalar_product_traits<T,Scalar>::Defined, + const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,T>, const Derived> >::type +operator*(const T& scalar) const { - return ScalarComplexMultipleReturnType(derived(), internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >(scalar)); +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,T>, const Derived>( + derived(), internal::scalar_multiple2_op<Scalar,T>(scalar) ); } EIGEN_DEVICE_FUNC @@ -83,11 +89,31 @@ inline friend const ScalarMultipleReturnType operator*(const Scalar& scalar, const StorageBaseType& matrix) { return matrix*scalar; } -EIGEN_DEVICE_FUNC -inline friend const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived> -operator*(const std::complex<Scalar>& scalar, const StorageBaseType& matrix) -{ return matrix*scalar; } +template <typename T> +EIGEN_DEVICE_FUNC inline friend +typename internal::enable_if<internal::scalar_product_traits<Scalar,T>::Defined, + const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,T>, const Derived> >::type +operator*(const T& scalar, const StorageBaseType& matrix) +{ +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,T>, const Derived>( + matrix.derived(), internal::scalar_multiple2_op<Scalar,T>(scalar) ); +} +template <typename T> +EIGEN_DEVICE_FUNC inline +typename internal::enable_if<internal::scalar_product_traits<Scalar,T>::Defined, + const CwiseUnaryOp<internal::scalar_quotient2_op<Scalar,T>, const Derived> >::type +operator/(const T& scalar) const +{ +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp<internal::scalar_quotient2_op<Scalar,T>, const Derived>( + derived(), internal::scalar_quotient2_op<Scalar,T>(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; }; |