From 2c00ac0b53a207b1bcc303d3e02ded2edb8ca021 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 2 Jun 2016 22:16:37 +0200 Subject: [PATCH] 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 without conversion. --- Eigen/src/Core/ArrayBase.h | 2 -- Eigen/src/Core/DenseBase.h | 2 -- Eigen/src/Core/MatrixBase.h | 2 -- Eigen/src/Core/util/XprHelper.h | 28 --------------- Eigen/src/SparseCore/SparseDenseProduct.h | 18 +++++----- Eigen/src/SparseCore/SparseMatrixBase.h | 3 +- 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 class ArrayBase typedef typename NumTraits::Real RealScalar; typedef DenseBase 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 class DenseBase typedef typename NumTraits::Real RealScalar; typedef internal::special_scalar_op_base > 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 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 struct special_scalar_op_base : public BaseType { - const CwiseUnaryOp, const Derived> - operator*(const OtherScalar& scalar) const - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return CwiseUnaryOp, const Derived> - (*static_cast(this), scalar_multiple2_op(scalar)); - } - - inline friend const CwiseUnaryOp, const Derived> - operator*(const OtherScalar& scalar, const Derived& matrix) - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return static_cast(matrix).operator*(scalar); - } - - const CwiseUnaryOp, const Derived> - operator/(const OtherScalar& scalar) const - { -#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN - EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN -#endif - return CwiseUnaryOp, const Derived> - (*static_cast(this), scalar_quotient2_op(scalar)); - } }; template 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 -struct scalar_product_traits > -{ - enum { - Defined = 1 - }; - typedef typename CwiseUnaryOp, T2>::PlainObject ReturnType; -}; +// -> let's disable it for now as it is conflicting with generic scalar*matrix and matrix*scalar operators +// template +// struct scalar_product_traits > +// { +// enum { +// Defined = 1 +// }; +// typedef typename CwiseUnaryOp, T2>::PlainObject ReturnType; +// }; + template struct sparse_time_dense_product_impl { 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 class SparseMatrixBase { return *static_cast(const_cast(this)); } typedef internal::special_scalar_op_base > 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)); } -/** Overloaded for efficient real matrix times complex scalar value */ -EIGEN_DEVICE_FUNC -inline const ScalarComplexMultipleReturnType -operator*(const std::complex& scalar) const +/** Overloaded for efficiently multipling with compatible scalar types */ +template +EIGEN_DEVICE_FUNC inline +typename internal::enable_if::Defined, + const CwiseUnaryOp, const Derived> >::type +operator*(const T& scalar) const { - return ScalarComplexMultipleReturnType(derived(), internal::scalar_multiple2_op >(scalar)); +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp, const Derived>( + derived(), internal::scalar_multiple2_op(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 >, const Derived> -operator*(const std::complex& scalar, const StorageBaseType& matrix) -{ return matrix*scalar; } +template +EIGEN_DEVICE_FUNC inline friend +typename internal::enable_if::Defined, + const CwiseUnaryOp, const Derived> >::type +operator*(const T& scalar, const StorageBaseType& matrix) +{ +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp, const Derived>( + matrix.derived(), internal::scalar_multiple2_op(scalar) ); +} +template +EIGEN_DEVICE_FUNC inline +typename internal::enable_if::Defined, + const CwiseUnaryOp, const Derived> >::type +operator/(const T& scalar) const +{ +#ifdef EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN + EIGEN_SPECIAL_SCALAR_MULTIPLE_PLUGIN +#endif + return CwiseUnaryOp, const Derived>( + derived(), internal::scalar_quotient2_op(scalar) ); +} template struct CastXpr { typedef typename internal::cast_return_type, const Derived> >::type Type; };