File: pchip_test.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (272 lines) | stat: -rw-r--r-- 7,883 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright Nick Thompson, 2020
 * Use, modification and distribution are subject to the
 * Boost Software License, Version 1.0. (See accompanying file
 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include "math_unit_test.hpp"
#include <numeric>
#include <utility>
#include <random>
#include <boost/math/interpolators/pchip.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/assert.hpp>
#ifdef BOOST_HAS_FLOAT128
#include <boost/multiprecision/float128.hpp>
using boost::multiprecision::float128;
#endif

#if __has_include(<stdfloat>)
#  include <stdfloat>
#endif

using boost::math::interpolators::pchip;

template<typename Real>
void test_constant()
{

    std::vector<Real> x{0,1,2,3, 9, 22, 81};
    std::vector<Real> y(x.size());
    for (auto & t : y) {
        t = 7;
    }

    auto x_copy = x;
    auto y_copy = y;
    auto pchip_spline = pchip(std::move(x_copy), std::move(y_copy));
    //std::cout << "Constant value pchip spline = " << pchip_spline << "\n";

    for (Real t = x[0]; t <= x.back(); t += Real(0.25)) {
        CHECK_ULP_CLOSE(Real(7), pchip_spline(t), 2);
        CHECK_ULP_CLOSE(Real(0), pchip_spline.prime(t), 2);
    }

    boost::circular_buffer<Real> x_buf(x.size());
    for (auto & t : x) {
        x_buf.push_back(t);
    }

    boost::circular_buffer<Real> y_buf(x.size());
    for (auto & t : y) {
        y_buf.push_back(t);
    }

    auto circular_pchip_spline = pchip(std::move(x_buf), std::move(y_buf));

    for (Real t = x[0]; t <= x.back(); t += Real(0.25)) {
        CHECK_ULP_CLOSE(Real(7), circular_pchip_spline(t), 2);
        CHECK_ULP_CLOSE(Real(0), pchip_spline.prime(t), 2);
    }

    circular_pchip_spline.push_back(x.back() + 1, 7);
    CHECK_ULP_CLOSE(Real(0), circular_pchip_spline.prime(x.back()+1), 2);

}

template<typename Real>
void test_linear()
{
    std::vector<Real> x{0,1,2,3};
    std::vector<Real> y{0,1,2,3};

    auto x_copy = x;
    auto y_copy = y;
    auto pchip_spline = pchip(std::move(x_copy), std::move(y_copy));

    CHECK_ULP_CLOSE(y[0], pchip_spline(x[0]), 0);
    CHECK_ULP_CLOSE(Real(1)/Real(2), pchip_spline(Real(1)/Real(2)), 10);
    CHECK_ULP_CLOSE(y[1], pchip_spline(x[1]), 0);
    CHECK_ULP_CLOSE(Real(3)/Real(2), pchip_spline(Real(3)/Real(2)), 10);
    CHECK_ULP_CLOSE(y[2], pchip_spline(x[2]), 0);
    CHECK_ULP_CLOSE(Real(5)/Real(2), pchip_spline(Real(5)/Real(2)), 10);
    CHECK_ULP_CLOSE(y[3], pchip_spline(x[3]), 0);

    x.resize(45);
    y.resize(45);
    for (size_t i = 0; i < x.size(); ++i) {
        x[i] = i;
        y[i] = i;
    }

    x_copy = x;
    y_copy = y;
    pchip_spline = pchip(std::move(x_copy), std::move(y_copy));
    for (Real t = 0; t < x.back(); t += Real(0.5)) {
        CHECK_ULP_CLOSE(t, pchip_spline(t), 0);
        CHECK_ULP_CLOSE(Real(1), pchip_spline.prime(t), 0);
    }

    x_copy = x;
    y_copy = y;
    // Test endpoint derivatives:
    pchip_spline = pchip(std::move(x_copy), std::move(y_copy), Real(1), Real(1));
    for (Real t = 0; t < x.back(); t += Real(0.5)) {
        CHECK_ULP_CLOSE(t, pchip_spline(t), 0);
        CHECK_ULP_CLOSE(Real(1), pchip_spline.prime(t), 0);
    }


    boost::circular_buffer<Real> x_buf(x.size());
    for (auto & t : x) {
        x_buf.push_back(t);
    }

    boost::circular_buffer<Real> y_buf(x.size());
    for (auto & t : y) {
        y_buf.push_back(t);
    }

    auto circular_pchip_spline = pchip(std::move(x_buf), std::move(y_buf));

    for (Real t = x[0]; t <= x.back(); t += Real(0.25)) {
        CHECK_ULP_CLOSE(t, circular_pchip_spline(t), 2);
        CHECK_ULP_CLOSE(Real(1), circular_pchip_spline.prime(t), 2);
    }

    circular_pchip_spline.push_back(x.back() + 1, y.back()+1);

    CHECK_ULP_CLOSE(Real(y.back() + 1), circular_pchip_spline(Real(x.back()+1)), 2);
    CHECK_ULP_CLOSE(Real(1), circular_pchip_spline.prime(Real(x.back()+1)), 2);

}

template<typename Real>
void test_interpolation_condition()
{
    for (size_t n = 4; n < 50; ++n) {
        std::vector<Real> x(n);
        std::vector<Real> y(n);
        std::default_random_engine rd;
        std::uniform_real_distribution<Real> dis(0,1);
        Real x0 = dis(rd);
        x[0] = x0;
        y[0] = dis(rd);
        for (size_t i = 1; i < n; ++i) {
            x[i] = x[i-1] + dis(rd);
            y[i] = dis(rd);
        }

        auto x_copy = x;
        auto y_copy = y;
        auto s = pchip(std::move(x_copy), std::move(y_copy));
        //std::cout << "s = " << s << "\n";
        for (size_t i = 0; i < x.size(); ++i) {
            CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
        }

        x_copy = x;
        y_copy = y;
        // The interpolation condition is not affected by the endpoint derivatives, even though these derivatives might be super weird:
        s = pchip(std::move(x_copy), std::move(y_copy), Real(0), Real(0));
        //std::cout << "s = " << s << "\n";
        for (size_t i = 0; i < x.size(); ++i) {
            CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
        }

    }
}

template<typename Real>
void test_monotonicity()
{
    for (size_t n = 4; n < 50; ++n) {
        std::vector<Real> x(n);
        std::vector<Real> y(n);
        std::default_random_engine rd;
        std::uniform_real_distribution<Real> dis(0,1);
        Real x0 = dis(rd);
        x[0] = x0;
        y[0] = dis(rd);
        // Monotone increasing:
        for (size_t i = 1; i < n; ++i) {
            x[i] = x[i-1] + dis(rd);
            y[i] = y[i-1] + dis(rd);
        }

        auto x_copy = x;
        auto y_copy = y;
        auto s = pchip(std::move(x_copy), std::move(y_copy));
        //std::cout << "s = " << s << "\n";
        for (size_t i = 0; i < x.size() - 1; ++i) {
            Real tmin = x[i];
            Real tmax = x[i+1];
            Real val = y[i];
            CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
            for (Real t = tmin; t < tmax; t += (tmax-tmin)/16) {
                Real greater_val = s(t);
                BOOST_ASSERT(val <= greater_val);
                val = greater_val;
            }
        }


        x[0] = dis(rd);
        y[0] = dis(rd);
        // Monotone decreasing:
        for (size_t i = 1; i < n; ++i) {
            x[i] = x[i-1] + dis(rd);
            y[i] = y[i-1] - dis(rd);
        }

        x_copy = x;
        y_copy = y;
        s = pchip(std::move(x_copy), std::move(y_copy));
        //std::cout << "s = " << s << "\n";
        for (size_t i = 0; i < x.size() - 1; ++i) {
            Real tmin = x[i];
            Real tmax = x[i+1];
            Real val = y[i];
            CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
            for (Real t = tmin; t < tmax; t += (tmax-tmin)/16) {
                Real lesser_val = s(t);
                BOOST_ASSERT(val >= lesser_val);
                val = lesser_val;
            }
        }

    }
}


int main()
{
#if (__GNUC__ > 7) || defined(_MSC_VER) || defined(__clang__)
    
    #ifdef __STDCPP_FLOAT32_T__
    test_constant<std::float32_t>();
    test_linear<std::float32_t>();
    test_interpolation_condition<std::float32_t>();
    test_monotonicity<std::float32_t>();
    #else
    test_constant<float>();
    test_linear<float>();
    test_interpolation_condition<float>();
    test_monotonicity<float>();
    #endif

    #ifdef __STDCPP_FLOAT64_T__
    test_constant<std::float64_t>();
    test_linear<std::float64_t>();
    test_interpolation_condition<std::float64_t>();
    test_monotonicity<std::float64_t>();
    #else
    test_constant<double>();
    test_linear<double>();
    test_interpolation_condition<double>();
    test_monotonicity<double>();
    #endif

    test_constant<long double>();
    test_linear<long double>();
    test_interpolation_condition<long double>();
    test_monotonicity<long double>();

    #ifdef BOOST_HAS_FLOAT128
    test_constant<float128>();
    test_linear<float128>();
    #endif
#endif
    return boost::math::test::report_errors();
}