QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
laplaceinterpolation.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Peter Caspers
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
24#ifndef quantlib_laplace_interpolation
25#define quantlib_laplace_interpolation
26
27#include <ql/math/matrixutilities/bicgstab.hpp>
28#include <ql/math/matrixutilities/sparsematrix.hpp>
29
30namespace QuantLib {
31
37template <class M> void laplaceInterpolation(M &A, Real relTol = 1E-6) {
38
39 struct f_A {
40 const SparseMatrix &g;
41 explicit f_A(const SparseMatrix &g) : g(g) {}
42 Array operator()(const Array &x) const {
43 return prod(g, x);
44 }
45 };
46
47 Size m = A.rows();
48 Size n = A.columns();
49
50 QL_REQUIRE(n > 1 && m > 1, "matrix (" << m << "," << n
51 << ") must at least be 2x2");
52
53 SparseMatrix g(m * n, m * n, 5 * m * n);
54 Array rhs(m * n, 0.0), guess(m * n, 0.0);
55 Real guessTmp = 0.0;
56 Size i1, i2, i3, i4, j1, j2, j3, j4;
57 bool inner;
58
59 for (Size l = 0, i = 0; i < m; ++i) {
60 for (Size j = 0; j < n; ++j) {
61
62 inner = false;
63
64 // top
65 if (i == 0) {
66 if (j == 0) {
67 i1 = 0;
68 j1 = 1;
69 i2 = 1;
70 j2 = 0;
71 } else {
72 if (j == n - 1) {
73 i1 = 0;
74 j1 = n - 2;
75 i2 = 1;
76 j2 = n - 1;
77 } else {
78 i1 = i2 = 0;
79 j1 = j - 1;
80 j2 = j + 1;
81 }
82 }
83 }
84
85 // bottom
86 if (i == m - 1) {
87 if (j == 0) {
88 i1 = m - 1;
89 j1 = 1;
90 i2 = m - 2;
91 j2 = 0;
92 } else {
93 if (j == n - 1) {
94 i1 = m - 1;
95 j1 = n - 2;
96 i2 = m - 2;
97 j2 = n - 1;
98 } else {
99 i1 = i2 = m - 1;
100 j1 = j - 1;
101 j2 = j + 1;
102 }
103 }
104 }
105
106 // left / right
107 if (i > 0 && i < m - 1) {
108 if (j == 0 || j == n - 1) {
109 j1 = j2 = j;
110 i1 = i - 1;
111 i2 = i + 1;
112 } else {
113 inner = true;
114 i1 = i - 1;
115 i2 = i - 1;
116 i3 = i + 1;
117 i4 = i + 1;
118 j1 = j - 1;
119 j2 = j + 1;
120 j3 = j - 1;
121 j4 = j + 1;
122 }
123 }
124
125 g(l, i * n + j) = 1.0;
126 if (A[i][j] == Null<Real>()) {
127 if (inner) {
128 g(l, i1 * n + j1) = -0.25;
129 g(l, i2 * n + j2) = -0.25;
130 g(l, i3 * n + j3) = -0.25;
131 g(l, i4 * n + j4) = -0.25;
132 } else {
133 g(l, i1 * n + j1) = -0.5;
134 g(l, i2 * n + j2) = -0.5;
135 }
136 rhs[l] = 0.0;
137 guess[l] = guessTmp;
138 } else {
139 rhs[l] = A[i][j];
140 guess[l] = guessTmp = A[i][j];
141 }
142 ++l;
143 }
144 }
145
146 // solve the equation (preconditioner is identiy)
147 Array s = BiCGstab(f_A(g), 10 * m * n, relTol)
148 .solve(rhs, guess)
149 .x;
150
151 // replace missing values by solution
152 for (Size i = 0; i < m; ++i) {
153 for (Size j = 0; j < n; ++j) {
154 if (A[i][j] == Null<Real>()) {
155 A[i][j] = s[i * n + j];
156 }
157 }
158 }
159};
160
161} // namespace QuantLib
162
163#endif // include guard
1-D array used in linear algebra.
Definition: array.hpp:52
BiCGStabResult solve(const Array &b, const Array &x0=Array()) const
Definition: bicgstab.cpp:37
template class providing a null value for a given type.
Definition: null.hpp:76
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Array prod(const SparseMatrix &A, const Array &x)
void laplaceInterpolation(M &A, Real relTol=1E-6)
boost::numeric::ublas::compressed_matrix< Real > SparseMatrix