ROOT  6.06/09
Reference Guide
TRFunctionExport.h
Go to the documentation of this file.
1 // @(#)root/r:$Id$
2 // Author: Omar Zapata 16/06/2013
3 
4 
5 /*************************************************************************
6  * Copyright (C) 2013-2015, Omar Andres Zapata Mesa *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 #ifndef ROOT_R_TRFunctionExport
13 #define ROOT_R_TRFunctionExport
14 
15 #ifndef ROOT_R_TRInternalFunction
16 #include<TRInternalFunction.h>
17 #endif
18 
19 
20 namespace ROOT {
21  namespace R {
22 
23  /**
24  \class TRFunctionExport
25 
26  This is a class to pass functions from ROOT to R
27  <center><h2>TRFunctionExport class</h2></center>
28  <p>
29  The TRFunctionExport class lets you pass ROOT's functions to R's environment<br>
30  </p>
31  <p>
32  The next example was based in <br>
33  <a href="http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html">
34  http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html
35  </a><br>
36  <a href="http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html">
37  http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html</a><br>
38 
39  </p>
40 
41  Let \f$ f(x,y)=(x-1)^{2} + 100(y-x^{2})^{2} \f$ , which is called the Rosenbrock
42  function.
43 
44  It's known that this function has a minimum when \f$ y = x^{2}\f$ , and \f$ x = 1.\f$
45  Let's get the minimum using R's optim package through ROOTR's interface.
46  In the code this function was called "Double_t RosenBrock(const TVectorD xx )", because for
47  optim, the input in your function deļ¬nition must be a single vector.
48 
49  The Gradient is formed by
50 
51  \f$ \frac{\partial f}{\partial x} = -400x(y - x^{2}) - 2(1 - x) \f$
52 
53  \f$ \frac{\partial f}{\partial y} = 200(y - x^{2}); \f$
54 
55  The "TVectorD RosenBrockGrad(const TVectorD xx )" function
56  must have a single vector as the argument a it will return a single vetor.
57 
58  \code{.cpp}
59  #include<TRInterface.h>
60 
61  //in the next function the pointer *double must be changed by TVectorD, because the pointer has no
62  //sense in R's environment.
63  Double_t RosenBrock(const TVectorD xx )
64  {
65  const Double_t x = xx[0];
66  const Double_t y = xx[1];
67  const Double_t tmp1 = y-x*x;
68  const Double_t tmp2 = 1-x;
69  return 100*tmp1*tmp1+tmp2*tmp2;
70  }
71 
72  TVectorD RosenBrockGrad(const TVectorD xx )
73  {
74  const Double_t x = xx[0];
75  const Double_t y = xx[1];
76  TVectorD grad(2);
77  grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
78  grad[1]=200 * (y - x * x);
79  return grad;
80  }
81 
82 
83  void Minimization()
84  {
85  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
86  //passing RosenBrock function to R
87  r["RosenBrock"]<<ROOT::R::TRFunctionExport(RosenBrock);
88 
89  //passing RosenBrockGrad function to R
90  r["RosenBrockGrad"]<<ROOT::R::TRFunctionExport(RosenBrockGrad);
91 
92  //the option "method" could be "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent"
93  //the option "control" lets you put some constraints like:
94  //"maxit" The maximum number of iterations
95  //"abstol" The absolute convergence tolerance.
96  //"reltol" Relative convergence tolerance.
97  r<<"result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )";
98 
99  //Getting results from R
100  TVectorD min=r.Eval("result$par");
101 
102  std::cout.precision(8);
103  //printing results
104  std::cout<<"-----------------------------------------"<<std::endl;
105  std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl;
106  std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl;
107 
108  //using the gradient
109  r<<"optimHess(result$par, RosenBrock, RosenBrockGrad)";
110  r<<"hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)";
111  //getting the minimum calculated with the gradient
112  TVectorD hmin=r.Eval("hresult$par");
113 
114  //printing results
115  std::cout<<"-----------------------------------------"<<std::endl;
116  std::cout<<"Minimization with the Gradient"<<endl;
117  std::cout<<"Minimum x="<<hmin[0]<<" y="<<hmin[1]<<std::endl;
118  std::cout<<"Value at minimum ="<<RosenBrock(hmin)<<std::endl;
119 
120  }
121  \endcode
122 
123  Output
124  \code
125  Processing Minimization.C...
126  -----------------------------------------
127  Minimum x=0.99980006 y=0.99960016
128  Value at minimum =3.9974288e-08
129  -----------------------------------------
130  Minimization with the Gradient
131  Minimum x=0.99980443 y=0.99960838
132  Value at minimum =3.8273828e-08
133  \endcode
134  <h2>Users Guide </h2>
135  <a href="http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide"> http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide</a><br>
136  <a href="https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface"> https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface</a>
137 
138  @ingroup R
139  */
140 
141 
142  class TRInterface;
143  class TRFunctionExport: public TObject {
144  friend class TRInterface;
145  friend SEXP Rcpp::wrap<TRFunctionExport>(const TRFunctionExport &f);
146  protected:
147  TRInternalFunction *f; //Internar Function to export
148  public:
149  /**
150  Default TRFunctionExport constructor
151  */
153  /**
154  TRFunctionExport copy constructor
155  \param fun other TRFunctionExport
156  */
158 
159  /**
160  TRFunctionExport template constructor that supports a lot of function's prototypes
161  \param fun supported function to be wrapped by Rcpp
162  */
163  template<class T> TRFunctionExport(T fun)
164  {
165  f = new TRInternalFunction(fun);
166  }
167 
168  /**
169  function to assign function to export,
170  template method that supports a lot of function's prototypes
171  \param fun supported function to be wrapped by Rcpp
172  */
173  template<class T> void SetFunction(T fun)
174  {
175  f = new TRInternalFunction(fun);
176  }
177 
179  };
180  }
181 }
182 
183 
184 
185 #endif
TRInternalFunction * f
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
#define ClassDef(name, id)
Definition: Rtypes.h:254
void SetFunction(T fun)
function to assign function to export, template method that supports a lot of function's prototypes ...
TRFunctionExport(T fun)
TRFunctionExport template constructor that supports a lot of function's prototypes.
Rcpp::TRInternalFunction_Impl< Rcpp::PreserveStorage > TRInternalFunction
Mother of all ROOT objects.
Definition: TObject.h:58
TRFunctionExport()
Default TRFunctionExport constructor.
This is a class to pass functions from ROOT to R
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28