source: trunk/test/interpolation_test.cc @ 1659

Last change on this file since 1659 was 1656, checked in by Jari Häkkinen, 13 years ago

Rearragned code to avoid compiler warning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: interpolation_test.cc 1656 2008-12-17 13:52:51Z jari $
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
35int main(int argc, char* argv[])
36{
37  using namespace theplu::yat;
38  using namespace theplu::yat::regression;
39
40  test::Suite suite(argc, argv);
41
42  /*
43    The test data is computed with R version 2.7.2 (2008-08-25) on
44    MacBook Pro Intel CPU running MacOSX 10.5.5
45
46    x <- c(13,17,19,22,27,35)
47    y <- c(100,97,111,120,117,103)
48    cspline <- splinefun(x,y,"natural")
49    w <- seq(15,25,1)
50    z <- cspline(w)
51    z
52    [1]  94.11579  93.91382  97.00000 103.66776 111.00000 116.02563 118.76822
53    [8] 120.00000 120.38499 120.15443 119.43138
54  */
55
56  utility::Vector x(6);
57  utility::Vector y(6);
58  x(0)= 13; x(1)= 17; x(2)= 19; x(3)= 22; x(4)= 27; x(5)= 35;
59  y(0)=100; y(1)= 97; y(2)=111; y(3)=120; y(4)=117; y(5)=103;
60
61  CSplineInterpolation cspline(x,y);
62
63  utility::Vector w(11);
64  utility::Vector z(11);
65  w( 0)=15;  z( 0)= 94.11579;
66  w( 1)=16;  z( 1)= 93.91382;
67  w( 2)=17;  z( 2)= 97;
68  w( 3)=18;  z( 3)=103.66776;
69  w( 4)=19;  z( 4)=111;
70  w( 5)=20;  z( 5)=116.02563;
71  w( 6)=21;  z( 6)=118.76822;
72  w( 7)=22;  z( 7)=120;
73  w( 8)=23;  z( 8)=120.38499;
74  w( 9)=24;  z( 9)=120.15443;
75  w(10)=25;  z(10)=119.43138;
76
77  for (size_t i=0; i<w.size(); ++i) {
78    if ( fabs(cspline.evaluate(w(i))-z(i)) > 1e-5) {
79      suite.err() << "cspline test failed for i=" << i << std::endl;
80      suite.err() << " difference between target and interpolation too large: "
81                  << fabs(cspline.evaluate(w(i))-z(i)) << std::endl;
82      suite.add(false);
83    }
84    // Check for errors
85    if (cspline.error_status()) {
86      suite.err() << "cspline test failed with GSL_EDOM error" << std::endl;
87      suite.add(false);
88    }
89  }
90
91  // An error should be generated, if not test fails
92  cspline.evaluate(w(0)-5);
93  if (!cspline.error_status()) {
94    suite.err() << "cspline test failed no GSL_EDOM error occured" << std::endl;
95    suite.add(false);
96  }
97
98  // lazy testing, at least try to create the objects
99  AkimaInterpolation(x,y);
100  AkimaPeriodicInterpolation(x,y);
101  CSplinePeriodicInterpolation(x,y);
102  LinearInterpolation(x,y);
103  PolynomialInterpolation(x,y);
104
105  return suite.return_value();
106}
Note: See TracBrowser for help on using the repository browser.