16 Double_t RosenBrock(
const TVectorD xx )
18 const Double_t x = xx[0];
19 const Double_t y = xx[1];
20 const Double_t tmp1 = y-x*x;
21 const Double_t tmp2 = 1-x;
22 return 100*tmp1*tmp1+tmp2*tmp2;
25 TVectorD RosenBrockGrad(
const TVectorD xx )
27 const Double_t x = xx[0];
28 const Double_t y = xx[1];
30 grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
31 grad[1]=200 * (y - x * x);
37 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
40 r[
"RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock);
43 r[
"RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad);
49 r.Execute(
"result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )");
53 TVectorD min=r.Eval(
"result$par");
55 std::cout.precision(8);
57 std::cout<<
"-----------------------------------------"<<std::endl;
58 std::cout<<
"Minimum x="<<min[0]<<
" y="<<min[1]<<std::endl;
59 std::cout<<
"Value at minimum ="<<RosenBrock(min)<<std::endl;
62 r.Execute(
"optimHess(result$par, RosenBrock, RosenBrockGrad)");
63 r.Execute(
"hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)");
65 TVectorD hmin=r.Eval(
"hresult$par");
68 std::cout<<
"-----------------------------------------"<<std::endl;
69 std::cout<<
"Minimization with the Gradient"<<std::endl;
70 std::cout<<
"Minimum x="<<hmin[0]<<
" y="<<hmin[1]<<std::endl;
71 std::cout<<
"Value at minimum ="<<RosenBrock(hmin)<<std::endl;