Logo ROOT   6.08/07
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  bool ret = rf.Solve(x0);
76 
77  if (!ret) {
78  std::cerr << "testMultiRootFinder - Error running derivative algorithm " << std::endl;
79  if (printlevel == 0) rf.PrintState(std::cout);
80  status += rf.Status();
81  }
82 
83  MultiRootFinder rf2(MultiRootFinder::kHybridS);
84  Functor f1(&f, &FuncSystem::F1, 2);
85  Functor f2(&f, &FuncSystem::F2, 2);
86  std::vector<ROOT::Math::IMultiGenFunction*> funlist;
87  funlist.push_back(&f1);
88  funlist.push_back(&f2);
89  rf2.SetFunctionList(funlist.begin(), funlist.end() );
90 
92 
93  bool ret2 = rf2.Solve(x0);
94  if (!ret2) {
95  std::cerr << "testMultiRootFinder - Error running non-derivative algorithm " << std::endl;
96  if (printlevel == 0) rf2.PrintState(std::cout);
97  status += 10*rf2.Status();
98  }
99 
100  return status;
101 
102 }
103 
104 int main (int argc, char **argv) {
105  int status = 0;
106 
107  if (argc > 1 )
108  printlevel = atoi(argv[1]);
109 
110  status += testMultiRootFinder();
111  if (status == 0) {
112  std::cout << "testMultiRootFinder --- \t" << "OK" << std::endl;
113  }
114  else {
115  std::cerr << "testMultiRootFinder --- \t" << "FAILED ! " << "\t with status = " << status << std::endl;
116  }
117 
118  return status;
119 }
Class for Multidimensional root finding algorithms bassed on GSL.
GradFunctor class for Multidimensional gradient functions.
Definition: Functor.h:593
Documentation for class Functor class.
Definition: Functor.h:394
TArc * a
Definition: textangle.C:12
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:30