QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
methods
finitedifferences
operators
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
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
(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
:
60
const
Array
offsets_
,
w_
;
61
const
ext::function<
Real
(
Real
)>
f_
;
62
};
63
64
65
inline
Real
NumericalDifferentiation::operator()
(
Real
x)
const
{
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
75
inline
const
Array
&
NumericalDifferentiation::weights
()
const
{
76
return
w_
;
77
}
78
79
inline
const
Array
&
NumericalDifferentiation::offsets
()
const
{
80
return
offsets_
;
81
}
82
}
83
84
85
#endif
86
array.hpp
1-D array used in linear algebra.
QuantLib::Array
1-D array used in linear algebra.
Definition:
array.hpp:52
QuantLib::Array::size
Size size() const
dimension of the array
Definition:
array.hpp:495
QuantLib::Forward
Abstract base forward class.
Definition:
forward.hpp:66
QuantLib::NumericalDifferentiation
Numerical Differentiation on arbitrarily spaced grids.
Definition:
numericaldifferentiation.hpp:41
QuantLib::NumericalDifferentiation::operator()
Real operator()(Real x) const
Definition:
numericaldifferentiation.hpp:65
QuantLib::NumericalDifferentiation::Scheme
Scheme
Definition:
numericaldifferentiation.hpp:43
QuantLib::NumericalDifferentiation::Backward
@ Backward
Definition:
numericaldifferentiation.hpp:43
QuantLib::NumericalDifferentiation::Central
@ Central
Definition:
numericaldifferentiation.hpp:43
QuantLib::NumericalDifferentiation::offsets
const Array & offsets() const
Definition:
numericaldifferentiation.hpp:79
QuantLib::NumericalDifferentiation::w_
const Array w_
Definition:
numericaldifferentiation.hpp:60
QuantLib::NumericalDifferentiation::offsets_
const Array offsets_
Definition:
numericaldifferentiation.hpp:60
QuantLib::NumericalDifferentiation::weights
const Array & weights() const
Definition:
numericaldifferentiation.hpp:75
QuantLib::NumericalDifferentiation::f_
const ext::function< Real(Real)> f_
Definition:
numericaldifferentiation.hpp:61
f
F f
Definition:
defaultdensitystructure.cpp:32
functional.hpp
Maps function, bind and cref to either the boost or std implementation.
QL_EPSILON
#define QL_EPSILON
Definition:
qldefines.hpp:178
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib::Size
std::size_t Size
size of a container
Definition:
types.hpp:58
QuantLib
Definition:
any.hpp:35
s
Real s
Definition:
perturbativebarrieroptionengine.cpp:1488
Generated by
Doxygen
1.9.5