/src/quantlib/ql/methods/finitedifferences/operators/numericaldifferentiation.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2015 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 | | <https://www.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 | | |
20 | | /*! \file numericaldifferentiation.cpp */ |
21 | | |
22 | | #include <ql/methods/finitedifferences/operators/numericaldifferentiation.hpp> |
23 | | |
24 | | #pragma push_macro("BOOST_DISABLE_ASSERTS") |
25 | | #ifndef BOOST_DISABLE_ASSERTS |
26 | | #define BOOST_DISABLE_ASSERTS |
27 | | #endif |
28 | | #include <boost/multi_array.hpp> |
29 | | #pragma pop_macro("BOOST_DISABLE_ASSERTS") |
30 | | |
31 | | #include <utility> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | namespace { |
36 | | Array calcOffsets( |
37 | 0 | Real h, Size n, NumericalDifferentiation::Scheme scheme) { |
38 | 0 | QL_REQUIRE(n > 1, "number of steps must be greater than one"); |
39 | | |
40 | 0 | Array retVal(n); |
41 | 0 | switch (scheme) { |
42 | 0 | case NumericalDifferentiation::Central: |
43 | 0 | QL_REQUIRE(n > 2 && (n % 2), |
44 | 0 | "number of steps must be an odd number greater than two"); |
45 | 0 | for (Integer i=0; i < Integer(n); ++i) |
46 | 0 | retVal[i] = (i-Integer(n/2))*h; |
47 | 0 | break; |
48 | 0 | case NumericalDifferentiation::Backward: |
49 | 0 | for (Size i=0; i < n; ++i) |
50 | 0 | retVal[i]=-(i*h); |
51 | 0 | break; |
52 | 0 | case NumericalDifferentiation::Forward: |
53 | 0 | for (Size i=0; i < n; ++i) |
54 | 0 | retVal[i]=i*h; |
55 | 0 | break; |
56 | 0 | default: |
57 | 0 | QL_FAIL("unknown numerical differentiation scheme"); |
58 | 0 | } |
59 | | |
60 | 0 | return retVal; |
61 | 0 | } |
62 | | |
63 | | // This is a C++ implementation of the algorithm/pseudo code in |
64 | | // B. Fornberg, 1998. Calculation of Weights |
65 | | // in Finite Difference Formulas |
66 | | // https://amath.colorado.edu/faculty/fornberg/Docs/sirev_cl.pdf |
67 | 0 | Array calcWeights(const Array& x, Size M) { |
68 | 0 | const Size N = x.size(); |
69 | 0 | QL_REQUIRE(N > M, "number of points must be greater " |
70 | 0 | "than the order of the derivative"); |
71 | | |
72 | 0 | QL_DEPRECATED_DISABLE_WARNING |
73 | 0 | boost::multi_array<Real, 3> d(boost::extents[M+1][N][N]); |
74 | 0 | QL_DEPRECATED_ENABLE_WARNING |
75 | 0 | d[0][0][0] = 1.0; |
76 | 0 | Real c1 = 1.0; |
77 | |
|
78 | 0 | for (Size n=1; n < N; ++n) { |
79 | 0 | Real c2 = 1.0; |
80 | 0 | for (Size nu=0; nu < n; ++nu) { |
81 | 0 | const Real c3 = x[n] - x[nu]; |
82 | 0 | c2*=c3; |
83 | |
|
84 | 0 | for (Size m=0; m <= std::min(n, M); ++m) { |
85 | 0 | d[m][n][nu] = (x[n]*d[m][n-1][nu] |
86 | 0 | - ((m > 0)? Real(m*d[m-1][n-1][nu]) : 0.0))/c3; |
87 | 0 | } |
88 | 0 | } |
89 | |
|
90 | 0 | for (Size m=0; m <= M; ++m) { |
91 | 0 | d[m][n][n] = c1/c2*( ((m > 0)? Real(m*d[m-1][n-1][n-1]) : 0.0) - |
92 | 0 | x[n-1]*d[m][n-1][n-1] ); |
93 | 0 | } |
94 | 0 | c1=c2; |
95 | 0 | } |
96 | | |
97 | |
|
98 | 0 | Array retVal(N); |
99 | 0 | for (Size i=0; i < N; ++i) { |
100 | 0 | retVal[i] = d[M][N-1][i]; |
101 | 0 | } |
102 | 0 | return retVal; |
103 | 0 | } |
104 | | } |
105 | | |
106 | | NumericalDifferentiation::NumericalDifferentiation(std::function<Real(Real)> f, |
107 | | Size orderOfDerivative, |
108 | | Array x_offsets) |
109 | 0 | : offsets_(std::move(x_offsets)), w_(calcWeights(offsets_, orderOfDerivative)), |
110 | 0 | f_(std::move(f)) {} |
111 | | |
112 | | |
113 | | NumericalDifferentiation::NumericalDifferentiation(std::function<Real(Real)> f, |
114 | | Size orderOfDerivative, |
115 | | Real stepSize, |
116 | | Size steps, |
117 | | Scheme scheme) |
118 | 0 | : offsets_(calcOffsets(stepSize, steps, scheme)), w_(calcWeights(offsets_, orderOfDerivative)), |
119 | 0 | f_(std::move(f)) {} |
120 | | } |