{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a665c034",
   "metadata": {},
   "source": [
    "# vo005_Combinations\n",
    "In this tutorial we learn how combinations of RVecs can be built.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Stefan Wunsch  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:28 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2a34255c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:10.646312Z",
     "iopub.status.busy": "2026-05-19T20:28:10.646196Z",
     "iopub.status.idle": "2026-05-19T20:28:11.603007Z",
     "shell.execute_reply": "2026-05-19T20:28:11.602172Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "from ROOT.VecOps import Take, Combinations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63433a4e",
   "metadata": {},
   "source": [
    "RVec can be sorted in Python with the inbuilt sorting function because\n",
    "PyROOT implements a Python iterator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "81229cca",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:11.604839Z",
     "iopub.status.busy": "2026-05-19T20:28:11.604696Z",
     "iopub.status.idle": "2026-05-19T20:28:11.739464Z",
     "shell.execute_reply": "2026-05-19T20:28:11.738788Z"
    }
   },
   "outputs": [],
   "source": [
    "v1 = ROOT.RVecD(3)\n",
    "v1[0], v1[1], v1[2] = 1, 2, 3\n",
    "v2 = ROOT.RVecD(2)\n",
    "v2[0], v2[1] = -4, -5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74b03caf",
   "metadata": {},
   "source": [
    "To get the indices, which result in all combinations, you can call the\n",
    "following helper.\n",
    "Note that you can also pass the size of the vectors directly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "80837cd4",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:11.741432Z",
     "iopub.status.busy": "2026-05-19T20:28:11.741307Z",
     "iopub.status.idle": "2026-05-19T20:28:11.894899Z",
     "shell.execute_reply": "2026-05-19T20:28:11.894063Z"
    }
   },
   "outputs": [],
   "source": [
    "idx = Combinations(v1, v2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21abbe62",
   "metadata": {},
   "source": [
    "Next, the respective elements can be taken via the computed indices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e34ba37",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:11.896534Z",
     "iopub.status.busy": "2026-05-19T20:28:11.896409Z",
     "iopub.status.idle": "2026-05-19T20:28:12.021175Z",
     "shell.execute_reply": "2026-05-19T20:28:12.020455Z"
    }
   },
   "outputs": [],
   "source": [
    "c1 = Take(v1, idx[0])\n",
    "c2 = Take(v2, idx[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f19c7d1",
   "metadata": {},
   "source": [
    "Finally, you can perform any set of operations conveniently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "009e07e7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:12.023097Z",
     "iopub.status.busy": "2026-05-19T20:28:12.022971Z",
     "iopub.status.idle": "2026-05-19T20:28:12.155564Z",
     "shell.execute_reply": "2026-05-19T20:28:12.154874Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Combinations of { 1.0000000, 2.0000000, 3.0000000 } and { -4.0000000, -5.0000000 }:\n",
      "1.0 * -4.0 = -4.0\n",
      "1.0 * -5.0 = -5.0\n",
      "2.0 * -4.0 = -8.0\n",
      "2.0 * -5.0 = -10.0\n",
      "3.0 * -4.0 = -12.0\n",
      "3.0 * -5.0 = -15.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print(*args, sep=' ', end='\\n', file=None, flush=False)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v3 = c1 * c2\n",
    "\n",
    "print(\"Combinations of {} and {}:\".format(v1, v2))\n",
    "for i in range(len(v3)):\n",
    "    print(\"{} * {} = {}\".format(c1[i], c2[i], v3[i]))\n",
    "print"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46b7d994",
   "metadata": {},
   "source": [
    "However, if you want to compute operations on unique combinations of a\n",
    "single RVec, you can perform this as follows."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad192f9c",
   "metadata": {},
   "source": [
    "Get the indices of unique triples for the given vector."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6d623e76",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:12.157830Z",
     "iopub.status.busy": "2026-05-19T20:28:12.157705Z",
     "iopub.status.idle": "2026-05-19T20:28:12.304280Z",
     "shell.execute_reply": "2026-05-19T20:28:12.303588Z"
    }
   },
   "outputs": [],
   "source": [
    "v4 = ROOT.RVecD(4)\n",
    "v4[0], v4[1], v4[2], v4[3] = 1, 2, 3, 4\n",
    "idx2 = Combinations(v4, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81c2fdab",
   "metadata": {},
   "source": [
    "Take the elements and compute any operation on the returned collections."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fdbb7894",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:12.306384Z",
     "iopub.status.busy": "2026-05-19T20:28:12.306256Z",
     "iopub.status.idle": "2026-05-19T20:28:12.410656Z",
     "shell.execute_reply": "2026-05-19T20:28:12.409887Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Unique triples of { 1.0000000, 2.0000000, 3.0000000, 4.0000000 }:\n",
      "1.0 * 2.0 * 3.0 = 6.0\n",
      "1.0 * 2.0 * 4.0 = 8.0\n",
      "1.0 * 3.0 * 4.0 = 12.0\n",
      "2.0 * 3.0 * 4.0 = 24.0\n"
     ]
    }
   ],
   "source": [
    "c3 = Take(v4, idx2[0])\n",
    "c4 = Take(v4, idx2[1])\n",
    "c5 = Take(v4, idx2[2])\n",
    "\n",
    "v5 = c3 * c4 * c5\n",
    "\n",
    "print(\"Unique triples of {}:\".format(v4))\n",
    "for i in range(len(v5)):\n",
    "    print(\"{} * {} * {} = {}\".format(c3[i], c4[i], c5[i], v5[i]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
