Coverage Report

Created: 2026-03-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/interpolations/chebyshevinterpolation.cpp
Line
Count
Source
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
 <https://www.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
23
namespace QuantLib {
24
    namespace chebyshev_interpolation_detail {
25
0
        Array apply(const Array& x, const std::function<Real(Real)>& f) {
26
0
            Array t(x.size());
27
0
            std::transform(std::begin(x), std::end(x), std::begin(t), f);
28
29
0
            return t;
30
0
        }
31
    }
32
33
    ChebyshevInterpolation::ChebyshevInterpolation(
34
        const Array& y, PointsType pointsType)
35
0
    : x_(ChebyshevInterpolation::nodes(y.size(), pointsType)), y_(y) {
36
37
0
        impl_ = ext::make_shared<detail::LagrangeInterpolationImpl<
38
0
            Array::const_iterator, Array::const_iterator> >(
39
0
            std::begin(x_), std::end(x_), std::begin(y_)
40
0
        );
41
42
0
        impl_->update();
43
0
    }
44
45
    ChebyshevInterpolation::ChebyshevInterpolation(
46
        Size n, const std::function<Real(Real)>& f, PointsType pointsType)
47
0
    : ChebyshevInterpolation(
48
0
          chebyshev_interpolation_detail::apply(
49
0
              ChebyshevInterpolation::nodes(n, pointsType), f),
50
0
              pointsType) {
51
0
    }
52
53
54
0
    Array ChebyshevInterpolation::nodes() const {
55
0
        return x_;
56
0
    }
57
58
0
    Array ChebyshevInterpolation::nodes(Size n, PointsType pointsType) {
59
0
        Array t(n);
60
61
0
        switch(pointsType) {
62
0
          case FirstKind:
63
0
            for (Size i=0; i < n; ++i)
64
0
                t[i] = -std::cos((i+0.5)*M_PI/n);
65
0
            break;
66
0
          case SecondKind:
67
0
            for (Size i=0; i < n; ++i)
68
0
                t[i] = -std::cos(i*M_PI/(n-1));
69
0
            break;
70
0
          default:
71
0
              QL_FAIL("unknonw Chebyshev interpolation points type");
72
0
        }
73
0
        return t;
74
0
    }
75
76
0
    void ChebyshevInterpolation::updateY(const Array& y) {
77
0
        QL_REQUIRE(y.size() == y_.size(),
78
0
            "interpolation override has the wrong length");
79
80
0
        std::copy(y.begin(), y.end(), y_.begin());
81
0
    }
82
}
83