QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
householder.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2024 Klaus Spanderen
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
21
22namespace QuantLib {
23
25 : v_(std::move(v)) {}
26
27
29 return x - (2.0*DotProduct(v_, x))*v_;
30 }
31
33 const Array y = v_ / Norm2(v_);
34 const Size n = y.size();
35
36 Matrix m(n, n);
37 for (Size i=0; i < n; ++i)
38 for (Size j=0; j < n; ++j)
39 m[i][j] = ((i == j)? 1.0: 0.0) - 2*y[i]*y[j];
40
41 return m;
42 }
43
45 : e_(std::move(e)) {}
46
48 const Real na = Norm2(a);
49 QL_REQUIRE(na > 0, "vector of length zero given");
50
51 const Real aDotE = DotProduct(a, e_);
52 const Array a1 = aDotE*e_;
53 const Array a2 = a - a1;
54
55 const Real eps = DotProduct(a2, a2) / (aDotE*aDotE);
56 if (eps < QL_EPSILON*QL_EPSILON) {
57 return Array(a.size(), 0.0);
58 }
59 else if (eps < 1e-4) {
60 const Real eps2 = eps*eps;
61 const Real eps3 = eps*eps2;
62 const Real eps4 = eps2*eps2;
63 const Array v =
64 (a2 - a1*(eps/2.0 - eps2/8.0 + eps3/16.0 - 5/128.0*eps4))
65 / (aDotE*std::sqrt(eps + eps2/4.0 - eps3/8.0 + 5/64.0*eps4));
66 return v;
67 }
68 else {
69 const Array c = a - na*e_;
70 return c / Norm2(c);
71 }
72 }
73
75 const Array v = reflectionVector(a);
77 }
78}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:483
Array reflectionVector(const Array &a) const
Definition: householder.cpp:47
Array operator()(const Array &a) const
Definition: householder.cpp:74
Array operator()(const Array &x) const
Definition: householder.cpp:28
Matrix used in linear algebra.
Definition: matrix.hpp:41
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_EPSILON
Definition: qldefines.hpp:178
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Householder transformation and Householder projection.
Definition: any.hpp:37
Real Norm2(const Array &v)
Definition: array.hpp:548
Real DotProduct(const Array &v1, const Array &v2)
Definition: array.hpp:541
STL namespace.
ext::shared_ptr< BlackVolTermStructure > v