ProteoWizard
Functions | Variables
LinearSolverTest.cpp File Reference
#include "LinearSolver.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/banded.hpp>
#include "pwiz/utility/misc/Std.hpp"
#include <cstring>

Go to the source code of this file.

Functions

void testDouble ()
 
void testComplex ()
 
void testDoubleQR ()
 
void testSparse ()
 
void testBanded ()
 
void testBandedComplex ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ testDouble()

void testDouble ( )

Definition at line 42 of file LinearSolverTest.cpp.

43 {
44  if (os_) *os_ << "testDouble()\n";
45 
46  LinearSolver<> solver;
47 
48  ublas::matrix<double> A(2,2);
49  A(0,0) = 1; A(0,1) = 2;
50  A(1,0) = 3; A(1,1) = 4;
51 
52  ublas::vector<double> y(2);
53  y(0) = 5;
54  y(1) = 11;
55 
56  ublas::vector<double> x = solver.solve(A, y);
57 
58  if (os_) *os_ << "A: " << A << endl;
59  if (os_) *os_ << "y: " << y << endl;
60  if (os_) *os_ << "x: " << x << endl;
61 
62  unit_assert(x(0) == 1.);
63  unit_assert(x(1) == 2.);
64 }

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

Referenced by main().

◆ testComplex()

void testComplex ( )

Definition at line 67 of file LinearSolverTest.cpp.

68 {
69  if (os_) *os_ << "testComplex()\n";
70 
71  LinearSolver<> solver;
72 
73  ublas::matrix< complex<double> > A(2,2);
74  A(0,0) = 1; A(0,1) = 2;
75  A(1,0) = 3; A(1,1) = 4;
76 
77  ublas::vector< complex<double> > y(2);
78  y(0) = 5;
79  y(1) = 11;
80 
81  ublas::vector< complex<double> > x = solver.solve(A, y);
82 
83  if (os_) *os_ << "A: " << A << endl;
84  if (os_) *os_ << "y: " << y << endl;
85  if (os_) *os_ << "x: " << x << endl;
86 
87  unit_assert(x(0) == 1.);
88  unit_assert(x(1) == 2.);
89 }

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

Referenced by main().

◆ testDoubleQR()

void testDoubleQR ( )

Definition at line 91 of file LinearSolverTest.cpp.

92 {
93  if (os_) *os_ << "testDoubleQR()\n";
94 
96 
97  ublas::matrix<double> A(2,2);
98  A(0,0) = 1.; A(0,1) = 2.;
99  A(1,0) = 3.; A(1,1) = 4.;
100 
101  ublas::vector<double> y(2);
102  y(0) = 5.;
103  y(1) = 11.;
104 
105  ublas::vector<double> x = solver.solve(A, y);
106 
107  if (os_) *os_ << "A: " << A << endl;
108  if (os_) *os_ << "y: " << y << endl;
109  if (os_) *os_ << "x: " << x << endl;
110 
111  if (os_) *os_ << x(0) << " - 1. = " << x(0) - 1. << endl;
112 
113  unit_assert_equal(x(0), 1., 1e-14);
114  unit_assert_equal(x(1), 2., 1e-14);
115 }

References A, os_, pwiz::math::LinearSolver< LinearSolverType_QR >::solve(), unit_assert_equal, x, and y.

Referenced by main().

◆ testSparse()

void testSparse ( )

Definition at line 144 of file LinearSolverTest.cpp.

145 {
146  if (os_) *os_ << "testSparse()\n";
147 
148  LinearSolver<> solver;
149 
150  ublas::mapped_matrix<double> A(2,2,4);
151  A(0,0) = 1.; A(0,1) = 2.;
152  A(1,0) = 3.; A(1,1) = 4.;
153 
154  ublas::vector<double> y(2);
155  y(0) = 5.;
156  y(1) = 11.;
157 
158  ublas::vector<double> x = solver.solve(A, y);
159 
160  if (os_) *os_ << "A: " << A << endl;
161  if (os_) *os_ << "y: " << y << endl;
162  if (os_) *os_ << "x: " << x << endl;
163 
164  unit_assert_equal(x(0), 1., 1e-14);
165  unit_assert_equal(x(1), 2., 1e-14);
166 }

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

Referenced by main().

◆ testBanded()

void testBanded ( )

Definition at line 196 of file LinearSolverTest.cpp.

