ProteoWizard
Functions | Variables
ParabolaTest.cpp File Reference
#include "Parabola.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include <cmath>
#include <limits>
#include <cstring>

Go to the source code of this file.

Functions

void testBasic ()
 
void testExactFit ()
 
void testLeastSquares ()
 
void testWeightedLeastSquares ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
double epsilon_ = numeric_limits<double>::epsilon()
 

Function Documentation

§ testBasic()

void testBasic ( )

Definition at line 40 of file ParabolaTest.cpp.

References pwiz::math::Parabola::coefficients(), epsilon_, os_, and unit_assert_equal.

Referenced by main().

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 }
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
ostream * os_
double epsilon_

§ testExactFit()

void testExactFit ( )

Definition at line 52 of file ParabolaTest.cpp.

References pwiz::math::Parabola::center(), pwiz::math::Parabola::coefficients(), epsilon_, os_, and unit_assert_equal.

Referenced by main().

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);
73  unit_assert_equal(p.center(), 1, epsilon_);
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 }
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
ostream * os_
double epsilon_

§ testLeastSquares()

void testLeastSquares ( )

Definition at line 81 of file ParabolaTest.cpp.

References pwiz::math::Parabola::center(), pwiz::math::Parabola::coefficients(), epsilon_, os_, and unit_assert_equal.

Referenced by main().

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 }
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
ostream * os_
double epsilon_

§ testWeightedLeastSquares()

void testWeightedLeastSquares ( )

Definition at line 111 of file ParabolaTest.cpp.

References pwiz::math::Parabola::center(), pwiz::math::Parabola::coefficients(), epsilon_, os_, unit_assert_equal, and y.

Referenced by main().

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 }
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
ostream * os_
double epsilon_
KernelTraitsBase< Kernel >::space_type::ordinate_type y

§ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 159 of file ParabolaTest.cpp.

References os_, TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testBasic(), testExactFit(), testLeastSquares(), and testWeightedLeastSquares().

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 }
void testLeastSquares()
#define TEST_EPILOG
Definition: unit.hpp:182
void testExactFit()
ostream * os_
void testBasic()
#define TEST_FAILED(x)
Definition: unit.hpp:176
void testWeightedLeastSquares()
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174

Variable Documentation

§ os_

ostream* os_ = 0

§ epsilon_

double epsilon_ = numeric_limits<double>::epsilon()