{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Combinatorial generators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we are going to expose the various desing of experiments that allow to generate all the integer collections satisfying a given combinatorial constraint:\n",
    "\n",
    "- The Tuples generator, which allows to generate all the elements of a Cartesian product $E=\\{0,\\dots,n_0-1\\}\\times\\dots\\times\\{0,\\dots,n_{d-1}-1\\}$.\n",
    "\n",
    "  The total number of generated points is $N=\\prod_{k=0}^{d-1}n_k$.\n",
    "\n",
    "- The K-permutations generator, which allows to generate all the injective functions from $\\{0,\\dots,k-1\\}$ into $\\{0,\\dots,n-1\\}$\n",
    "\n",
    "  The total number of generated points is $N=\\dfrac{n!}{(n-k)!}$.\n",
    "\n",
    "- The Combinations generator, which allows to generate all the subsets of size $k$ of $\\{0,\\dots,n-1\\}$\n",
    "\n",
    "  The total number of generated points is $N=\\dfrac{n!}{k!(n-k)!}$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,2,0],[1,2,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1],[0,2,1],[1,2,1],[0,0,2],[1,0,2],[0,1,2],[1,1,2],[0,2,2],[1,2,2],[0,0,3],[1,0,3],[0,1,3],[1,1,3],[0,2,3],[1,2,3],[0,0,4],[1,0,4],[0,1,4],[1,1,4],[0,2,4],[1,2,4]]#30\n"
     ]
    }
   ],
   "source": [
    "# Tuples\n",
    "experiment = ot.Tuples([2, 3, 5])\n",
    "print(experiment.generate())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0],[0,1,3],[0,3,1],[1,0,3],[1,3,0],[3,0,1],[3,1,0],[0,2,3],[0,3,2],[2,0,3],[2,3,0],[3,0,2],[3,2,0],[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]#24\n"
     ]
    }
   ],
   "source": [
    "# K-permutations\n",
    "experiment = ot.KPermutations(3, 4)\n",
    "print(experiment.generate())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0,1,2,3],[0,1,2,4],[0,1,2,5],[0,1,3,4],[0,1,3,5],[0,1,4,5],[0,2,3,4],[0,2,3,5],[0,2,4,5],[0,3,4,5],[1,2,3,4],[1,2,3,5],[1,2,4,5],[1,3,4,5],[2,3,4,5]]#15\n"
     ]
    }
   ],
   "source": [
    "# Combinations\n",
    "experiment = ot.Combinations(4, 6)\n",
    "print(experiment.generate())"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
