12 #ifndef KRONECKER_TENSOR_PRODUCT_H
13 #define KRONECKER_TENSOR_PRODUCT_H
15 #include "./InternalHeaderCheck.h"
26 template<
typename Derived>
30 typedef typename internal::traits<Derived> Traits;
31 typedef typename Traits::Scalar Scalar;
34 typedef typename Traits::Lhs Lhs;
35 typedef typename Traits::Rhs Rhs;
43 inline Index rows()
const {
return m_A.rows() * m_B.rows(); }
44 inline Index cols()
const {
return m_A.cols() * m_B.cols(); }
52 return m_A.coeff(row / m_B.rows(), col / m_B.cols()) *
53 m_B.coeff(row % m_B.rows(), col % m_B.cols());
62 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
63 return m_A.coeff(i / m_A.size()) * m_B.coeff(i % m_A.size());
67 typename Lhs::Nested m_A;
68 typename Rhs::Nested m_B;
83 template<
typename Lhs,
typename Rhs>
98 template<
typename Dest>
void evalTo(Dest& dst)
const;
116 template<
typename Lhs,
typename Rhs>
131 template<
typename Dest>
void evalTo(Dest& dst)
const;
134 template<
typename Lhs,
typename Rhs>
135 template<
typename Dest>
138 const int BlockRows = Rhs::RowsAtCompileTime,
139 BlockCols = Rhs::ColsAtCompileTime;
140 const Index Br = m_B.rows(),
142 for (
Index i=0; i < m_A.rows(); ++i)
143 for (
Index j=0; j < m_A.cols(); ++j)
147 template<
typename Lhs,
typename Rhs>
148 template<
typename Dest>
151 Index Br = m_B.rows(), Bc = m_B.cols();
152 dst.resize(this->rows(), this->cols());
153 dst.resizeNonZeros(0);
156 typedef typename internal::nested_eval<Lhs,Dynamic>::type Lhs1;
157 typedef internal::remove_all_t<Lhs1> Lhs1Cleaned;
158 const Lhs1 lhs1(m_A);
159 typedef typename internal::nested_eval<Rhs,Dynamic>::type Rhs1;
160 typedef internal::remove_all_t<Rhs1> Rhs1Cleaned;
161 const Rhs1 rhs1(m_B);
164 typedef Eigen::InnerIterator<Lhs1Cleaned> LhsInnerIterator;
165 typedef Eigen::InnerIterator<Rhs1Cleaned> RhsInnerIterator;
171 for (
Index kA=0; kA < m_A.outerSize(); ++kA)
172 for (LhsInnerIterator itA(lhs1,kA); itA; ++itA)
173 nnzA(Dest::IsRowMajor ? itA.row() : itA.col())++;
176 for (
Index kB=0; kB < m_B.outerSize(); ++kB)
177 for (RhsInnerIterator itB(rhs1,kB); itB; ++itB)
178 nnzB(Dest::IsRowMajor ? itB.row() : itB.col())++;
184 for (
Index kA=0; kA < m_A.outerSize(); ++kA)
186 for (
Index kB=0; kB < m_B.outerSize(); ++kB)
188 for (LhsInnerIterator itA(lhs1,kA); itA; ++itA)
190 for (RhsInnerIterator itB(rhs1,kB); itB; ++itB)
192 Index i = itA.row() * Br + itB.row(),
193 j = itA.col() * Bc + itB.col();
194 dst.insert(i,j) = itA.value() * itB.value();
203 template<
typename Lhs_,
typename Rhs_>
206 typedef remove_all_t<Lhs_> Lhs;
207 typedef remove_all_t<Rhs_> Rhs;
209 typedef typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type StorageIndex;
212 Rows = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime),
213 Cols = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime),
214 MaxRows = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime),
215 MaxCols = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime)
218 typedef Matrix<Scalar,Rows,Cols> ReturnType;
221 template<
typename Lhs_,
typename Rhs_>
222 struct traits<KroneckerProductSparse<Lhs_,Rhs_> >
224 typedef MatrixXpr XprKind;
225 typedef remove_all_t<Lhs_> Lhs;
226 typedef remove_all_t<Rhs_> Rhs;
227 typedef typename ScalarBinaryOpTraits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
228 typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
typename traits<Rhs>::StorageKind, scalar_product_op<typename Lhs::Scalar, typename Rhs::Scalar> >::ret StorageKind;
229 typedef typename promote_index_type<typename Lhs::StorageIndex, typename Rhs::StorageIndex>::type StorageIndex;
232 LhsFlags = Lhs::Flags,
233 RhsFlags = Rhs::Flags,
235 RowsAtCompileTime = size_at_compile_time(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime),
236 ColsAtCompileTime = size_at_compile_time(traits<Lhs>::ColsAtCompileTime, traits<Rhs>::ColsAtCompileTime),
237 MaxRowsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime),
238 MaxColsAtCompileTime = size_at_compile_time(traits<Lhs>::MaxColsAtCompileTime, traits<Rhs>::MaxColsAtCompileTime),
240 EvalToRowMajor = (int(LhsFlags) & int(RhsFlags) &
RowMajorBit),
243 Flags = ((int(LhsFlags) | int(RhsFlags)) & HereditaryBits & RemovedBits)
248 typedef SparseMatrix<Scalar, 0, StorageIndex> ReturnType;
272 template<
typename A,
typename B>
299 template<
typename A,
typename B>
static const ConstantReturnType Zero()
TransposeReturnType transpose()
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
The base class of dense and sparse Kronecker product.
Definition: KroneckerTensorProduct.h:28
Scalar coeff(Index row, Index col) const
Definition: KroneckerTensorProduct.h:50
KroneckerProductBase(const Lhs &A, const Rhs &B)
Constructor.
Definition: KroneckerTensorProduct.h:39
Scalar coeff(Index i) const
Definition: KroneckerTensorProduct.h:60
Kronecker tensor product helper class for sparse matrices.
Definition: KroneckerTensorProduct.h:118
void evalTo(Dest &dst) const
Evaluate the Kronecker tensor product.
Definition: KroneckerTensorProduct.h:149
KroneckerProductSparse(const Lhs &A, const Rhs &B)
Constructor.
Definition: KroneckerTensorProduct.h:126
Kronecker tensor product helper class for dense matrices.
Definition: KroneckerTensorProduct.h:85
KroneckerProduct(const Lhs &A, const Rhs &B)
Constructor.
Definition: KroneckerTensorProduct.h:93
void evalTo(Dest &dst) const
Evaluate the Kronecker tensor product.
Definition: KroneckerTensorProduct.h:136
static ConstMapType Map(const Scalar *data)
KroneckerProduct< A, B > kroneckerProduct(const MatrixBase< A > &a, const MatrixBase< B > &b)
Definition: KroneckerTensorProduct.h:273
const unsigned int EvalBeforeNestingBit
const unsigned int RowMajorBit
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index