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