11#ifndef EIGEN_SPARSE_DIAGONAL_PRODUCT_H
12#define EIGEN_SPARSE_DIAGONAL_PRODUCT_H
15#include "./InternalHeaderCheck.h"
33enum { SDP_AsScalarProduct, SDP_AsCwiseProduct };
35template <
typename SparseXprType,
typename DiagonalCoeffType,
int SDP_Tag,
bool DiagonalOnLeft = true>
36struct sparse_diagonal_product_evaluator;
38template <
typename Lhs,
typename Rhs,
int ProductTag>
39struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseShape>
40 :
public sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType,
41 Rhs::Flags & RowMajorBit ? SDP_AsScalarProduct : SDP_AsCwiseProduct,
43 using XprType = Product<Lhs, Rhs, DefaultProduct>;
45 CoeffReadCost = HugeCost,
51 sparse_diagonal_product_evaluator<Rhs,
typename Lhs::DiagonalVectorType,
52 Rhs::Flags &
RowMajorBit ? SDP_AsScalarProduct : SDP_AsCwiseProduct,
true>;
53 explicit product_evaluator(
const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
56template <
typename Lhs,
typename Rhs,
int ProductTag>
57struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseShape, DiagonalShape>
58 :
public sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>,
59 Lhs::Flags & RowMajorBit ? SDP_AsCwiseProduct : SDP_AsScalarProduct,
61 using XprType = Product<Lhs, Rhs, DefaultProduct>;
63 CoeffReadCost = HugeCost,
69 sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>,
70 Lhs::Flags &
RowMajorBit ? SDP_AsCwiseProduct : SDP_AsScalarProduct,
false>;
71 explicit product_evaluator(
const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal().transpose()) {}
76template <
int Mode,
int ProductOrder,
typename SelfAdjo
intViewType,
typename DiagonalType,
typename Dest>
77struct sparse_selfadjoint_diagonal_product_impl {
78 using MatrixType =
typename SelfAdjointViewType::MatrixTypeNested_;
79 using MatrixEvaluator = evaluator<MatrixType>;
80 using MatrixIterator =
typename MatrixEvaluator::InnerIterator;
81 using StorageIndex =
typename Dest::StorageIndex;
82 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
83 enum { IsFullMode = Mode == int(
Upper |
Lower), IsLowerMode = (Mode & int(
Lower)) ==
int(
Lower) };
85 static void run(Dest& dest,
const SelfAdjointViewType& selfadjoint,
const DiagonalType& diagonal) {
86 MatrixEvaluator matrixEval(selfadjoint.matrix());
87 const Index size = selfadjoint.rows();
90 dest.resize(size, size);
92 for (Index outer = 0; outer < selfadjoint.matrix().outerSize(); ++outer) {
93 for (MatrixIterator it(matrixEval, outer); it; ++it) countEntry(count, it.row(), it.col());
96 Index nnz = count.sum();
97 dest.resizeNonZeros(nnz);
98 dest.outerIndexPtr()[0] = 0;
99 for (Index outer = 0; outer < size; ++outer)
100 dest.outerIndexPtr()[outer + 1] = dest.outerIndexPtr()[outer] + count[outer];
101 for (Index outer = 0; outer < size; ++outer) count[outer] = dest.outerIndexPtr()[outer];
103 for (Index outer = 0; outer < selfadjoint.matrix().outerSize(); ++outer) {
104 for (MatrixIterator it(matrixEval, outer); it; ++it) {
105 const Index row = it.row();
106 const Index col = it.col();
107 if (isStored(row, col)) {
108 insertEntry(dest, count, diagonal, row, col, it.value());
110 if (mirrorsStoredEntry(row, col)) {
111 insertEntry(dest, count, diagonal, col, row, numext::conj(it.value()));
118 static EIGEN_STRONG_INLINE
bool isStored(Index row, Index col) {
119 return IsFullMode || row == col || (IsLowerMode ? row > col : row < col);
122 static EIGEN_STRONG_INLINE
bool mirrorsStoredEntry(Index row, Index col) {
123 return !IsFullMode && row != col && (IsLowerMode ? row > col : row < col);
126 static void countEntry(VectorI& count, Index row, Index col) {
127 if (isStored(row, col)) {
128 ++count[outerIndex(row, col)];
130 if (mirrorsStoredEntry(row, col)) {
131 ++count[outerIndex(col, row)];
135 static EIGEN_STRONG_INLINE StorageIndex outerIndex(Index row, Index col) {
136 return internal::convert_index<StorageIndex>(Dest::IsRowMajor ? row : col);
139 static EIGEN_STRONG_INLINE StorageIndex innerIndex(Index row, Index col) {
140 return internal::convert_index<StorageIndex>(Dest::IsRowMajor ? col : row);
143 template <
typename Coeff>
144 static void insertEntry(Dest& dest, VectorI& count,
const DiagonalType& diagonal, Index row, Index col,
145 const Coeff& coeff) {
146 const StorageIndex outer = outerIndex(row, col);
147 const Index k = count[outer]++;
148 dest.innerIndexPtr()[k] = innerIndex(row, col);
149 EIGEN_IF_CONSTEXPR (ProductOrder ==
OnTheLeft) {
150 dest.valuePtr()[k] = diagonal.coeff(row) * coeff;
152 dest.valuePtr()[k] = coeff * diagonal.coeff(col);
157template <
typename Lhs,
typename Rhs>
158struct materialized_left_sparse_product_evaluator_base
159 :
public evaluator<typename Product<Lhs, typename Rhs::PlainObject, DefaultProduct>::PlainObject> {
160 using XprType = Product<Lhs, Rhs, DefaultProduct>;
161 using PlainObject =
typename XprType::PlainObject;
162 using Base = evaluator<PlainObject>;
164 explicit materialized_left_sparse_product_evaluator_base(
const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
165 internal::construct_at<Base>(
this, m_result);
166 sparse_selfadjoint_diagonal_product_impl<Rhs::Mode,
OnTheLeft, Rhs,
typename Lhs::DiagonalVectorType,
167 PlainObject>::run(m_result, xpr.rhs(), xpr.lhs().diagonal());
171 PlainObject m_result;
174template <
typename Lhs,
typename Rhs>
175struct materialized_right_sparse_product_evaluator_base
176 :
public evaluator<typename Product<typename Lhs::PlainObject, Rhs, DefaultProduct>::PlainObject> {
177 using XprType = Product<Lhs, Rhs, DefaultProduct>;
178 using PlainObject =
typename XprType::PlainObject;
179 using Base = evaluator<PlainObject>;
181 explicit materialized_right_sparse_product_evaluator_base(
const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
182 internal::construct_at<Base>(
this, m_result);
183 sparse_selfadjoint_diagonal_product_impl<Lhs::Mode,
OnTheRight, Lhs,
typename Rhs::DiagonalVectorType,
184 PlainObject>::run(m_result, xpr.lhs(), xpr.rhs().diagonal());
188 PlainObject m_result;
191template <
typename Lhs,
typename Rhs,
int ProductTag>
192struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseTriangularShape>
193 : product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseShape> {
194 using Base = product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseShape>;
198template <
typename Lhs,
typename Rhs,
int ProductTag>
199struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseSelfAdjointShape>
200 : materialized_left_sparse_product_evaluator_base<Lhs, Rhs> {
201 using materialized_left_sparse_product_evaluator_base<Lhs, Rhs>::materialized_left_sparse_product_evaluator_base;
204template <
typename Lhs,
typename Rhs,
int ProductTag>
205struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseTriangularShape, DiagonalShape>
206 : product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseShape, DiagonalShape> {
207 using Base = product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseShape, DiagonalShape>;
211template <
typename Lhs,
typename Rhs,
int ProductTag>
212struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseSelfAdjointShape, DiagonalShape>
213 : materialized_right_sparse_product_evaluator_base<Lhs, Rhs> {
214 using materialized_right_sparse_product_evaluator_base<Lhs, Rhs>::materialized_right_sparse_product_evaluator_base;
217template <
typename SparseXprType,
typename DiagonalCoeffType,
bool DiagonalOnLeft>
218struct sparse_diagonal_product_evaluator<SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct, DiagonalOnLeft> {
220 using SparseXprInnerIterator =
typename evaluator<SparseXprType>::InnerIterator;
221 using SparseScalar =
typename SparseXprType::Scalar;
222 using DiagonalScalar =
typename DiagonalCoeffType::Scalar;
223 using Scalar =
typename std::conditional_t<DiagonalOnLeft, ScalarBinaryOpTraits<DiagonalScalar, SparseScalar>,
224 ScalarBinaryOpTraits<SparseScalar, DiagonalScalar>>::ReturnType;
225 using ProductOp = sparse_scalar_product_op<DiagonalScalar, SparseScalar, !DiagonalOnLeft>;
228 class InnerIterator :
public SparseXprInnerIterator {
230 InnerIterator(
const sparse_diagonal_product_evaluator& xprEval, Index outer)
231 : SparseXprInnerIterator(xprEval.m_sparseXprImpl, outer), m_coeff(xprEval.m_diagCoeffImpl.coeff(outer)) {}
233 EIGEN_STRONG_INLINE Scalar value()
const {
return ProductOp()(m_coeff, SparseXprInnerIterator::value()); }
236 typename DiagonalCoeffType::Scalar m_coeff;
239 sparse_diagonal_product_evaluator(
const SparseXprType& sparseXpr,
const DiagonalCoeffType& diagCoeff)
240 : m_sparseXprImpl(sparseXpr), m_diagCoeffImpl(diagCoeff) {}
242 Index nonZerosEstimate()
const {
return m_sparseXprImpl.nonZerosEstimate(); }
245 evaluator<SparseXprType> m_sparseXprImpl;
246 evaluator<DiagonalCoeffType> m_diagCoeffImpl;
249template <
typename SparseXprType,
typename DiagCoeffType,
bool DiagonalOnLeft>
250struct sparse_diagonal_product_evaluator<SparseXprType, DiagCoeffType, SDP_AsCwiseProduct, DiagonalOnLeft> {
251 using SparseScalar =
typename SparseXprType::Scalar;
252 using DiagonalScalar =
typename DiagCoeffType::Scalar;
253 using Scalar =
typename std::conditional_t<DiagonalOnLeft, ScalarBinaryOpTraits<DiagonalScalar, SparseScalar>,
254 ScalarBinaryOpTraits<SparseScalar, DiagonalScalar>>::ReturnType;
255 using ProductOp = sparse_scalar_product_op<SparseScalar, DiagonalScalar, DiagonalOnLeft>;
256 using StorageIndex =
typename SparseXprType::StorageIndex;
258 using DiagCoeffNested =
259 typename nested_eval<DiagCoeffType, SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime
260 : SparseXprType::ColsAtCompileTime>::type;
262 class InnerIterator {
263 using SparseXprIter =
typename evaluator<SparseXprType>::InnerIterator;
266 InnerIterator(
const sparse_diagonal_product_evaluator& xprEval, Index outer)
267 : m_sparseIter(xprEval.m_sparseXprEval, outer), m_diagCoeffNested(xprEval.m_diagCoeffNested) {}
269 EIGEN_STRONG_INLINE Scalar value()
const {
270 return ProductOp()(m_sparseIter.value(), m_diagCoeffNested.coeff(index()));
272 inline StorageIndex index()
const {
return m_sparseIter.index(); }
273 inline Index outer()
const {
return m_sparseIter.outer(); }
274 inline Index col()
const {
return SparseXprType::IsRowMajor ? m_sparseIter.index() : m_sparseIter.outer(); }
275 inline Index row()
const {
return SparseXprType::IsRowMajor ? m_sparseIter.outer() : m_sparseIter.index(); }
277 EIGEN_STRONG_INLINE InnerIterator& operator++() {
281 inline operator bool()
const {
return m_sparseIter; }
284 SparseXprIter m_sparseIter;
285 DiagCoeffNested m_diagCoeffNested;
288 sparse_diagonal_product_evaluator(
const SparseXprType& sparseXpr,
const DiagCoeffType& diagCoeff)
289 : m_sparseXprEval(sparseXpr), m_diagCoeffNested(diagCoeff) {}
291 Index nonZerosEstimate()
const {
return m_sparseXprEval.nonZerosEstimate(); }
294 evaluator<SparseXprType> m_sparseXprEval;
295 DiagCoeffNested m_diagCoeffNested;
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ OnTheLeft
Definition Constants.h:332
@ OnTheRight
Definition Constants.h:334
constexpr unsigned int RowMajorBit
Definition Constants.h:71