11#ifndef EIGEN_SELFADJOINTMATRIX_H
12#define EIGEN_SELFADJOINTMATRIX_H
15#include "./InternalHeaderCheck.h"
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);
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));
54 numext::swap(matrix.coeffRef(i, i), matrix.coeffRef(j, j));
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));
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));
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;
81 FlagsLvalueBit = is_lvalue<MatrixType>::value ?
LvalueBit : 0,
82 Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) &
89template <
typename MatrixType_,
unsigned int UpLo>
90class SelfAdjointView :
public TriangularBase<SelfAdjointView<MatrixType_, UpLo> > {
92 EIGEN_STATIC_ASSERT(UpLo ==
Lower || UpLo ==
Upper, SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY)
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;
101 using Scalar =
typename internal::traits<SelfAdjointView>::Scalar;
104 using StorageIndex =
typename MatrixType::StorageIndex;
107 Mode = internal::traits<SelfAdjointView>::Mode,
108 Flags = internal::traits<SelfAdjointView>::Flags,
111 using PlainObject =
typename MatrixType::PlainObject;
113 EIGEN_DEVICE_FUNC
explicit inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {}
114 using Base::operator*;
115 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SelfAdjointView)
118 template <
typename OtherDerived>
125 template <
typename OtherDerived>
126 EIGEN_DEVICE_FUNC SelfAdjointView&
operator=(
const TriangularBase<OtherDerived>& other) {
132 return *
this =
static_cast<const Base&
>(other);
136 template <
typename OtherDerived>
143 template <
typename OtherDerived>
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.");
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.");
180 template <
typename PermutationDerived>
182 typename PermutationDerived::PlainPermutationType remaining(permutation);
183 return applyPermutationInPlace(remaining);
187 template <
typename PermutationDerived>
189 const InverseImpl<PermutationDerived, PermutationStorage>& permutation) {
190 typename PermutationDerived::PlainPermutationType remaining(permutation);
191 return applyPermutationInPlace(remaining);
199 template <
typename TranspositionsDerived>
200 EIGEN_DEVICE_FUNC SelfAdjointView&
applyPermutation(
const TranspositionsBase<TranspositionsDerived>& transpositions) {
201 return applyTranspositions<false>(transpositions);
205 template <
typename TranspositionsDerived>
207 const Transpose<TranspositionsBase<TranspositionsDerived>>& transpositions) {
208 return applyTranspositions<true>(transpositions.nestedExpression());
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);
226 auto transposed = m_matrix.transpose();
227 internal::selfadjoint_swap_lower(transposed, i, j);
233 EIGEN_DEVICE_FUNC
constexpr const MatrixTypeNestedCleaned& _expression() const noexcept {
return m_matrix; }
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 {
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>();
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>();
262 template <
typename DerivedU,
typename DerivedV>
276 template <
typename DerivedU>
290 template <
unsigned int TriMode>
296 typename MatrixType::ConstTransposeReturnType>
299 typename MatrixType::AdjointReturnType>
311 EIGEN_DEVICE_FUNC
typename MatrixType::ConstDiagonalReturnType
diagonal()
const {
312 return typename MatrixType::ConstDiagonalReturnType(m_matrix);
320 EIGEN_DEVICE_FUNC
RealScalar l1Norm()
const {
return internal::selfadjoint_l1_norm<UpLo>(m_matrix); }
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);
344 auto transposed = m_matrix.transpose();
345 internal::selfadjoint_apply_permutation_lower(transposed, remaining);
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());
358 const Index i = Reverse ?
size - 1 - k : k;
365 MatrixTypeNested m_matrix;
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;
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)))
394EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void selfadjoint_mirror_barrier<true, double>(
double& value) {
395 EIGEN_OPTIMIZATION_BARRIER(value);
399template <
int UpLo,
int SetOpposite,
typename DstEvaluatorTypeT,
typename SrcEvaluatorTypeT,
typename Functor,
401class triangular_dense_assignment_kernel<UpLo,
SelfAdjoint, SetOpposite, DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor,
403 :
public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> {
405 using Base = generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>;
406 using DstXprType =
typename Base::DstXprType;
407 using SrcXprType =
typename Base::SrcXprType;
409 using Base::m_functor;
413 using DstEvaluatorType =
typename Base::DstEvaluatorType;
414 using SrcEvaluatorType =
typename Base::SrcEvaluatorType;
415 using Scalar =
typename Base::Scalar;
416 using AssignmentTraits =
typename Base::AssignmentTraits;
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) {}
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));
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);
438 EIGEN_DEVICE_FUNC
void assignDiagonalCoeff(Index
id) { Base::assignCoeff(
id,
id); }
440 EIGEN_DEVICE_FUNC
void assignOppositeCoeff(Index, Index) { eigen_internal_assert(
false &&
"should never be called"); }
450template <
typename Derived>
451template <
unsigned int UpLo>
453MatrixBase<Derived>::selfadjointView()
const {
454 return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived());
467template <
typename Derived>
468template <
unsigned int UpLo>
470MatrixBase<Derived>::selfadjointView() {
471 return typename SelfAdjointViewReturnType<UpLo>::Type(derived());
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