Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SelfAdjointView.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 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_SELFADJOINTMATRIX_H
12#define EIGEN_SELFADJOINTMATRIX_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
34
35namespace internal {
36
37// Requires 0 <= i < j < matrix.rows(). The row prefixes may contain already-computed factors.
38template <typename MatrixType>
39EIGEN_DEVICE_FUNC void selfadjoint_swap_lower(MatrixType& matrix, Index i, Index j) {
40 using Scalar = typename MatrixType::Scalar;
41 const Index size = matrix.rows();
42 eigen_internal_assert(matrix.cols() == size && i >= 0 && i < j && j < size);
43 matrix.template block<1, Dynamic>(i, 0, 1, i).swap(matrix.template block<1, Dynamic>(j, 0, 1, i));
44 for (Index k = i + 1; k < j; ++k) {
45 const Scalar value = matrix.coeff(k, i);
46 matrix.coeffRef(k, i) = numext::conj(matrix.coeff(j, k));
47 matrix.coeffRef(j, k) = numext::conj(value);
48 }
49 matrix.template block<Dynamic, 1>(j + 1, i, size - j - 1, 1)
50 .swap(matrix.template block<Dynamic, 1>(j + 1, j, size - j - 1, 1));
51 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
52 matrix.coeffRef(j, i) = numext::conj(matrix.coeff(j, i));
53 }
54 numext::swap(matrix.coeffRef(i, i), matrix.coeffRef(j, j));
55}
56
57// Consumes an owning copy of the permutation, disjoint from matrix.
58template <typename MatrixType, typename PermutationType>
59EIGEN_DEVICE_FUNC void selfadjoint_apply_permutation_lower(MatrixType& matrix, PermutationType& remaining) {
60 const Index size = matrix.rows();
61 for (Index i = 0; i < size; ++i) {
62 while (Index(remaining.indices().coeff(i)) != i) {
63 const Index j = convert_index<Index>(remaining.indices().coeff(i));
64 // Earlier indices are fixed, so j > i. Each swap removes one edge from the permutation cycle.
65 // If P(j) == j as well, index j repeats and this loop would not terminate.
66 eigen_assert(i < j && j < size && Index(remaining.indices().coeff(j)) != j && "indices are not a permutation");
67 selfadjoint_swap_lower(matrix, i, j);
68 numext::swap(remaining.indices().coeffRef(i), remaining.indices().coeffRef(j));
69 }
70 }
71}
72
73template <typename MatrixType, unsigned int UpLo>
74struct traits<SelfAdjointView<MatrixType, UpLo>> : traits<MatrixType> {
75 using MatrixTypeNested = typename ref_selector<MatrixType>::non_const_type;
76 using MatrixTypeNestedCleaned = remove_all_t<MatrixTypeNested>;
77 using ExpressionType = MatrixType;
78 using FullMatrixType = typename MatrixType::PlainObject;
79 enum {
80 Mode = UpLo | SelfAdjoint,
81 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
82 Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) &
83 (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved
84 };
85};
86
87} // namespace internal
88
89template <typename MatrixType_, unsigned int UpLo>
90class SelfAdjointView : public TriangularBase<SelfAdjointView<MatrixType_, UpLo> > {
91 public:
92 EIGEN_STATIC_ASSERT(UpLo == Lower || UpLo == Upper, SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY)
93
94 using MatrixType = MatrixType_;
95 using Base = TriangularBase<SelfAdjointView>;
96 using MatrixTypeNested = typename internal::traits<SelfAdjointView>::MatrixTypeNested;
97 using MatrixTypeNestedCleaned = typename internal::traits<SelfAdjointView>::MatrixTypeNestedCleaned;
98 using NestedExpression = MatrixTypeNestedCleaned;
99
101 using Scalar = typename internal::traits<SelfAdjointView>::Scalar;
103 using RealScalar = typename NumTraits<Scalar>::Real;
104 using StorageIndex = typename MatrixType::StorageIndex;
105
106 enum {
107 Mode = internal::traits<SelfAdjointView>::Mode,
108 Flags = internal::traits<SelfAdjointView>::Flags,
109 TransposeMode = ((int(Mode) & int(Upper)) ? Lower : 0) | ((int(Mode) & int(Lower)) ? Upper : 0)
110 };
111 using PlainObject = typename MatrixType::PlainObject;
112
113 EIGEN_DEVICE_FUNC explicit inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {}
114 using Base::operator*;
115 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SelfAdjointView)
116
117
118 template <typename OtherDerived>
119 EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const MatrixBase<OtherDerived>& other) {
120 m_matrix.template triangularView<UpLo>() = other;
121 return *this;
122 }
123
125 template <typename OtherDerived>
126 EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const TriangularBase<OtherDerived>& other) {
127 other.evalToLazy(m_matrix);
128 return *this;
129 }
130
131 EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const SelfAdjointView& other) {
132 return *this = static_cast<const Base&>(other);
133 }
134
136 template <typename OtherDerived>
137 EIGEN_DEVICE_FUNC SelfAdjointView& operator+=(const DenseBase<OtherDerived>& other) {
138 m_matrix.template triangularView<UpLo>() += other;
139 return *this;
140 }
141
143 template <typename OtherDerived>
144 EIGEN_DEVICE_FUNC SelfAdjointView& operator-=(const DenseBase<OtherDerived>& other) {
145 m_matrix.template triangularView<UpLo>() -= other;
146 return *this;
147 }
148
150 EIGEN_DEVICE_FUNC SelfAdjointView& operator*=(const Scalar& other) {
151 eigen_assert(numext::imag(other) == typename NumTraits<Scalar>::Real(0) &&
152 "SelfAdjointView in-place scaling requires a real scalar; "
153 "scaling only the stored triangle by a non-real scalar would "
154 "leave conj(other) on the unstored half.");
155 m_matrix.template triangularView<UpLo>() *= other;
156 return *this;
157 }
158
160 EIGEN_DEVICE_FUNC SelfAdjointView& operator/=(const Scalar& other) {
161 eigen_assert(numext::imag(other) == typename NumTraits<Scalar>::Real(0) &&
162 "SelfAdjointView in-place division requires a real scalar; "
163 "dividing only the stored triangle by a non-real scalar would "
164 "leave conj(other) on the unstored half.");
165 m_matrix.template triangularView<UpLo>() /= other;
166 return *this;
167 }
168
180 template <typename PermutationDerived>
181 EIGEN_DEVICE_FUNC SelfAdjointView& applyPermutation(const PermutationBase<PermutationDerived>& permutation) {
182 typename PermutationDerived::PlainPermutationType remaining(permutation);
183 return applyPermutationInPlace(remaining);
184 }
185
187 template <typename PermutationDerived>
188 EIGEN_DEVICE_FUNC SelfAdjointView& applyPermutation(
189 const InverseImpl<PermutationDerived, PermutationStorage>& permutation) {
190 typename PermutationDerived::PlainPermutationType remaining(permutation);
191 return applyPermutationInPlace(remaining);
192 }
193
199 template <typename TranspositionsDerived>
200 EIGEN_DEVICE_FUNC SelfAdjointView& applyPermutation(const TranspositionsBase<TranspositionsDerived>& transpositions) {
201 return applyTranspositions<false>(transpositions);
202 }
203
205 template <typename TranspositionsDerived>
206 EIGEN_DEVICE_FUNC SelfAdjointView& applyPermutation(
207 const Transpose<TranspositionsBase<TranspositionsDerived>>& transpositions) {
208 return applyTranspositions<true>(transpositions.nestedExpression());
209 }
210
218 EIGEN_DEVICE_FUNC SelfAdjointView& applyTransposition(Index i, Index j) {
219 EIGEN_STATIC_ASSERT_LVALUE(MatrixType);
220 eigen_assert(this->rows() == this->cols() && i >= 0 && j >= 0 && i < this->rows() && j < this->rows());
221 if (i == j) return *this;
222 if (i > j) numext::swap(i, j);
223 EIGEN_IF_CONSTEXPR (UpLo == Lower) {
224 internal::selfadjoint_swap_lower(m_matrix, i, j);
225 } else {
226 auto transposed = m_matrix.transpose();
227 internal::selfadjoint_swap_lower(transposed, i, j);
228 }
229 return *this;
230 }
231
233 EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& _expression() const noexcept { return m_matrix; }
234
235 EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& nestedExpression() const noexcept { return m_matrix; }
236 EIGEN_DEVICE_FUNC constexpr std::remove_reference_t<MatrixTypeNested>& nestedExpression() noexcept {
237 return m_matrix;
238 }
239
240 EIGEN_DEVICE_FUNC const SelfAdjointView<
241 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(MatrixType, Scalar, internal::scalar_product_op), UpLo>
242 operator*(const Scalar& s) const {
243 return (nestedExpression() * s).template selfadjointView<UpLo>();
244 }
245
246 friend EIGEN_DEVICE_FUNC const SelfAdjointView<
247 const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, MatrixType, internal::scalar_product_op), UpLo>
248 operator*(const Scalar& s, const SelfAdjointView& mat) {
249 return (s * mat.nestedExpression()).template selfadjointView<UpLo>();
250 }
251
262 template <typename DerivedU, typename DerivedV>
263 EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const MatrixBase<DerivedV>& v,
264 const Scalar& alpha = Scalar(1));
265
276 template <typename DerivedU>
277 EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha = Scalar(1));
278
290 template <unsigned int TriMode>
291 EIGEN_DEVICE_FUNC
292 std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), TriangularView<MatrixType, TriMode>,
295 std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), MatrixType&,
296 typename MatrixType::ConstTransposeReturnType>
297 tmp1(m_matrix);
298 std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), MatrixType&,
299 typename MatrixType::AdjointReturnType>
300 tmp2(tmp1);
301 return std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)),
304 }
305
311 EIGEN_DEVICE_FUNC typename MatrixType::ConstDiagonalReturnType diagonal() const {
312 return typename MatrixType::ConstDiagonalReturnType(m_matrix);
313 }
314
320 EIGEN_DEVICE_FUNC RealScalar l1Norm() const { return internal::selfadjoint_l1_norm<UpLo>(m_matrix); }
321
323
327
329
332
333 EIGEN_DEVICE_FUNC EigenvaluesReturnType eigenvalues() const;
334 EIGEN_DEVICE_FUNC RealScalar operatorNorm() const;
335
336 private:
337 template <typename PermutationType>
338 EIGEN_DEVICE_FUNC SelfAdjointView& applyPermutationInPlace(PermutationType& remaining) {
339 EIGEN_STATIC_ASSERT_LVALUE(MatrixType);
340 eigen_assert(this->rows() == this->cols() && this->rows() == remaining.size());
341 EIGEN_IF_CONSTEXPR (UpLo == Lower) {
342 internal::selfadjoint_apply_permutation_lower(m_matrix, remaining);
343 } else {
344 auto transposed = m_matrix.transpose();
345 internal::selfadjoint_apply_permutation_lower(transposed, remaining);
346 }
347 return *this;
348 }
349
350 template <bool Reverse, typename TranspositionsDerived>
351 EIGEN_DEVICE_FUNC SelfAdjointView& applyTranspositions(
352 const TranspositionsBase<TranspositionsDerived>& transpositions) {
353 EIGEN_STATIC_ASSERT_LVALUE(MatrixType);
354 eigen_assert(this->rows() == this->cols() && this->rows() == transpositions.size());
355 const typename TranspositionsDerived::IndicesType::PlainObject indices(transpositions.indices());
356 const Index size = this->rows();
357 for (Index k = 0; k < size; ++k) {
358 const Index i = Reverse ? size - 1 - k : k;
359 applyTransposition(i, internal::convert_index<Index>(indices.coeff(i)));
360 }
361 return *this;
362 }
363
364 protected:
365 MatrixTypeNested m_matrix;
366};
367
368// selfadjoint to dense matrix
369
370namespace internal {
371
372// TODO currently a selfadjoint expression has the form SelfAdjointView<.,.>
373// in the future selfadjoint-ness should be defined by the expression traits
374// such that Transpose<SelfAdjointView<.,.> > is valid. (currently TriangularBase::transpose() is overloaded to
375// make it work)
376template <typename MatrixType, unsigned int Mode>
377struct evaluator_traits<SelfAdjointView<MatrixType, Mode> > {
378 using Kind = typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind;
379 using Shape = SelfAdjointShape;
380};
381
382// GCC PR target/124138 (https://gcc.gnu.org/PR124138): since GCC 4.9, avx_vpermilp_parallel accepted selectors 4-7
383// for elements 2 and 3 of a V8DF permutation, emitting the lane-crossing {0, 1, 2, 6, 4, 5, 6, 7} as vpermilpd, which
384// yields {0, 1, 2, 2, 4, 5, 6, 7}. SLP forms that permutation from the unrolled mirrored stores of a 4x4 double copy,
385// at -O2 since GCC 12. Hiding the copied value from the vectorizer avoids it; the loop used for larger sizes is
386// unaffected. Fixed in GCC 14.4, 15.3 and 16, and on the GCC 13 branch after 13.4.
387template <bool Unrolled, typename Scalar>
388EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void selfadjoint_mirror_barrier(Scalar&) {}
389#if EIGEN_COMP_GNUC_STRICT && defined(__AVX512F__) && !defined(EIGEN_GPU_COMPILE_PHASE) && \
390 (EIGEN_GNUC_STRICT_LESS_THAN(13, 5, 0) || \
391 (EIGEN_GNUC_STRICT_AT_LEAST(14, 0, 0) && EIGEN_GNUC_STRICT_LESS_THAN(14, 4, 0)) || \
392 (EIGEN_GNUC_STRICT_AT_LEAST(15, 0, 0) && EIGEN_GNUC_STRICT_LESS_THAN(15, 3, 0)))
393template <>
394EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void selfadjoint_mirror_barrier<true, double>(double& value) {
395 EIGEN_OPTIMIZATION_BARRIER(value);
396}
397#endif
398
399template <int UpLo, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor,
400 int Version>
401class triangular_dense_assignment_kernel<UpLo, SelfAdjoint, SetOpposite, DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor,
402 Version>
403 : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> {
404 protected:
405 using Base = generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>;
406 using DstXprType = typename Base::DstXprType;
407 using SrcXprType = typename Base::SrcXprType;
408 using Base::m_dst;
409 using Base::m_functor;
410 using Base::m_src;
411
412 public:
413 using DstEvaluatorType = typename Base::DstEvaluatorType;
414 using SrcEvaluatorType = typename Base::SrcEvaluatorType;
415 using Scalar = typename Base::Scalar;
416 using AssignmentTraits = typename Base::AssignmentTraits;
417
418 EIGEN_DEVICE_FUNC triangular_dense_assignment_kernel(DstEvaluatorType& dst, const SrcEvaluatorType& src,
419 const Functor& func, DstXprType& dstExpr)
420 : Base(dst, src, func, dstExpr) {}
421
422 EIGEN_DEVICE_FUNC void assignCoeff(Index row, Index col) {
423 eigen_internal_assert(row != col);
424 Scalar tmp = m_src.coeff(row, col);
425 selfadjoint_mirror_barrier<triangular_assignment_unrolls<DstEvaluatorType, SrcEvaluatorType>::value>(tmp);
426 m_functor.assignCoeff(m_dst.coeffRef(row, col), tmp);
427 m_functor.assignCoeff(m_dst.coeffRef(col, row), numext::conj(tmp));
428 }
429
430 // Override to ensure the SelfAdjoint assignCoeff (which mirrors conjugates) is called,
431 // not the base class version (which is a plain copy).
432 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void assignCoeffByOuterInner(Index outer, Index inner) {
433 Index row = Base::rowIndexByOuterInner(outer, inner);
434 Index col = Base::colIndexByOuterInner(outer, inner);
435 assignCoeff(row, col);
436 }
437
438 EIGEN_DEVICE_FUNC void assignDiagonalCoeff(Index id) { Base::assignCoeff(id, id); }
439
440 EIGEN_DEVICE_FUNC void assignOppositeCoeff(Index, Index) { eigen_internal_assert(false && "should never be called"); }
441};
442
443} // end namespace internal
444
445/***************************************************************************
446 * Implementation of MatrixBase methods
447 ***************************************************************************/
448
450template <typename Derived>
451template <unsigned int UpLo>
452EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type
453MatrixBase<Derived>::selfadjointView() const {
454 return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived());
455}
456
467template <typename Derived>
468template <unsigned int UpLo>
469EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type
470MatrixBase<Derived>::selfadjointView() {
471 return typename SelfAdjointViewReturnType<UpLo>::Type(derived());
472}
473
474} // end namespace Eigen
475
476#endif // EIGEN_SELFADJOINTMATRIX_H
Bunch-Kaufman factorization of a symmetric / Hermitian indefinite matrix.
Definition BunchKaufman.h:105
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
Robust Cholesky decomposition of a matrix with pivoting.
Definition LDLT.h:75
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition LLT.h:85
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
Base class for permutations.
Definition PermutationMatrix.h:92
Expression of a selfadjoint matrix from a triangular part of a dense matrix.
Definition SelfAdjointView.h:90
RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition MatrixBaseEigenvalues.h:135
BunchKaufman< PlainObject, UpLo > bunchKaufman() const
Definition BunchKaufman.h:1152
SelfAdjointView & applyPermutation(const PermutationBase< PermutationDerived > &permutation)
Definition SelfAdjointView.h:181
std::conditional_t<(TriMode &(Upper|Lower))==(UpLo &(Upper|Lower)), TriangularView< MatrixType, TriMode >, TriangularView< typename MatrixType::AdjointReturnType, TriMode > > triangularView() const
Definition SelfAdjointView.h:294
SelfAdjointView & operator+=(const DenseBase< OtherDerived > &other)
Definition SelfAdjointView.h:137
SelfAdjointView & rankUpdate(const MatrixBase< DerivedU > &u, const MatrixBase< DerivedV > &v, const Scalar &alpha=Scalar(1))
LDLT< PlainObject, UpLo > ldlt() const
Definition LDLT.h:858
typename internal::traits< SelfAdjointView >::Scalar Scalar
Definition SelfAdjointView.h:101
SelfAdjointView & applyPermutation(const InverseImpl< PermutationDerived, PermutationStorage > &permutation)
Definition SelfAdjointView.h:188
SelfAdjointView & applyPermutation(const TranspositionsBase< TranspositionsDerived > &transpositions)
Definition SelfAdjointView.h:200
LLT< PlainObject, UpLo > llt() const
Definition LLT.h:672
SelfAdjointView & operator/=(const Scalar &other)
Definition SelfAdjointView.h:160
typename NumTraits< Scalar >::Real RealScalar
Definition SelfAdjointView.h:103
SelfAdjointView & operator=(const MatrixBase< OtherDerived > &other)
Definition SelfAdjointView.h:119
Matrix< RealScalar, internal::traits< MatrixType >::ColsAtCompileTime, 1 > EigenvaluesReturnType
Definition SelfAdjointView.h:331
SelfAdjointView & operator=(const TriangularBase< OtherDerived > &other)
Definition SelfAdjointView.h:126
SelfAdjointView & operator*=(const Scalar &other)
Definition SelfAdjointView.h:150
SelfAdjointView & rankUpdate(const MatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
MatrixType::ConstDiagonalReturnType diagonal() const
Definition SelfAdjointView.h:311
SelfAdjointView & applyPermutation(const Transpose< TranspositionsBase< TranspositionsDerived > > &transpositions)
Definition SelfAdjointView.h:206
RealScalar l1Norm() const
Definition SelfAdjointView.h:320
SelfAdjointView & applyTransposition(Index i, Index j)
Definition SelfAdjointView.h:218
EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition MatrixBaseEigenvalues.h:84
SelfAdjointView & operator-=(const DenseBase< OtherDerived > &other)
Definition SelfAdjointView.h:144
Expression of the transpose of a matrix.
Definition Transpose.h:57
void evalToLazy(MatrixBase< DenseDerived > &other) const
Definition TriangularMatrix.h:1083
Expression of a triangular part in a matrix.
Definition TriangularMatrix.h:426
@ SelfAdjoint
Definition Constants.h:228
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr Index cols() const noexcept
Definition EigenBase.h:62
constexpr Index rows() const noexcept
Definition EigenBase.h:60
constexpr Index size() const noexcept
Definition EigenBase.h:65
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44