10#ifndef EIGEN_STRUCTURED_KRONECKER_SPARSE_VIEW_H
11#define EIGEN_STRUCTURED_KRONECKER_SPARSE_VIEW_H
14#include "./InternalHeaderCheck.h"
22template <typename Factor, int Kind = kron_factor_kind<Factor>()>
23struct kron_view_storage_index {
26template <
typename Scalar,
int Options,
typename StorageIndex>
27struct kron_view_storage_index<SparseMatrix<Scalar, Options, StorageIndex>, kKronSparseFactor> {
28 using type = StorageIndex;
30template <
typename LhsMatrix,
typename RhsMatrix>
31struct kron_view_storage_index<KroneckerOperator<LhsMatrix, RhsMatrix>, kKronKroneckerFactor>
32 : promote_index_type<typename kron_view_storage_index<LhsMatrix>::type,
33 typename kron_view_storage_index<RhsMatrix>::type> {};
34template <
typename LhsMatrix,
typename RhsMatrix>
35struct kron_view_storage_index<KroneckerSum<LhsMatrix, RhsMatrix>, kKronSumFactor>
36 : promote_index_type<typename kron_view_storage_index<LhsMatrix>::type,
37 typename kron_view_storage_index<RhsMatrix>::type> {};
39template <
typename OperatorType,
int Options>
40struct traits<KroneckerSparseView<OperatorType, Options>> {
41 using Scalar =
typename OperatorType::Scalar;
42 using StorageKind = Sparse;
43 using XprKind = MatrixXpr;
46 using StorageIndex =
typename kron_view_storage_index<OperatorType>::type;
47 static constexpr int RowsAtCompileTime = OperatorType::RowsAtCompileTime;
48 static constexpr int ColsAtCompileTime = OperatorType::ColsAtCompileTime;
49 static constexpr int MaxRowsAtCompileTime = OperatorType::MaxRowsAtCompileTime;
50 static constexpr int MaxColsAtCompileTime = OperatorType::MaxColsAtCompileTime;
53 static constexpr unsigned int Flags = (unsigned(Options) &
RowMajorBit) | NestByRefBit;
59template <typename Factor, int Options, int Kind = kron_factor_kind<Factor>()>
60struct kron_view_factor {
62 static const Factor& convert(
const Factor& f) {
return f; }
64template <
typename Scalar,
int FactorOptions,
typename StorageIndex,
int Options>
65struct kron_view_factor<SparseMatrix<Scalar, FactorOptions, StorageIndex>, Options, kKronSparseFactor> {
66 using type = SparseMatrix<Scalar, Options & RowMajorBit ? RowMajor : ColMajor, StorageIndex>;
69 static type convert(
const SparseMatrix<Scalar, FactorOptions, StorageIndex>& f) {
71 if (g.innerIndicesAreSorted() != g.outerSize()) g.sortInnerIndices();
75template <
typename LhsMatrix,
typename RhsMatrix,
int Options>
76struct kron_view_factor<KroneckerOperator<LhsMatrix, RhsMatrix>, Options, kKronKroneckerFactor> {
77 using Lhs = kron_view_factor<LhsMatrix, Options>;
78 using Rhs = kron_view_factor<RhsMatrix, Options>;
79 using type = KroneckerOperator<typename Lhs::type, typename Rhs::type>;
80 static type convert(
const KroneckerOperator<LhsMatrix, RhsMatrix>& f) {
81 return type(Lhs::convert(f.lhs()), Rhs::convert(f.rhs()));
84template <
typename LhsMatrix,
typename RhsMatrix,
int Options>
85struct kron_view_factor<KroneckerSum<LhsMatrix, RhsMatrix>, Options, kKronSumFactor> {
86 using Lhs = kron_view_factor<LhsMatrix, Options>;
87 using Rhs = kron_view_factor<RhsMatrix, Options>;
88 using type = KroneckerSum<typename Lhs::type, typename Rhs::type>;
89 static type convert(
const KroneckerSum<LhsMatrix, RhsMatrix>& f) {
90 return type(Lhs::convert(f.lhs()), Rhs::convert(f.rhs()));
97template <typename Factor, bool RowMajor, int Kind = kron_factor_kind<Factor>()>
98class kron_view_iterator {
100 using Scalar =
typename Factor::Scalar;
101 kron_view_iterator(
const Factor& f, Index outer)
102 : m_f(&f), m_outer(outer), m_inner(0), m_end(
RowMajor ? f.cols() : f.rows()) {}
103 explicit operator bool()
const {
return m_inner < m_end; }
104 kron_view_iterator& operator++() {
108 Index index()
const {
return m_inner; }
109 Scalar value()
const {
return RowMajor ? m_f->coeff(m_outer, m_inner) : m_f->coeff(m_inner, m_outer); }
113 Index m_outer, m_inner, m_end;
117template <
typename Factor,
bool RowMajor>
118class kron_view_iterator<Factor,
RowMajor, kKronDiagonalFactor> {
120 using Scalar =
typename Factor::Scalar;
121 kron_view_iterator(
const Factor& f, Index outer) : m_f(&f), m_outer(outer), m_valid(true) {}
122 explicit operator bool()
const {
return m_valid; }
123 kron_view_iterator& operator++() {
127 Index index()
const {
return m_outer; }
128 Scalar value()
const {
return m_f->diagonal().coeff(m_outer); }
136template <
typename Factor,
bool RowMajor>
137class kron_view_iterator<Factor,
RowMajor, kKronIdentityFactor> {
139 using Scalar =
typename Factor::Scalar;
140 kron_view_iterator(
const Factor& f, Index outer)
141 : m_outer(outer), m_valid(outer < numext::mini(f.rows(), f.cols())) {}
142 explicit operator bool()
const {
return m_valid; }
143 kron_view_iterator& operator++() {
147 Index index()
const {
return m_outer; }
148 Scalar value()
const {
return Scalar(1); }
158template <
typename Factor,
bool RowMajor>
159class kron_view_iterator<Factor,
RowMajor, kKronSparseFactor> {
161 using Scalar =
typename Factor::Scalar;
162 static_assert(bool(Factor::IsRowMajor) ==
RowMajor,
"the view converts sparse factors to its storage order");
163 kron_view_iterator(
const Factor& f, Index outer)
164 : m_values(f.valuePtr()),
165 m_indices(f.innerIndexPtr()),
166 m_pos(convert_index<Index>(f.outerIndexPtr()[outer])),
167 m_end(convert_index<Index>(f.outerIndexPtr()[outer + 1])) {
168 eigen_internal_assert(f.isCompressed());
170 explicit operator bool()
const {
return m_pos < m_end; }
171 kron_view_iterator& operator++() {
175 Index index()
const {
return Index(m_indices[m_pos]); }
176 Scalar value()
const {
return m_values[m_pos]; }
179 const Scalar* m_values;
180 const typename Factor::StorageIndex* m_indices;
188template <
typename LhsMatrix,
typename RhsMatrix,
bool RowMajor>
189class kron_view_iterator<KroneckerOperator<LhsMatrix, RhsMatrix>,
RowMajor, kKronKroneckerFactor> {
191 using Factor = KroneckerOperator<LhsMatrix, RhsMatrix>;
192 using Scalar =
typename Factor::Scalar;
193 kron_view_iterator(
const Factor& f, Index outer)
195 m_outerR(outer % outerSize(f.rhs())),
196 m_innerR(
RowMajor ? f.rhs().cols() : f.rhs().rows()),
197 m_itL(f.lhs(), outer / outerSize(f.rhs())),
198 m_itR(f.rhs(), m_outerR),
199 m_nonEmptyR(bool(m_itR)) {}
200 explicit operator bool()
const {
return m_nonEmptyR && bool(m_itL); }
201 kron_view_iterator& operator++() {
205 if (m_itL) m_itR = RhsIterator(*m_rhs, m_outerR);
209 Index index()
const {
return m_itL.index() * m_innerR + m_itR.index(); }
210 Scalar value()
const {
return m_itL.value() * m_itR.value(); }
213 using LhsIterator = kron_view_iterator<LhsMatrix, RowMajor>;
214 using RhsIterator = kron_view_iterator<RhsMatrix, RowMajor>;
215 template <
typename F>
216 static Index outerSize(
const F& f) {
217 return RowMajor ? f.rows() : f.cols();
220 const RhsMatrix* m_rhs;
221 Index m_outerR, m_innerR;
231template <
typename LhsMatrix,
typename RhsMatrix,
bool RowMajor>
232class kron_view_iterator<KroneckerSum<LhsMatrix, RhsMatrix>,
RowMajor, kKronSumFactor> {
234 using Factor = KroneckerSum<LhsMatrix, RhsMatrix>;
235 using Scalar =
typename Factor::Scalar;
236 kron_view_iterator(
const Factor& f, Index outer)
237 : m_n2(f.rhs().rows()),
238 m_outerL(outer / m_n2),
239 m_outerR(outer % m_n2),
240 m_itL(f.lhs(), m_outerL),
241 m_itR(f.rhs(), m_outerR) {}
242 explicit operator bool()
const {
return bool(m_itL) || bool(m_itR); }
243 kron_view_iterator& operator++() {
244 const Index current = index();
245 if (indexL() == current) ++m_itL;
246 if (indexR() == current) ++m_itR;
249 Index index()
const {
return numext::mini(indexL(), indexR()); }
252 Scalar value()
const {
253 const Index l = indexL(), r = indexR();
254 if (l == r)
return m_itL.value() + m_itR.value();
255 return l < r ? m_itL.value() + Scalar(0) : Scalar(0) + m_itR.value();
259 using LhsIterator = kron_view_iterator<LhsMatrix, RowMajor>;
260 using RhsIterator = kron_view_iterator<RhsMatrix, RowMajor>;
261 Index indexL()
const {
return m_itL ? m_itL.index() * m_n2 + m_outerR : NumTraits<Index>::highest(); }
262 Index indexR()
const {
return m_itR ? m_outerL * m_n2 + m_itR.index() : NumTraits<Index>::highest(); }
264 Index m_n2, m_outerL, m_outerR;
271template <typename Factor, int Kind = kron_factor_kind<Factor>()>
272struct kron_view_nonzeros {
273 static Index run(
const Factor& f) {
return kron_factor_ops<Factor>::innerNonZeros(f,
false).sum(); }
275template <
typename LhsMatrix,
typename RhsMatrix>
276struct kron_view_nonzeros<KroneckerOperator<LhsMatrix, RhsMatrix>, kKronKroneckerFactor> {
277 static Index run(
const KroneckerOperator<LhsMatrix, RhsMatrix>& f) {
278 return kron_view_nonzeros<LhsMatrix>::run(f.lhs()) * kron_view_nonzeros<RhsMatrix>::run(f.rhs());
281template <
typename LhsMatrix,
typename RhsMatrix>
282struct kron_view_nonzeros<KroneckerSum<LhsMatrix, RhsMatrix>, kKronSumFactor> {
283 static Index run(
const KroneckerSum<LhsMatrix, RhsMatrix>& f) {
284 return kron_view_nonzeros<LhsMatrix>::run(f.lhs()) * f.rhs().rows() +
285 f.lhs().rows() * kron_view_nonzeros<RhsMatrix>::run(f.rhs());
341template <
typename OperatorType,
int Options>
342class KroneckerSparseView :
public SparseMatrixBase<KroneckerSparseView<OperatorType, Options>> {
343 using ViewFactor = internal::kron_view_factor<OperatorType, Options>;
347 EIGEN_SPARSE_PUBLIC_INTERFACE(KroneckerSparseView)
348 using ViewOperator =
typename ViewFactor::type;
349 static constexpr bool RowMajorView = (unsigned(Options) &
RowMajorBit) != 0;
351 explicit KroneckerSparseView(
const OperatorType& op) : m_op(ViewFactor::convert(op)) {}
353 Index rows()
const {
return m_op.rows(); }
354 Index cols()
const {
return m_op.cols(); }
361 class InnerIterator {
363 InnerIterator(
const KroneckerSparseView& view,
Index outer) : m_it(view.m_op, outer), m_outer(outer) {}
364 InnerIterator& operator++() {
368 operator bool()
const {
return bool(m_it); }
369 Index index()
const {
return m_it.index(); }
370 Index outer()
const {
return m_outer; }
371 Index row()
const {
return RowMajorView ? m_outer : index(); }
372 Index col()
const {
return RowMajorView ? index() : m_outer; }
373 Scalar value()
const {
return m_it.value(); }
376 internal::kron_view_iterator<ViewOperator, RowMajorView> m_it;
386template <
typename OperatorType,
int Options>
388 : evaluator_base<KroneckerSparseView<OperatorType, Options>> {
390 using Scalar =
typename XprType::Scalar;
392 static constexpr unsigned int Flags = traits<XprType>::Flags;
399 explicit evaluator(
const XprType& xpr) : m_view(xpr) {}
400 Index nonZerosEstimate()
const {
401 return kron_view_nonzeros<typename XprType::ViewOperator>::run(m_view.nestedOperator());
404 const XprType& m_view;
A KroneckerOperator or KroneckerSum as a lazy sparse expression.
Definition KroneckerSparseView.h:342
const ViewOperator & nestedOperator() const
Definition KroneckerSparseView.h:358
constexpr unsigned int RowMajorBit
Namespace containing all symbols from the Eigen library.