testMinou.cc
Go to the documentation of this file.
1 
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <tst/Verify.h>
12 #include <utl/Minou.h>
13 
14 using namespace utl;
15 using namespace std;
16 using namespace tst;
17 
18 
19 #define EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
20 
21 
22 struct MyFunc : public Minou::Base {
23 
24  MyFunc() : Base({{"x", 2, 0.1}}, 0.5) { }
25 
26  double operator()(const vector<double>& pars) const
27  { return utl::Sqr(pars.front() - 1); }
28 
29 };
30 
31 
32 struct HenonHeiles : public Minou::Base {
33 
34  HenonHeiles() : Base({{"x", 2, 0.1}, {"y", -1, 0.1}}, 0.5) { }
35 
36  double
37  operator()(const vector<double>& pars)
38  const
39  {
40  const double& x = pars[0];
41  const double& y = pars[1];
42  return 0.5*(Sqr(x) + Sqr(y)) + Sqr(x)*y - (1/3.)*Sqr(y)*y;
43  }
44 
45 };
46 
47 
48 struct Polynomial : public Minou::Base {
49 
50  Polynomial(const unsigned int n) { GetParameterDefs() = GenerateParameters(n); }
51 
52  vector<Minou::ParameterDef>
53  GenerateParameters(const unsigned int n)
54  {
55  vector<Minou::ParameterDef> p;
56  for (unsigned int i = 0; i <= n; ++i)
57  p.emplace_back(std::string("a") + std::to_string(i), 0, 1);
58  return p;
59  }
60 
61  double operator()(const vector<double>& pars) { return pars.at(0); }
62 
63 };
64 
65 
69 class TestMinou : public CppUnit::TestFixture {
70 
71  CPPUNIT_TEST_SUITE(TestMinou);
72  CPPUNIT_TEST(TestMyFunc);
73  CPPUNIT_TEST(TestHenonHeiles);
74  CPPUNIT_TEST(TestPolynomial);
75  CPPUNIT_TEST_SUITE_END();
76 
77 public:
78  void setUp() { }
79 
80  void tearDown() { }
81 
82  void
84  {
85  MyFunc func;
86  Minou::Minimizer<MyFunc> mini(func);
87  CPPUNIT_ASSERT(!mini.Minimize());
88  const auto x = mini.GetParameterAndError(0);
89  cout << x.first << ' ' << x.second << endl;
90  }
91 
92  void
94  {
95  HenonHeiles func;
97  if (mini.Minimize()) {
98  cout << "ok: failed first time" << endl;
99  func.GetParameterDefs()[0].fValue = 0.1;
100  func.GetParameterDefs()[1].fValue = -0.1;
101  mini.DefineParameters();
102  CPPUNIT_ASSERT(!mini.Minimize());
103  }
104  double x, ex;
105  mini.GetMinuit().GetParameter(0, x, ex);
106  double y, ey;
107  mini.GetMinuit().GetParameter(1, y, ey);
108  cout
109  << x << ' ' << ex << ' '
110  << y << ' ' << ey
111  << endl;
112  }
113 
114  void
116  {
117  Polynomial p(10);
119  }
120 
121 };
122 
123 
constexpr T Sqr(const T &x)
Simple polynomial container.
Definition: Polynomial.h:22
Polynomial(const unsigned int n)
Definition: testMinou.cc:50
double operator()(const vector< double > &pars)
Definition: testMinou.cc:61
void DefineParameters()
Definition: Minou.h:215
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
void TestPolynomial()
Definition: testMinou.cc:115
void TestMyFunc()
Definition: testMinou.cc:83
void setUp()
Definition: testMinou.cc:78
vector< Minou::ParameterDef > GenerateParameters(const unsigned int n)
Definition: testMinou.cc:53
std::vector< ParameterDef > & GetParameterDefs()
Definition: Minou.h:97
void tearDown()
Definition: testMinou.cc:80
int Minimize(const int n=500)
Definition: Minou.h:250
void TestHenonHeiles()
Definition: testMinou.cc:93
TMinuit & GetMinuit()
Definition: Minou.h:247
MyFunc()
Definition: testMinou.cc:24

, generated on Tue Sep 26 2023.