ROOT   Reference Guide
Loading...
Searching...
No Matches
TRFunctionExport.h
Go to the documentation of this file.
1// @(#)root/r:$Id$
2// Author: Omar Zapata Omar.Zapata@cern.ch 16/06/2013
3
4
5/*************************************************************************
6 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
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
18namespace 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="https://oproject.org/pages/ROOT%20R%20Users%20Guide"> https://oproject.org/pages/ROOT R Users Guide</a><br>
134
135 @ingroup R
136 */
137
138
139 class TRInterface;
140 class TRFunctionExport: public TObject {
141 friend class TRInterface;
142 friend SEXP Rcpp::wrap<TRFunctionExport>(const TRFunctionExport &f);
143 protected:
144 TRInternalFunction *f; //Internar Function to export
145 public:
146 /**
147 Default TRFunctionExport constructor
148 */
150
151 /**
152 Default TRFunctionExport destructor
153 */
155 {
156 if (f) delete f;
157 }
158 /**
159 TRFunctionExport copy constructor
160 \param fun other TRFunctionExport
161 */
163
164 /**
165 TRFunctionExport template constructor that supports a lot of function's prototypes
166 \param fun supported function to be wrapped by Rcpp
167 */
168 template<class T> TRFunctionExport(T fun)
169 {
170 f = new TRInternalFunction(fun);
171 }
172
173 /**
174 function to assign function to export,
175 template method that supports a lot of function's prototypes
176 \param fun supported function to be wrapped by Rcpp
177 */
178 template<class T> void SetFunction(T fun)
179 {
180 f = new TRInternalFunction(fun);
181 }
182
184 };
185 }
186}
187
188
189
190#endif
#define ClassDef(name, id)
Definition Rtypes.h:325
This is a class to pass functions from ROOT to R.
~TRFunctionExport()
Default TRFunctionExport destructor.
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.
TRFunctionExport()
Default TRFunctionExport constructor.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
Mother of all ROOT objects.
Definition TObject.h:37
Rcpp::TRInternalFunction_Impl< Rcpp::PreserveStorage > TRInternalFunction
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...