/src/quantlib/ql/methods/finitedifferences/operators/numericaldifferentiation.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) 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 | | <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 | | |
20 | | /*! \file numericaldifferentiation.hpp |
21 | | \brief numerical differentiation of arbitrary order |
22 | | and on irregular grids |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_numerical_differentiation_hpp |
26 | | #define quantlib_numerical_differentiation_hpp |
27 | | |
28 | | #include <ql/math/array.hpp> |
29 | | #include <ql/functional.hpp> |
30 | | |
31 | | namespace QuantLib { |
32 | | |
33 | | //! Numerical Differentiation on arbitrarily spaced grids |
34 | | |
35 | | /*! References: |
36 | | |
37 | | B. Fornberg, 1988. Generation of Finite Difference Formulas |
38 | | on Arbitrarily Spaced Grids, |
39 | | http://amath.colorado.edu/faculty/fornberg/Docs/MathComp_88_FD_formulas.pdf |
40 | | */ |
41 | | class NumericalDifferentiation { |
42 | | public: |
43 | | enum Scheme { Central, Backward, Forward }; |
44 | | |
45 | | NumericalDifferentiation(std::function<Real(Real)> f, |
46 | | Size orderOfDerivative, |
47 | | Array x_offsets); |
48 | | |
49 | | NumericalDifferentiation(std::function<Real(Real)> f, |
50 | | Size orderOfDerivative, |
51 | | Real stepSize, |
52 | | Size steps, |
53 | | Scheme scheme); |
54 | | |
55 | | Real operator()(Real x) const; |
56 | | const Array& offsets() const; |
57 | | const Array& weights() const; |
58 | | |
59 | | private: |
60 | | const Array offsets_, w_; |
61 | | const std::function<Real(Real)> f_; |
62 | | }; |
63 | | |
64 | | |
65 | 0 | inline Real NumericalDifferentiation::operator()(Real x) const { |
66 | 0 | Real s = 0.0; |
67 | 0 | for (Size i=0; i < w_.size(); ++i) { |
68 | 0 | if (std::fabs(w_[i]) > QL_EPSILON*QL_EPSILON) { |
69 | 0 | s += w_[i] * f_(x+offsets_[i]); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | return s; |
73 | 0 | } |
74 | | |
75 | 0 | inline const Array& NumericalDifferentiation::weights() const { |
76 | 0 | return w_; |
77 | 0 | } |
78 | | |
79 | 0 | inline const Array& NumericalDifferentiation::offsets() const { |
80 | 0 | return offsets_; |
81 | 0 | } |
82 | | } |
83 | | |
84 | | |
85 | | #endif |
86 | | |