test_interpolation.cpp 2.34 KB
Newer Older
1
#include "catch.hpp"
Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
2
#include "toolboxes/interpolation.h"
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

Vector testFunction1( const Vector& x ) {
    Vector res( x.size() );
    for( unsigned i = 0; i < x.size(); ++i ) {
        res[i] = x[i];
    }
    return res;
}

Vector testFunction2( const Vector& x ) {
    Vector res( x.size() );
    for( unsigned i = 0; i < x.size(); ++i ) {
        res[i] = std::exp( x[i] );
    }
    return res;
}

Vector testFunction3( const Vector& x ) {
    Vector res( x.size() );
    for( unsigned i = 0; i < x.size(); ++i ) {
        res[i] = x[i] * x[i];
    }
    return res;
}

TEST_CASE( "interpolation tests", "[interpolation]" ) {
    SECTION( "linear" ) {
        Vector x   = blaze::linspace( 10, -2.0, 2.0 );
        Vector y   = testFunction1( x );
        Vector xq  = blaze::linspace( 15, -1.0, 1.0 );
        Vector ref = testFunction1( xq );
        Interpolation interp( x, y, Interpolation::linear );
        Vector res = interp( xq );
        REQUIRE( res.size() == ref.size() );
37
        bool errorWithinBounds = true;
38
        for( unsigned i = 0; i < res.size(); ++i ) {
39
            if( std::fabs( res[i] - ref[i] ) > 1e-6 ) errorWithinBounds = false;
40
        }
41
        REQUIRE( errorWithinBounds );
42
43
44
45
46
47
48
49
50
51
    }

    SECTION( "loglinear" ) {
        Vector x   = blaze::linspace( 10, 0.5, 3.0 );
        Vector y   = testFunction2( x );
        Vector xq  = blaze::linspace( 15, 1.0, 2.5 );
        Vector ref = testFunction2( xq );
        Interpolation interp( x, y, Interpolation::loglinear );
        Vector res = interp( xq );
        REQUIRE( res.size() == ref.size() );
52
        bool errorWithinBounds = true;
53
        for( unsigned i = 0; i < res.size(); ++i ) {
54
            if( std::fabs( res[i] - ref[i] ) > 1e-6 ) errorWithinBounds = false;
55
        }
56
        REQUIRE( errorWithinBounds );
57
58
59
60
61
62
63
64
65
66
    }

    SECTION( "cubic" ) {
        Vector x   = blaze::linspace( 30, -2.0, 2.0 );
        Vector y   = testFunction3( x );
        Vector xq  = blaze::linspace( 10, -1.0, 1.0 );
        Vector ref = testFunction3( xq );
        Interpolation interp( x, y, Interpolation::cubic );
        Vector res = interp( xq );
        REQUIRE( res.size() == ref.size() );
67
        bool errorWithinBounds = true;
68
        for( unsigned i = 0; i < res.size(); ++i ) {
69
            if( std::fabs( res[i] - ref[i] ) > 1e-6 ) errorWithinBounds = false;
70
        }
71
        REQUIRE( errorWithinBounds );
72
73
    }
}