{
"cells": [
{
"cell_type": "markdown",
"id": "4831871d",
"metadata": {},
"source": [
"# Interpolation\n",
"More Information for R interpolation in\n",
"http://stat.ethz.ch/R-manual/R-patched/library/stats/html/approxfun.html\n",
"NOTE: this example illustrates an interpolation with random points given from ROOT\n",
"and procedures made in R's environment.\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": "4a1ea75e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:44.633879Z",
"iopub.status.busy": "2026-05-19T20:27:44.633770Z",
"iopub.status.idle": "2026-05-19T20:27:44.954833Z",
"shell.execute_reply": "2026-05-19T20:27:44.954417Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_44:2:8: error: 'R' is not a class, namespace, or enumeration\n",
" ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();\n",
" ^\n",
"input_line_44:2:8: note: 'R' declared here\n",
"input_line_44: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": "362900f0",
"metadata": {},
"source": [
"Creating points"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba435e9c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:44.956177Z",
"iopub.status.busy": "2026-05-19T20:27:44.956060Z",
"iopub.status.idle": "2026-05-19T20:27:45.161278Z",
"shell.execute_reply": "2026-05-19T20:27:45.160818Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:2:3: error: use of undeclared identifier 'r'\n",
" (r[\"x\"] = ((*(std::vector*)0x7f222f444050)))\n",
" ^\n",
"Error in : Error evaluating expression (r[\"x\"] = ((*(std::vector*)0x7f222f444050)))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"TRandom rg;\n",
"std::vector x(10),y(10);\n",
"for(int i=0;i<10;i++)\n",
"{\n",
" x[i]=i;\n",
" y[i]=rg.Gaus();\n",
"}\n",
"\n",
"r[\"x\"]=x;\n",
"r[\"y\"]=y;"
]
},
{
"cell_type": "markdown",
"id": "5b37e0fd",
"metadata": {},
"source": [
"do plotting only in non-batch mode"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "67ec1999",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:45.162653Z",
"iopub.status.busy": "2026-05-19T20:27:45.162516Z",
"iopub.status.idle": "2026-05-19T20:27:45.364983Z",
"shell.execute_reply": "2026-05-19T20:27:45.364467Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_56:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"print('Interpolated points')\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"print('Interpolated points')\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"if (!gROOT->IsBatch() ) {\n",
"\n",
" r<<\"dev.new()\";//Required to activate new window for plot\n",
" //Plot parameter. Plotting using two rows and one column\n",
" r<<\"par(mfrow = c(2,1))\";\n",
"\n",
" //plotting the points\n",
" r<<\"plot(x, y, main = 'approx(.) and approxfun(.)')\";\n",
"\n",
" //The function \"approx\" returns a list with components x and y\n",
" //containing n coordinates which interpolate the given data points according to the method (and rule) desired.\n",
" r<<\"points(approx(x, y), col = 2, pch = '*')\";\n",
" r<<\"points(approx(x, y, method = 'constant'), col = 4, pch = '*')\";\n",
"}\n",
"else {\n",
" r << \"print('Interpolated points')\";\n",
" r << \"print(approx(x,y,n=20))\";\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "e2b72177",
"metadata": {},
"source": [
"The function \"approxfun\" returns a function performing (linear or constant)\n",
"interpolation of the given data.\n",
"For a given set of x values, this function will return the corresponding interpolated values."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "78d3574d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:45.366316Z",
"iopub.status.busy": "2026-05-19T20:27:45.366199Z",
"iopub.status.idle": "2026-05-19T20:27:45.571301Z",
"shell.execute_reply": "2026-05-19T20:27:45.570856Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"f <- approxfun(x, y)\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"f <- approxfun(x, y)\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"f <- approxfun(x, y)\";"
]
},
{
"cell_type": "markdown",
"id": "3288c1cd",
"metadata": {},
"source": [
"using approxfun with const method"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d6c48ab3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:45.572687Z",
"iopub.status.busy": "2026-05-19T20:27:45.572548Z",
"iopub.status.idle": "2026-05-19T20:27:45.775085Z",
"shell.execute_reply": "2026-05-19T20:27:45.774635Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_60:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"fc <- approxfun(x, y, method = 'const')\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"fc <- approxfun(x, y, method = 'const')\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"fc <- approxfun(x, y, method = 'const')\";\n",
"\n",
"if (!gROOT->IsBatch() ) {\n",
" r<<\"curve(f(x), 0, 11, col = 'green2')\";\n",
" r<<\"points(x, y)\";\n",
"\n",
" r<<\"curve(fc(x), 0, 10, col = 'darkblue', add = TRUE)\";\n",
" // different interpolation on left and right side :\n",
" r<<\"plot(approxfun(x, y, rule = 2:1), 0, 11,col = 'tomato', add = TRUE, lty = 3, lwd = 2)\";\n",
"}\n",
"else {\n",
" r << \"x2=x+0.5\";\n",
" r << \"print('Result of approxfun with default method')\";\n",
" r << \"print(paste('x = ',x,' f(x) = ',f(x2)))\";\n",
" r << \"print('Result of approxfun with const method')\";\n",
" r << \"print(paste('x = ',x,' f(x) = ',fc(x2)))\";\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}