ProteoWizard
CubicHermiteSplineTest.cpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Austin Keller <atkeller .@. uw.edu>
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 
23 
24 using namespace pwiz::util;
25 using namespace pwiz::analysis;
26 
28 public:
29  void Run()
30  {
31  SetUp();
32  InterpolationTest();
33  ErrorTest();
34  TearDown();
35  }
36 
37 protected:
38 
39  virtual void SetUp()
40  {
41  }
42 
43  void TearDown()
44  {
45  }
46 
48  {
49  vector<double> x;
50  vector<double> y;
51  x.push_back(3.1415);
52  x.push_back(3.63753);
53  x.push_back(4.13355);
54  x.push_back(4.62958);
55  x.push_back(5.12561);
56  x.push_back(5.62163);
57  x.push_back(6.11766);
58  x.push_back(6.61368);
59  x.push_back(7.10971);
60  x.push_back(7.60574);
61  x.push_back(8.10176);
62  x.push_back(8.59779);
63  x.push_back(9.09382);
64  x.push_back(9.58984);
65  x.push_back(10.0859);
66  x.push_back(10.5819);
67  x.push_back(11.0779);
68  x.push_back(11.5739);
69  x.push_back(12.07);
70  x.push_back(12.566);
71  y.push_back(-5.89868e-005);
72  y.push_back(0.233061);
73  y.push_back(0.216427);
74  y.push_back(0.0486148);
75  y.push_back(-0.133157);
76  y.push_back(-0.172031);
77  y.push_back(-0.0456079);
78  y.push_back(0.0906686);
79  y.push_back(0.116462);
80  y.push_back(0.0557287);
81  y.push_back(-0.03875);
82  y.push_back(-0.10346);
83  y.push_back(-0.0734111);
84  y.push_back(0.0298435);
85  y.push_back(0.094886);
86  y.push_back(0.0588743);
87  y.push_back(-0.0171021);
88  y.push_back(-0.0630512);
89  y.push_back(-0.0601684);
90  y.push_back(-0.00994154);
91 
92  vector<double> xIn;
93  vector<double> yIn;
94  const double PI = 3.1415;
95  const int N = 12;
96  double xx = PI;
97  double step = 4 * PI / (N - 1);
98  for (int i = 0; i < N; ++i, xx += step)
99  {
100  xIn.push_back(xx);
101  yIn.push_back(sin(2 * xx) / xx);
102  }
103 
104  CubicHermiteSpline interpolator(xIn, yIn);
105  const int N_out = 20;
106  xx = PI;
107  step = (3 * PI) / (N_out - 1);
108  for (int i = 0; i < N_out; ++i, xx += step)
109  {
110  double interpolatedX = interpolator.Interpolate(xx);
111  double diff = abs(interpolatedX - y[i]);
112  unit_assert(diff < 1.0);
113  }
114  }
115 
116  void CubicSplineDummyInitialize(const vector<double>& x, const vector<double>& y)
117  {
118  CubicHermiteSpline interpolator(x, y);
119  }
120 
121  void ErrorTest()
122  {
123  // Should throw on empty data
124  vector<double> x;
125  vector<double> y;
126  unit_assert_throws_what(CubicSplineDummyInitialize(x, y), std::runtime_error, "[CubicHermiteSpline] unusable values were provided to the spline function.");
127 
128  // Should throw on unsorted x data
129  x.clear();
130  y.clear();
131  x.push_back(1.0);
132  x.push_back(0.0);
133  x.push_back(2.0);
134  for (size_t i = 0; i < x.size(); ++i)
135  {
136  y.push_back(0.0);
137  }
138  unit_assert_throws_what(CubicSplineDummyInitialize(x, y), std::runtime_error, "[CubicHermiteSpline] unusable values were provided to the spline function.");
139 
140  // Should throw on non-unique x data
141  x.clear();
142  y.clear();
143  x.push_back(1.0);
144  x.push_back(2.0);
145  x.push_back(2.0);
146  for (size_t i = 0; i < x.size(); ++i)
147  {
148  y.push_back(0.0);
149  }
150  unit_assert_throws_what(CubicSplineDummyInitialize(x, y), std::runtime_error, "[CubicHermiteSpline] unusable values were provided to the spline function.");
151 
152  // Should throw on different lengths of x and y data
153  x.clear();
154  y.clear();
155  x.push_back(1.0);
156  x.push_back(2.0);
157  x.push_back(3.0);
158  for (size_t i = 0; i < x.size(); ++i)
159  {
160  y.push_back(0.0);
161  }
162  y.push_back(0.0); // add one more to y data
163  unit_assert_throws_what(CubicSplineDummyInitialize(x, y), std::runtime_error, "[CubicHermiteSpline] unusable values were provided to the spline function.");
164 
165  // Should throw on different lengths of x and y data
166  x.clear();
167  y.clear();
168  x.push_back(1.0);
169  x.push_back(2.0);
170  x.push_back(3.0);
171  y.push_back(0.0);
172  y.push_back(0.0);
173  unit_assert_throws_what(CubicSplineDummyInitialize(x, y), std::runtime_error, "[CubicHermiteSpline] unusable values were provided to the spline function.");
174  }
175 
177  {
178  vector<double> x;
179  vector<double> y;
180  x.push_back(1.0);
181  x.push_back(2.0);
182  x.push_back(3.0);
183  x.push_back(4.0);
184  y.push_back(1.0);
185  y.push_back(2.0);
186  y.push_back(3.0);
187  y.push_back(6.0);
188 
189  CubicHermiteSpline interpolator(x, y);
190 
191  unit_assert(!interpolator.IsDifferentiable());
192  unit_assert(!interpolator.IsIntegrable());
193 
194  if (interpolator.IsDifferentiable())
195  {
196  // Differentiation is not implemented. This is a placeholder for if the feature is added.
197  unit_assert(false);
198 
199  // Placeholder test of differentiation
200  unit_assert_equal(interpolator.Differentiate(2.0), 1.0, 0.00001);
201  }
202  else
203  {
204  unit_assert_throws_what(interpolator.Differentiate(2.0), runtime_error, "[CubicHermiteSpline] attempted to differentiate a non-differentiable implementation of IInterpolation.")
205  }
206 
207  if (interpolator.IsIntegrable())
208  {
209  // Integration is not implemented. This is a placeholder for if the feature is added.
210  unit_assert(false);
211 
212  // Placeholder test of integration
213  unit_assert_equal(interpolator.Integrate(1.0, 2.0), 0.5, 0.00001);
214  }
215  else
216  {
217  unit_assert_throws_what(interpolator.Integrate(1.0, 2.0), runtime_error, "[CubicHermiteSpline] attempted to integrate a non-integrable implementation of IInterpolation.")
218  }
219  }
220 };
221 
222 int main(int argc, char* argv[])
223 {
224  TEST_PROLOG(argc, argv)
225 
226  try
227  {
228  CubicHermiteSplineTest tester;
229  tester.Run();
230  }
231  catch (exception& e)
232  {
233  TEST_FAILED(e.what())
234  }
235  catch (...)
236  {
237  TEST_FAILED("Caught unknown exception.")
238  }
239 
241 }
main
int main(int argc, char *argv[])
Definition: CubicHermiteSplineTest.cpp:222
unit_assert_throws_what
#define unit_assert_throws_what(x, exception, whatStr)
Definition: unit.hpp:119
unit_assert_equal
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
CubicHermiteSplineTest::SetUp
virtual void SetUp()
Definition: CubicHermiteSplineTest.cpp:39
pwiz::analysis::CubicHermiteSpline
An implementation of the IInterpolation interface that acts as a wrapper for a cSpline.
Definition: CubicHermiteSpline.hpp:34
CubicHermiteSplineTest::DiffAndIntegrateTest
void DiffAndIntegrateTest()
Definition: CubicHermiteSplineTest.cpp:176
pwiz::analysis
Definition: ChromatogramList_Filter.hpp:37
CubicHermiteSplineTest::Run
void Run()
Definition: CubicHermiteSplineTest.cpp:29
y
KernelTraitsBase< Kernel >::space_type::ordinate_type y
Definition: MatchedFilter.hpp:143
CubicHermiteSplineTest
Definition: CubicHermiteSplineTest.cpp:27
pwiz::util
Definition: almost_equal.hpp:33
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
CubicHermiteSplineTest::InterpolationTest
void InterpolationTest()
Definition: CubicHermiteSplineTest.cpp:47
Std.hpp
diff
void diff(const string &filename1, const string &filename2)
Definition: FrequencyDataTest.cpp:40
x
KernelTraitsBase< Kernel >::space_type::abscissa_type x
Definition: MatchedFilter.hpp:142
pwiz::analysis::CubicHermiteSpline::IsDifferentiable
bool IsDifferentiable() override
Indicates whether the algorithm can provide an interpolated derivative.
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
pwiz::analysis::CubicHermiteSpline::Differentiate
double Differentiate(double x) override
Derivative at the point x.
pwiz::analysis::CubicHermiteSpline::Interpolate
double Interpolate(double x) override
Interpolate at point x.
pwiz::analysis::CubicHermiteSpline::Integrate
double Integrate(double a, double b) override
Definite integral between points a and b over function f.
CubicHermiteSplineTest::CubicSplineDummyInitialize
void CubicSplineDummyInitialize(const vector< double > &x, const vector< double > &y)
Definition: CubicHermiteSplineTest.cpp:116
CubicHermiteSplineTest::ErrorTest
void ErrorTest()
Definition: CubicHermiteSplineTest.cpp:121
ralab::constants::PI
const double PI(3.14159265358979323846264338327950288)
the ratio of the circumference of a circle to its diameter;
unit.hpp
unit_assert
#define unit_assert(x)
Definition: unit.hpp:85
N
N
Definition: Chemistry.hpp:80
pwiz::analysis::CubicHermiteSpline::IsIntegrable
bool IsIntegrable() override
Indicates whether the algorithm can provide an interpolated integral.
CubicHermiteSplineTest::TearDown
void TearDown()
Definition: CubicHermiteSplineTest.cpp:43
CubicHermiteSpline.hpp