testPolynomialInterpolation.cc
Go to the documentation of this file.
1 
11 #include <cmath>
12 #include <iostream>
13 #include <vector>
14 #include <fstream>
15 
16 #include <utl/Math.h>
17 #include <utl/PolynomialInterpolation.h>
18 
19 #include <tst/Verify.h>
20 #include <cppunit/extensions/HelperMacros.h>
21 
22 using namespace std;
23 using namespace utl;
24 using namespace tst;
25 
26 
30 class PolynomialInterpolationTest : public CppUnit::TestFixture {
31 
32  CPPUNIT_TEST_SUITE(PolynomialInterpolationTest);
33  CPPUNIT_TEST(testSimple);
34  CPPUNIT_TEST_SUITE_END();
35 
36 public:
37  void
39  {
40  fX.push_back(-1); fY.push_back(0);
41  fX.push_back(0); fY.push_back(1);
42  fX.push_back(1); fY.push_back(0);
43  fX.push_back(2); fY.push_back(2);
44  fX.push_back(3); fY.push_back(-1);
45  }
46 
47  void tearDown() { }
48 
49  void
51  {
52  const double np = 100;
53  for (unsigned int n = 0; n <= fX.size(); ++n)
54  for (int i = 0; i <= np; ++i) {
55  const double x = -2 + i*6./np;
56  double y = PolynomialInterpolation(n, &fX[0], &fY[0], x);
57  const double expected = ExpectedValue(n, x);
58  CPPUNIT_ASSERT(Verify<CloseTo>(y, expected));
59  double dy;
60  y = PolynomialInterpolation(n, &fX[0], &fY[0], x, dy);
61  CPPUNIT_ASSERT(Verify<CloseTo>(y, expected));
62  }
63  }
64 
65 private:
66  vector<double> fX;
67  vector<double> fY;
68 
69  double
70  ExpectedValue(const unsigned int n, const double x)
71  {
72  switch (n) {
73  case 0:
74  return 0;
75  case 1:
76  return 0;
77  case 2:
78  return 1+x;
79  case 3:
80  return 1-Sqr(x);
81  case 4:
82  return 1-5*x/6-Sqr(x)+5*pow(x, 3)/6;
83  case 5:
84  return 1-23*x/12-11*Sqr(x)/24+23*pow(x, 3)/12-13*pow(x, 4)/24;
85  }
86  cerr << "ERROR!" << endl;
87  return 0;
88  }
89 
90 };
91 
92 
94 
95 
96 // Configure (x)emacs for this file ...
97 // Local Variables:
98 // mode: c++
99 // End:
constexpr T Sqr(const T &x)
double pow(const double x, const unsigned int i)
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
double PolynomialInterpolation(const unsigned int n, const double px[], const double py[], const double x, double &dy)
Perform polynomial interpolation or extrapolation.
double ExpectedValue(const unsigned int n, const double x)

, generated on Tue Sep 26 2023.