File: householder.cpp

package info (click to toggle)
quantlib 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,480 kB
  • sloc: cpp: 400,885; makefile: 6,547; python: 214; sh: 150; lisp: 86
file content (78 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2024 Klaus Spanderen

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/math/matrixutilities/householder.hpp>

namespace QuantLib {

    HouseholderTransformation::HouseholderTransformation(Array v)
    : v_(std::move(v)) {}


    Array HouseholderTransformation::operator()(const Array& x) const {
        return x - (2.0*DotProduct(v_, x))*v_;
    }

    Matrix HouseholderTransformation::getMatrix() const {
        const Array y = v_ / Norm2(v_);
        const Size n = y.size();

        Matrix m(n, n);
        for (Size i=0; i < n; ++i)
            for (Size j=0; j < n; ++j)
                m[i][j] = ((i == j)? 1.0: 0.0) - 2*y[i]*y[j];

        return m;
    }

    HouseholderReflection::HouseholderReflection(Array e)
    : e_(std::move(e)) {}

    Array HouseholderReflection::reflectionVector(const Array& a) const {
        const Real na = Norm2(a);
        QL_REQUIRE(na > 0, "vector of length zero given");

        const Real aDotE = DotProduct(a, e_);
        const Array a1 = aDotE*e_;
        const Array a2 = a - a1;

        const Real eps = DotProduct(a2, a2) / (aDotE*aDotE);
        if (eps < QL_EPSILON*QL_EPSILON) {
            return Array(a.size(), 0.0);
        }
        else if (eps < 1e-4) {
            const Real eps2 = eps*eps;
            const Real eps3 = eps*eps2;
            const Real eps4 = eps2*eps2;
            const Array v =
                (a2 - a1*(eps/2.0 - eps2/8.0 + eps3/16.0 - 5/128.0*eps4))
                / (aDotE*std::sqrt(eps + eps2/4.0 - eps3/8.0 + 5/64.0*eps4));
            return v;
        }
        else {
            const Array c = a - na*e_;
            return c / Norm2(c);
        }
    }

    Array HouseholderReflection::operator()(const Array& a) const {
        const Array v = reflectionVector(a);
        return HouseholderTransformation(v)(a);
    }
}