QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
linearinterpolation.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2008 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_linear_interpolation_hpp
26#define quantlib_linear_interpolation_hpp
27
28#include <ql/math/interpolation.hpp>
29#include <vector>
30
31namespace QuantLib {
32
33 namespace detail {
34 template<class I1, class I2> class LinearInterpolationImpl;
35 }
36
38
43 public:
45 template <class I1, class I2>
46 LinearInterpolation(const I1& xBegin, const I1& xEnd,
47 const I2& yBegin) {
48 impl_ = ext::shared_ptr<Interpolation::Impl>(new
50 yBegin));
51 impl_->update();
52 }
53 };
54
56
57 class Linear {
58 public:
59 template <class I1, class I2>
60 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
61 const I2& yBegin) const {
62 return LinearInterpolation(xBegin, xEnd, yBegin);
63 }
64 static const bool global = false;
65 static const Size requiredPoints = 2;
66 };
67
68 namespace detail {
69
70 template <class I1, class I2>
72 : public Interpolation::templateImpl<I1,I2> {
73 public:
74 LinearInterpolationImpl(const I1& xBegin, const I1& xEnd,
75 const I2& yBegin)
76 : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
77 Linear::requiredPoints),
78 primitiveConst_(xEnd-xBegin), s_(xEnd-xBegin) {}
79 void update() override {
80 primitiveConst_[0] = 0.0;
81 for (Size i=1; i<Size(this->xEnd_-this->xBegin_); ++i) {
82 Real dx = this->xBegin_[i]-this->xBegin_[i-1];
83 s_[i-1] = (Real(this->yBegin_[i])-Real(this->yBegin_[i-1]))/dx;
85 + dx*(this->yBegin_[i-1] +0.5*dx*s_[i-1]);
86 }
87 }
88 Real value(Real x) const override {
89 Size i = this->locate(x);
90 return this->yBegin_[i] + (x-this->xBegin_[i])*s_[i];
91 }
92 Real primitive(Real x) const override {
93 Size i = this->locate(x);
94 Real dx = x-this->xBegin_[i];
95 return primitiveConst_[i] +
96 dx*(this->yBegin_[i] + 0.5*dx*s_[i]);
97 }
98 Real derivative(Real x) const override {
99 Size i = this->locate(x);
100 return s_[i];
101 }
102 Real secondDerivative(Real) const override { return 0.0; }
103
104 private:
105 std::vector<Real> primitiveConst_, s_;
106 };
107
108 }
109
110}
111
112#endif
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
ext::shared_ptr< Impl > impl_
Linear-interpolation factory and traits
static const bool global
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Linear interpolation between discrete points
LinearInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
LinearInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
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