| // This file is part of Eigen, a lightweight C++ template library | |
| // for linear algebra. | |
| // | |
| // Copyright (C) 2011 Kolja Brix <brix@igpm.rwth-aachen.de> | |
| // Copyright (C) 2011 Andreas Platen <andiplaten@gmx.de> | |
| // Copyright (C) 2012 Chen-Pang He <jdh8@ms63.hinet.net> | |
| // | |
| // This Source Code Form is subject to the terms of the Mozilla | |
| // Public License v. 2.0. If a copy of the MPL was not distributed | |
| // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
| // SPDX-License-Identifier: MPL-2.0 | |
| #ifndef KRONECKER_TENSOR_PRODUCT_H | |
| #define KRONECKER_TENSOR_PRODUCT_H | |
| // IWYU pragma: private | |
| #include "./InternalHeaderCheck.h" | |
| namespace Eigen { | |
| /*! | |
| * \ingroup KroneckerProduct_Module | |
| * | |
| * \brief The base class of dense and sparse Kronecker product. | |
| * | |
| * \tparam Derived is the derived type. | |
| */ | |
| template <typename Derived> | |
| class KroneckerProductBase : public ReturnByValue<Derived> { | |
| private: | |
| using Traits = typename internal::traits<Derived>; | |
| using Scalar = typename Traits::Scalar; | |
| protected: | |
| using Lhs = typename Traits::Lhs; | |
| using Rhs = typename Traits::Rhs; | |
| public: | |
| /*! \brief Constructor. Throws std::bad_alloc if a dimension of the product does not fit in Index. */ | |
| KroneckerProductBase(const Lhs& A, const Rhs& B) : m_A(A), m_B(B) { | |
| // rows() and cols() size the destination, which evalTo() then fills with one block of B per coefficient of A: a | |
| // wrapped dimension would give a destination smaller than what is written. Checked in all build modes, and here | |
| // rather than in rows() and cols() because ReturnByValue forwards to those from noexcept functions. | |
| check_dimension_for_overflow(m_A.rows(), m_B.rows()); | |
| check_dimension_for_overflow(m_A.cols(), m_B.cols()); | |
| } | |
| inline Index rows() const { return m_A.rows() * m_B.rows(); } | |
| inline Index cols() const { return m_A.cols() * m_B.cols(); } | |
| /*! | |
| * This overrides ReturnByValue::coeff because this function is | |
| * efficient enough. | |
| */ | |
| Scalar coeff(Index row, Index col) const { | |
| return m_A.coeff(row / m_B.rows(), col / m_B.cols()) * m_B.coeff(row % m_B.rows(), col % m_B.cols()); | |
| } | |
| /*! | |
| * This overrides ReturnByValue::coeff because this function is | |
| * efficient enough. | |
| */ | |
| Scalar coeff(Index i) const { | |
| EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived); | |
| return m_A.coeff(i / m_B.size()) * m_B.coeff(i % m_B.size()); | |
| } | |
| protected: | |
| typename Lhs::Nested m_A; | |
| typename Rhs::Nested m_B; | |
| private: | |
| static void check_dimension_for_overflow(Index a, Index b) { | |
| if (b != 0 && a > NumTraits<Index>::highest() / b) internal::throw_std_bad_alloc(); | |
| } | |
| }; | |
| /*! | |
| * \ingroup KroneckerProduct_Module | |
| * | |
| * \brief Kronecker tensor product helper class for dense matrices | |
| * | |
| * This class is the return value of kroneckerProduct(MatrixBase, | |
| * MatrixBase). Use the function rather than construct this class | |
| * directly to avoid specifying template parameters. | |
| * | |
| * \tparam Lhs Type of the left-hand side, a matrix expression. | |
| * \tparam Rhs Type of the right-hand side, a matrix expression. | |
| */ | |
| template <typename Lhs, typename Rhs> | |
| class KroneckerProduct : public KroneckerProductBase<KroneckerProduct<Lhs, Rhs> > { | |
| private: | |
| using Base = KroneckerProductBase<KroneckerProduct>; | |
| using Base::m_A; | |
| using Base::m_B; | |
| public: | |
| /*! \brief Constructor. */ | |
| KroneckerProduct(const Lhs& A, const Rhs& B) : Base(A, B) {} | |
| /*! \brief Evaluate the Kronecker tensor product. */ | |
| template <typename Dest> | |
| void evalTo(Dest& dst) const; | |
| }; | |
| /*! | |
| * \ingroup KroneckerProduct_Module | |
| * | |
| * \brief Kronecker tensor product helper class for sparse matrices | |
| * | |
| * If at least one of the operands is a sparse matrix expression, | |
| * then this class is returned and evaluates into a sparse matrix. | |
| * | |
| * This class is the return value of kroneckerProduct(EigenBase, | |
| * EigenBase). Use the function rather than construct this class | |
| * directly to avoid specifying template parameters. | |
| * | |
| * \tparam Lhs Type of the left-hand side, a matrix expression. | |
| * \tparam Rhs Type of the right-hand side, a matrix expression. | |
| */ | |
| template <typename Lhs, typename Rhs> | |
| class KroneckerProductSparse : public KroneckerProductBase<KroneckerProductSparse<Lhs, Rhs> > { | |
| private: | |
| using Base = KroneckerProductBase<KroneckerProductSparse>; | |
| using Base::m_A; | |
| using Base::m_B; | |
| public: | |
| /*! \brief Constructor. */ | |
| KroneckerProductSparse(const Lhs& A, const Rhs& B) : Base(A, B) {} | |
| /*! \brief Evaluate the Kronecker tensor product. */ | |
| template <typename Dest> | |
| void evalTo(Dest& dst) const; | |
| }; | |
| template <typename Lhs, typename Rhs> | |
| template <typename Dest> | |
| void KroneckerProduct<Lhs, Rhs>::evalTo(Dest& dst) const { | |
| const int BlockRows = Rhs::RowsAtCompileTime, BlockCols = Rhs::ColsAtCompileTime; | |
| const Index Br = m_B.rows(), Bc = m_B.cols(); | |
| for (Index i = 0; i < m_A.rows(); ++i) | |
| for (Index j = 0; j < m_A.cols(); ++j) | |
| Block<Dest, BlockRows, BlockCols>(dst, i * Br, j * Bc, Br, Bc) = m_A.coeff(i, j) * m_B; | |
| } | |
| template <typename Lhs, typename Rhs> | |
| template <typename Dest> | |
| void KroneckerProductSparse<Lhs, Rhs>::evalTo(Dest& dst) const { | |
| Index Br = m_B.rows(), Bc = m_B.cols(); | |
| dst.resize(this->rows(), this->cols()); | |
| dst.resizeNonZeros(0); | |
| // 1 - evaluate the operands if needed: | |
| using Lhs1 = typename internal::nested_eval<Lhs, Dynamic>::type; | |
| using Lhs1Cleaned = internal::remove_all_t<Lhs1>; | |
| const Lhs1 lhs1(m_A); | |
| using Rhs1 = typename internal::nested_eval<Rhs, Dynamic>::type; | |
| using Rhs1Cleaned = internal::remove_all_t<Rhs1>; | |
| const Rhs1 rhs1(m_B); | |
| // 2 - construct respective iterators | |
| using LhsInnerIterator = Eigen::InnerIterator<Lhs1Cleaned>; | |
| using RhsInnerIterator = Eigen::InnerIterator<Rhs1Cleaned>; | |
| // compute number of non-zeros per innervectors of dst | |
| { | |
| using IndexVector = Matrix<Index, Dynamic, 1>; | |
| IndexVector nnzA = IndexVector::Zero(Dest::IsRowMajor ? m_A.rows() : m_A.cols()); | |
| for (Index kA = 0; kA < m_A.outerSize(); ++kA) | |
| for (LhsInnerIterator itA(lhs1, kA); itA; ++itA) nnzA(Dest::IsRowMajor ? itA.row() : itA.col())++; | |
| IndexVector nnzB = IndexVector::Zero(Dest::IsRowMajor ? m_B.rows() : m_B.cols()); | |
| for (Index kB = 0; kB < m_B.outerSize(); ++kB) | |
| for (RhsInnerIterator itB(rhs1, kB); itB; ++itB) nnzB(Dest::IsRowMajor ? itB.row() : itB.col())++; | |
| // dst stores nnz(A) * nnz(B) entries, which bounds every nnzB(i) * nnzA(j) below. That count must fit Index and | |
| // StorageIndex, which can be wider than Index. A product with more entries throws, as SparseMatrix::insert() does. | |
| using StorageIndex = typename Dest::StorageIndex; | |
| using WideIndex = typename std::common_type<Index, StorageIndex>::type; | |
| const Index maxSize = | |
| static_cast<Index>(numext::mini<WideIndex>(NumTraits<StorageIndex>::highest(), NumTraits<Index>::highest())); | |
| const Index totalA = nnzA.sum(), totalB = nnzB.sum(); | |
| if (totalB > 0 && totalA > maxSize / totalB) internal::throw_std_bad_alloc(); | |
| Matrix<Index, Dynamic, Dynamic, ColMajor> nnzAB = nnzB * nnzA.transpose(); | |
| dst.reserve(nnzAB.reshaped()); | |
| } | |
| for (Index kA = 0; kA < m_A.outerSize(); ++kA) { | |
| for (Index kB = 0; kB < m_B.outerSize(); ++kB) { | |
| for (LhsInnerIterator itA(lhs1, kA); itA; ++itA) { | |
| for (RhsInnerIterator itB(rhs1, kB); itB; ++itB) { | |
| Index i = itA.row() * Br + itB.row(), j = itA.col() * Bc + itB.col(); | |
| dst.insert(i, j) = itA.value() * itB.value(); | |
| } | |
| } | |
| } | |
| } | |
| dst.makeCompressed(); // reserve() and insert() leave the matrix uncompressed | |
| } | |
| namespace internal { | |
| template <typename Lhs_, typename Rhs_> | |
| struct traits<KroneckerProduct<Lhs_, Rhs_> > { | |
| using Lhs = remove_all_t<Lhs_>; | |
| using Rhs = remove_all_t<Rhs_>; | |
| using Scalar = typename ScalarBinaryOpTraits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType; | |
| using StorageIndex = typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type; | |
| enum { | |
| Rows = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime), | |
| Cols = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime), | |
| MaxRows = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime), | |
| MaxCols = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime) | |
| }; | |
| using ReturnType = Matrix<Scalar, Rows, Cols>; | |
| }; | |
| template <typename Lhs_, typename Rhs_> | |
| struct traits<KroneckerProductSparse<Lhs_, Rhs_> > { | |
| using XprKind = MatrixXpr; | |
| using Lhs = remove_all_t<Lhs_>; | |
| using Rhs = remove_all_t<Rhs_>; | |
| using Scalar = typename ScalarBinaryOpTraits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType; | |
| using StorageKind = | |
| typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind, | |
| scalar_product_op<typename Lhs::Scalar, typename Rhs::Scalar> >::ret; | |
| using StorageIndex = typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type; | |
| enum { | |
| LhsFlags = Lhs::Flags, | |
| RhsFlags = Rhs::Flags, | |
| RowsAtCompileTime = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime), | |
| ColsAtCompileTime = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime), | |
| MaxRowsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime), | |
| MaxColsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime), | |
| EvalToRowMajor = (int(LhsFlags) & int(RhsFlags) & RowMajorBit), | |
| RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit), | |
| Flags = ((int(LhsFlags) | int(RhsFlags)) & HereditaryBits & RemovedBits) | EvalBeforeNestingBit, | |
| CoeffReadCost = HugeCost | |
| }; | |
| using ReturnType = SparseMatrix<Scalar, 0, StorageIndex>; | |
| }; | |
| } // 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 | |
| */ | |
| template <typename A, typename B> | |
| KroneckerProduct<A, B> kroneckerProduct(const MatrixBase<A>& a, const MatrixBase<B>& b) { | |
| return KroneckerProduct<A, B>(a.derived(), b.derived()); | |
| } | |
| /*! | |
| * \ingroup KroneckerProduct_Module | |
| * | |
| * Computes Kronecker tensor product of two matrices, at least one of | |
| * which is sparse | |
| * | |
| * \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/sparse matrix a | |
| * \param b Dense/sparse matrix b | |
| * \return Kronecker tensor product of a and b, stored in a sparse | |
| * matrix | |
| */ | |
| template <typename A, typename B> | |
| KroneckerProductSparse<A, B> kroneckerProduct(const EigenBase<A>& a, const EigenBase<B>& b) { | |
| return KroneckerProductSparse<A, B>(a.derived(), b.derived()); | |
| } | |
| } // end namespace Eigen | |
| #endif // KRONECKER_TENSOR_PRODUCT_H |