From 0508a0620b51a9faaffea0c520b5c1840dd32d29 Mon Sep 17 00:00:00 2001 From: Chen-Pang He Date: Mon, 15 Oct 2012 19:45:50 +0800 Subject: [PATCH] Let KroneckerProduct inherit ReturnByValue to eliminate temporary evaluation. It's uncommon to store the product back to one of the operands. --- Eigen/src/Core/MatrixBase.h | 3 - Eigen/src/Core/util/ForwardDeclarations.h | 1 - .../KroneckerProduct/KroneckerTensorProduct.h | 141 +++++++++--------- 3 files changed, 73 insertions(+), 72 deletions(-) diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 25a26304f..7b8850eb7 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -165,9 +165,6 @@ template class MatrixBase template Derived& lazyAssign(const MatrixPowerProductBase& other); - - template - Derived& lazyAssign(const KroneckerProduct& other); #endif // not EIGEN_PARSED_BY_DOXYGEN template diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 61d092ba8..c755ea3c1 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -283,7 +283,6 @@ struct stem_function } // KroneckerProduct module -template class KroneckerProduct; template class KroneckerProductSparse; #ifdef EIGEN2_SUPPORT diff --git a/unsupported/Eigen/src/KroneckerProduct/KroneckerTensorProduct.h b/unsupported/Eigen/src/KroneckerProduct/KroneckerTensorProduct.h index c33d8f0ce..5149566a9 100644 --- a/unsupported/Eigen/src/KroneckerProduct/KroneckerTensorProduct.h +++ b/unsupported/Eigen/src/KroneckerProduct/KroneckerTensorProduct.h @@ -18,59 +18,6 @@ namespace Eigen { -namespace internal { - -template -struct traits > -{ - typedef MatrixXpr XprKind; - typedef typename remove_all<_Lhs>::type Lhs; - typedef typename remove_all<_Rhs>::type Rhs; - typedef typename scalar_product_traits::ReturnType Scalar; - typedef Dense StorageKind; - typedef typename promote_index_type::type Index; - - enum { - RowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::RowsAtCompileTime, traits::RowsAtCompileTime), - ColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::ColsAtCompileTime, traits::ColsAtCompileTime), - MaxRowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxRowsAtCompileTime, traits::MaxRowsAtCompileTime), - MaxColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxColsAtCompileTime, traits::MaxColsAtCompileTime), - Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0) - | EvalBeforeNestingBit | EvalBeforeAssigningBit | NestByRefBit, - CoeffReadCost = Lhs::CoeffReadCost + Rhs::CoeffReadCost + NumTraits::MulCost - }; -}; - -template -struct traits > -{ - typedef MatrixXpr XprKind; - typedef typename remove_all<_Lhs>::type Lhs; - typedef typename remove_all<_Rhs>::type Rhs; - typedef typename scalar_product_traits::ReturnType Scalar; - typedef Sparse StorageKind; - typedef typename promote_index_type::type Index; - - enum { - LhsFlags = Lhs::Flags, - RhsFlags = Rhs::Flags, - - RowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::RowsAtCompileTime, traits::RowsAtCompileTime), - ColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::ColsAtCompileTime, traits::ColsAtCompileTime), - MaxRowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxRowsAtCompileTime, traits::MaxRowsAtCompileTime), - MaxColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxColsAtCompileTime, traits::MaxColsAtCompileTime), - - EvalToRowMajor = (LhsFlags & RhsFlags & RowMajorBit), - RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit), - - Flags = ((LhsFlags | RhsFlags) & HereditaryBits & RemovedBits) - | EvalBeforeNestingBit | EvalBeforeAssigningBit, - CoeffReadCost = Dynamic - }; -}; - -} // end namespace internal - /*! * \brief Kronecker tensor product helper class for dense matrices * @@ -82,12 +29,14 @@ struct traits > * \tparam Rhs Type of the rignt-hand side, a matrix expression. */ template -class KroneckerProduct : public MatrixBase > +class KroneckerProduct : public ReturnByValue > { - public: - typedef MatrixBase Base; - EIGEN_DENSE_PUBLIC_INTERFACE(KroneckerProduct) + private: + typedef ReturnByValue Base; + typedef typename Base::Scalar Scalar; + typedef typename Base::Index Index; + public: /*! \brief Constructor. */ KroneckerProduct(const Lhs& A, const Rhs& B) : m_A(A), m_B(B) @@ -99,13 +48,13 @@ class KroneckerProduct : public MatrixBase > inline Index rows() const { return m_A.rows() * m_B.rows(); } inline Index cols() const { return m_A.cols() * m_B.cols(); } - typename Base::CoeffReturnType coeff(Index row, Index col) const + Scalar coeff(Index row, Index col) const { return m_A.coeff(row / m_A.cols(), col / m_A.rows()) * m_B.coeff(row % m_A.cols(), col % m_A.rows()); } - typename Base::CoeffReturnType coeff(Index i) const + Scalar coeff(Index i) const { EIGEN_STATIC_ASSERT_VECTOR_ONLY(KroneckerProduct); return m_A.coeff(i / m_A.size()) * m_B.coeff(i % m_A.size()); @@ -198,9 +147,71 @@ void KroneckerProductSparse::evalTo(Dest& dst) const } } +namespace internal { + +template +struct traits > +{ + typedef typename remove_all<_Lhs>::type Lhs; + typedef typename remove_all<_Rhs>::type Rhs; + typedef typename scalar_product_traits::ReturnType Scalar; + + enum { + Rows = EIGEN_SIZE_PRODUCT(traits::RowsAtCompileTime, traits::RowsAtCompileTime), + Cols = EIGEN_SIZE_PRODUCT(traits::ColsAtCompileTime, traits::ColsAtCompileTime), + MaxRows = EIGEN_SIZE_PRODUCT(traits::MaxRowsAtCompileTime, traits::MaxRowsAtCompileTime), + MaxCols = EIGEN_SIZE_PRODUCT(traits::MaxColsAtCompileTime, traits::MaxColsAtCompileTime), + CoeffReadCost = Lhs::CoeffReadCost + Rhs::CoeffReadCost + NumTraits::MulCost + }; + + typedef Matrix ReturnType; +}; + +template +struct traits > +{ + typedef MatrixXpr XprKind; + typedef typename remove_all<_Lhs>::type Lhs; + typedef typename remove_all<_Rhs>::type Rhs; + typedef typename scalar_product_traits::ReturnType Scalar; + typedef Sparse StorageKind; + typedef typename promote_index_type::type Index; + + enum { + LhsFlags = Lhs::Flags, + RhsFlags = Rhs::Flags, + + RowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::RowsAtCompileTime, traits::RowsAtCompileTime), + ColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::ColsAtCompileTime, traits::ColsAtCompileTime), + MaxRowsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxRowsAtCompileTime, traits::MaxRowsAtCompileTime), + MaxColsAtCompileTime = EIGEN_SIZE_PRODUCT(traits::MaxColsAtCompileTime, traits::MaxColsAtCompileTime), + + EvalToRowMajor = (LhsFlags & RhsFlags & RowMajorBit), + RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit), + + Flags = ((LhsFlags | RhsFlags) & HereditaryBits & RemovedBits) + | EvalBeforeNestingBit | EvalBeforeAssigningBit, + CoeffReadCost = Dynamic + }; +}; + +} // end namespace internal + /*! + * \ingroup KroneckerProduct_Module + * * Computes Kronecker tensor product of two dense matrices * + * \warning If you want to replace a matrix by its Kronecker product + * with some matrix, do \b NOT do this: + * \code + * A = kroneckerProduct(A,B); // bug!!! caused by aliasing effect + * \endcode + * instead, use eval() to work around this: + * \code + * A = kroneckerProduct(A,B).eval(); + * \endcode + * * \param a Dense matrix a * \param b Dense matrix b * \return Kronecker tensor product of a and b @@ -212,8 +223,10 @@ KroneckerProduct kroneckerProduct(const MatrixBase& a, const MatrixBase< } /*! + * \ingroup KroneckerProduct_Module + * * Computes Kronecker tensor product of two matrices, at least one of - * which is sparse. + * which is sparse * * \param a Dense/sparse matrix a * \param b Dense/sparse matrix b @@ -226,14 +239,6 @@ KroneckerProductSparse kroneckerProduct(const EigenBase& a, const EigenB return KroneckerProductSparse(a.derived(), b.derived()); } -template -template -Derived& MatrixBase::lazyAssign(const KroneckerProduct& other) -{ - other.evalTo(derived()); - return derived(); -} - template template Derived& SparseMatrixBase::operator=(const KroneckerProductSparse& product)