Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
MatrixBaseEigenvalues.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_MATRIXBASEEIGENVALUES_H
13#define EIGEN_MATRIXBASEEIGENVALUES_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22template <typename Derived, bool IsComplex>
23struct eigenvalues_selector {
24 // this is the implementation for the case IsComplex = true
25 static inline typename MatrixBase<Derived>::EigenvaluesReturnType const run(const MatrixBase<Derived>& m) {
26 using PlainObject = typename Derived::PlainObject;
27 PlainObject m_eval(m);
28 return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
29 }
30};
31
32template <typename Derived>
33struct eigenvalues_selector<Derived, false> {
34 static inline typename MatrixBase<Derived>::EigenvaluesReturnType const run(const MatrixBase<Derived>& m) {
35 using PlainObject = typename Derived::PlainObject;
36 PlainObject m_eval(m);
37 return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
38 }
39};
40
41} // end namespace internal
42
63template <typename Derived>
64inline typename MatrixBase<Derived>::EigenvaluesReturnType MatrixBase<Derived>::eigenvalues() const {
65 return internal::eigenvalues_selector<Derived, NumTraits<Scalar>::IsComplex>::run(derived());
66}
67
82template <typename MatrixType, unsigned int UpLo>
85 PlainObject thisAsMatrix(*this);
86 return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
87}
88
111template <typename Derived>
112inline typename MatrixBase<Derived>::RealScalar MatrixBase<Derived>::operatorNorm() const {
113 typename Derived::PlainObject m_eval(derived());
114 if (m_eval.size() == 0) return RealScalar(0);
115 // Scale by a power of two first, so that forming A*A^* can neither overflow nor underflow: ||A||_2 = s*||A/s||_2.
116 // A NaN or an infinity in A makes the norm NaN or infinite, as it does for the other norms.
117 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(m_eval);
118 if (!(numext::isfinite)(maxCoeff) || numext::is_exactly_zero(maxCoeff)) return maxCoeff;
119 const auto factors = internal::safe_scaling<RealScalar>::scale_in_place(m_eval, maxCoeff);
120 const auto gram = (m_eval * m_eval.adjoint()).eval();
121 SelfAdjointEigenSolver<std::decay_t<decltype(gram)>> solver(gram, EigenvaluesOnly);
122 if (solver.info() != Success) return NumTraits<RealScalar>::quiet_NaN();
123 RealScalar result = numext::sqrt(numext::maxi(solver.eigenvalues().maxCoeff(), RealScalar(0)));
124 internal::safe_scaling<RealScalar>::unscale_in_place(result, factors);
125 return result;
126}
127
143template <typename MatrixType, unsigned int UpLo>
144EIGEN_DEVICE_FUNC inline typename SelfAdjointView<MatrixType, UpLo>::RealScalar
146 PlainObject thisAsMatrix(*this);
147 if (thisAsMatrix.size() == 0) return RealScalar(0);
148 // The solver ignores the imaginary part of the diagonal, so does the check below.
149 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
150 thisAsMatrix.diagonal() = thisAsMatrix.diagonal().real();
151 }
152 // A NaN gives NaN and an infinity +infinity, as for MatrixBase::operatorNorm(): the solver rejects both, and the
153 // eigenvalues are unspecified after it fails.
154 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(thisAsMatrix);
155 if (!(numext::isfinite)(maxCoeff)) return maxCoeff;
157 if (solver.info() != Success) return NumTraits<RealScalar>::quiet_NaN();
158 return solver.eigenvalues().cwiseAbs().maxCoeff();
159}
160
161} // end namespace Eigen
162
163#endif
internal::traits< Derived >::Scalar maxCoeff() const
Definition Redux.h:853
RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition MatrixBaseEigenvalues.h:112
EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition MatrixBaseEigenvalues.h:64
Computes eigenvalues and eigenvectors of selfadjoint matrices.
Definition SelfAdjointEigenSolver.h:84
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SelfAdjointEigenSolver.h:414
const RealVectorType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition SelfAdjointEigenSolver.h:342
RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition MatrixBaseEigenvalues.h:145
typename NumTraits< Scalar >::Real RealScalar
Definition SelfAdjointView.h:103
Matrix< RealScalar, internal::traits< MatrixType >::ColsAtCompileTime, 1 > EigenvaluesReturnType
Definition SelfAdjointView.h:335
EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition MatrixBaseEigenvalues.h:84
@ Success
Definition Constants.h:457
@ EigenvaluesOnly
Definition Constants.h:403