source: trunk/test/interpolation_test.cc @ 1724

Last change on this file since 1724 was 1724, checked in by Jari Häkkinen, 12 years ago

Fixes #466. GSL_error is now thrown on GSL errors. Last thing in this ticket.

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