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