WPILibC++ 2023.4.3-108-ge5452e3
MatrixBaseEigenvalues.h
Go to the documentation of this file.
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
11#ifndef EIGEN_MATRIXBASEEIGENVALUES_H
12#define EIGEN_MATRIXBASEEIGENVALUES_H
13
14namespace Eigen {
15
16namespace internal {
17
18template<typename Derived, bool IsComplex>
20{
21 // this is the implementation for the case IsComplex = true
22 static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
24 {
25 typedef typename Derived::PlainObject PlainObject;
26 PlainObject m_eval(m);
27 return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
28 }
29};
30
31template<typename Derived>
32struct eigenvalues_selector<Derived, false>
33{
34 static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
36 {
37 typedef typename Derived::PlainObject PlainObject;
38 PlainObject m_eval(m);
39 return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
40 }
41};
42
43} // end namespace internal
44
45/** \brief Computes the eigenvalues of a matrix
46 * \returns Column vector containing the eigenvalues.
47 *
48 * \eigenvalues_module
49 * This function computes the eigenvalues with the help of the EigenSolver
50 * class (for real matrices) or the ComplexEigenSolver class (for complex
51 * matrices).
52 *
53 * The eigenvalues are repeated according to their algebraic multiplicity,
54 * so there are as many eigenvalues as rows in the matrix.
55 *
56 * The SelfAdjointView class provides a better algorithm for selfadjoint
57 * matrices.
58 *
59 * Example: \include MatrixBase_eigenvalues.cpp
60 * Output: \verbinclude MatrixBase_eigenvalues.out
61 *
62 * \sa EigenSolver::eigenvalues(), ComplexEigenSolver::eigenvalues(),
63 * SelfAdjointView::eigenvalues()
64 */
65template<typename Derived>
68{
70}
71
72/** \brief Computes the eigenvalues of a matrix
73 * \returns Column vector containing the eigenvalues.
74 *
75 * \eigenvalues_module
76 * This function computes the eigenvalues with the help of the
77 * SelfAdjointEigenSolver class. The eigenvalues are repeated according to
78 * their algebraic multiplicity, so there are as many eigenvalues as rows in
79 * the matrix.
80 *
81 * Example: \include SelfAdjointView_eigenvalues.cpp
82 * Output: \verbinclude SelfAdjointView_eigenvalues.out
83 *
84 * \sa SelfAdjointEigenSolver::eigenvalues(), MatrixBase::eigenvalues()
85 */
86template<typename MatrixType, unsigned int UpLo>
89{
90 PlainObject thisAsMatrix(*this);
91 return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
92}
93
94
95
96/** \brief Computes the L2 operator norm
97 * \returns Operator norm of the matrix.
98 *
99 * \eigenvalues_module
100 * This function computes the L2 operator norm of a matrix, which is also
101 * known as the spectral norm. The norm of a matrix \f$ A \f$ is defined to be
102 * \f[ \|A\|_2 = \max_x \frac{\|Ax\|_2}{\|x\|_2} \f]
103 * where the maximum is over all vectors and the norm on the right is the
104 * Euclidean vector norm. The norm equals the largest singular value, which is
105 * the square root of the largest eigenvalue of the positive semi-definite
106 * matrix \f$ A^*A \f$.
107 *
108 * The current implementation uses the eigenvalues of \f$ A^*A \f$, as computed
109 * by SelfAdjointView::eigenvalues(), to compute the operator norm of a
110 * matrix. The SelfAdjointView class provides a better algorithm for
111 * selfadjoint matrices.
112 *
113 * Example: \include MatrixBase_operatorNorm.cpp
114 * Output: \verbinclude MatrixBase_operatorNorm.out
115 *
116 * \sa SelfAdjointView::eigenvalues(), SelfAdjointView::operatorNorm()
117 */
118template<typename Derived>
121{
122 using std::sqrt;
123 typename Derived::PlainObject m_eval(derived());
124 // FIXME if it is really guaranteed that the eigenvalues are already sorted,
125 // then we don't need to compute a maxCoeff() here, comparing the 1st and last ones is enough.
126 return sqrt((m_eval*m_eval.adjoint())
127 .eval()
128 .template selfadjointView<Lower>()
129 .eigenvalues()
130 .maxCoeff()
131 );
132}
133
134/** \brief Computes the L2 operator norm
135 * \returns Operator norm of the matrix.
136 *
137 * \eigenvalues_module
138 * This function computes the L2 operator norm of a self-adjoint matrix. For a
139 * self-adjoint matrix, the operator norm is the largest eigenvalue.
140 *
141 * The current implementation uses the eigenvalues of the matrix, as computed
142 * by eigenvalues(), to compute the operator norm of the matrix.
143 *
144 * Example: \include SelfAdjointView_operatorNorm.cpp
145 * Output: \verbinclude SelfAdjointView_operatorNorm.out
146 *
147 * \sa eigenvalues(), MatrixBase::operatorNorm()
148 */
149template<typename MatrixType, unsigned int UpLo>
152{
153 return eigenvalues().cwiseAbs().maxCoeff();
154}
155
156} // end namespace Eigen
157
158#endif
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:986
\eigenvalues_module
Definition: ComplexEigenSolver.h:46
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: ComplexEigenSolver.h:182
\eigenvalues_module
Definition: EigenSolver.h:65
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: EigenSolver.h:244
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition: MatrixBaseEigenvalues.h:120
EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition: MatrixBaseEigenvalues.h:67
NumTraits< Scalar >::Real RealScalar
Definition: MatrixBase.h:58
Matrix< std::complex< RealScalar >, internal::traits< Derived >::ColsAtCompileTime, 1, ColMajor > EigenvaluesReturnType
Definition: MatrixBase.h:115
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
\eigenvalues_module
Definition: SelfAdjointEigenSolver.h:77
EIGEN_DEVICE_FUNC const RealVectorType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: SelfAdjointEigenSolver.h:300
EIGEN_DEVICE_FUNC RealScalar operatorNorm() const
Computes the L2 operator norm.
Definition: MatrixBaseEigenvalues.h:151
EIGEN_DEVICE_FUNC EigenvaluesReturnType eigenvalues() const
Computes the eigenvalues of a matrix.
Definition: MatrixBaseEigenvalues.h:88
MatrixType::PlainObject PlainObject
Definition: SelfAdjointView.h:71
NumTraits< Scalar >::Real RealScalar
Real part of Scalar.
Definition: SelfAdjointView.h:256
auto sqrt(const UnitType &value) noexcept -> unit_t< square_root< typename units::traits::unit_t_traits< UnitType >::unit_type >, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the square root of value
Definition: math.h:483
Namespace containing all symbols from the Eigen library.
Definition: Core:141
Definition: Eigen_Colamd.h:50
static MatrixBase< Derived >::EigenvaluesReturnType const run(const MatrixBase< Derived > &m)
Definition: MatrixBaseEigenvalues.h:35
Definition: MatrixBaseEigenvalues.h:20
static MatrixBase< Derived >::EigenvaluesReturnType const run(const MatrixBase< Derived > &m)
Definition: MatrixBaseEigenvalues.h:23