ProteoWizard
Functions | Variables
LinearLeastSquaresTest.cpp File Reference
#include "LinearLeastSquares.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include <cstring>

Go to the source code of this file.

Functions

void testDouble ()
 
void testDoubleQR ()
 
void testExactFitQR ()
 
void testSimpleRectangleQR ()
 
void testLeastSquaresQR ()
 
int main (int argc, char *argv[])
 

Variables

const double epsilon = 1e-16
 
ostream * os_ = 0
 

Function Documentation

§ testDouble()

void testDouble ( )

Definition at line 40 of file LinearLeastSquaresTest.cpp.

References A, os_, unit_assert_equal, x, and y.

Referenced by main().

41 {
42  if (os_) *os_ << "testDouble()\n";
43 
45  ublas::matrix<double> A(2, 2);
46  A(0,0) = 1; A(0,1) = 2;
47  A(1,0) = 3; A(1,1) = 4;
48 
49  ublas::vector<double> y(2);
50  y(0) = 5;
51  y(1) = 11;
52 
53  ublas::vector<double> x = lls.solve(A, y);
54 
55  if (os_) *os_ << "A: " << A << endl;
56  if (os_) *os_ << "y: " << y << endl;
57  if (os_) *os_ << "x: " << x << endl;
58 
59  unit_assert_equal(x(0), 1., 1e-13);
60  unit_assert_equal(x(1), 2., 1e-13);
61 }
ostream * os_
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
#define A
KernelTraitsBase< Kernel >::space_type::abscissa_type x
KernelTraitsBase< Kernel >::space_type::ordinate_type y

§ testDoubleQR()

void testDoubleQR ( )

Definition at line 63 of file LinearLeastSquaresTest.cpp.

References A, epsilon, os_, pwiz::math::LinearLeastSquares< LinearLeastSquaresType_QR >::solve(), unit_assert_equal, x, and y.

Referenced by main().

64 {
65  if (os_) *os_ << "testDoubleQR()\n";
66 
68  ublas::matrix<double> A(2, 2);
69  A(0,0) = 1; A(0,1) = 2;
70  A(1,0) = 3; A(1,1) = 4;
71 
72  ublas::vector<double> y(2);
73  y(0) = 5;
74  y(1) = 11;
75 
76  ublas::vector<double> x = lls.solve(A, y);
77 
78  if (os_) *os_ << "A: " << A << endl;
79  if (os_) *os_ << "y: " << y << endl;
80  if (os_) *os_ << "x: " << x << endl;
81 
82  if (os_) *os_ << "x(0) = " << x(0) - 1. << ", x(1) = " << x(1) - 2. << endl;
83  unit_assert_equal(x(0), 1., 100*epsilon);
84  unit_assert_equal(x(1), 2., 100*epsilon);
85 }
ostream * os_
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
#define A
boost::numeric::ublas::vector< T > solve(const boost::numeric::ublas::matrix< T > &A, const boost::numeric::ublas::vector< T > &x)
KernelTraitsBase< Kernel >::space_type::abscissa_type x
KernelTraitsBase< Kernel >::space_type::ordinate_type y
const double epsilon

§ testExactFitQR()

void testExactFitQR ( )

Definition at line 87 of file LinearLeastSquaresTest.cpp.

References epsilon, os_, pwiz::math::LinearLeastSquares< LinearLeastSquaresType_QR >::solve(), and unit_assert_equal.

Referenced by main().

88 {
89  if (os_) *os_ << "***************************\n";
90  if (os_) *os_ << "testExactFit()\n";
91 
92  dmatrix m(2,2);
93  dvector obs(2);
94 
95  m(0, 0) = 1;
96  m(1, 0) = 0;
97  m(0, 1) = 0;
98  m(1, 1) = 1;
99 
100  obs(0) = 1;
101  obs(1) = 1;
102 
104  const dvector result = lls.solve(m,obs);
105 
106  unit_assert_equal(obs(0), 1, epsilon);
107  unit_assert_equal(obs(1), 1, epsilon);
108 
109  if (os_) *os_ << "testExactFit(): success\n";
110 }
ostream * os_
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
boost::numeric::ublas::vector< double > dvector
Definition: Types.hpp:34
boost::numeric::ublas::vector< T > solve(const boost::numeric::ublas::matrix< T > &A, const boost::numeric::ublas::vector< T > &x)
boost::numeric::ublas::matrix< double > dmatrix
Definition: Types.hpp:33
const double epsilon

