Eigen  5.0.1
 
Loading...
Searching...
No Matches
SparseSelfAdjointView.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSE_SELFADJOINTVIEW_H
12#define EIGEN_SPARSE_SELFADJOINTVIEW_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
33namespace internal {
34
35template <typename MatrixType, unsigned int Mode>
36struct traits<SparseSelfAdjointView<MatrixType, Mode> > : traits<MatrixType> {};
37
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);
43
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);
49
50} // namespace internal
51
52template <typename MatrixType, unsigned int Mode_>
53class SparseSelfAdjointView : public EigenBase<SparseSelfAdjointView<MatrixType, Mode_> > {
54 public:
55 enum {
56 Mode = Mode_,
57 TransposeMode = ((int(Mode) & int(Upper)) ? Lower : 0) | ((int(Mode) & int(Lower)) ? Upper : 0),
58 RowsAtCompileTime = internal::traits<SparseSelfAdjointView>::RowsAtCompileTime,
59 ColsAtCompileTime = internal::traits<SparseSelfAdjointView>::ColsAtCompileTime
60 };
61
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>;
68 using PlainObject =
69 SparseMatrix<Scalar, (MatrixTypeNested_::Flags & RowMajorBit) ? RowMajor : ColMajor, StorageIndex>;
70
71 explicit inline SparseSelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {
72 eigen_assert(rows() == cols() && "SelfAdjointView is only for squared matrices");
73 }
74
75 inline Index rows() const { return m_matrix.rows(); }
76 inline Index cols() const { return m_matrix.cols(); }
77
79 const MatrixTypeNested_& matrix() const { return m_matrix; }
80 std::remove_reference_t<MatrixTypeNested>& matrix() { return m_matrix; }
81
89 template <typename OtherDerived>
93
101 template <typename OtherDerived>
103 const SparseSelfAdjointView& rhs) {
104 return Product<OtherDerived, SparseSelfAdjointView>(lhs.derived(), rhs);
105 }
106
108 template <typename OtherDerived>
112
113 template <typename OtherDerived>
116 }
117
119 template <typename OtherDerived>
121 const SparseSelfAdjointView& rhs) {
122 return Product<OtherDerived, SparseSelfAdjointView>(lhs.derived(), rhs);
123 }
124
125 template <typename OtherDerived>
127 const SparseSelfAdjointView& rhs) {
128 return Product<OtherDerived, SparseSelfAdjointView>(lhs.derived(), rhs);
129 }
130
131 // Scalar multiplication intentionally materializes the full matrix, unlike dense SelfAdjointView's lazy wrapper,
132 // matching the existing SparseSelfAdjointView products.
133 PlainObject operator*(const Scalar& s) const { return s * *this; }
134
135 friend PlainObject operator*(const Scalar& s, const SparseSelfAdjointView& mat) {
136 PlainObject res(mat);
137 res *= s;
138 return res;
139 }
140
151 template <typename DerivedU>
152 SparseSelfAdjointView& rankUpdate(const SparseMatrixBase<DerivedU>& u, const Scalar& alpha = Scalar(1));
153
155 // TODO: implement twists in a more evaluator friendly fashion
156 SparseSymmetricPermutationProduct<MatrixTypeNested_, Mode> twistedBy(
158 return SparseSymmetricPermutationProduct<MatrixTypeNested_, Mode>(m_matrix, perm);
159 }
160
161 template <typename SrcMatrixType, int SrcMode>
162 SparseSelfAdjointView& operator=(const SparseSymmetricPermutationProduct<SrcMatrixType, SrcMode>& permutedMatrix) {
163 internal::call_assignment_no_alias_no_transpose(*this, permutedMatrix);
164 return *this;
165 }
166
167 SparseSelfAdjointView& operator=(const SparseSelfAdjointView& src) {
168 PermutationMatrix<Dynamic, Dynamic, StorageIndex> pnull;
169 return *this = src.twistedBy(pnull);
170 }
171
172 // Since we override the copy-assignment operator, we need to explicitly redeclare the copy-constructor
173 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SparseSelfAdjointView)
174
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);
179 }
180
181 void resize(Index rows, Index cols) {
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.");
186 }
187
188 protected:
189 MatrixTypeNested m_matrix;
190
191 private:
192 template <typename Dest>
193 void evalTo(Dest&) const;
194};
195
196/***************************************************************************
197 * Implementation of SparseMatrixBase methods
198 ***************************************************************************/
199
200template <typename Derived>
201template <unsigned int UpLo>
202typename SparseMatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type
203SparseMatrixBase<Derived>::selfadjointView() const {
205}
206
207template <typename Derived>
208template <unsigned int UpLo>
209typename SparseMatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type
210SparseMatrixBase<Derived>::selfadjointView() {
211 return SparseSelfAdjointView<Derived, UpLo>(derived());
212}
213
214/***************************************************************************
215 * Implementation of SparseSelfAdjointView methods
216 ***************************************************************************/
217
218template <typename MatrixType, unsigned int Mode>
219template <typename DerivedU>
221 const SparseMatrixBase<DerivedU>& u, const Scalar& alpha) {
222 SparseMatrix<Scalar, (MatrixType::Flags & RowMajorBit) ? RowMajor : ColMajor> tmp = u * u.adjoint();
223 if (alpha == Scalar(0))
224 m_matrix = tmp.template triangularView<Mode>();
225 else
226 m_matrix += alpha * tmp.template triangularView<Mode>();
227
228 return *this;
229}
230
231namespace internal {
232
233// TODO: currently a selfadjoint expression has the form SelfAdjointView<.,.>
234// in the future selfadjoint-ness should be defined by the expression traits
235// such that Transpose<SelfAdjointView<.,.> > is valid. (currently TriangularBase::transpose() is overloaded to
236// make it work)
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;
241};
242
243struct SparseSelfAdjoint2Sparse {};
244
245template <>
246struct AssignmentKind<SparseShape, SparseSelfAdjointShape> {
247 using Kind = SparseSelfAdjoint2Sparse;
248};
249template <>
250struct AssignmentKind<SparseSelfAdjointShape, SparseShape> {
251 using Kind = Sparse2Sparse;
252};
253
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>;
258
259 template <typename DestScalar, int StorageOrder>
260 static void run(SparseMatrix<DestScalar, StorageOrder, StorageIndex>& dst, const SrcXprType& src,
261 const AssignOpType& /*func*/) {
262 internal::permute_symm_to_fullsymm<SrcXprType::Mode, false>(src.matrix(), dst);
263 }
264
265 // FIXME: the handling of += and -= in sparse matrices should be cleanup so that next two overloads could be reduced
266 // to:
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);
273 }
274
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>& /* func */) {
278 SparseMatrix<DestScalar, StorageOrder, StorageIndex> tmp(src.rows(), src.cols());
279 run(tmp, src, AssignOpType());
280 dst += tmp;
281 }
282
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>& /* func */) {
286 SparseMatrix<DestScalar, StorageOrder, StorageIndex> tmp(src.rows(), src.cols());
287 run(tmp, src, AssignOpType());
288 dst -= tmp;
289 }
290};
291
292} // end namespace internal
293
294/***************************************************************************
295 * Implementation of sparse self-adjoint times dense matrix
296 ***************************************************************************/
297
298namespace internal {
299
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);
304
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;
310
311 enum {
312 LhsIsRowMajor = (LhsEval::Flags & RowMajorBit) == RowMajorBit,
313 ProcessFirstHalf = ((Mode & (Upper | Lower)) == (Upper | Lower)) || ((Mode & Upper) && !LhsIsRowMajor) ||
314 ((Mode & Lower) && LhsIsRowMajor),
315 ProcessSecondHalf = !ProcessFirstHalf
316 };
317
318 SparseLhsTypeNested lhs_nested(lhs);
319 LhsEval lhsEval(lhs_nested);
320
321 // work on one column at once
322 for (Index k = 0; k < rhs.cols(); ++k) {
323 for (Index j = 0; j < lhs.outerSize(); ++j) {
324 LhsIterator i(lhsEval, j);
325 // handle diagonal coeff
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);
330 ++i;
331 }
332 }
333
334 // premultiplied rhs for scatters
335 typename ScalarBinaryOpTraits<AlphaType, typename DenseRhsType::Scalar>::ReturnType rhs_j(alpha * rhs(j, k));
336 // accumulator for partial scalar product
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);
342 }
343 res_j += lhs_ij * rhs.coeff(i.index(), k);
344 res(i.index(), k) += numext::conj(lhs_ij) * rhs_j;
345 }
346 res.coeffRef(j, k) += alpha * res_j;
347
348 // handle diagonal coeff
349 if (ProcessFirstHalf && i && (i.index() == j)) res.coeffRef(j, k) += alpha * i.value() * rhs.coeff(j, k);
350 }
351 }
352}
353
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);
365
366 internal::sparse_selfadjoint_time_dense_product<LhsView::Mode>(lhsNested, rhsNested, dst, alpha);
367 }
368};
369
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());
381
382 // transpose everything
383 Transpose<Dest> dstT(dst);
384 internal::sparse_selfadjoint_time_dense_product<RhsView::TransposeMode>(rhsNested.transpose(),
385 lhsNested.transpose(), dstT, alpha);
386 }
387};
388
389// NOTE: these two overloads are needed to evaluate the sparse selfadjoint view into a full sparse matrix
390// TODO: maybe the copy could be handled by generic_product_impl so that these overloads would not be needed anymore
391
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>;
398
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,
402 xpr.rhs());
403 }
404
405 protected:
406 typename Rhs::PlainObject m_lhs;
407 PlainObject m_result;
408};
409
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>;
416
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);
421 }
422
423 protected:
424 typename Lhs::PlainObject m_rhs;
425 PlainObject m_result;
426};
427
428} // namespace internal
429
430/***************************************************************************
431 * Implementation of symmetric copies and permutations
432 ***************************************************************************/
433namespace internal {
434
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;
446
447 MatEval matEval(mat);
448 Dest& dest(_dest.derived());
449 enum { StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor) };
450
451 Index size = mat.rows();
452 VectorI count;
453 count.resize(size);
454 count.setZero();
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();
460 Index r = it.row();
461 Index c = it.col();
462 Index ip = perm ? perm[i] : i;
463 EIGEN_IF_CONSTEXPR (Mode == int(Upper | Lower))
464 count[StorageOrderMatch ? jp : ip]++;
465 else if (r == c)
466 count[ip]++;
467 else if ((Mode == Lower && r > c) || (Mode == Upper && r < c)) {
468 count[ip]++;
469 count[jp]++;
470 }
471 }
472 }
473 Index nnz = count.sum();
474
475 // reserve space
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];
480
481 // copy data
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());
485 Index r = it.row();
486 Index c = it.col();
487
488 StorageIndex jp = perm ? perm[j] : j;
489 StorageIndex ip = perm ? perm[i] : i;
490
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();
495 } else if (r == c) {
496 Index k = count[ip]++;
497 dest.innerIndexPtr()[k] = ip;
498 dest.valuePtr()[k] = it.value();
499 } else if (((Mode & Lower) == Lower && r > c) || ((Mode & Upper) == Upper && r < c)) {
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();
504 k = count[ip]++;
505 dest.innerIndexPtr()[k] = jp;
506 dest.valuePtr()[k] = (NonHermitian ? it.value() : numext::conj(it.value()));
507 }
508 }
509 }
510}
511
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;
522
523 enum {
524 SrcOrder = MatrixType::IsRowMajor ? RowMajor : ColMajor,
525 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
526 DstMode = DstOrder == RowMajor ? (DstMode_ == Upper ? Lower : Upper) : DstMode_,
527 SrcMode = SrcOrder == RowMajor ? (SrcMode_ == Upper ? Lower : Upper) : SrcMode_
528 };
529
530 MatEval matEval(mat);
531
532 Index size = mat.rows();
533 VectorI count(size);
534 count.setZero();
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;
541
542 StorageIndex ip = perm ? perm[i] : i;
543 count[int(DstMode) == int(Lower) ? (std::min)(ip, jp) : (std::max)(ip, jp)]++;
544 }
545 }
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];
550
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;
555
556 StorageIndex jp = perm ? perm[j] : j;
557 StorageIndex ip = perm ? perm[i] : i;
558
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);
561
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()));
565 else
566 dest.valuePtr()[k] = it.value();
567 }
568 }
569}
570
571} // namespace internal
572
573namespace internal {
574
575template <typename MatrixType, int Mode>
576struct traits<SparseSymmetricPermutationProduct<MatrixType, Mode> > : traits<MatrixType> {};
577
578} // namespace internal
579
580template <typename MatrixType, int Mode>
581class SparseSymmetricPermutationProduct : public EigenBase<SparseSymmetricPermutationProduct<MatrixType, Mode> > {
582 public:
583 using Scalar = typename MatrixType::Scalar;
584 using StorageIndex = typename MatrixType::StorageIndex;
585 enum {
586 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
587 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
588 };
589
590 protected:
591 using Perm = PermutationMatrix<Dynamic, Dynamic, StorageIndex>;
592
593 public:
594 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
595 using MatrixTypeNested = typename MatrixType::Nested;
596 using NestedExpression = internal::remove_all_t<MatrixTypeNested>;
597
598 SparseSymmetricPermutationProduct(const MatrixType& mat, const Perm& perm) : m_matrix(mat), m_perm(perm) {}
599
600 inline Index rows() const { return m_matrix.rows(); }
601 inline Index cols() const { return m_matrix.cols(); }
602
603 const NestedExpression& matrix() const { return m_matrix; }
604 const Perm& perm() const { return m_perm; }
605
606 protected:
607 MatrixTypeNested m_matrix;
608 const Perm& m_perm;
609};
610
611namespace internal {
612
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());
623 dst = tmp;
624 }
625
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());
630 }
631};
632
633} // end namespace internal
634
635} // end namespace Eigen
636
637#endif // EIGEN_SPARSE_SELFADJOINTVIEW_H
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