{
"cells": [
{
"cell_type": "markdown",
"id": "e041e38a",
"metadata": {},
"source": [
"# tmva001_RTensor\n",
"This tutorial illustrates the basic features of the RTensor class,\n",
"RTensor is a std::vector-like container with additional shape information.\n",
"The class serves as an interface in C++ between multi-dimensional data and\n",
"the algorithm such as in machine learning workflows. The interface is similar\n",
"to Numpy arrays and provides a subset of the functionality.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Stefan Wunsch \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:22 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1079e162",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:29.772749Z",
"iopub.status.busy": "2026-05-19T20:22:29.772551Z",
"iopub.status.idle": "2026-05-19T20:22:30.091041Z",
"shell.execute_reply": "2026-05-19T20:22:30.090468Z"
}
},
"outputs": [],
"source": [
"using namespace TMVA::Experimental;"
]
},
{
"cell_type": "markdown",
"id": "c5e4acd3",
"metadata": {},
"source": [
"Create RTensor from scratch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "61b92bfe",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:30.092663Z",
"iopub.status.busy": "2026-05-19T20:22:30.092521Z",
"iopub.status.idle": "2026-05-19T20:22:30.300237Z",
"shell.execute_reply": "2026-05-19T20:22:30.299676Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ { 0, 0 } { 0, 0 } }\n"
]
}
],
"source": [
"RTensor x({2, 2});\n",
"cout << x << endl;"
]
},
{
"cell_type": "markdown",
"id": "06f73d19",
"metadata": {},
"source": [
"Assign some data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f18fa620",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:30.301725Z",
"iopub.status.busy": "2026-05-19T20:22:30.301584Z",
"iopub.status.idle": "2026-05-19T20:22:30.507203Z",
"shell.execute_reply": "2026-05-19T20:22:30.506655Z"
}
},
"outputs": [],
"source": [
"x(0, 0) = 1;\n",
"x(0, 1) = 2;\n",
"x(1, 0) = 3;\n",
"x(1, 1) = 4;"
]
},
{
"cell_type": "markdown",
"id": "d2bdc1f9",
"metadata": {},
"source": [
"Apply transformations"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c61b00ff",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:30.508837Z",
"iopub.status.busy": "2026-05-19T20:22:30.508718Z",
"iopub.status.idle": "2026-05-19T20:22:30.729838Z",
"shell.execute_reply": "2026-05-19T20:22:30.729363Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 1, 2, 3, 4 }\n"
]
}
],
"source": [
"auto x2 = x.Reshape({1, 4}).Squeeze();\n",
"cout << x2 << endl;"
]
},
{
"cell_type": "markdown",
"id": "c50a1a3b",
"metadata": {},
"source": [
"Slice"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "298d09de",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:30.732931Z",
"iopub.status.busy": "2026-05-19T20:22:30.732818Z",
"iopub.status.idle": "2026-05-19T20:22:30.938148Z",
"shell.execute_reply": "2026-05-19T20:22:30.937766Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 1, 3 }\n"
]
}
],
"source": [
"auto x3 = x.Reshape({2, 2}).Slice({{0, 2}, {0, 1}});\n",
"cout << x3 << endl;"
]
},
{
"cell_type": "markdown",
"id": "1ff4caa9",
"metadata": {},
"source": [
"Create tensor as view on data without ownership"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "55c07411",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:30.939273Z",
"iopub.status.busy": "2026-05-19T20:22:30.939164Z",
"iopub.status.idle": "2026-05-19T20:22:31.144791Z",
"shell.execute_reply": "2026-05-19T20:22:31.144392Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ { 5, 6 } { 7, 8 } }\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n",
" float data[] = {5, 6, 7, 8};\n",
" ^\n"
]
}
],
"source": [
"float data[] = {5, 6, 7, 8};\n",
"RTensor y(data, {2, 2});\n",
"cout << y << endl;"
]
},
{
"cell_type": "markdown",
"id": "2e9549e9",
"metadata": {},
"source": [
"Create tensor as view on data with ownership"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eb8fb8b2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:22:31.145888Z",
"iopub.status.busy": "2026-05-19T20:22:31.145782Z",
"iopub.status.idle": "2026-05-19T20:22:31.351186Z",
"shell.execute_reply": "2026-05-19T20:22:31.350815Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ { 9, 10 } { 11, 12 } }\n"
]
}
],
"source": [
"auto data2 = std::make_shared>(4);\n",
"float c = 9;\n",
"for (auto &v : *data2) {\n",
" v = c;\n",
" c++;\n",
"}\n",
"\n",
"RTensor z(data2, {2, 2});\n",
"cout << z << endl;"
]
}
],
"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
}