11#ifndef EIGEN_SPARSE_SELFADJOINTVIEW_H
12#define EIGEN_SPARSE_SELFADJOINTVIEW_H
15#include "./InternalHeaderCheck.h"
35template <
typename MatrixType,
unsigned int Mode>
36struct traits<SparseSelfAdjointView<MatrixType, Mode> > : traits<MatrixType> {};
38template <
int SrcMode,
int DstMode,
bool NonHermitian,
typename MatrixType,
int DestOrder>
39void permute_symm_to_symm(
40 const MatrixType& mat,
41 SparseMatrix<typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex>& _dest,
42 const typename MatrixType::StorageIndex* perm = 0);
44template <
int Mode,
bool NonHermitian,
typename MatrixType,
int DestOrder>
45void permute_symm_to_fullsymm(
46 const MatrixType& mat,
47 SparseMatrix<typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex>& _dest,
48 const typename MatrixType::StorageIndex* perm = 0);
52template <
typename MatrixType,
unsigned int Mode_>
53class SparseSelfAdjointView :
public EigenBase<SparseSelfAdjointView<MatrixType, Mode_> > {
58 RowsAtCompileTime = internal::traits<SparseSelfAdjointView>::RowsAtCompileTime,
59 ColsAtCompileTime = internal::traits<SparseSelfAdjointView>::ColsAtCompileTime
63 using Scalar =
typename MatrixType::Scalar;
64 using StorageIndex =
typename MatrixType::StorageIndex;
66 using MatrixTypeNested =
typename internal::ref_selector<MatrixType>::non_const_type;
67 using MatrixTypeNested_ = internal::remove_all_t<MatrixTypeNested>;
71 explicit inline SparseSelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {
72 eigen_assert(rows() == cols() &&
"SelfAdjointView is only for squared matrices");
75 inline Index rows()
const {
return m_matrix.rows(); }
76 inline Index cols()
const {
return m_matrix.cols(); }
79 const MatrixTypeNested_& matrix()
const {
return m_matrix; }
80 std::remove_reference_t<MatrixTypeNested>& matrix() {
return m_matrix; }
89 template <
typename OtherDerived>
101 template <
typename OtherDerived>
103 const SparseSelfAdjointView& rhs) {
108 template <
typename OtherDerived>
113 template <
typename OtherDerived>
119 template <
typename OtherDerived>
121 const SparseSelfAdjointView& rhs) {
125 template <
typename OtherDerived>
133 PlainObject
operator*(
const Scalar& s)
const {
return s * *
this; }
135 friend PlainObject
operator*(
const Scalar& s,
const SparseSelfAdjointView& mat) {
136 PlainObject res(mat);
151 template <
typename DerivedU>
156 SparseSymmetricPermutationProduct<MatrixTypeNested_, Mode>
twistedBy(
158 return SparseSymmetricPermutationProduct<MatrixTypeNested_, Mode>(m_matrix, perm);
161 template <
typename SrcMatrixType,
int SrcMode>
162 SparseSelfAdjointView& operator=(
const SparseSymmetricPermutationProduct<SrcMatrixType, SrcMode>& permutedMatrix) {
163 internal::call_assignment_no_alias_no_transpose(*
this, permutedMatrix);
167 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView& src) {
168 PermutationMatrix<Dynamic, Dynamic, StorageIndex> pnull;
169 return *
this = src.twistedBy(pnull);
173 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SparseSelfAdjointView)
175 template <
typename SrcMatrixType,
unsigned int SrcMode>
176 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView<SrcMatrixType, SrcMode>& src) {
177 PermutationMatrix<Dynamic, Dynamic, StorageIndex> pnull;
178 return *
this = src.twistedBy(pnull);
182 EIGEN_ONLY_USED_FOR_DEBUG(rows);
183 EIGEN_ONLY_USED_FOR_DEBUG(cols);
184 eigen_assert(rows == this->rows() && cols == this->cols() &&
185 "SparseSelfadjointView::resize() does not actually allow to resize.");
189 MatrixTypeNested m_matrix;
192 template <
typename Dest>
193 void evalTo(Dest&)
const;
200template <
typename Derived>
201template <
unsigned int UpLo>
203SparseMatrixBase<Derived>::selfadjointView()
const {
207template <
typename Derived>
208template <
unsigned int UpLo>
210SparseMatrixBase<Derived>::selfadjointView() {
218template <
typename MatrixType,
unsigned int Mode>
219template <
typename DerivedU>
223 if (alpha == Scalar(0))
224 m_matrix = tmp.template triangularView<Mode>();
226 m_matrix += alpha * tmp.template triangularView<Mode>();
237template <
typename MatrixType,
unsigned int Mode>
238struct evaluator_traits<SparseSelfAdjointView<MatrixType, Mode> > {
239 using Kind =
typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind;
240 using Shape = SparseSelfAdjointShape;
243struct SparseSelfAdjoint2Sparse {};
246struct AssignmentKind<SparseShape, SparseSelfAdjointShape> {
247 using Kind = SparseSelfAdjoint2Sparse;
250struct AssignmentKind<SparseSelfAdjointShape, SparseShape> {
251 using Kind = Sparse2Sparse;
254template <
typename DstXprType,
typename SrcXprType,
typename Functor>
255struct Assignment<DstXprType, SrcXprType, Functor, SparseSelfAdjoint2Sparse> {
256 using StorageIndex =
typename DstXprType::StorageIndex;
257 using AssignOpType = internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>;
259 template <
typename DestScalar,
int StorageOrder>
260 static void run(SparseMatrix<DestScalar, StorageOrder, StorageIndex>& dst,
const SrcXprType& src,
261 const AssignOpType& ) {
262 internal::permute_symm_to_fullsymm<SrcXprType::Mode, false>(src.matrix(), dst);
267 template <
typename DestScalar,
int StorageOrder,
typename AssignFunc>
268 static void run(SparseMatrix<DestScalar, StorageOrder, StorageIndex>& dst,
const SrcXprType& src,
269 const AssignFunc& func) {
270 SparseMatrix<DestScalar, StorageOrder, StorageIndex> tmp(src.rows(), src.cols());
271 run(tmp, src, AssignOpType());
272 call_assignment_no_alias_no_transpose(dst, tmp, func);
275 template <
typename DestScalar,
int StorageOrder>
276 static void run(SparseMatrix<DestScalar, StorageOrder, StorageIndex>& dst,
const SrcXprType& src,
277 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& ) {
278 SparseMatrix<DestScalar, StorageOrder, StorageIndex> tmp(src.rows(), src.cols());
279 run(tmp, src, AssignOpType());
283 template <
typename DestScalar,
int StorageOrder>
284 static void run(SparseMatrix<DestScalar, StorageOrder, StorageIndex>& dst,
const SrcXprType& src,
285 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& ) {
286 SparseMatrix<DestScalar, StorageOrder, StorageIndex> tmp(src.rows(), src.cols());
287 run(tmp, src, AssignOpType());
300template <
int Mode,
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
301inline void sparse_selfadjoint_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
302 const AlphaType& alpha) {
303 EIGEN_ONLY_USED_FOR_DEBUG(alpha);
305 using SparseLhsTypeNested =
typename internal::nested_eval<SparseLhsType, DenseRhsType::MaxColsAtCompileTime>::type;
306 using SparseLhsTypeNestedCleaned = internal::remove_all_t<SparseLhsTypeNested>;
307 using LhsEval = evaluator<SparseLhsTypeNestedCleaned>;
308 using LhsIterator =
typename LhsEval::InnerIterator;
309 using LhsScalar =
typename SparseLhsType::Scalar;
314 ((Mode &
Lower) && LhsIsRowMajor),
315 ProcessSecondHalf = !ProcessFirstHalf
318 SparseLhsTypeNested lhs_nested(lhs);
319 LhsEval lhsEval(lhs_nested);
322 for (Index k = 0; k < rhs.cols(); ++k) {
323 for (Index j = 0; j < lhs.outerSize(); ++j) {
324 LhsIterator i(lhsEval, j);
326 EIGEN_IF_CONSTEXPR (ProcessSecondHalf) {
327 while (i && i.index() < j) ++i;
328 if (i && i.index() == j) {
329 res.coeffRef(j, k) += alpha * i.value() * rhs.coeff(j, k);
335 typename ScalarBinaryOpTraits<AlphaType, typename DenseRhsType::Scalar>::ReturnType rhs_j(alpha * rhs(j, k));
337 typename DenseResType::Scalar res_j(0);
338 for (; (ProcessFirstHalf ? i && i.index() < j : i); ++i) {
339 LhsScalar lhs_ij = i.value();
340 EIGEN_IF_CONSTEXPR (!LhsIsRowMajor) {
341 lhs_ij = numext::conj(lhs_ij);
343 res_j += lhs_ij * rhs.coeff(i.index(), k);
344 res(i.index(), k) += numext::conj(lhs_ij) * rhs_j;
346 res.coeffRef(j, k) += alpha * res_j;
349 if (ProcessFirstHalf && i && (i.index() == j)) res.coeffRef(j, k) += alpha * i.value() * rhs.coeff(j, k);
354template <
typename LhsView,
typename Rhs,
int ProductType>
355struct generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType>
356 : generic_product_impl_base<LhsView, Rhs,
357 generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType> > {
358 template <
typename Dest>
359 static void scaleAndAddTo(Dest& dst,
const LhsView& lhsView,
const Rhs& rhs,
const typename Dest::Scalar& alpha) {
360 using Lhs =
typename LhsView::MatrixTypeNested_;
361 using LhsNested =
typename nested_eval<Lhs, Dynamic>::type;
362 using RhsNested =
typename nested_eval<Rhs, Dynamic>::type;
363 LhsNested lhsNested(lhsView.matrix());
364 RhsNested rhsNested(rhs);
366 internal::sparse_selfadjoint_time_dense_product<LhsView::Mode>(lhsNested, rhsNested, dst, alpha);
370template <
typename Lhs,
typename RhsView,
int ProductType>
371struct generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType>
372 : generic_product_impl_base<Lhs, RhsView,
373 generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType> > {
374 template <
typename Dest>
375 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const RhsView& rhsView,
const typename Dest::Scalar& alpha) {
376 using Rhs =
typename RhsView::MatrixTypeNested_;
377 using LhsNested =
typename nested_eval<Lhs, Dynamic>::type;
378 using RhsNested =
typename nested_eval<Rhs, Dynamic>::type;
379 LhsNested lhsNested(lhs);
380 RhsNested rhsNested(rhsView.matrix());
383 Transpose<Dest> dstT(dst);
384 internal::sparse_selfadjoint_time_dense_product<RhsView::TransposeMode>(rhsNested.transpose(),
385 lhsNested.transpose(), dstT, alpha);
392template <
typename LhsView,
typename Rhs,
int ProductTag>
393struct product_evaluator<Product<LhsView, Rhs, DefaultProduct>, ProductTag, SparseSelfAdjointShape, SparseShape>
394 :
public evaluator<typename Product<typename Rhs::PlainObject, Rhs, DefaultProduct>::PlainObject> {
395 using XprType = Product<LhsView, Rhs, DefaultProduct>;
396 using PlainObject =
typename XprType::PlainObject;
397 using Base = evaluator<PlainObject>;
399 product_evaluator(
const XprType& xpr) : m_lhs(xpr.lhs()), m_result(xpr.rows(), xpr.cols()) {
400 internal::construct_at<Base>(
this, m_result);
401 generic_product_impl<typename Rhs::PlainObject, Rhs, SparseShape, SparseShape, ProductTag>::evalTo(m_result, m_lhs,
406 typename Rhs::PlainObject m_lhs;
407 PlainObject m_result;
410template <
typename Lhs,
typename RhsView,
int ProductTag>
411struct product_evaluator<Product<Lhs, RhsView, DefaultProduct>, ProductTag, SparseShape, SparseSelfAdjointShape>
412 :
public evaluator<typename Product<Lhs, typename Lhs::PlainObject, DefaultProduct>::PlainObject> {
413 using XprType = Product<Lhs, RhsView, DefaultProduct>;
414 using PlainObject =
typename XprType::PlainObject;
415 using Base = evaluator<PlainObject>;
417 product_evaluator(
const XprType& xpr) : m_rhs(xpr.rhs()), m_result(xpr.rows(), xpr.cols()) {
418 ::new (
static_cast<Base*
>(
this)) Base(m_result);
419 generic_product_impl<Lhs, typename Lhs::PlainObject, SparseShape, SparseShape, ProductTag>::evalTo(
420 m_result, xpr.lhs(), m_rhs);
424 typename Lhs::PlainObject m_rhs;
425 PlainObject m_result;
435template <
int Mode,
bool NonHermitian,
typename MatrixType,
int DestOrder>
436void permute_symm_to_fullsymm(
437 const MatrixType& mat,
438 SparseMatrix<typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex>& _dest,
439 const typename MatrixType::StorageIndex* perm) {
440 using StorageIndex =
typename MatrixType::StorageIndex;
441 using Scalar =
typename MatrixType::Scalar;
442 using Dest = SparseMatrix<Scalar, DestOrder, StorageIndex>;
443 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
444 using MatEval = evaluator<MatrixType>;
445 using MatIterator =
typename evaluator<MatrixType>::InnerIterator;
447 MatEval matEval(mat);
448 Dest& dest(_dest.derived());
449 enum { StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor) };
451 Index size = mat.rows();
455 dest.resize(size, size);
456 for (Index j = 0; j < size; ++j) {
457 Index jp = perm ? perm[j] : j;
458 for (MatIterator it(matEval, j); it; ++it) {
459 Index i = it.index();
462 Index ip = perm ? perm[i] : i;
463 EIGEN_IF_CONSTEXPR (Mode ==
int(
Upper |
Lower))
464 count[StorageOrderMatch ? jp : ip]++;
467 else if ((Mode ==
Lower && r > c) || (Mode ==
Upper && r < c)) {
473 Index nnz = count.sum();
476 dest.resizeNonZeros(nnz);
477 dest.outerIndexPtr()[0] = 0;
478 for (Index j = 0; j < size; ++j) dest.outerIndexPtr()[j + 1] = dest.outerIndexPtr()[j] + count[j];
479 for (Index j = 0; j < size; ++j) count[j] = dest.outerIndexPtr()[j];
482 for (StorageIndex j = 0; j < size; ++j) {
483 for (MatIterator it(matEval, j); it; ++it) {
484 StorageIndex i = internal::convert_index<StorageIndex>(it.index());
488 StorageIndex jp = perm ? perm[j] : j;
489 StorageIndex ip = perm ? perm[i] : i;
491 EIGEN_IF_CONSTEXPR (Mode ==
int(
Upper |
Lower)) {
492 Index k = count[StorageOrderMatch ? jp : ip]++;
493 dest.innerIndexPtr()[k] = StorageOrderMatch ? ip : jp;
494 dest.valuePtr()[k] = it.value();
496 Index k = count[ip]++;
497 dest.innerIndexPtr()[k] = ip;
498 dest.valuePtr()[k] = it.value();
500 EIGEN_IF_CONSTEXPR (!StorageOrderMatch) std::swap(ip, jp);
501 Index k = count[jp]++;
502 dest.innerIndexPtr()[k] = ip;
503 dest.valuePtr()[k] = it.value();
505 dest.innerIndexPtr()[k] = jp;
506 dest.valuePtr()[k] = (NonHermitian ? it.value() : numext::conj(it.value()));
512template <
int SrcMode_,
int DstMode_,
bool NonHermitian,
typename MatrixType,
int DstOrder>
513void permute_symm_to_symm(
const MatrixType& mat,
514 SparseMatrix<typename MatrixType::Scalar, DstOrder, typename MatrixType::StorageIndex>& _dest,
515 const typename MatrixType::StorageIndex* perm) {
516 using StorageIndex =
typename MatrixType::StorageIndex;
517 using Scalar =
typename MatrixType::Scalar;
518 SparseMatrix<Scalar, DstOrder, StorageIndex>& dest(_dest.derived());
519 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
520 using MatEval = evaluator<MatrixType>;
521 using MatIterator =
typename evaluator<MatrixType>::InnerIterator;
525 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
530 MatEval matEval(mat);
532 Index size = mat.rows();
535 dest.resize(size, size);
536 for (StorageIndex j = 0; j < size; ++j) {
537 StorageIndex jp = perm ? perm[j] : j;
538 for (MatIterator it(matEval, j); it; ++it) {
539 StorageIndex i = it.index();
540 if ((
int(SrcMode) ==
int(
Lower) && i < j) || (
int(SrcMode) ==
int(
Upper) && i > j))
continue;
542 StorageIndex ip = perm ? perm[i] : i;
543 count[int(DstMode) == int(
Lower) ? (std::min)(ip, jp) : (std::max)(ip, jp)]++;
546 dest.outerIndexPtr()[0] = 0;
547 for (Index j = 0; j < size; ++j) dest.outerIndexPtr()[j + 1] = dest.outerIndexPtr()[j] + count[j];
548 dest.resizeNonZeros(dest.outerIndexPtr()[size]);
549 for (Index j = 0; j < size; ++j) count[j] = dest.outerIndexPtr()[j];
551 for (StorageIndex j = 0; j < size; ++j) {
552 for (MatIterator it(matEval, j); it; ++it) {
553 StorageIndex i = it.index();
554 if ((
int(SrcMode) ==
int(
Lower) && i < j) || (
int(SrcMode) ==
int(
Upper) && i > j))
continue;
556 StorageIndex jp = perm ? perm[j] : j;
557 StorageIndex ip = perm ? perm[i] : i;
559 Index k = count[int(DstMode) == int(
Lower) ? (std::min)(ip, jp) : (std::max)(ip, jp)]++;
560 dest.innerIndexPtr()[k] = int(DstMode) == int(
Lower) ? (std::max)(ip, jp) : (std::min)(ip, jp);
562 EIGEN_IF_CONSTEXPR (!StorageOrderMatch) std::swap(ip, jp);
563 if ((
int(DstMode) ==
int(
Lower) && ip < jp) || (
int(DstMode) ==
int(
Upper) && ip > jp))
564 dest.valuePtr()[k] = (NonHermitian ? it.value() : numext::conj(it.value()));
566 dest.valuePtr()[k] = it.value();
575template <
typename MatrixType,
int Mode>
576struct traits<SparseSymmetricPermutationProduct<MatrixType, Mode> > : traits<MatrixType> {};
580template <
typename MatrixType,
int Mode>
581class SparseSymmetricPermutationProduct :
public EigenBase<SparseSymmetricPermutationProduct<MatrixType, Mode> > {
583 using Scalar =
typename MatrixType::Scalar;
584 using StorageIndex =
typename MatrixType::StorageIndex;
586 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
587 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
591 using Perm = PermutationMatrix<Dynamic, Dynamic, StorageIndex>;
594 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
595 using MatrixTypeNested =
typename MatrixType::Nested;
596 using NestedExpression = internal::remove_all_t<MatrixTypeNested>;
598 SparseSymmetricPermutationProduct(
const MatrixType& mat,
const Perm& perm) : m_matrix(mat), m_perm(perm) {}
600 inline Index rows()
const {
return m_matrix.rows(); }
601 inline Index cols()
const {
return m_matrix.cols(); }
603 const NestedExpression& matrix()
const {
return m_matrix; }
604 const Perm& perm()
const {
return m_perm; }
607 MatrixTypeNested m_matrix;
613template <
typename DstXprType,
typename MatrixType,
int Mode,
typename Scalar>
614struct Assignment<DstXprType, SparseSymmetricPermutationProduct<MatrixType, Mode>,
615 internal::assign_op<Scalar, typename MatrixType::Scalar>, Sparse2Sparse> {
616 using SrcXprType = SparseSymmetricPermutationProduct<MatrixType, Mode>;
617 using DstIndex =
typename DstXprType::StorageIndex;
618 template <
int Options>
619 static void run(SparseMatrix<Scalar, Options, DstIndex>& dst,
const SrcXprType& src,
620 const internal::assign_op<Scalar, typename MatrixType::Scalar>&) {
621 SparseMatrix<Scalar, (Options &
RowMajor) == RowMajor ? ColMajor :
RowMajor, DstIndex> tmp;
622 internal::permute_symm_to_fullsymm<Mode, false>(src.matrix(), tmp, src.perm().indices().data());
626 template <
typename DestType,
unsigned int DestMode>
627 static void run(SparseSelfAdjointView<DestType, DestMode>& dst,
const SrcXprType& src,
628 const internal::assign_op<Scalar, typename MatrixType::Scalar>&) {
629 internal::permute_symm_to_symm<Mode, DestMode, false>(src.matrix(), dst.matrix(), src.perm().indices().data());
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const Derived & derived() const
Definition DiagonalMatrix.h:60
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Permutation matrix.
Definition PermutationMatrix.h:346
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:203
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
A versatile sparse matrix representation.
Definition SparseMatrix.h:122
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition SparseSelfAdjointView.h:53
SparseSymmetricPermutationProduct< MatrixTypeNested_, Mode > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition SparseSelfAdjointView.h:156
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition SparseSelfAdjointView.h:120
SparseSelfAdjointView & rankUpdate(const SparseMatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
Product< SparseSelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition SparseSelfAdjointView.h:109
Product< SparseSelfAdjointView, OtherDerived > operator*(const SparseMatrixBase< OtherDerived > &rhs) const
Definition SparseSelfAdjointView.h:90
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const SparseMatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition SparseSelfAdjointView.h:102
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Eigen::Index Index
Definition EigenBase.h:44