ProteoWizard
ParabolaTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: ParabolaTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2006 Louis Warschaw Prostate Cancer Center
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #include "Parabola.hpp"
27 #include <cmath>
28 #include <limits>
29 #include <cstring>
30 
31 
32 using namespace pwiz::util;
33 using namespace pwiz::math;
34 
35 
36 ostream* os_ = 0;
38 
39 
40 void testBasic()
41 {
42  if (os_) *os_ << "***************************\n";
43  if (os_) *os_ << "testBasic()\n";
44  Parabola p(2, 3, 4);
45  unit_assert_equal(p(5), 69, epsilon_);
46  p.coefficients()[0] = 3;
47  unit_assert_equal(p(5), 94, epsilon_);
48  if (os_) *os_ << "testBasic(): success\n";
49 }
50 
51 
53 {
54  if (os_) *os_ << "***************************\n";
55  if (os_) *os_ << "testExactFit()\n";
56 
57  vector< pair<double,double> > samples;
58  samples.push_back(make_pair(1,1));
59  samples.push_back(make_pair(2,3));
60  samples.push_back(make_pair(3,9));
61 
62  const Parabola p(samples);
63 
64  if (os_) *os_ << p << endl;
65  if (os_) *os_ << "center: (" << p.center() << ", " << p(p.center()) << ")\n";
66 
67  const vector<double>& a = p.coefficients();
68 
69  unit_assert_equal(a[0], 2, epsilon_*10);
70  unit_assert_equal(a[1], -4, epsilon_*10);
71 
72  unit_assert_equal(a[2], 3, epsilon_*5);
74  unit_assert_equal(p(p.center()), 1, epsilon_*10);
75  unit_assert_equal(p(0), 3, epsilon_*5);
76 
77  if (os_) *os_ << "testExactFit(): success\n";
78 }
79 
80 
82 {
83  if (os_) *os_ << "***************************\n";
84  if (os_) *os_ << "testLeastSquares()\n";
85 
86  vector< pair<double,double> > samples;
87  samples.push_back(make_pair(1,1));
88  samples.push_back(make_pair(2,3));
89  samples.push_back(make_pair(3,9));
90  samples.push_back(make_pair(0,3));
91  samples.push_back(make_pair(-1,9));
92 
93  const Parabola p(samples);
94 
95  if (os_) *os_ << p << endl;
96  if (os_) *os_ << "center: (" << p.center() << ", " << p(p.center()) << ")\n";
97 
98  const vector<double>& a = p.coefficients();
99 
100  unit_assert_equal(a[0], 2, epsilon_*10);
101  unit_assert_equal(a[1], -4, epsilon_*100);
102  unit_assert_equal(a[2], 3, epsilon_*10);
103  unit_assert_equal(p.center(), 1, epsilon_*10);
104  unit_assert_equal(p(p.center()), 1, epsilon_*100);
105  unit_assert_equal(p(0), 3, epsilon_*10);
106 
107  if (os_) *os_ << "testLeastSquares(): success\n";
108 }
109 
110 
112 {
113  if (os_) *os_ << "***************************\n";
114  if (os_) *os_ << "testWeightedLeastSquares()\n";
115 
116  // fit to f(x) = 1/sqrt(x*x+1)
117 
118  // samples ( x, 1/(f(x)*f(x)) ), i.e. (x, x*x+1)
119  vector< pair<double,double> > samples;
120  samples.push_back(make_pair(0,1));
121  samples.push_back(make_pair(1,2));
122  samples.push_back(make_pair(2,5));
123  samples.push_back(make_pair(-3,10));
124  samples.push_back(make_pair(-4,17));
125 
126  // weights w = (y^6)/4 => fits data to 1/sqrt(a[0]x^2 + a[1]x + a[2])
127  vector<double> weights;
128  for (unsigned int i=0; i<samples.size(); i++)
129  {
130  double y = samples[i].second;
131  weights.push_back(pow(y,6)/4);
132  }
133 
134  if (os_)
135  {
136  *os_ << "weights: ";
137  copy(weights.begin(), weights.end(), ostream_iterator<double>(*os_, " " ));
138  }
139 
140  const Parabola p(samples, weights);
141 
142  if (os_) *os_ << p << endl;
143  if (os_) *os_ << "center: (" << p.center() << ", " << p(p.center()) << ")\n";
144 
145  if (os_)
146  {
147  *os_ << "coefficients: " << setprecision(14);
148  copy(p.coefficients().begin(), p.coefficients().end(), ostream_iterator<double>(*os_, " "));
149  *os_ << endl;
150  }
151 
152  unit_assert_equal(p.coefficients()[0], 1, epsilon_*1000);
153  unit_assert_equal(p.coefficients()[1], 0, epsilon_*10e4);
154  unit_assert_equal(p.coefficients()[2], 1, epsilon_*10e4);
155  if (os_) *os_ << "testWeightedLeastSquares(): success\n";
156 }
157 
158 
159 int main(int argc, char* argv[])
160 {
161  TEST_PROLOG(argc, argv)
162 
163  try
164  {
165  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
166  if (os_) *os_ << "ParabolaTest\n";
167  testBasic();
168  testExactFit();
171  }
172  catch (exception& e)
173  {
174  TEST_FAILED(e.what())
175  }
176  catch (...)
177  {
178  TEST_FAILED("Caught unknown exception.")
179  }
180 
182 }
183