§ testSimpleRectangleQR()

void testSimpleRectangleQR ( )

Definition at line 113 of file LinearLeastSquaresTest.cpp.

References epsilon, os_, pwiz::math::LinearLeastSquares< LinearLeastSquaresType_QR >::solve(), and unit_assert_equal.

Referenced by main().

114 {
115  if (os_) *os_ << "***************************\n";
116  if (os_) *os_ << "testSimpleRectangleQR()\n";
117 
118  dmatrix samples(4, 2);
119 
120  samples.clear();
121  samples(0,0) = 1;
122  samples(0,1) = 0;
123  samples(1,0) = 0;
124  samples(1,1) = 1;
125  samples(2,0) = 1;
126  samples(2,1) = 0;
127  samples(3,0) = 0;
128  samples(3,1) = 1;
129 
130  dvector obs(4);
131  obs(0) = 2;
132  obs(1) = 2;
133  obs(2) = 2;
134  obs(3) = 2;
135 
137 
138  dvector a = lls.solve(samples, obs);
139 
140  unit_assert_equal(a(0), 2, epsilon);
141  unit_assert_equal(a(1), 2, epsilon);
142 
143  if (os_) *os_ << "testSimpleRectangleQR(): success\n";
144 }
ostream * os_
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
boost::numeric::ublas::vector< double > dvector
Definition: Types.hpp:34
boost::numeric::ublas::vector< T > solve(const boost::numeric::ublas::matrix< T > &A, const boost::numeric::ublas::vector< T > &x)
boost::numeric::ublas::matrix< double > dmatrix
Definition: Types.hpp:33
const double epsilon

§ testLeastSquaresQR()

void testLeastSquaresQR ( )

Definition at line 146 of file LinearLeastSquaresTest.cpp.

References epsilon, os_, pwiz::math::LinearLeastSquares< LinearLeastSquaresType_QR >::solve(), and unit_assert_equal.

Referenced by main().

147 {
148  if (os_) *os_ << "***************************\n";
149  if (os_) *os_ << "testLeastSquaresQR()\n";
150 
151  dmatrix samples(5, 2);
152 
153  samples.clear();
154  samples(0,0) = 1;
155  samples(0,1) = 1;
156  samples(1,0) = 2;
157  samples(1,1) = 2;
158  samples(2,0) = 3;
159  samples(2,1) = 3;
160  samples(3,0) = 0;
161  samples(3,1) = 4;
162  samples(4,0) = -1;
163  samples(4,1) = 5;
164 
165  dvector obs(5);
166  obs(0) = 1;
167  obs(1) = 3;
168  obs(2) = 9;
169  obs(3) = 3;
170  obs(4) = -9;
171 
173 
174  dvector a = lls.solve(samples, obs);
175 
176  unit_assert_equal(a(0), 3.16666666666667, epsilon*100);
177  unit_assert_equal(a(1), -0.5, epsilon*100);
178 
179  if (os_) *os_ << "testLeastSquaresQR(): success\n";
180 }
ostream * os_
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
boost::numeric::ublas::vector< double > dvector
Definition: Types.hpp:34
boost::numeric::ublas::vector< T > solve(const boost::numeric::ublas::matrix< T > &A, const boost::numeric::ublas::vector< T > &x)
boost::numeric::ublas::matrix< double > dmatrix
Definition: Types.hpp:33
const double epsilon

§ main()

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

Definition at line 182 of file LinearLeastSquaresTest.cpp.

References os_, TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testDouble(), testDoubleQR(), testExactFitQR(), testLeastSquaresQR(), and testSimpleRectangleQR().

183 {
184  TEST_PROLOG(argc, argv)
185 
186  try
187  {
188  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
189  if (os_) *os_ << "LinearLeastSquaresTest\n";
190  testDouble();
191  testDoubleQR();
192  testExactFitQR();
195  }
196  catch (exception& e)
197  {
198  TEST_FAILED(e.what())
199  }
200  catch (...)
201  {
202  TEST_FAILED("Caught unknown exception.")
203  }
204 
206 }
void testExactFitQR()
void testDouble()
ostream * os_
#define TEST_EPILOG
Definition: unit.hpp:182
void testSimpleRectangleQR()
void testLeastSquaresQR()
#define TEST_FAILED(x)
Definition: unit.hpp:176
void testDoubleQR()
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174

Variable Documentation

§ epsilon

const double epsilon = 1e-16

§ os_

ostream* os_ = 0