// @(#)root/r:$Id$ // Author: Omar Zapata 16/06/2013 /************************************************************************* * Copyright (C) 2013-2015, Omar Andres Zapata Mesa * * All rights reserved. * * * * For the licensing terms see $ROOTSYS/LICENSE. * * For the list of contributors see $ROOTSYS/README/CREDITS. * *************************************************************************/ #ifndef ROOT_R_TRFunctionExport #define ROOT_R_TRFunctionExport #ifndef ROOT_R_TRInternalFunction #include #endif namespace ROOT { namespace R { /** \class TRFunctionExport This is a class to pass functions from ROOT to R

TRFunctionExport class

The TRFunctionExport class lets you pass ROOT's functions to R's environment

The next example was based in
http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html
http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html

Let \f$ f(x,y)=(x-1)^{2} + 100(y-x^{2})^{2} \f$ , which is called the Rosenbrock function. It's known that this function has a minimum when \f$ y = x^{2}\f$ , and \f$ x = 1.\f$ Let's get the minimum using R's optim package through ROOTR's interface. In the code this function was called "Double_t RosenBrock(const TVectorD xx )", because for optim, the input in your function deļ¬nition must be a single vector. The Gradient is formed by \f$ \frac{\partial f}{\partial x} = -400x(y - x^{2}) - 2(1 - x) \f$ \f$ \frac{\partial f}{\partial y} = 200(y - x^{2}); \f$ The "TVectorD RosenBrockGrad(const TVectorD xx )" function must have a single vector as the argument a it will return a single vetor. \code{.cpp} #include //in the next function the pointer *double must be changed by TVectorD, because the pointer has no //sense in R's environment. Double_t RosenBrock(const TVectorD xx ) { const Double_t x = xx[0]; const Double_t y = xx[1]; const Double_t tmp1 = y-x*x; const Double_t tmp2 = 1-x; return 100*tmp1*tmp1+tmp2*tmp2; } TVectorD RosenBrockGrad(const TVectorD xx ) { const Double_t x = xx[0]; const Double_t y = xx[1]; TVectorD grad(2); grad[0]=-400 * x * (y - x * x) - 2 * (1 - x); grad[1]=200 * (y - x * x); return grad; } void Minimization() { ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance(); //passing RosenBrock function to R r["RosenBrock"]< http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide
https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface @ingroup R */ class TRInterface; class TRFunctionExport: public TObject { friend class TRInterface; friend SEXP Rcpp::wrap(const TRFunctionExport &f); protected: TRInternalFunction *f; //Internar Function to export public: /** Default TRFunctionExport constructor */ TRFunctionExport(); /** TRFunctionExport copy constructor \param fun other TRFunctionExport */ TRFunctionExport(const TRFunctionExport &fun); /** TRFunctionExport template constructor that supports a lot of function's prototypes \param fun supported function to be wrapped by Rcpp */ template TRFunctionExport(T fun) { f = new TRInternalFunction(fun); } /** function to assign function to export, template method that supports a lot of function's prototypes \param fun supported function to be wrapped by Rcpp */ template void SetFunction(T fun) { f = new TRInternalFunction(fun); } ClassDef(TRFunctionExport, 0) // }; } } #endif