Logo ROOT   6.10/09
Reference Guide
testMultiRootFinder.cxx
Go to the documentation of this file.
1 #include "Math/Functor.h"
2 #include "Math/MultiRootFinder.h"
3 
4 #ifdef HAVE_ROOTLIBS
5 #include "TStopwatch.h"
6 #else
7 struct TStopwatch {
8  void Start(){}
9  void Stop(){}
10  void Reset(){}
11  double RealTime() { return 0; }
12  double CpuTime() { return 0; }
13 };
14 #endif
15 
16 #include <iostream>
17 #include <stdlib.h>
18 
19 // solve Roots of rosenbrock function
20 // f1(x,y) = a(1-x)
21 // f2(x,y) = b(y-x^2)
22 // with 1 = 1, b=10
23 
24 
25 // define system of functions to find the roots
26 struct FuncSystem {
27 
28  double F1(const double *xx) {
29  double x = xx[0];
30  return a * (1. - x );
31  }
32  double F2(const double *xx) {
33  double x = xx[0]; double y = xx[1];
34  return b * (y - x*x );
35  }
36 
37  // derivative
38  double DerivF1(const double *, int icoord) {
39  if (icoord == 0) return -a;
40  else return 0;
41  }
42  double DerivF2(const double *xx, int icoord) {
43  double x = xx[0];
44  if (icoord == 0) return -2 * b * x;
45  else return b;
46  }
47 
48  double a;
49  double b;
50 };
51 
52 int printlevel = 0;
53 
54 using namespace ROOT::Math;
55 
57 
58  int status = 0;
59 
60  // methods using derivatives
61  FuncSystem f;
62  f.a = 1;
63  f.b = 10;
64 
65 
66  MultiRootFinder rf(MultiRootFinder::kHybridSJ);
67  GradFunctor g1(&f, &FuncSystem::F1, &FuncSystem::DerivF1,2);
68  GradFunctor g2(&f, &FuncSystem::F2, &FuncSystem::DerivF2,2);
69  rf.AddFunction(g1);
70  rf.AddFunction(g2);
72 
73  double x0[] = {-1.,-1.};
74 
75  std::cout << "Testing Multi-RootFinder - ";
76  bool ret = rf.Solve(x0);
77 
78  if (!ret) {
79  std::cout << rf.Name() << "\t : FAILED\n";
80  std::cerr << "testMultiRootFinder - Error running derivative algorithm " << std::endl;
81  if (printlevel == 0) rf.PrintState(std::cout);
82  status += rf.Status();
83  }
84  else
85  std::cout << rf.Name() << "\t : OK\n";
86 
87 
88  MultiRootFinder rf2(MultiRootFinder::kHybridS);
89  Functor f1(&f, &FuncSystem::F1, 2);
90  Functor f2(&f, &FuncSystem::F2, 2);
91  std::vector<ROOT::Math::IMultiGenFunction*> funlist;
92  funlist.push_back(&f1);
93  funlist.push_back(&f2);
94  rf2.SetFunctionList(funlist.begin(), funlist.end() );
95 
97 
98  std::cout << "Testing Multi-RootFinder - ";
99  bool ret2 = rf2.Solve(x0);
100  if (!ret2) {
101  std::cout << rf2.Name() << "\t : FAILED\n";
102  std::cout << "\t FAILED\n";
103  std::cerr << "testMultiRootFinder - Error running non-derivative algorithm " << std::endl;
104  if (printlevel == 0) rf2.PrintState(std::cout);
105  status += 10*rf2.Status();
106  }
107  else
108  std::cout << rf2.Name() << "\t : OK\n";
109 
110  return status;
111 
112 }
113 
114 int main (int argc, char **argv) {
115  int status = 0;
116 
117  if (argc > 1 )
118  printlevel = atoi(argv[1]);
119 
120  status += testMultiRootFinder();
121  if (status == 0) {
122  std::cout << "testMultiRootFinder --- \t" << "OK" << std::endl;
123  }
124  else {
125  std::cerr << "testMultiRootFinder --- \t" << "FAILED ! " << "\t with status = " << status << std::endl;
126  }
127 
128  return status;
129 }
Class for Multidimensional root finding algorithms bassed on GSL.
GradFunctor class for Multidimensional gradient functions.
Definition: Functor.h:577
Documentation for class Functor class.
Definition: Functor.h:392
TArc * a
Definition: textangle.C:12
const char * Name() const
Return the algorithm name used for solving Note the name is available only after having called solved...
Double_t x[n]
Definition: legend1.C:17
bool SetFunctionList(FuncIterator begin, FuncIterator end)
int printlevel
#define F1(x, y, z)
Definition: TMD5.cxx:267
bool Solve(const double *x, int maxIter=0, double absTol=0, double relTol=0)
Find the root starting from the point X; Use the number of iteration and tolerance if given otherwise...
#define F2(x, y, z)
Definition: TMD5.cxx:268
int main(int argc, char **argv)
int AddFunction(const ROOT::Math::IMultiGenFunction &func)
double f(double x)
Double_t y[n]
Definition: legend1.C:17
int testMultiRootFinder()
int Status() const
Return the status of last root finding.
double f2(const double *x)
TF1 * f1
Definition: legend1.C:11
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
void PrintState(std::ostream &os=std::cout)
print iteration state
Stopwatch class.
Definition: TStopwatch.h:28