{ "cells": [ { "cell_type": "markdown", "id": "bc8d5b39", "metadata": {}, "source": [ "# vo002_VectorCalculations\n", "In this tutorial we learn how the RVec class can be used to\n", "express easily mathematical operations involving arrays and scalars.\n", "\n", "\n", "\n", "\n", "**Author:** Danilo Piparo \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": "markdown", "id": "90dbc623", "metadata": {}, "source": [ "Operations on RVec instances are made to be *fast* (vectorisation is exploited)\n", "and easy to use. RVecF, RVecD, RVecI, ..., are handy aliases for RVec, RVec, RVec, ..." ] }, { "cell_type": "code", "execution_count": 1, "id": "f646db42", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:01.843823Z", "iopub.status.busy": "2026-05-19T20:28:01.843711Z", "iopub.status.idle": "2026-05-19T20:28:02.189640Z", "shell.execute_reply": "2026-05-19T20:28:02.188965Z" } }, "outputs": [], "source": [ "ROOT::RVecF v1{1., 2., 3.};\n", "ROOT::RVecF v2{4., 5., 6.};" ] }, { "cell_type": "markdown", "id": "939174cf", "metadata": {}, "source": [ "Arithmetic operations are to be intended on pairs of elements with identical index" ] }, { "cell_type": "code", "execution_count": 2, "id": "6a1748be", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:02.191624Z", "iopub.status.busy": "2026-05-19T20:28:02.191486Z", "iopub.status.idle": "2026-05-19T20:28:02.399002Z", "shell.execute_reply": "2026-05-19T20:28:02.398291Z" } }, "outputs": [], "source": [ "auto v_sum = v1 + v2;\n", "auto v_mul = v1 * v2;" ] }, { "cell_type": "markdown", "id": "6387326f", "metadata": {}, "source": [ "Easy to inspect:" ] }, { "cell_type": "code", "execution_count": 3, "id": "f05db608", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:02.413383Z", "iopub.status.busy": "2026-05-19T20:28:02.413245Z", "iopub.status.idle": "2026-05-19T20:28:02.621810Z", "shell.execute_reply": "2026-05-19T20:28:02.621237Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "v1 = { 1, 2, 3 }\n", "v2 = { 4, 5, 6 }\n", "v1 + v2 = { 5, 7, 9 }\n", "v1 * v2 = { 4, 10, 18 }\n" ] } ], "source": [ "std::cout << \"v1 = \" << v1 << \"\\n\"\n", " << \"v2 = \" << v2 << \"\\n\"\n", " << \"v1 + v2 = \" << v_sum << \"\\n\"\n", " << \"v1 * v2 = \" << v_mul << std::endl;" ] }, { "cell_type": "markdown", "id": "759e4658", "metadata": {}, "source": [ "It's also possible to mix scalars and RVecs" ] }, { "cell_type": "code", "execution_count": 4, "id": "6b17f9de", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:02.623509Z", "iopub.status.busy": "2026-05-19T20:28:02.623389Z", "iopub.status.idle": "2026-05-19T20:28:02.831722Z", "shell.execute_reply": "2026-05-19T20:28:02.831127Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{ 1, 2, 3 } - 2 = { -1, 0, 1 }\n", "2 - { 1, 2, 3 } = { 1, 0, -1 }\n", "{ 1, 2, 3 } / 2 = { 0.5, 1, 1.5 }\n", "2 / { 1, 2, 3 } = { 2, 1, 0.666667 }\n" ] } ], "source": [ "auto v_diff_s_0 = v1 - 2;\n", "auto v_diff_s_1 = 2 - v1;\n", "auto v_div_s_0 = v1 / 2.;\n", "auto v_div_s_1 = 2. / v1;\n", "\n", "std::cout << v1 << \" - 2 = \" << v_diff_s_0 << \"\\n\"\n", " << \"2 - \" << v1 << \" = \" << v_diff_s_1 << \"\\n\"\n", " << v1 << \" / 2 = \" << v_div_s_0 << \"\\n\"\n", " << \"2 / \" << v1 << \" = \" << v_div_s_1 << std::endl;" ] }, { "cell_type": "markdown", "id": "91aa166c", "metadata": {}, "source": [ "Dot product and the extraction of quantities such as Mean, Min and Max\n", "are also easy to express (see here for the full list:\n", "https://root.cern/doc/master/namespaceROOT_1_1VecOps.html)" ] }, { "cell_type": "code", "execution_count": 5, "id": "576e1336", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:02.833365Z", "iopub.status.busy": "2026-05-19T20:28:02.833245Z", "iopub.status.idle": "2026-05-19T20:28:03.041691Z", "shell.execute_reply": "2026-05-19T20:28:03.041132Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:2:17: error: cannot deduce 'auto' from unknown expression\n", " auto v1_mean = Mean(v1);\n", " ^\n" ] } ], "source": [ "auto v1_mean = Mean(v1);\n", "auto v1_dot_v2 = Dot(v1, v2);\n", "\n", "std::cout << \"Mean of \" << v1 << \" is \" << v1_mean << \"\\n\"\n", " << \"Dot product of \" << v1 << \" and \" << v2 << \" is \" << v1_dot_v2 << std::endl;" ] }, { "cell_type": "markdown", "id": "62452886", "metadata": {}, "source": [ "Most used mathematical functions are supported" ] }, { "cell_type": "code", "execution_count": 6, "id": "feceda00", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:03.043083Z", "iopub.status.busy": "2026-05-19T20:28:03.042969Z", "iopub.status.idle": "2026-05-19T20:28:03.251056Z", "shell.execute_reply": "2026-05-19T20:28:03.250617Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "exp({ 1, 2, 3 }) = { 2.71828, 7.38906, 20.0855 }\n", "log({ 1, 2, 3 }) = { 0, 0.693147, 1.09861 }\n", "sin({ 1, 2, 3 }) = { 0.841471, 0.909297, 0.14112 }\n" ] } ], "source": [ "auto v_exp = exp(v1);\n", "auto v_log = log(v1);\n", "auto v_sin = sin(v1);\n", "\n", "std::cout << \"exp(\" << v1 << \") = \" << v_exp << \"\\n\"\n", " << \"log(\" << v1 << \") = \" << v_log << \"\\n\"\n", " << \"sin(\" << v1 << \") = \" << v_sin << std::endl;" ] }, { "cell_type": "markdown", "id": "c8ea7e6a", "metadata": {}, "source": [ "Even an optimised version of the functions is available\n", "provided that VDT is not disabled during the configuration" ] }, { "cell_type": "code", "execution_count": 7, "id": "5efe5598", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:03.252572Z", "iopub.status.busy": "2026-05-19T20:28:03.252461Z", "iopub.status.idle": "2026-05-19T20:28:03.459685Z", "shell.execute_reply": "2026-05-19T20:28:03.459181Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Unbalanced braces. This cell was not processed.\n" ] } ], "source": [ "#ifdef R__HAS_VDT\n", "auto v_fast_exp = fast_exp(v1);\n", "auto v_fast_log = fast_log(v1);\n", "auto v_fast_sin = fast_sin(v1);\n", "\n", "std::cout << \"fast_exp(\" << v1 << \") = \" << v_fast_exp << \"\\n\"\n", " << \"fast_log(\" << v1 << \") = \" << v_fast_log << \"\\n\"\n", " << \"fast_sin(\" << v1 << \") = \" << v_fast_sin << std::endl;" ] }, { "cell_type": "markdown", "id": "11659821", "metadata": {}, "source": [ "It may happen that a custom operation needs to be applied to the RVec.\n", "In this case, the Map utility can be used:" ] }, { "cell_type": "code", "execution_count": 8, "id": "49d958d7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:03.461142Z", "iopub.status.busy": "2026-05-19T20:28:03.461032Z", "iopub.status.idle": "2026-05-19T20:28:03.666064Z", "shell.execute_reply": "2026-05-19T20:28:03.665544Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:5:2: error: #endif without #if\n", "#endif\n", " ^\n", "input_line_59:2:18: error: cannot deduce 'auto' from unknown expression\n", " auto v_transf = Map(v1, [](double x) { return x * 2 / 3; });\n", " ^\n" ] } ], "source": [ "auto v_transf = Map(v1, [](double x) { return x * 2 / 3; });\n", "\n", "std::cout << \"Applying [](double x){return x * 2 / 3;} to \" << v1 << \" leads to \" << v_transf << \"\\n\";\n", "#endif" ] } ], "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 }