Code-Eli  0.3.6
least_squares_test_suite.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef least_squares_test_suite_hpp
14 #define least_squares_test_suite_hpp
15 
16 #include <cmath> // std::sqrt()
17 #include <limits> // std::numeric_limits<>
18 
20 
21 template<typename data__>
22 class least_squares_test_suite : public Test::Suite
23 {
24  protected:
25  void AddTests(const float &)
26  {
30  }
31 
32  void AddTests(const double &)
33  {
37  }
38 
39  void AddTests(const long double &)
40  {
44  }
45 
46  public:
48  {
49  // add the tests
50  AddTests(data__());
51  }
53  {
54  }
55 
56  private:
58  {
59  Eigen::Matrix<data__, 4, 3> A;
60  Eigen::Matrix<data__, 4, 1> r;
61  Eigen::Matrix<data__, 3, 1> x, x_ans;
62  data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon()));
63 
64  // set the coefficient matrix
65  A(0,0)=2.0;
66  A(0,1)=-1.0;
67  A(0,2)=1.0;
68  A(1,0)=1.0;
69  A(1,1)=-5.0;
70  A(1,2)=2.0;
71  A(2,0)=-3.0;
72  A(2,1)=1.0;
73  A(2,2)=-4.0;
74  A(3,0)=1.0;
75  A(3,1)=-1.0;
76  A(3,2)=1.0;
77 
78  // set the rhs vector and answer vector
79  r(0)=-4.0;
80  r(1)=2.0;
81  r(2)=5.0;
82  r(3)=-1.0;
83  x_ans(0)=-static_cast<data__>(18)/7;
84  x_ans(1)=-static_cast<data__>(151)/210;
85  x_ans(2)=static_cast<data__>(107)/210;
86 
87  // calculate the answer and compare
89  TEST_ASSERT((x_ans-x).norm()<=delta);
90 
91  // set the rhs matrix and answer matrix
92  Eigen::Matrix<data__, 4, 3> rmat;
93  Eigen::Matrix<data__, 3, 3> xmat, xmat_ans;
94  rmat.col(0)=r;
95  rmat.col(1)=r;
96  rmat.col(2)=r;
97  xmat_ans.col(0)=x_ans;
98  xmat_ans.col(1)=x_ans;
99  xmat_ans.col(2)=x_ans;
100 
101  // calculate the answer and compare
103  TEST_ASSERT((xmat_ans-xmat).norm()<=delta);
104  }
105 
107  {
108  Eigen::Matrix<data__, 5, 4> A;
109  Eigen::Matrix<data__, 3, 4> B;
110  Eigen::Matrix<data__, 5, 1> b;
111  Eigen::Matrix<data__, 3, 1> d;
112  Eigen::Matrix<data__, 4, 1> x, x_ans;
113  data__ delta(std::sqrt(std::numeric_limits<data__>::epsilon()));
114 
115  // set coefficient matrices
116  A(0,0)=1.0;
117  A(0,1)=1.0;
118  A(0,2)=1.0;
119  A(0,3)=1.0;
120  A(1,0)=1.0;
121  A(1,1)=3.0;
122  A(1,2)=1.0;
123  A(1,3)=1.0;
124  A(2,0)=1.0;
125  A(2,1)=-1.0;
126  A(2,2)=3.0;
127  A(2,3)=1.0;
128  A(3,0)=1.0;
129  A(3,1)=1.0;
130  A(3,2)=1.0;
131  A(3,3)=3.0;
132  A(4,0)=1.0;
133  A(4,1)=1.0;
134  A(4,2)=1.0;
135  A(4,3)=-1.0;
136  B(0,0)=1.0;
137  B(0,1)=1.0;
138  B(0,2)=1.0;
139  B(0,3)=-1.0;
140  B(1,0)=1.0;
141  B(1,1)=-1.0;
142  B(1,2)=1.0;
143  B(1,3)=1.0;
144  B(2,0)=1.0;
145  B(2,1)=1.0;
146  B(2,2)=-1.0;
147  B(2,3)=1.0;
148 
149  // set right hand side vectors and solution vector
150  b(0,0)=2.0;
151  b(1,0)=1.0;
152  b(2,0)=6.0;
153  b(3,0)=3.0;
154  b(4,0)=1.0;
155  d(0,0)=1.0;
156  d(1,0)=3.0;
157  d(2,0)=-1.0;
158  x_ans(0,0)=0.5;
159  x_ans(1,0)=-0.5;
160  x_ans(2,0)=1.5;
161  x_ans(3,0)=0.5;
162 
163  // calculate the answer and compare
165  TEST_ASSERT((x_ans-x).norm()<=delta);
166 
167  // set the rhs matrix and answer matrix
168  Eigen::Matrix<data__, 5, 3> bmat;
169  Eigen::Matrix<data__, 3, 3> dmat;
170  Eigen::Matrix<data__, 4, 3> xmat, xmat_ans;
171  bmat.col(0)=b;
172  bmat.col(1)=b;
173  bmat.col(2)=b;
174  dmat.col(0)=d;
175  dmat.col(1)=d;
176  dmat.col(2)=d;
177  xmat_ans.col(0)=x_ans;
178  xmat_ans.col(1)=x_ans;
179  xmat_ans.col(2)=x_ans;
180 
181  // calculate the answer and compare
182  eli::mutil::opt::least_squares_eqcon(xmat, A, bmat, B, dmat);
183  TEST_ASSERT((xmat_ans-xmat).norm()<=delta);
184  }
185 
187  {
188  }
189 };
190 
191 #endif
Definition: least_squares_test_suite.hpp:22
void least_squares_eqcon(Eigen::MatrixBase< data1__ > &x, const Eigen::MatrixBase< data2__ > &A, const Eigen::MatrixBase< data3__ > &b, const Eigen::MatrixBase< data4__ > &B, const Eigen::MatrixBase< data5__ > &d)
Definition: least_squares.hpp:38
void least_squares_uncon(Eigen::MatrixBase< data1__ > &x, const Eigen::MatrixBase< data2__ > &A, const Eigen::MatrixBase< data3__ > &r)
Definition: least_squares.hpp:25
void AddTests(const long double &)
Definition: least_squares_test_suite.hpp:39
void least_squares_eqcon_test()
Definition: least_squares_test_suite.hpp:106
void least_squares_ineqcon_test()
Definition: least_squares_test_suite.hpp:186
void AddTests(const double &)
Definition: least_squares_test_suite.hpp:32
void least_squares_unc_test()
Definition: least_squares_test_suite.hpp:57
~least_squares_test_suite()
Definition: least_squares_test_suite.hpp:52
void AddTests(const float &)
Definition: least_squares_test_suite.hpp:25
least_squares_test_suite()
Definition: least_squares_test_suite.hpp:47