diff --git a/Eigen/src/Core/PermutationMatrix.h b/Eigen/src/Core/PermutationMatrix.h index 9add80c54..61aa0ce31 100644 --- a/Eigen/src/Core/PermutationMatrix.h +++ b/Eigen/src/Core/PermutationMatrix.h @@ -288,6 +288,10 @@ class PermutationMatrix : public PermutationBase Traits; public: +#ifdef EIGEN_TEST_EVALUATORS + typedef const PermutationMatrix& Nested; +#endif + #ifndef EIGEN_PARSED_BY_DOXYGEN typedef typename Traits::IndicesType IndicesType; #endif @@ -461,6 +465,22 @@ class Map, struct PermutationStorage {}; +#ifdef EIGEN_TEST_EVALUATORS + +// storage type of product of permutation wrapper with dense + +namespace internal { + +template<> struct promote_storage_type +{ typedef Dense ret; }; + +template<> struct promote_storage_type +{ typedef Dense ret; }; + +} // end namespace internal + +#endif // EIGEN_TEST_EVALUATORS + template class TranspositionsWrapper; namespace internal { template @@ -473,8 +493,13 @@ struct traits > enum { RowsAtCompileTime = _IndicesType::SizeAtCompileTime, ColsAtCompileTime = _IndicesType::SizeAtCompileTime, - MaxRowsAtCompileTime = IndicesType::MaxRowsAtCompileTime, +#ifdef EIGEN_TEST_EVALUATORS + MaxRowsAtCompileTime = IndicesType::MaxSizeAtCompileTime, + MaxColsAtCompileTime = IndicesType::MaxSizeAtCompileTime, +#else + MaxRowsAtCompileTime = IndicesType::MaxRowsAtCompileTime, // is this a bug in Eigen 2.2 ? MaxColsAtCompileTime = IndicesType::MaxColsAtCompileTime, +#endif Flags = 0 #ifndef EIGEN_TEST_EVALUATORS , @@ -508,6 +533,37 @@ class PermutationWrapper : public PermutationBase +EIGEN_DEVICE_FUNC +const Product +operator*(const MatrixBase &matrix, + const PermutationBase& permutation) +{ + return Product + (matrix.derived(), permutation.derived()); +} + +/** \returns the matrix with the permutation applied to the rows. + */ +template +EIGEN_DEVICE_FUNC +const Product +operator*(const PermutationBase &permutation, + const MatrixBase& matrix) +{ + return Product + (permutation.derived(), matrix.derived()); +} + +#else // EIGEN_TEST_EVALUATORS + /** \returns the matrix with the permutation applied to the columns. */ template @@ -533,6 +589,8 @@ operator*(const PermutationBase &permutation, (permutation.derived(), matrix.derived()); } +#endif // EIGEN_TEST_EVALUATORS + namespace internal { template @@ -662,6 +720,28 @@ class Transpose > DenseMatrixType toDenseMatrix() const { return *this; } +#ifdef EIGEN_TEST_EVALUATORS + + /** \returns the matrix with the inverse permutation applied to the columns. + */ + template friend + const Product + operator*(const MatrixBase& matrix, const Transpose& trPerm) + { + return Product(matrix.derived(), trPerm.derived()); + } + + /** \returns the matrix with the inverse permutation applied to the rows. + */ + template + const Product + operator*(const MatrixBase& matrix) const + { + return Product(*this, matrix.derived()); + } + +#else // EIGEN_TEST_EVALUATORS + /** \returns the matrix with the inverse permutation applied to the columns. */ template friend @@ -680,6 +760,8 @@ class Transpose > return internal::permut_matrix_product_retval(m_permutation, matrix.derived()); } +#endif // EIGEN_TEST_EVALUATORS + const PermutationType& nestedPermutation() const { return m_permutation; } protected: @@ -692,6 +774,38 @@ const PermutationWrapper MatrixBase::asPermutation() con return derived(); } +#ifdef EIGEN_TEST_EVALUATORS +namespace internal { + +// TODO currently a permutation matrix expression has the form PermutationMatrix or PermutationWrapper +// or their transpose; in the future shape should be defined by the expression traits +template +struct evaluator_traits > +{ + typedef typename storage_kind_to_evaluator_kind::Kind Kind; + typedef PermutationShape Shape; + static const int AssumeAliasing = 0; +}; + +template +struct evaluator_traits > +{ + typedef typename storage_kind_to_evaluator_kind::Kind Kind; + typedef PermutationShape Shape; + static const int AssumeAliasing = 0; +}; + +template +struct evaluator_traits > > +{ + typedef typename storage_kind_to_evaluator_kind::Kind Kind; + typedef PermutationShape Shape; + static const int AssumeAliasing = 0; +}; + +} // end namespace internal +#endif // EIGEN_TEST_EVALUATORS + } // end namespace Eigen #endif // EIGEN_PERMUTATIONMATRIX_H diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index 2785847a6..626b737c7 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -29,8 +29,30 @@ template class Pro * */ -// Use ProductReturnType to get correct traits, in particular vectorization flags + namespace internal { + +// Determine the scalar of Product. This is normally the same as Lhs::Scalar times +// Rhs::Scalar, but product with permutation matrices inherit the scalar of the other factor. +template::Shape, + typename RhsShape = typename evaluator_traits::Shape > +struct product_result_scalar +{ + typedef typename scalar_product_traits::ReturnType Scalar; +}; + +template +struct product_result_scalar +{ + typedef typename Rhs::Scalar Scalar; +}; + +template + struct product_result_scalar +{ + typedef typename Lhs::Scalar Scalar; +}; + template struct traits > { @@ -39,7 +61,7 @@ struct traits > typedef MatrixXpr XprKind; - typedef typename scalar_product_traits::ReturnType Scalar; + typedef typename product_result_scalar::Scalar Scalar; typedef typename promote_storage_type::StorageKind, typename traits::StorageKind>::ret StorageKind; typedef typename promote_index_type::Index, diff --git a/Eigen/src/Core/ProductEvaluators.h b/Eigen/src/Core/ProductEvaluators.h index 1159c2f44..7298c51b1 100644 --- a/Eigen/src/Core/ProductEvaluators.h +++ b/Eigen/src/Core/ProductEvaluators.h @@ -885,6 +885,93 @@ struct product_evaluator, ProductTag, DenseShape, }; +/*************************************************************************** +* Products with permutation matrices +***************************************************************************/ + +template +struct generic_product_impl +{ + template + static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) + { + permut_matrix_product_retval pmpr(lhs, rhs); + pmpr.evalTo(dst); + } +}; + +template +struct generic_product_impl +{ + template + static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) + { + permut_matrix_product_retval pmpr(rhs, lhs); + pmpr.evalTo(dst); + } +}; + +template +struct generic_product_impl, Rhs, PermutationShape, DenseShape, ProductType> +{ + template + static void evalTo(Dest& dst, const Transpose& lhs, const Rhs& rhs) + { + permut_matrix_product_retval pmpr(lhs.nestedPermutation(), rhs); + pmpr.evalTo(dst); + } +}; + +template +struct generic_product_impl, DenseShape, PermutationShape, ProductType> +{ + template + static void evalTo(Dest& dst, const Lhs& lhs, const Transpose& rhs) + { + permut_matrix_product_retval pmpr(rhs.nestedPermutation(), lhs); + pmpr.evalTo(dst); + } +}; + +// TODO: left/right and self-adj/symmetric/permutation look the same ... Too much boilerplate? +template +struct product_evaluator, ProductTag, PermutationShape, DenseShape, typename Lhs::Scalar, typename Rhs::Scalar> + : public evaluator::PlainObject>::type +{ + typedef Product XprType; + typedef typename XprType::PlainObject PlainObject; + typedef typename evaluator::type Base; + + product_evaluator(const XprType& xpr) + : m_result(xpr.rows(), xpr.cols()) + { + ::new (static_cast(this)) Base(m_result); + generic_product_impl::evalTo(m_result, xpr.lhs(), xpr.rhs()); + } + +protected: + PlainObject m_result; +}; + +template +struct product_evaluator, ProductTag, DenseShape, PermutationShape, typename Lhs::Scalar, typename Rhs::Scalar> + : public evaluator::PlainObject>::type +{ + typedef Product XprType; + typedef typename XprType::PlainObject PlainObject; + typedef typename evaluator::type Base; + + product_evaluator(const XprType& xpr) + : m_result(xpr.rows(), xpr.cols()) + { + ::new (static_cast(this)) Base(m_result); + generic_product_impl::evalTo(m_result, xpr.lhs(), xpr.rhs()); + } + +protected: + PlainObject m_result; +}; + } // end namespace internal } // end namespace Eigen diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h index 64efac8e9..aae8625d1 100644 --- a/Eigen/src/Core/util/Constants.h +++ b/Eigen/src/Core/util/Constants.h @@ -447,6 +447,7 @@ struct DiagonalShape { static std::string debugName() { return "DiagonalShape struct BandShape { static std::string debugName() { return "BandShape"; } }; struct TriangularShape { static std::string debugName() { return "TriangularShape"; } }; struct SelfAdjointShape { static std::string debugName() { return "SelfAdjointShape"; } }; +struct PermutationShape { static std::string debugName() { return "PermutationShape"; } }; struct SparseShape { static std::string debugName() { return "SparseShape"; } }; #endif