source: trunk/test/interpolation_test.cc @ 1643

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

Addresses #466 and #425. Added cubic spline interpolation (not complete interface to GSL interpolation stuff).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: interpolation_test.cc 1643 2008-12-13 00:23:39Z jari $
2
3/*
4  Copyright (C) 2007 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/CSpline.h"
25#include "yat/utility/Vector.h"
26#include "yat/utility/VectorConstView.h"
27
28#include <cmath>
29
30int main(int argc, char* argv[])
31{
32  using namespace theplu::yat;
33
34  test::Suite suite(argc, argv);
35
36  /*
37    The test data is computed with R version 2.7.2 (2008-08-25) on
38    MacBook Pro Intel CPU running MacOSX 10.5.5
39
40    x <- c(13,17,19,22,27,35)
41    y <- c(100,97,111,120,117,103)
42    cspline <- splinefun(x,y,"natural")
43    w <- seq(15,25,1)
44    z <- cspline(w)
45    z
46    [1]  94.11579  93.91382  97.00000 103.66776 111.00000 116.02563 118.76822
47    [8] 120.00000 120.38499 120.15443 119.43138
48  */
49
50  utility::Vector x(6);
51  utility::Vector y(6);
52  x(0)= 13; x(1)= 17; x(2)= 19; x(3)= 22; x(4)= 27; x(5)= 35;
53  y(0)=100; y(1)= 97; y(2)=111; y(3)=120; y(4)=117; y(5)=103;
54
55  regression::CSpline cspline(x,y);
56
57  utility::Vector w(11);
58  utility::Vector z(11);
59  w( 0)=15;  z( 0)= 94.11579;
60  w( 1)=16;  z( 1)= 93.91382;
61  w( 2)=17;  z( 2)= 97;
62  w( 3)=18;  z( 3)=103.66776;
63  w( 4)=19;  z( 4)=111;
64  w( 5)=20;  z( 5)=116.02563;
65  w( 6)=21;  z( 6)=118.76822;
66  w( 7)=22;  z( 7)=120;
67  w( 8)=23;  z( 8)=120.38499;
68  w( 9)=24;  z( 9)=120.15443;
69  w(10)=25;  z(10)=119.43138;
70
71  for (size_t i=0; i<w.size(); ++i)
72    if (!suite.equal(cspline.evaluate(w(i)),z(i),1e9)) {
73      suite.err() << "cspline test failed for i=" << i << std::endl;
74      suite.add(false);
75    }
76
77  return suite.return_value();
78}
Note: See TracBrowser for help on using the repository browser.