Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/math/matrixutilities/symmetricschurdecomposition.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003 Ferdinando Ametrano
5
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <https://www.quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
/*! \file symmetricschurdecomposition.hpp
22
    \brief Eigenvalues/eigenvectors of a real symmetric matrix
23
*/
24
25
#ifndef quantlib_math_jacobi_decomposition_h
26
#define quantlib_math_jacobi_decomposition_h
27
28
#include <ql/math/matrix.hpp>
29
30
namespace QuantLib {
31
32
    //! symmetric threshold Jacobi algorithm.
33
    /*! Given a real symmetric matrix S, the Schur decomposition
34
        finds the eigenvalues and eigenvectors of S. If D is the
35
        diagonal matrix formed by the eigenvalues and U the
36
        unitarian matrix of the eigenvectors we can write the
37
        Schur decomposition as
38
        \f[ S = U \cdot D \cdot U^T \, ,\f]
39
        where \f$ \cdot \f$ is the standard matrix product
40
        and  \f$ ^T  \f$ is the transpose operator.
41
        This class implements the Schur decomposition using the
42
        symmetric threshold Jacobi algorithm. For details on the
43
        different Jacobi transfomations see "Matrix computation,"
44
        second edition, by Golub and Van Loan,
45
        The Johns Hopkins University Press
46
47
        \test the correctness of the returned values is tested by
48
              checking their properties.
49
    */
50
    class SymmetricSchurDecomposition {
51
      public:
52
        /*! \pre s must be symmetric */
53
        SymmetricSchurDecomposition(const Matrix &s);
54
0
        const Array& eigenvalues() const { return diagonal_; }
55
0
        const Matrix& eigenvectors() const { return eigenVectors_; }
56
      private:
57
        Array diagonal_;
58
        Matrix eigenVectors_;
59
        void jacobiRotate_(Matrix & m, Real rot, Real dil,
60
                           Size j1, Size k1, Size j2, Size k2) const;
61
    };
62
63
64
    // inline definitions
65
66
    //! This routines implements the Jacobi, a.k.a. Givens, rotation
67
    inline void SymmetricSchurDecomposition::jacobiRotate_(
68
                                      Matrix &m, Real rot, Real dil, Size j1,
69
0
                                      Size k1, Size j2, Size k2) const {
70
0
        Real x1, x2;
71
0
        x1 = m[j1][k1];
72
0
        x2 = m[j2][k2];
73
0
        m[j1][k1] = x1 - dil*(x2 + x1*rot);
74
0
        m[j2][k2] = x2 + dil*(x1 - x2*rot);
75
0
    }
76
77
}
78
79
80
#endif
81
82