Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
GeneralizedSelfAdjointEigenSolver.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 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_GENERALIZEDSELFADJOINTEIGENSOLVER_H
13#define EIGEN_GENERALIZEDSELFADJOINTEIGENSOLVER_H
14
15#include "./Tridiagonalization.h"
16
17// IWYU pragma: private
18#include "./InternalHeaderCheck.h"
19
20namespace Eigen {
21
52template <typename MatrixType_>
55
56 public:
57 using MatrixType = MatrixType_;
58
66 GeneralizedSelfAdjointEigenSolver() : Base(), m_cholB(), m_matC() {}
67
80 explicit GeneralizedSelfAdjointEigenSolver(Index size) : Base(size), m_cholB(size), m_matC(size, size) {}
81
111 template <typename InputTypeA, typename InputTypeB>
113 int options = ComputeEigenvectors | Ax_lBx)
114 : Base(matA.cols()) {
115 compute(matA.derived(), matB.derived(), options);
116 }
117
134 template <typename InputTypeA, typename InputTypeB, bool IsRef = internal::is_ref<MatrixType>::value,
135 std::enable_if_t<IsRef, int> = 0>
137 int options = ComputeEigenvectors | Ax_lBx)
138 : Base(matA, typename Base::BindStorageTag()), m_cholB(matB.derived()), m_matC(matA.derived()) {
139 // Complete the upper triangle from the referenced lower one.
140 m_matC = m_matC.template selfadjointView<Lower>();
141 computeInPlace(options);
142 }
143
188 template <typename InputTypeA, typename InputTypeB>
190 int options = ComputeEigenvectors | Ax_lBx);
191
192 protected:
193 // Reused across compute() calls so that a solver constructed with its size,
194 // or computed with once, does not allocate again.
195 LLT<MatrixType> m_cholB;
196 MatrixType m_matC;
197
198 private:
199 GeneralizedSelfAdjointEigenSolver& computeInPlace(int options);
200};
201
202template <typename MatrixType>
203template <typename InputTypeA, typename InputTypeB>
205 const EigenBase<InputTypeA>& matA, const EigenBase<InputTypeB>& matB, int options) {
206 EIGEN_INTERNAL_ASSERT_SQUARE(matA);
207 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(matA, matB);
208
209 // Compute the cholesky decomposition of matB = L L' = U'U
210 m_cholB.compute(matB.derived());
211 m_matC = matA.derived().template selfadjointView<Lower>();
212 return computeInPlace(options);
213}
214
217template <typename MatrixType>
218GeneralizedSelfAdjointEigenSolver<MatrixType>& GeneralizedSelfAdjointEigenSolver<MatrixType>::computeInPlace(
219 int options) {
220 EIGEN_INTERNAL_ASSERT_SQUARE(m_matC);
221 EIGEN_INTERNAL_ASSERT_SAME_ROWS(m_cholB, m_matC);
222 eigen_assert((options & ~(EigVecMask | GenEigMask)) == 0 && (options & EigVecMask) != EigVecMask &&
223 ((options & GenEigMask) == 0 || (options & GenEigMask) == Ax_lBx || (options & GenEigMask) == ABx_lx ||
224 (options & GenEigMask) == BAx_lx) &&
225 "invalid option parameter");
226
227 bool computeEigVecs = ((options & EigVecMask) == 0) || ((options & EigVecMask) == ComputeEigenvectors);
228
229 int type = (options & GenEigMask);
230 if (type == 0) type = Ax_lBx;
231
232 // A selfadjoint matrix has a real diagonal. Drop the imaginary part now: the products and solves below would
233 // spread it into the off-diagonal of C, which the base solver cannot undo. The LLT factor of B ignores that of B,
234 // but LLT's rcond estimate does not.
235 EIGEN_IF_CONSTEXPR (NumTraits<typename MatrixType::Scalar>::IsComplex) {
236 m_matC.diagonal() = m_matC.diagonal().real();
237 }
238
239 // In the inplace decomposition the eigenvector storage is the matrix A itself, i.e. m_matC: the products of the
240 // ABx_lx and BAx_lx forms, which cannot run in place, then go through a temporary instead.
241 const bool eivecIsMatC = internal::is_same_dense(Base::m_eivec, m_matC);
242
243 if (type == Ax_lBx) {
244 // compute C = inv(L) A inv(L')
245 m_cholB.matrixL().template solveInPlace<OnTheLeft>(m_matC);
246 m_cholB.matrixU().template solveInPlace<OnTheRight>(m_matC);
247
248 Base::compute(m_matC, computeEigVecs ? ComputeEigenvectors : EigenvaluesOnly);
249
250 // transform back the eigen vectors: evecs = inv(U) * evecs
251 if (computeEigVecs) m_cholB.matrixU().solveInPlace(Base::m_eivec);
252 } else if (type == ABx_lx) {
253 // compute C = L' A L, using Base::m_eivec for the intermediate product: Base::compute()
254 // overwrites it before reading it.
255 if (eivecIsMatC) {
256 typename Base::PlainMatrixType tmp = m_matC * m_cholB.matrixL();
257 m_matC.noalias() = m_cholB.matrixU() * tmp;
258 } else {
259 Base::m_eivec.noalias() = m_matC * m_cholB.matrixL();
260 m_matC.noalias() = m_cholB.matrixU() * Base::m_eivec;
261 }
262
263 Base::compute(m_matC, computeEigVecs ? ComputeEigenvectors : EigenvaluesOnly);
264
265 // transform back the eigen vectors: evecs = inv(U) * evecs
266 if (computeEigVecs) m_cholB.matrixU().solveInPlace(Base::m_eivec);
267 } else if (type == BAx_lx) {
268 // compute C = L' A L
269 if (eivecIsMatC) {
270 typename Base::PlainMatrixType tmp = m_matC * m_cholB.matrixL();
271 m_matC.noalias() = m_cholB.matrixU() * tmp;
272 } else {
273 Base::m_eivec.noalias() = m_matC * m_cholB.matrixL();
274 m_matC.noalias() = m_cholB.matrixU() * Base::m_eivec;
275 }
276
277 Base::compute(m_matC, computeEigVecs ? ComputeEigenvectors : EigenvaluesOnly);
278
279 // transform back the eigen vectors: evecs = L * evecs, using m_matC as the
280 // intermediate: Base::compute() has consumed it.
281 if (computeEigVecs) {
282 if (eivecIsMatC) {
283 Base::m_eivec = m_cholB.matrixL() * Base::m_eivec;
284 } else {
285 m_matC.noalias() = m_cholB.matrixL() * Base::m_eivec;
286 Base::m_eivec = m_matC;
287 }
288 }
289 }
290
291 // The reduction above is only valid for a positive definite B.
292 if (m_cholB.info() != Success) Base::m_info = NumericalIssue;
293
294 return *this;
295}
296
297} // end namespace Eigen
298
299#endif // EIGEN_GENERALIZEDSELFADJOINTEIGENSOLVER_H
Computes eigenvalues and eigenvectors of the generalized selfadjoint eigen problem.
Definition GeneralizedSelfAdjointEigenSolver.h:53
GeneralizedSelfAdjointEigenSolver()
Default constructor for fixed-size matrices.
Definition GeneralizedSelfAdjointEigenSolver.h:66
GeneralizedSelfAdjointEigenSolver(Index size)
Constructor, pre-allocates memory for dynamic-size matrices.
Definition GeneralizedSelfAdjointEigenSolver.h:80
GeneralizedSelfAdjointEigenSolver(const EigenBase< InputTypeA > &matA, const EigenBase< InputTypeB > &matB, int options=ComputeEigenvectors|Ax_lBx)
Constructor; computes generalized eigendecomposition of given matrix pencil.
Definition GeneralizedSelfAdjointEigenSolver.h:112
GeneralizedSelfAdjointEigenSolver & compute(const EigenBase< InputTypeA > &matA, const EigenBase< InputTypeB > &matB, int options=ComputeEigenvectors|Ax_lBx)
Computes generalized eigendecomposition of given matrix pencil.
GeneralizedSelfAdjointEigenSolver(EigenBase< InputTypeA > &matA, EigenBase< InputTypeB > &matB, int options=ComputeEigenvectors|Ax_lBx)
Constructor for inplace decomposition .
Definition GeneralizedSelfAdjointEigenSolver.h:136
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition LLT.h:85
SelfAdjointEigenSolver()
Default constructor for fixed-size matrices.
Definition SelfAdjointEigenSolver.h:141
Eigen::Index Index
Definition SelfAdjointEigenSolver.h:96
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ Ax_lBx
Definition Constants.h:411
@ ComputeEigenvectors
Definition Constants.h:406
@ BAx_lx
Definition Constants.h:417
@ ABx_lx
Definition Constants.h:414
@ EigenvaluesOnly
Definition Constants.h:403
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50