Eigen  5.0.1-dev+master
 
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_INTERNAL_ASSERT_SQUARE_MSG(matrix, "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 ? internal::convert_index<Index>(perm[j]) : j;
458 for (MatIterator it(matEval, j); it; ++it) {
459 Index i = internal::convert_index<Index>(it.index());
460 Index r = it.row();
461 Index c = it.col();
462 Index ip = perm ? internal::convert_index<Index>(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 = internal::convert_index<Index>(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 (Index j = 0; j < size; ++j) {
483 for (MatIterator it(matEval, j); it; ++it) {
484 Index i = internal::convert_index<Index>(it.index());
485 Index r = it.row();
486 Index c = it.col();
487
488 // Outer indices can exceed the range of StorageIndex; only the stored inner indices are narrowed.
489 Index jp = perm ? internal::convert_index<Index>(perm[j]) : j;
490 Index ip = perm ? internal::convert_index<Index>(perm[i]) : i;
491
492 EIGEN_IF_CONSTEXPR (Mode == int(Upper | Lower)) {
493 StorageIndex k = count[StorageOrderMatch ? jp : ip]++;
494 dest.innerIndexPtr()[k] = internal::convert_index<StorageIndex>(StorageOrderMatch ? ip : jp);
495 dest.valuePtr()[k] = it.value();
496 } else if (r == c) {
497 StorageIndex k = count[ip]++;
498 dest.innerIndexPtr()[k] = internal::convert_index<StorageIndex>(ip);
499 dest.valuePtr()[k] = it.value();
500 } else if (((Mode & Lower) == Lower && r > c) || ((Mode & Upper) == Upper && r < c)) {
501 EIGEN_IF_CONSTEXPR (!StorageOrderMatch) std::swap(ip, jp);
502 StorageIndex k = count[jp]++;
503 dest.innerIndexPtr()[k] = internal::convert_index<StorageIndex>(ip);
504 dest.valuePtr()[k] = it.value();
505 k = count[ip]++;
506 dest.innerIndexPtr()[k] = internal::convert_index<StorageIndex>(jp);
507 dest.valuePtr()[k] = (NonHermitian ? it.value() : numext::conj(it.value()));
508 }
509 }
510 }
511}
512
513template <int SrcMode_, int DstMode_, bool NonHermitian, typename MatrixType, int DstOrder>
514void permute_symm_to_symm(const MatrixType& mat,
515 SparseMatrix<typename MatrixType::Scalar, DstOrder, typename MatrixType::StorageIndex>& _dest,
516 const typename MatrixType::StorageIndex* perm) {
517 using StorageIndex = typename MatrixType::StorageIndex;
518 using Scalar = typename MatrixType::Scalar;
519 SparseMatrix<Scalar, DstOrder, StorageIndex>& dest(_dest.derived());
520 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
521 using MatEval = evaluator<MatrixType>;
522 using MatIterator = typename evaluator<MatrixType>::InnerIterator;
523
524 enum {
525 SrcOrder = MatrixType::IsRowMajor ? RowMajor : ColMajor,
526 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
527 DstMode = DstOrder == RowMajor ? (DstMode_ == Upper ? Lower : Upper) : DstMode_,
528 SrcMode = SrcOrder == RowMajor ? (SrcMode_ == Upper ? Lower : Upper) : SrcMode_
529 };
530
531 MatEval matEval(mat);
532
533 Index size = mat.rows();
534 VectorI count(size);
535 count.setZero();
536 dest.resize(size, size);
537 // Outer indices can exceed the range of StorageIndex; only the stored inner indices are narrowed.
538 for (Index j = 0; j < size; ++j) {
539 Index jp = perm ? internal::convert_index<Index>(perm[j]) : j;
540 for (MatIterator it(matEval, j); it; ++it) {
541 Index i = internal::convert_index<Index>(it.index());
542 if ((int(SrcMode) == int(Lower) && i < j) || (int(SrcMode) == int(Upper) && i > j)) continue;
543
544 Index ip = perm ? internal::convert_index<Index>(perm[i]) : i;
545 count[int(DstMode) == int(Lower) ? (std::min)(ip, jp) : (std::max)(ip, jp)]++;
546 }
547 }
548 dest.outerIndexPtr()[0] = 0;
549 for (Index j = 0; j < size; ++j) dest.outerIndexPtr()[j + 1] = dest.outerIndexPtr()[j] + count[j];
550 dest.resizeNonZeros(internal::convert_index<Index>(dest.outerIndexPtr()[size]));
551 for (Index j = 0; j < size; ++j) count[j] = dest.outerIndexPtr()[j];
552
553 for (Index j = 0; j < size; ++j) {
554 for (MatIterator it(matEval, j); it; ++it) {
555 Index i = internal::convert_index<Index>(it.index());
556 if ((int(SrcMode) == int(Lower) && i < j) || (int(SrcMode) == int(Upper) && i > j)) continue;
557
558 Index jp = perm ? internal::convert_index<Index>(perm[j]) : j;
559 Index ip = perm ? internal::convert_index<Index>(perm[i]) : i;
560
561 Index outer = int(DstMode) == int(Lower) ? (std::min)(ip, jp) : (std::max)(ip, jp);
562 Index inner = int(DstMode) == int(Lower) ? (std::max)(ip, jp) : (std::min)(ip, jp);
563 StorageIndex k = count[outer]++;
564 dest.innerIndexPtr()[k] = internal::convert_index<StorageIndex>(inner);
565
566 EIGEN_IF_CONSTEXPR (!StorageOrderMatch) std::swap(ip, jp);
567 if ((int(DstMode) == int(Lower) && ip < jp) || (int(DstMode) == int(Upper) && ip > jp))
568 dest.valuePtr()[k] = (NonHermitian ? it.value() : numext::conj(it.value()));
569 else
570 dest.valuePtr()[k] = it.value();
571 }
572 }
573}
574
575} // namespace internal
576
577namespace internal {
578
579template <typename MatrixType, int Mode>
580struct traits<SparseSymmetricPermutationProduct<MatrixType, Mode> > : traits<MatrixType> {};
581
582} // namespace internal
583
584template <typename MatrixType, int Mode>
585class SparseSymmetricPermutationProduct : public EigenBase<SparseSymmetricPermutationProduct<MatrixType, Mode> > {
586 public:
587 using Scalar = typename MatrixType::Scalar;
588 using StorageIndex = typename MatrixType::StorageIndex;
589 enum {
590 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
591 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
592 };
593
594 protected:
595 using Perm = PermutationMatrix<Dynamic, Dynamic, StorageIndex>;
596
597 public:
598 using VectorI = Matrix<StorageIndex, Dynamic, 1>;
599 using MatrixTypeNested = typename MatrixType::Nested;
600 using NestedExpression = internal::remove_all_t<MatrixTypeNested>;
601
602 SparseSymmetricPermutationProduct(const MatrixType& mat, const Perm& perm) : m_matrix(mat), m_perm(perm) {}
603
604 inline Index rows() const { return m_matrix.rows(); }
605 inline Index cols() const { return m_matrix.cols(); }
606
607 const NestedExpression& matrix() const { return m_matrix; }
608 const Perm& perm() const { return m_perm; }
609
610 protected:
611 MatrixTypeNested m_matrix;
612 const Perm& m_perm;
613};
614
615namespace internal {
616
617template <typename DstXprType, typename MatrixType, int Mode, typename Scalar>
618struct Assignment<DstXprType, SparseSymmetricPermutationProduct<MatrixType, Mode>,
619 internal::assign_op<Scalar, typename MatrixType::Scalar>, Sparse2Sparse> {
620 using SrcXprType = SparseSymmetricPermutationProduct<MatrixType, Mode>;
621 using DstIndex = typename DstXprType::StorageIndex;
622 template <int Options>
623 static void run(SparseMatrix<Scalar, Options, DstIndex>& dst, const SrcXprType& src,
624 const internal::assign_op<Scalar, typename MatrixType::Scalar>&) {
625 SparseMatrix<Scalar, (Options & RowMajor) == RowMajor ? ColMajor : RowMajor, DstIndex> tmp;
626 internal::permute_symm_to_fullsymm<Mode, false>(src.matrix(), tmp, src.perm().indices().data());
627 dst = tmp;
628 }
629
630 template <typename DestType, unsigned int DestMode>
631 static void run(SparseSelfAdjointView<DestType, DestMode>& dst, const SrcXprType& src,
632 const internal::assign_op<Scalar, typename MatrixType::Scalar>&) {
633 internal::permute_symm_to_symm<Mode, DestMode, false>(src.matrix(), dst.matrix(), src.perm().indices().data());
634 }
635};
636
637} // end namespace internal
638
639} // end namespace Eigen
640
641#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:211
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
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