QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
filonintegral.cpp
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) 2014 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 filonintegral.cpp
21 \brief Filon's formulae for sine and cosine Integrals
22*/
23
24#include <ql/errors.hpp>
25#include <ql/utilities/null.hpp>
26#include <ql/math/array.hpp>
29
30#include <cmath>
31
32namespace QuantLib {
34 : Integrator(Null<Real>(), intervals+1),
35 type_(type),
36 t_(t),
37 intervals_(intervals),
38 n_ (intervals/2){
39 QL_REQUIRE( !(intervals_ & 1), "number of intervals must be even");
40 }
41
42 Real FilonIntegral::integrate(const ext::function<Real (Real)>& f,
43 Real a, Real b) const {
44 const Real h = (b-a)/(2*n_);
45 Array x(2*n_+1, a, h);
46
47 const Real theta = t_*h;
48 const Real theta2 = theta*theta;
49 const Real theta3 = theta2*theta;
50
51 const Real alpha = 1/theta + std::sin(2*theta)/(2*theta2)
52 - 2*squared(std::sin(theta))/theta3;
53 const Real beta = 2*( (1+squared(std::cos(theta)))/theta2
54 - std::sin(2*theta)/theta3);
55 const Real gamma = 4*(std::sin(theta)/theta3 - std::cos(theta)/theta2);
56
57 Array v(x.size());
58 std::transform(x.begin(), x.end(), v.begin(), f);
59
60 ext::function<Real(Real)> f1, f2;
61 switch(type_) {
62 case Cosine:
63 f1 = [](Real x) -> Real { return std::sin(x); };
64 f2 = [](Real x) -> Real { return std::cos(x); };
65 break;
66 case Sine:
67 f1 = [](Real x) -> Real { return std::cos(x); };
68 f2 = [](Real x) -> Real { return std::sin(x); };
69 break;
70 default:
71 QL_FAIL("unknown integration type");
72 }
73
74 Real c_2n_1 = 0.0;
75 Real c_2n = v[0]*f2(t_*a)
76 - 0.5*(v[2*n_]*f2(t_*b) + v[0]*f2(t_*a));
77
78 for (Size i=1; i <= n_; ++i) {
79 c_2n += v[2*i] *f2(t_*x[2*i]);
80 c_2n_1 += v[2*i-1]*f2(t_*x[2*i-1]);
81 }
82
83 return h*(alpha*(v[2*n_]*f1(t_*x[2*n_]) - v[0]*f1(t_*x[0]))
84 *((type_ == Cosine) ? 1.0 : -1.0)
85 + beta*c_2n + gamma*c_2n_1);
86 }
87}
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
FilonIntegral(Type type, Real t, Size intervals)
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
template class providing a null value for a given type.
Definition: null.hpp:76
const DefaultType & t
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
ext::function< Real(Real)> b
Filon's formulae for sine and cosine Integrals.
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Real theta
functionals and combinators not included in the STL
Definition: any.hpp:35
T squared(T x)
Definition: functional.hpp:37
null values
ext::shared_ptr< BlackVolTermStructure > v
Real beta
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200