1 | // $Id: interpolation_test.cc 1661 2008-12-18 16:11:21Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 Jari Häkkinen |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "Suite.h" |
---|
23 | |
---|
24 | #include "yat/regression/AkimaInterpolation.h" |
---|
25 | #include "yat/regression/AkimaPeriodicInterpolation.h" |
---|
26 | #include "yat/regression/CSplineInterpolation.h" |
---|
27 | #include "yat/regression/CSplinePeriodicInterpolation.h" |
---|
28 | #include "yat/regression/LinearInterpolation.h" |
---|
29 | #include "yat/regression/PolynomialInterpolation.h" |
---|
30 | #include "yat/utility/Vector.h" |
---|
31 | #include "yat/utility/VectorConstView.h" |
---|
32 | |
---|
33 | #include <cmath> |
---|
34 | |
---|
35 | using namespace theplu::yat; |
---|
36 | void test_delete(test::Suite&); |
---|
37 | |
---|
38 | int main(int argc, char* argv[]) |
---|
39 | { |
---|
40 | using namespace theplu::yat::regression; |
---|
41 | |
---|
42 | test::Suite suite(argc, argv); |
---|
43 | test_delete(suite); |
---|
44 | |
---|
45 | /* |
---|
46 | The test data is computed with R version 2.7.2 (2008-08-25) on |
---|
47 | MacBook Pro Intel CPU running MacOSX 10.5.5 |
---|
48 | |
---|
49 | x <- c(13,17,19,22,27,35) |
---|
50 | y <- c(100,97,111,120,117,103) |
---|
51 | cspline <- splinefun(x,y,"natural") |
---|
52 | w <- seq(15,25,1) |
---|
53 | z <- cspline(w) |
---|
54 | z |
---|
55 | [1] 94.11579 93.91382 97.00000 103.66776 111.00000 116.02563 118.76822 |
---|
56 | [8] 120.00000 120.38499 120.15443 119.43138 |
---|
57 | */ |
---|
58 | |
---|
59 | utility::Vector x(6); |
---|
60 | utility::Vector y(6); |
---|
61 | x(0)= 13; x(1)= 17; x(2)= 19; x(3)= 22; x(4)= 27; x(5)= 35; |
---|
62 | y(0)=100; y(1)= 97; y(2)=111; y(3)=120; y(4)=117; y(5)=103; |
---|
63 | |
---|
64 | CSplineInterpolation cspline(x,y); |
---|
65 | |
---|
66 | utility::Vector w(11); |
---|
67 | utility::Vector z(11); |
---|
68 | w( 0)=15; z( 0)= 94.11579; |
---|
69 | w( 1)=16; z( 1)= 93.91382; |
---|
70 | w( 2)=17; z( 2)= 97; |
---|
71 | w( 3)=18; z( 3)=103.66776; |
---|
72 | w( 4)=19; z( 4)=111; |
---|
73 | w( 5)=20; z( 5)=116.02563; |
---|
74 | w( 6)=21; z( 6)=118.76822; |
---|
75 | w( 7)=22; z( 7)=120; |
---|
76 | w( 8)=23; z( 8)=120.38499; |
---|
77 | w( 9)=24; z( 9)=120.15443; |
---|
78 | w(10)=25; z(10)=119.43138; |
---|
79 | |
---|
80 | for (size_t i=0; i<w.size(); ++i) { |
---|
81 | if ( fabs(cspline.evaluate(w(i))-z(i)) > 1e-5) { |
---|
82 | suite.err() << "cspline test failed for i=" << i << std::endl; |
---|
83 | suite.err() << " difference between target and interpolation too large: " |
---|
84 | << fabs(cspline.evaluate(w(i))-z(i)) << std::endl; |
---|
85 | suite.add(false); |
---|
86 | } |
---|
87 | // Check for errors |
---|
88 | if (cspline.error_status()) { |
---|
89 | suite.err() << "cspline test failed with GSL_EDOM error" << std::endl; |
---|
90 | suite.add(false); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | // An error should be generated, if not test fails |
---|
95 | cspline.evaluate(w(0)-5); |
---|
96 | if (!cspline.error_status()) { |
---|
97 | suite.err() << "cspline test failed no GSL_EDOM error occured" << std::endl; |
---|
98 | suite.add(false); |
---|
99 | } |
---|
100 | |
---|
101 | // lazy testing, at least try to create the objects |
---|
102 | AkimaInterpolation(x,y); |
---|
103 | AkimaPeriodicInterpolation(x,y); |
---|
104 | CSplinePeriodicInterpolation(x,y); |
---|
105 | LinearInterpolation(x,y); |
---|
106 | PolynomialInterpolation(x,y); |
---|
107 | |
---|
108 | return suite.return_value(); |
---|
109 | } |
---|
110 | |
---|
111 | void test_delete(test::Suite& suite) |
---|
112 | { |
---|
113 | utility::Vector x(10); |
---|
114 | for (size_t i=0; i<x.size(); ++i) |
---|
115 | x(i)=i; |
---|
116 | regression::GSLInterpolation* ip = new regression::CSplineInterpolation(x,x); |
---|
117 | delete ip; |
---|
118 | } |
---|
119 | |
---|