QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
numericaldifferentiation.hpp
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) 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
31namespace 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 */
42 public:
44
45 NumericalDifferentiation(ext::function<Real(Real)> f,
46 Size orderOfDerivative,
47 Array x_offsets);
48
49 NumericalDifferentiation(ext::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:
61 const ext::function<Real(Real)> f_;
62 };
63
64
66 Real s = 0.0;
67 for (Size i=0; i < w_.size(); ++i) {
68 if (std::fabs(w_[i]) > QL_EPSILON*QL_EPSILON) {
69 s += w_[i] * f_(x+offsets_[i]);
70 }
71 }
72 return s;
73 }
74
76 return w_;
77 }
78
80 return offsets_;
81 }
82}
83
84
85#endif
86
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Abstract base forward class.
Definition: forward.hpp:66
Numerical Differentiation on arbitrarily spaced grids.
const ext::function< Real(Real)> f_
Maps function, bind and cref to either the boost or std implementation.
#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
Definition: any.hpp:35