197 {
198  if (os_) *os_ << "testBanded()\n";
199 
200  LinearSolver<> solver;
201 
202  ublas::banded_matrix<double> A(2,2,1,1);
203  A(0,0) = 1.; A(0,1) = 2.;
204  A(1,0) = 3.; A(1,1) = 4.;
205 
206  ublas::vector<double> y(2);
207  y(0) = 5.;
208  y(1) = 11.;
209 
210  ublas::vector<double> x = solver.solve(A, y);
211 
212  if (os_) *os_ << "A: " << A << endl;
213  if (os_) *os_ << "y: " << y << endl;
214  if (os_) *os_ << "x: " << x << endl;
215 
216  unit_assert_equal(x(0), 1., 1e-14);
217  unit_assert_equal(x(1), 2., 1e-14);
218 }

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

Referenced by main().

◆ testBandedComplex()

void testBandedComplex ( )

Definition at line 221 of file LinearSolverTest.cpp.

222 {
223  if (os_) *os_ << "testBandedComplex()\n";
224 
225  LinearSolver<> solver;
226 
227  ublas::banded_matrix< complex<double> > A(2,2,1,1);
228  A(0,0) = 1.; A(0,1) = 2.;
229  A(1,0) = 3.; A(1,1) = 4.;
230 
231  ublas::vector< complex<double> > y(2);
232  y(0) = 5.;
233  y(1) = 11.;
234 
235  ublas::vector< complex<double> > x = solver.solve(A, y);
236 
237  if (os_) *os_ << "A: " << A << endl;
238  if (os_) *os_ << "y: " << y << endl;
239  if (os_) *os_ << "x: " << x << endl;
240 
241  unit_assert(norm(x(0)-1.) < 1e-14);
242  unit_assert(norm(x(1)-2.) < 1e-14);
243 }

References A, pwiz::math::MatchedFilter::details::norm(), os_, unit_assert, x, and y.

◆ main()

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

Definition at line 246 of file LinearSolverTest.cpp.

247 {
248  TEST_PROLOG(argc, argv)
249 
250  try
251  {
252  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
253  if (os_) *os_ << "LinearSolverTest\n";
254 
255  testDouble();
256  testComplex();
257  testDoubleQR();
258  //testComplexQR();
259  testSparse();
260  //testSparseComplex(); // lu_factorize doesn't like mapped_matrix<complex>
261  testBanded();
262  //testBandedComplex(); // FIXME: GCC 4.2 doesn't like this test with link=shared
263  }
264  catch (exception& e)
265  {
266  TEST_FAILED(e.what())
267  }
268  catch (...)
269  {
270  TEST_FAILED("Caught unknown exception.")
271  }
272 
274 }

References os_, TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testBanded(), testComplex(), testDouble(), testDoubleQR(), and testSparse().

Variable Documentation

◆ os_

ostream* os_ = 0
testSparse
void testSparse()
Definition: LinearSolverTest.cpp:144
unit_assert_equal
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
testDouble
void testDouble()
Definition: LinearSolverTest.cpp:42
testBanded
void testBanded()
Definition: LinearSolverTest.cpp:196
pwiz::math::LinearSolver< LinearSolverType_QR >
Definition: LinearSolver.hpp:83
y
KernelTraitsBase< Kernel >::space_type::ordinate_type y
Definition: MatchedFilter.hpp:143
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
pwiz::math::LinearSolver< LinearSolverType_QR >::solve
vector_type solve(const matrix_type &A, const vector_type &y)
solve system of linear equations Ax = y using boost::ublas; note: extra copying inefficiencies for ea...
Definition: LinearSolver.hpp:89
x
KernelTraitsBase< Kernel >::space_type::abscissa_type x
Definition: MatchedFilter.hpp:142
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
testDoubleQR
void testDoubleQR()
Definition: LinearSolverTest.cpp:91
testComplex
void testComplex()
Definition: LinearSolverTest.cpp:67
unit_assert
#define unit_assert(x)
Definition: unit.hpp:85
A
#define A
Definition: FilesystemTest.cpp:50
pwiz::math::LinearSolver
Definition: LinearSolver.hpp:51
os_
ostream * os_
Definition: LinearSolverTest.cpp:39
pwiz::math::MatchedFilter::details::norm
double norm(double d)
Definition: MatchedFilter.hpp:221