QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
chebyshevinterpolation.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2021 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#include <ql/math/interpolations/lagrangeinterpolation.hpp>
21#include <ql/math/interpolations/chebyshevinterpolation.hpp>
22
23namespace QuantLib {
24 namespace chebyshev_interpolation_detail {
25 Array apply(const Array& x, const ext::function<Real(Real)>& f) {
26 Array t(x.size());
27 std::transform(std::begin(x), std::end(x), std::begin(t), f);
28
29 return t;
30 }
31 }
32
34 const Array& y, PointsType pointsType)
35 : x_(ChebyshevInterpolation::nodes(y.size(), pointsType)), y_(y) {
36
39 std::begin(x_), std::end(x_), std::begin(y_)
40 );
41
42 impl_->update();
43 }
44
46 Size n, const ext::function<Real(Real)>& f, PointsType pointsType)
48 chebyshev_interpolation_detail::apply(
49 ChebyshevInterpolation::nodes(n, pointsType), f),
50 pointsType) {
51 }
52
53
55 return x_;
56 }
57
59 Array t(n);
60
61 switch(pointsType) {
62 case FirstKind:
63 for (Size i=0; i < n; ++i)
64 t[i] = -std::cos((i+0.5)*M_PI/n);
65 break;
66 case SecondKind:
67 for (Size i=0; i < n; ++i)
68 t[i] = -std::cos(i*M_PI/(n-1));
69 break;
70 default:
71 QL_FAIL("unknonw Chebyshev interpolation points type");
72 }
73 return t;
74 }
75
77 QL_REQUIRE(y.size() == y_.size(),
78 "interpolation override has the wrong length");
79
80 std::copy(y.begin(), y.end(), y_.begin());
81 }
82}
83
1-D array used in linear algebra.
Definition: array.hpp:52
const Real * const_iterator
Definition: array.hpp:124
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
ChebyshevInterpolation(const Array &y, PointsType pointsType=SecondKind)
ext::shared_ptr< Impl > impl_
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Array apply(const Array &x, const ext::function< Real(Real)> &f)
Definition: any.hpp:35