{ "cells": [ { "cell_type": "markdown", "id": "0adca4eb", "metadata": {}, "source": [ "# Minimization\n", "Example based on\n", "http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html\n", "\n", "\n", "\n", "\n", "**Author:** Omar Zapata \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:27 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "4eb43fae", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.025822Z", "iopub.status.busy": "2026-05-19T20:27:49.025717Z", "iopub.status.idle": "2026-05-19T20:27:49.033094Z", "shell.execute_reply": "2026-05-19T20:27:49.032713Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_44:1:9: fatal error: 'TRInterface.h' file not found\n", "#include\n", " ^~~~~~~~~~~~~~~\n" ] } ], "source": [ "%%cpp -d\n", "#include" ] }, { "cell_type": "markdown", "id": "096c892a", "metadata": {}, "source": [ " in the next function the *double pointer must be changed by a TVectorD,\n", "because the pointer has no meaning in R enviroment.\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "3a82e9b4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.034524Z", "iopub.status.busy": "2026-05-19T20:27:49.034416Z", "iopub.status.idle": "2026-05-19T20:27:49.038687Z", "shell.execute_reply": "2026-05-19T20:27:49.038214Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "Double_t RosenBrock(const TVectorD xx )\n", "{\n", " const Double_t x = xx[0];\n", " const Double_t y = xx[1];\n", " const Double_t tmp1 = y-x*x;\n", " const Double_t tmp2 = 1-x;\n", " return 100*tmp1*tmp1+tmp2*tmp2;\n", "}" ] }, { "cell_type": "markdown", "id": "caab2cca", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "59fd2a79", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.040026Z", "iopub.status.busy": "2026-05-19T20:27:49.039917Z", "iopub.status.idle": "2026-05-19T20:27:49.048848Z", "shell.execute_reply": "2026-05-19T20:27:49.048352Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "TVectorD RosenBrockGrad(const TVectorD xx )\n", "{\n", " const Double_t x = xx[0];\n", " const Double_t y = xx[1];\n", " TVectorD grad(2);\n", " grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);\n", " grad[1]=200 * (y - x * x);\n", " return grad;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "aaaf6bf7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.050277Z", "iopub.status.busy": "2026-05-19T20:27:49.050150Z", "iopub.status.idle": "2026-05-19T20:27:49.376732Z", "shell.execute_reply": "2026-05-19T20:27:49.376029Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_47:2:8: error: 'R' is not a class, namespace, or enumeration\n", " ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();\n", " ^\n", "input_line_47:2:8: note: 'R' declared here\n", "input_line_47:2:32: error: no member named 'R' in namespace 'ROOT'\n", " ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();\n", " ~~~~~~^\n" ] } ], "source": [ "ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();" ] }, { "cell_type": "markdown", "id": "cef0dc61", "metadata": {}, "source": [ "passsing RosenBrock function to R" ] }, { "cell_type": "code", "execution_count": 5, "id": "0ba30df7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.378241Z", "iopub.status.busy": "2026-05-19T20:27:49.378126Z", "iopub.status.idle": "2026-05-19T20:27:49.583306Z", "shell.execute_reply": "2026-05-19T20:27:49.582740Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:2:24: error: 'R' is not a class, namespace, or enumeration\n", " r[\"RosenBrock\"]=ROOT::R::TRFunctionExport(RosenBrock);\n", " ^\n", "input_line_56:2:24: note: 'R' declared here\n" ] } ], "source": [ "r[\"RosenBrock\"]=ROOT::R::TRFunctionExport(RosenBrock);" ] }, { "cell_type": "markdown", "id": "ce85f479", "metadata": {}, "source": [ "passsing RosenBrockGrad function to R" ] }, { "cell_type": "code", "execution_count": 6, "id": "dc21a42c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.585066Z", "iopub.status.busy": "2026-05-19T20:27:49.584950Z", "iopub.status.idle": "2026-05-19T20:27:49.790446Z", "shell.execute_reply": "2026-05-19T20:27:49.789538Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_57:2:28: error: 'R' is not a class, namespace, or enumeration\n", " r[\"RosenBrockGrad\"]=ROOT::R::TRFunctionExport(RosenBrockGrad);\n", " ^\n", "input_line_57:2:28: note: 'R' declared here\n" ] } ], "source": [ "r[\"RosenBrockGrad\"]=ROOT::R::TRFunctionExport(RosenBrockGrad);" ] }, { "cell_type": "markdown", "id": "5d969480", "metadata": {}, "source": [ "the option \"method\" could be \"Nelder-Mead\", \"BFGS\", \"CG\", \"L-BFGS-B\", \"SANN\",\"Brent\"" ] }, { "cell_type": "markdown", "id": "22e128c0", "metadata": {}, "source": [ "the option \"control\" lets you put some constraints like\n", "\"maxit\" The maximum number of iterations.\n", "\"abstol\" The absolute convergence tolerance." ] }, { "cell_type": "code", "execution_count": 7, "id": "e8bbbf27", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.791995Z", "iopub.status.busy": "2026-05-19T20:27:49.791870Z", "iopub.status.idle": "2026-05-19T20:27:49.994654Z", "shell.execute_reply": "2026-05-19T20:27:49.994045Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:2:3: error: use of undeclared identifier 'r'\n", " (r.Execute(\"result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )\"))\n", " ^\n", "Error in : Error evaluating expression (r.Execute(\"result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )\"))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "r.Execute(\"result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )\");" ] }, { "cell_type": "markdown", "id": "f2db2d49", "metadata": {}, "source": [ "\"reltol\" Relative convergence tolerance." ] }, { "cell_type": "markdown", "id": "b9ca892d", "metadata": {}, "source": [ "Getting results from R" ] }, { "cell_type": "code", "execution_count": 8, "id": "f5bac971", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:49.996262Z", "iopub.status.busy": "2026-05-19T20:27:49.996147Z", "iopub.status.idle": "2026-05-19T20:27:50.364809Z", "shell.execute_reply": "2026-05-19T20:27:50.360408Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_60:2:2: warning: 'min' shadows a declaration with the same name in the 'std' namespace; use '::min' to reference this declaration\n", " TVectorD min=r.Eval(\"result$par\");\n", " ^\n", "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-292, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN12__cling_N52316__cling_Un1Qu323EPv, _ZN12__cling_N52324__dynamic__cling_Un1Qu30E, cling_module_292_.6, cling_module_292_, _ZNSt8ios_base9precisionEl, _GLOBAL__sub_I_cling_module_292, _ZN12__cling_N5233minE, $.cling-module-292.__inits.0 }) }\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*)\n", "Maybe you need to load the corresponding shared library?\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler()\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "TVectorD min=r.Eval(\"result$par\");\n", "\n", "std::cout.precision(8);" ] }, { "cell_type": "markdown", "id": "469abe41", "metadata": {}, "source": [ "printing results" ] }, { "cell_type": "code", "execution_count": 9, "id": "de9e1fa6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:50.366425Z", "iopub.status.busy": "2026-05-19T20:27:50.366302Z", "iopub.status.idle": "2026-05-19T20:27:50.589703Z", "shell.execute_reply": "2026-05-19T20:27:50.584394Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_61:3:26: error: reference to 'min' is ambiguous\n", "std::cout<<\"Minimum x=\"< __l, _Compare __comp)\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:416:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:421:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&, _Compare);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/stl_algo.h:5686:5: note: candidate found by name lookup is 'std::min'\n", " min(initializer_list<_Tp> __l)\n", " ^\n", "input_line_61:3:41: error: reference to 'min' is ambiguous\n", "std::cout<<\"Minimum x=\"< __l, _Compare __comp)\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:416:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:421:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&, _Compare);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/stl_algo.h:5686:5: note: candidate found by name lookup is 'std::min'\n", " min(initializer_list<_Tp> __l)\n", " ^\n", "input_line_61:4:45: error: reference to 'min' is ambiguous\n", "std::cout<<\"Value at minimum =\"< __l, _Compare __comp)\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:416:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/algorithmfwd.h:421:5: note: candidate found by name lookup is 'std::min'\n", " min(const _Tp&, const _Tp&, _Compare);\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/stl_algo.h:5686:5: note: candidate found by name lookup is 'std::min'\n", " min(initializer_list<_Tp> __l)\n", " ^\n" ] } ], "source": [ "std::cout<<\"-----------------------------------------\"<: Error evaluating expression (r.Execute(\"optimHess(result$par, RosenBrock, RosenBrockGrad)\"))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "r.Execute(\"optimHess(result$par, RosenBrock, RosenBrockGrad)\");\n", "r.Execute(\"hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)\");" ] }, { "cell_type": "markdown", "id": "0510673f", "metadata": {}, "source": [ "getting the min calculated with the gradient" ] }, { "cell_type": "code", "execution_count": 11, "id": "d4c08c8c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:50.798129Z", "iopub.status.busy": "2026-05-19T20:27:50.798012Z", "iopub.status.idle": "2026-05-19T20:27:51.003443Z", "shell.execute_reply": "2026-05-19T20:27:51.002837Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv }) }\n", "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-295, _ZN12__cling_N52624__dynamic__cling_Un1Qu31E, _GLOBAL__sub_I_cling_module_295, cling_module_295_.6, _ZN12__cling_N52616__cling_Un1Qu327EPv, _ZN12__cling_N5264hminE, $.cling-module-295.__inits.0, cling_module_295_ }) }\n" ] } ], "source": [ "TVectorD hmin=r.Eval(\"hresult$par\");" ] }, { "cell_type": "markdown", "id": "1bc8944a", "metadata": {}, "source": [ "printing results" ] }, { "cell_type": "code", "execution_count": 12, "id": "0776bed4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:27:51.005295Z", "iopub.status.busy": "2026-05-19T20:27:51.005178Z", "iopub.status.idle": "2026-05-19T20:27:51.210623Z", "shell.execute_reply": "2026-05-19T20:27:51.209946Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-295 }) }\n", "cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5264hminE }) }\n" ] } ], "source": [ "std::cout<<\"-----------------------------------------\"<