{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is Ruby/Numo::NArray version of\n",
    "[100 numpy exercises](http://www.labri.fr/perso/nrougier/teaching/numpy.100/)\n",
    "([Repository](https://github.com/rougier/numpy-100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. Import the numpy package under the name `np` (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "import numpy as np\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "require \"numo/narray\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. Print the numpy version and the configuration (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "print(np.__version__)\n",
    "np.show_config()\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"0.9.0.3\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"0.9.0.3\""
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p Numo::NArray::VERSION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. Create a null vector of size 10 (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros(10)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.zeros(10)\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.  How to find the memory size of any array (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros((10,10))\n",
    "print(\"%d bytes\" % (Z.size * Z.itemsize))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "800 bytes"
     ]
    }
   ],
   "source": [
    "z = Numo::DFloat.zeros(10,10)\n",
    "printf \"%d bytes\", z.byte_size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.  How to get the documentation of the numpy add function from the command line? (★☆☆)\n",
    "\n",
    "Python:\n",
    "\n",
    "$ `python -c \"import numpy; numpy.info(numpy.add)\"`\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[0m\u001b[1;32mNumo::DFloat#+\u001b[m\n",
      "\n",
      "(from gem numo-narray-0.9.0.3)\n",
      "\u001b[32mImplementation from DFloat\u001b[m\n",
      "------------------------------------------------------------------------------\n",
      "  +(p1)\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "Binary add. @overload + other @param [Numo::NArray,Numeric] other @return\n",
      "[Numo::NArray] self + other\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ri 'Numo::DFloat#+'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.  Create a null vector of size 10 but the fifth value which is 1 (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros(10)\n",
    "Z[4] = 1\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.zeros(10)\n",
    "z[4] = 1\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 7.  Create a vector with values ranging from 10 to 49 (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(10,50)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[40]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[40]\n",
       "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, ...]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat[10..49]\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.  Reverse a vector (first element becomes last) (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(50)\n",
    "Z = Z[::-1]\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::Int32(view)#shape=[50]\n",
       "[49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, ...]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(50).seq\n",
    "z = z.reverse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.  Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(9).reshape(3,3)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[3,3]\n",
      "[[0, 1, 2], \n",
      " [3, 4, 5], \n",
      " [6, 7, 8]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[3,3]\n",
       "[[0, 1, 2], \n",
       " [3, 4, 5], \n",
       " [6, 7, 8]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(3,3).seq\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10. Find indices of non-zero elements from \\[1,2,0,0,4,0\\] (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "nz = np.nonzero([1,2,0,0,4,0])\n",
    "print(nz)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[3]\n",
      "[0, 1, 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[3]\n",
       "[0, 1, 4]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nz = Numo::NArray[1,2,0,0,4,0].ne(0).where\n",
    "p nz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 11. Create a 3x3 identity matrix (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.eye(3)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[3,3]\n",
      "[[1, 0, 0], \n",
      " [0, 1, 0], \n",
      " [0, 0, 1]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[3,3]\n",
       "[[1, 0, 0], \n",
       " [0, 1, 0], \n",
       " [0, 0, 1]]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.eye(3)\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 12. Create a 3x3x3 array with random values (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random((3,3,3))\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[3,3,3]\n",
      "[[[0.0617545, 0.373067, 0.794815], \n",
      "  [0.201042, 0.116041, 0.344032], \n",
      "  [0.539948, 0.737815, 0.165089]], \n",
      " [[0.0508827, 0.108065, 0.0687079], \n",
      "  [0.904121, 0.478644, 0.342969], \n",
      "  [0.164541, 0.74603, 0.138994]], \n",
      " [[0.411576, 0.292532, 0.869421], \n",
      "  [0.0854984, 0.688965, 0.159977], \n",
      "  [0.279215, 0.625155, 0.676329]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[3,3,3]\n",
       "[[[0.0617545, 0.373067, 0.794815], \n",
       "  [0.201042, 0.116041, 0.344032], \n",
       "  [0.539948, 0.737815, 0.165089]], \n",
       " [[0.0508827, 0.108065, 0.0687079], \n",
       "  [0.904121, 0.478644, 0.342969], \n",
       "  [0.164541, 0.74603, 0.138994]], \n",
       " [[0.411576, 0.292532, 0.869421], \n",
       "  [0.0854984, 0.688965, 0.159977], \n",
       "  [0.279215, 0.625155, 0.676329]]]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(3,3,3).rand\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random((10,10))\n",
    "Zmin, Zmax = Z.min(), Z.max()\n",
    "print(Zmin, Zmax)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0007664325967829586\n",
      "0.995590771731077\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.0007664325967829586, 0.995590771731077]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10,10).rand\n",
    "zmin, zmax = z.minmax\n",
    "p zmin, zmax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random(30)\n",
    "m = Z.mean()\n",
    "print(m)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5609149765660713\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.5609149765660713"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(30).rand\n",
    "m = z.mean\n",
    "p m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.ones((10,10))\n",
    "Z[1:-1,1:-1] = 0\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10,10]\n",
      "[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
      " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10,10]\n",
       "[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n",
       " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.ones(10,10)\n",
    "z[1..-2,1..-2] = 0\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.ones((5,5))\n",
    "Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: pad\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 17. What is the result of the following expression? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "print(0 * np.nan)\n",
    "print(np.nan == np.nan)\n",
    "print(np.inf > np.nan)\n",
    "print(np.nan - np.nan)\n",
    "print(0.3 == 3 * 0.1)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 * Float::NAN\n",
    "Float::NAN == Float::NAN\n",
    "Float::INFINITY > Float::NAN\n",
    "Float::NAN - Float::NAN\n",
    "0.3 == 3 * 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.diag(1+np.arange(4),k=-1)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[5,5]\n",
      "[[0, 0, 0, 0, 0], \n",
      " [1, 0, 0, 0, 0], \n",
      " [0, 2, 0, 0, 0], \n",
      " [0, 0, 3, 0, 0], \n",
      " [0, 0, 0, 4, 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[5,5]\n",
       "[[0, 0, 0, 0, 0], \n",
       " [1, 0, 0, 0, 0], \n",
       " [0, 2, 0, 0, 0], \n",
       " [0, 0, 3, 0, 0], \n",
       " [0, 0, 0, 4, 0]]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.zeros(5,5)\n",
    "z.diagonal(-1)[] = Numo::Int32[1..4]\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros((8,8),dtype=int)\n",
    "Z[1::2,::2] = 1\n",
    "Z[::2,1::2] = 1\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[8,8]\n",
      "[[0, 1, 0, 1, 0, 1, 0, 1], \n",
      " [1, 0, 1, 0, 1, 0, 1, 0], \n",
      " [0, 1, 0, 1, 0, 1, 0, 1], \n",
      " [1, 0, 1, 0, 1, 0, 1, 0], \n",
      " [0, 1, 0, 1, 0, 1, 0, 1], \n",
      " [1, 0, 1, 0, 1, 0, 1, 0], \n",
      " [0, 1, 0, 1, 0, 1, 0, 1], \n",
      " [1, 0, 1, 0, 1, 0, 1, 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[8,8]\n",
       "[[0, 1, 0, 1, 0, 1, 0, 1], \n",
       " [1, 0, 1, 0, 1, 0, 1, 0], \n",
       " [0, 1, 0, 1, 0, 1, 0, 1], \n",
       " [1, 0, 1, 0, 1, 0, 1, 0], \n",
       " [0, 1, 0, 1, 0, 1, 0, 1], \n",
       " [1, 0, 1, 0, 1, 0, 1, 0], \n",
       " [0, 1, 0, 1, 0, 1, 0, 1], \n",
       " [1, 0, 1, 0, 1, 0, 1, 0]]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# todo: rangewithstep\n",
    "x = Numo::Int32.new(1,8).seq\n",
    "y = Numo::Int32.new(8,1).seq\n",
    "z = (x+y)%2\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "print(np.unravel_index(100,(6,7,8)))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# NArray allows unraveled index access\n",
    "z = Numo::Int32.new(6,7,8).seq\n",
    "p z[100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.tile( np.array([[0,1],[1,0]]), (4,4))\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: tile\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 22. Normalize a 5x5 random matrix (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random((5,5))\n",
    "Zmax, Zmin = Z.max(), Z.min()\n",
    "Z = (Z - Zmin)/(Zmax - Zmin)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[5,5]\n",
      "[[0.766088, 0.769435, 0.641326, 0.836021, 0.287046], \n",
      " [0.838608, 0.290923, 0.0930798, 0.235153, 0.57146], \n",
      " [0.167737, 0.548881, 1, 0.771149, 0.683695], \n",
      " [0.766882, 0.486607, 0.942667, 0, 0.45248], \n",
      " [0.801575, 0.23934, 0.267108, 0.536452, 0.382229]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[5,5]\n",
       "[[0.766088, 0.769435, 0.641326, 0.836021, 0.287046], \n",
       " [0.838608, 0.290923, 0.0930798, 0.235153, 0.57146], \n",
       " [0.167737, 0.548881, 1, 0.771149, 0.683695], \n",
       " [0.766882, 0.486607, 0.942667, 0, 0.45248], \n",
       " [0.801575, 0.23934, 0.267108, 0.536452, 0.382229]]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(5,5).rand\n",
    "zmin, zmax = z.minmax\n",
    "z = (z - zmin)/(zmax - zmin)\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 23. Create a custom dtype that describes a color as four unisgned bytes (RGBA) (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "color = np.dtype([(\"r\", np.ubyte, 1),\n",
    "                  (\"g\", np.ubyte, 1),\n",
    "                  (\"b\", np.ubyte, 1),\n",
    "                  (\"a\", np.ubyte, 1)])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#<Class:0x007f8cfab26a68>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# todo: record\n",
    "color = Numo::Struct.new do\n",
    "  uint8 \"r\"\n",
    "  uint8 \"g\"\n",
    "  uint8 \"b\"\n",
    "  uint8 \"a\"\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[5,2]\n",
      "[[3, 3], \n",
      " [3, 3], \n",
      " [3, 3], \n",
      " [3, 3], \n",
      " [3, 3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[5,2]\n",
       "[[3, 3], \n",
       " [3, 3], \n",
       " [3, 3], \n",
       " [3, 3], \n",
       " [3, 3]]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Numo::DFloat.ones(5,3)\n",
    "y = Numo::DFloat.ones(3,2)\n",
    "z = x.dot y\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Evgeni Burovski\n",
    "\n",
    "Z = np.arange(11)\n",
    "Z[(3 < Z) & (Z <= 8)] *= -1\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[11]\n",
      "[0, 1, 2, 3, -4, -5, -6, -7, -8, 9, 10]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[11]\n",
       "[0, 1, 2, 3, -4, -5, -6, -7, -8, 9, 10]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(11).seq\n",
    "z[(3 < z) & (z <= 8)] *= -1\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 26. What is the output of the following script? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jake VanderPlas\n",
    "\n",
    "print(sum(range(5),-1))\n",
    "from numpy import *\n",
    "print(sum(range(5),-1))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p [*0...5,-1].inject(:+)\n",
    "p Numo::Int32[0...5].sum(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(5)\n",
    "Z**Z\n",
    "2 << Z >> 2\n",
    "Z <- Z\n",
    "1j*Z\n",
    "Z/1/1\n",
    "Z<Z>Z\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "no implicit conversion of Numo::Int32 into Integer",
     "output_type": "error",
     "traceback": [
      "\u001b[31mTypeError\u001b[0m: no implicit conversion of Numo::Int32 into Integer",
      "\u001b[37m(pry):51:in `<<'\u001b[0m",
      "\u001b[37m(pry):51:in `<main>'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "z = Numo::Int32.new(5).seq\n",
    "z**z\n",
    "2 << z >> 2\n",
    "z <- z\n",
    "1i*z\n",
    "z/1/1\n",
    "z<z>z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 28. What are the result of the following expressions?\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "print(np.array(0) / np.array(0))\n",
    "print(np.array(0) // np.array(0))\n",
    "print(np.array([np.nan]).astype(int).astype(float))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "error in NArray operation",
     "output_type": "error",
     "traceback": [
      "\u001b[31mZeroDivisionError\u001b[0m: error in NArray operation",
      "\u001b[37m(pry):56:in `/'\u001b[0m",
      "\u001b[37m(pry):56:in `<main>'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "p Numo::Int32[0] / Numo::Int32[0]\n",
    "p Numo::DFloat[Float::NAN].cast_to(Numo::Int32).cast_to(Numo::DFloat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 29. How to round away from zero a float array ? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Charles R Harris\n",
    "\n",
    "Z = np.random.uniform(-10,+10,10)\n",
    "print (np.trunc(Z + np.copysign(0.5, Z)))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[-7, -0, 10, -5, -7, -1, -10, -3, -1, -5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[-7, -0, 10, -5, -7, -1, -10, -3, -1, -5]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10).rand(-10,+10)\n",
    "p (z + (0.5*z.sign)).trunc\n",
    "# todo: copysign"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 30. How to find common values between two arrays? (★☆☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z1 = np.random.randint(0,10,10)\n",
    "Z2 = np.random.randint(0,10,10)\n",
    "print(np.intersect1d(Z1,Z2))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# todo: intersect1d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 35. How to compute ((A+B)\\*(-A/2)) in place (without copy)? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "A = np.ones(3)*1\n",
    "B = np.ones(3)*2\n",
    "np.add(A,B,out=B)\n",
    "np.divide(A,2,out=A)\n",
    "np.negative(A,out=A)\n",
    "np.multiply(A,B,out=A)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[3]\n",
      "[-1.5, -1.5, -1.5]\n",
      "Numo::DFloat#shape=[3]\n",
      "[-1.5, -1.5, -1.5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[3]\n",
       "[-1.5, -1.5, -1.5]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::DFloat.new(3).fill(1)\n",
    "b = Numo::DFloat.new(3).fill(2)\n",
    "p (a+b)*(-a/2)\n",
    "(a+b.inplace)*(-a.inplace/2)\n",
    "p b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 36. Extract the integer part of a random array using 5 different methods (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.uniform(0,10,10)\n",
    "\n",
    "print (Z - Z%1)\n",
    "print (np.floor(Z))\n",
    "print (np.ceil(Z)-1)\n",
    "print (Z.astype(int))\n",
    "print (np.trunc(Z))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]\n",
      "Numo::DFloat#shape=[10]\n",
      "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]\n",
      "Numo::DFloat#shape=[10]\n",
      "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]\n",
      "Numo::Int32#shape=[10]\n",
      "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]\n",
      "Numo::DFloat#shape=[10]\n",
      "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[0, 3, 7, 2, 1, 3, 5, 7, 1, 0]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10).rand(10)\n",
    "\n",
    "p z - z%1\n",
    "p z.floor\n",
    "p z.ceil - 1\n",
    "p z.cast_to(Numo::Int32)\n",
    "p z.trunc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros((5,5))\n",
    "Z += np.arange(5)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[5,5]\n",
      "[[0, 1, 2, 3, 4], \n",
      " [0, 1, 2, 3, 4], \n",
      " [0, 1, 2, 3, 4], \n",
      " [0, 1, 2, 3, 4], \n",
      " [0, 1, 2, 3, 4]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[5,5]\n",
       "[[0, 1, 2, 3, 4], \n",
       " [0, 1, 2, 3, 4], \n",
       " [0, 1, 2, 3, 4], \n",
       " [0, 1, 2, 3, 4], \n",
       " [0, 1, 2, 3, 4]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.zeros(5,5)\n",
    "z += Numo::Int32.new(5).seq\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.linspace(0,1,12,endpoint=True)[1:-1]\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat(view)#shape=[10]\n",
      "[0.0909091, 0.181818, 0.272727, 0.363636, 0.454545, 0.545455, 0.636364, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat(view)#shape=[10]\n",
       "[0.0909091, 0.181818, 0.272727, 0.363636, 0.454545, 0.545455, 0.636364, ...]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.linspace(0,1,12)[1..-2]\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 40. Create a random vector of size 10 and sort it (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random(10)\n",
    "Z.sort()\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0.0687079, 0.108065, 0.138994, 0.164541, 0.292532, 0.342969, 0.411576, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[0.0687079, 0.108065, 0.138994, 0.164541, 0.292532, 0.342969, 0.411576, ...]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10).rand\n",
    "z = z.sort\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 43. Make an array immutable (read-only) (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.zeros(10)\n",
    "Z.flags.writeable = False\n",
    "Z[0] = 1\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "cannot write to frozen NArray.",
     "output_type": "error",
     "traceback": [
      "\u001b[31mRuntimeError\u001b[0m: cannot write to frozen NArray.",
      "\u001b[37m(pry):7:in `[]='\u001b[0m",
      "\u001b[37m(pry):7:in `<main>'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/var/lib/gems/2.1.0/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/usr/local/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "z = Numo::DFloat.zeros(10)\n",
    "z.freeze\n",
    "z[0] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random((10,2))\n",
    "X,Y = Z[:,0], Z[:,1]\n",
    "R = np.sqrt(X**2+Y**2)\n",
    "T = np.arctan2(Y,X)\n",
    "print(R)\n",
    "print(T)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0.378143, 0.819847, 0.363075, 0.914284, 0.172752, 0.128057, 1.023, ...]\n",
      "Numo::DFloat#shape=[10]\n",
      "[1.40675, 0.247746, 1.24548, 0.939032, 0.298976, 0.566331, 0.486892, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[1.40675, 0.247746, 1.24548, 0.939032, 0.298976, 0.566331, 0.486892, ...]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10,2).rand\n",
    "x,y = z[true,0], z[true,1]\n",
    "r = Numo::NMath.sqrt(x**2+y**2)\n",
    "t = Numo::NMath.atan2(y,x)\n",
    "p r\n",
    "p t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.random(10)\n",
    "Z[Z.argmax()] = 0\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[0, 0.0854984, 0.688965, 0.159977, 0.279215, 0.625155, 0.676329, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[0, 0.0854984, 0.688965, 0.159977, 0.279215, 0.625155, 0.676329, ...]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10).rand\n",
    "z[z.max_index] = 0\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 50. How to find the closest value (to a given scalar) in an array? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(100)\n",
    "v = np.random.uniform(0,100)\n",
    "index = (np.abs(Z-v)).argmin()\n",
    "print(Z[index])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "86\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "86"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(100).seq\n",
    "v = rand*100\n",
    "index = (z-v).abs.min_index\n",
    "p z[index]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 54. How to read the following file? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "from io import StringIO\n",
    "\n",
    "# Fake file\n",
    "s = StringIO(\"\"\"1, 2, 3, 4, 5\\n\n",
    "                6,  ,  , 7, 8\\n\n",
    "                 ,  , 9,10,11\\n\"\"\")\n",
    "Z = np.genfromtxt(s, delimiter=\",\", dtype=np.int)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[3,5]\n",
       "[[1, 2, 3, 4, 5], \n",
       " [6, nan, nan, 7, 8], \n",
       " [nan, nan, 9, 10, 11]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "require \"stringio\"\n",
    "s = StringIO.new(\"1, 2, 3, 4, 5\n",
    "                  6,  ,  , 7, 8\n",
    "                   ,  , 9,10,11\")\n",
    "z = Numo::NArray[*s.readlines.map{|l| l.split(\",\").map{|x| x.strip.empty? ? Float::NAN : x.to_f}}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(9).reshape(3,3)\n",
    "for index, value in np.ndenumerate(Z):\n",
    "    print(index, value)\n",
    "for index in np.ndindex(Z.shape):\n",
    "    print(index, Z[index])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 0], 0]\n",
      "[[0, 1], 1]\n",
      "[[0, 2], 2]\n",
      "[[1, 0], 3]\n",
      "[[1, 1], 4]\n",
      "[[1, 2], 5]\n",
      "[[2, 0], 6]\n",
      "[[2, 1], 7]\n",
      "[[2, 2], 8]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[3,3]\n",
       "[[0, 1, 2], \n",
       " [3, 4, 5], \n",
       " [6, 7, 8]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(3,3).seq\n",
    "z.each_with_index{|x,*i| p [i,x]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 56. Generate a generic 2D Gaussian-like array (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))\n",
    "D = np.sqrt(X*X+Y*Y)\n",
    "sigma, mu = 1.0, 0.0\n",
    "G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )\n",
    "print(G)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10,10]\n",
      "[[0.367879, 0.448221, 0.519795, 0.573753, 0.602798, 0.602798, 0.573753, ...], \n",
      " [0.448221, 0.546108, 0.633313, 0.699056, 0.734444, 0.734444, 0.699056, ...], \n",
      " [0.519795, 0.633313, 0.734444, 0.810684, 0.851723, 0.851723, 0.810684, ...], \n",
      " [0.573753, 0.699056, 0.810684, 0.894839, 0.940138, 0.940138, 0.894839, ...], \n",
      " [0.602798, 0.734444, 0.851723, 0.940138, 0.98773, 0.98773, 0.940138, ...], \n",
      " [0.602798, 0.734444, 0.851723, 0.940138, 0.98773, 0.98773, 0.940138, ...], \n",
      " [0.573753, 0.699056, 0.810684, 0.894839, 0.940138, 0.940138, 0.894839, ...], \n",
      " [0.519795, 0.633313, 0.734444, 0.810684, 0.851723, 0.851723, 0.810684, ...], \n",
      " [0.448221, 0.546108, 0.633313, 0.699056, 0.734444, 0.734444, 0.699056, ...], \n",
      " [0.367879, 0.448221, 0.519795, 0.573753, 0.602798, 0.602798, 0.573753, ...]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10,10]\n",
       "[[0.367879, 0.448221, 0.519795, 0.573753, 0.602798, 0.602798, 0.573753, ...], \n",
       " [0.448221, 0.546108, 0.633313, 0.699056, 0.734444, 0.734444, 0.699056, ...], \n",
       " [0.519795, 0.633313, 0.734444, 0.810684, 0.851723, 0.851723, 0.810684, ...], \n",
       " [0.573753, 0.699056, 0.810684, 0.894839, 0.940138, 0.940138, 0.894839, ...], \n",
       " [0.602798, 0.734444, 0.851723, 0.940138, 0.98773, 0.98773, 0.940138, ...], \n",
       " [0.602798, 0.734444, 0.851723, 0.940138, 0.98773, 0.98773, 0.940138, ...], \n",
       " [0.573753, 0.699056, 0.810684, 0.894839, 0.940138, 0.940138, 0.894839, ...], \n",
       " [0.519795, 0.633313, 0.734444, 0.810684, 0.851723, 0.851723, 0.810684, ...], \n",
       " [0.448221, 0.546108, 0.633313, 0.699056, 0.734444, 0.734444, 0.699056, ...], \n",
       " [0.367879, 0.448221, 0.519795, 0.573753, 0.602798, 0.602798, 0.573753, ...]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Numo::DFloat.linspace(-1,1,10)\n",
    "y = Numo::DFloat.linspace(-1,1,10).expand_dims(1)\n",
    "d = Numo::NMath.sqrt(x*x+y*y)\n",
    "sigma, mu = 1.0, 0.0\n",
    "g = Numo::NMath.exp(-( (d-mu)**2 / ( 2.0 * sigma**2 ) ) )\n",
    "p g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 58. Subtract the mean of each row of a matrix (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Warren Weckesser\n",
    "\n",
    "X = np.random.rand(5, 10)\n",
    "\n",
    "# Recent versions of numpy\n",
    "Y = X - X.mean(axis=1, keepdims=True)\n",
    "\n",
    "# Older versions of numpy\n",
    "Y = X - X.mean(axis=1).reshape(-1, 1)\n",
    "\n",
    "print(Y)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[5,10]\n",
       "[[-0.276694, 0.0346182, 0.456367, -0.137406, -0.222408, 0.00558358, ...], \n",
       " [-0.257554, -0.29691, 0.538503, 0.113026, -0.0226486, -0.201077, ...], \n",
       " [0.49633, -0.287593, 0.315874, -0.213114, -0.0938766, 0.252064, 0.303238, ...], \n",
       " [-0.0359266, -0.116908, 0.508809, 0.423319, -0.309937, -0.404925, ...], \n",
       " [-0.168796, -0.165424, 0.464085, -0.0274508, 0.0251129, 0.0339921, ...]]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Numo::DFloat.new(5, 10).rand\n",
    "y = x - x.mean(1).expand_dims(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 59. How to I sort an array by the nth column? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Steve Tjoa\n",
    "\n",
    "Z = np.random.randint(0,10,(3,3))\n",
    "print(Z)\n",
    "print(Z[Z[:,1].argsort()])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[3,3]\n",
      "[[7, 3, 0], \n",
      " [2, 5, 0], \n",
      " [2, 7, 7]]\n",
      "Numo::Int32(view)#shape=[3,3]\n",
      "[[7, 3, 0], \n",
      " [2, 5, 0], \n",
      " [2, 7, 7]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32(view)#shape=[3,3]\n",
       "[[7, 3, 0], \n",
       " [2, 5, 0], \n",
       " [2, 7, 7]]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(3,3).rand(10)\n",
    "p z\n",
    "p z[z[true,1].sort_index,true]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 60. How to tell if a given 2D array has null columns? (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Warren Weckesser\n",
    "\n",
    "Z = np.random.randint(0,3,(3,10))\n",
    "print((~Z.any(axis=0)).any())\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(3,10).rand(3)\n",
    "(~z.ne(0).any?(0)).any?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 61. Find the nearest value from a given value in an array (★★☆)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.uniform(0,1,10)\n",
    "z = 0.5\n",
    "m = Z.flat[np.abs(Z - z).argmin()]\n",
    "print(m)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4115760120668863\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.4115760120668863"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10).rand\n",
    "x = 0.5\n",
    "m = z[(z - x).abs.min_index]\n",
    "p m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Nadav Horesh\n",
    "\n",
    "w,h = 16,16\n",
    "I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)\n",
    "F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]\n",
    "n = len(np.unique(F))\n",
    "print(np.unique(I))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 256, 257, 65536, 65793, 65792, 65537]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 1, 256, 257, 65536, 65793, 65792, 65537]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# todo: unique\n",
    "w,h = 16,16\n",
    "i = Numo::UInt32.new(h,w,3).rand(2)\n",
    "f = i[false,0]*256*256 + i[false,1]*256 +i[false,2]\n",
    "p f.flatten.sort.to_a.uniq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "A = np.random.randint(0,10,(3,4,3,4))\n",
    "sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n",
    "print(sum)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[3,4]\n",
      "[[80, 51, 48, 48], \n",
      " [51, 58, 56, 43], \n",
      " [59, 61, 73, 54]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[3,4]\n",
       "[[80, 51, 48, 48], \n",
       " [51, 58, 56, 43], \n",
       " [59, 61, 73, 54]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::Int32.new(3,4,3,4).rand(10)\n",
    "sum = a.sum(-2,-1)\n",
    "p sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset  indices? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jaime Fernández del Río\n",
    "\n",
    "D = np.random.uniform(0,1,100)\n",
    "S = np.random.randint(0,10,100)\n",
    "D_sums = np.bincount(S, weights=D)\n",
    "D_counts = np.bincount(S)\n",
    "D_means = D_sums / D_counts\n",
    "print(D_means)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: bincount\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 69. How to get the diagonal of a dot product? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Mathieu Blondel\n",
    "\n",
    "A = np.random.uniform(0,1,(5,5))\n",
    "B = np.random.uniform(0,1,(5,5))\n",
    "\n",
    "# Slow version\n",
    "np.diag(np.dot(A, B))\n",
    "\n",
    "# Fast version\n",
    "np.sum(A * B.T, axis=1)\n",
    "\n",
    "# Faster version\n",
    "np.einsum(\"ij,ji->i\", A, B)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[3]\n",
      "[15, 54, 111]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[3]\n",
       "[15, 54, 111]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::DFloat.new(3,3).seq\n",
    "b = Numo::DFloat.new(3,3).seq\n",
    "p a.mulsum(b.transpose,1)\n",
    "# speed?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 70. Consider the vector \\[1, 2, 3, 4, 5\\], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Warren Weckesser\n",
    "\n",
    "Z = np.array([1,2,3,4,5])\n",
    "nz = 3\n",
    "Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n",
    "Z0[::nz+1] = Z\n",
    "print(Z0)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[17]\n",
       "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::NArray[1,2,3,4,5]\n",
    "nz = 3\n",
    "z0 = Numo::Int32.zeros(z.size + (z.size-1)*(nz))\n",
    "# todo: rangewithstep\n",
    "# z0[(0..-1).step(nz+1)] = z\n",
    "# p z0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "A = np.ones((5,5,3))\n",
    "B = 2*np.ones((5,5))\n",
    "print(A * B[:,:,None])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[5,5,3]\n",
      "[[[2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2]], \n",
      " [[2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2]], \n",
      " [[2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2]], \n",
      " [[2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2], \n",
      "  [2, 2, 2]], \n",
      " ...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[5,5,3]\n",
       "[[[2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2]], \n",
       " [[2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2]], \n",
       " [[2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2]], \n",
       " [[2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2], \n",
       "  [2, 2, 2]], \n",
       " ..."
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::Int32.ones(5,5,3)\n",
    "b = Numo::Int32.new(5,5).fill(2)\n",
    "p a * b[:*,:*,:-]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 72. How to swap two rows of an array? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Eelco Hoogendoorn\n",
    "\n",
    "A = np.arange(25).reshape(5,5)\n",
    "A[[0,1]] = A[[1,0]]\n",
    "print(A)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[5,5]\n",
      "[[5, 6, 7, 8, 9], \n",
      " [0, 1, 2, 3, 4], \n",
      " [10, 11, 12, 13, 14], \n",
      " [15, 16, 17, 18, 19], \n",
      " [20, 21, 22, 23, 24]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[5,5]\n",
       "[[5, 6, 7, 8, 9], \n",
       " [0, 1, 2, 3, 4], \n",
       " [10, 11, 12, 13, 14], \n",
       " [15, 16, 17, 18, 19], \n",
       " [20, 21, 22, 23, 24]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::Int32.new(5,5).seq\n",
    "a[[0,1],true] = a[[1,0],true].copy\n",
    "p a\n",
    "# todo: identity check between read/write array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the  triangles (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Nicolas P. Rougier\n",
    "\n",
    "faces = np.random.randint(0,100,(10,3))\n",
    "F = np.roll(faces.repeat(2,axis=1),-1,axis=1)\n",
    "F = F.reshape(len(F)*3,2)\n",
    "F = np.sort(F,axis=1)\n",
    "G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )\n",
    "G = np.unique(G)\n",
    "print(G)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: roll\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 74. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jaime Fernández del Río\n",
    "\n",
    "C = np.bincount([1,1,2,3,4,4,6])\n",
    "A = np.repeat(np.arange(len(C)), C)\n",
    "print(A)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: bincount, repeat\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 75. How to compute averages using a sliding window over an array? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jaime Fernández del Río\n",
    "\n",
    "def moving_average(a, n=3) :\n",
    "    ret = np.cumsum(a, dtype=float)\n",
    "    ret[n:] = ret[n:] - ret[:-n]\n",
    "    return ret[n - 1:] / n\n",
    "Z = np.arange(20)\n",
    "print(moving_average(Z, n=3))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[18]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[18]\n",
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def moving_average(a, n=3)\n",
    "  ret = a.cumsum\n",
    "  ret[n..-1] = ret[n..-1] - ret[0..-n-1]\n",
    "  ret[n-1..-1] / n\n",
    "end\n",
    "z = Numo::DFloat.new(20).seq\n",
    "p moving_average(z, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z\\[0\\],Z\\[1\\],Z\\[2\\]) and each subsequent row is  shifted by 1 (last row should be (Z\\[-3\\],Z\\[-2\\],Z\\[-1\\]) (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Joe Kington / Erik Rigtorp\n",
    "from numpy.lib import stride_tricks\n",
    "\n",
    "def rolling(a, window):\n",
    "    shape = (a.size - window + 1, window)\n",
    "    strides = (a.itemsize, a.itemsize)\n",
    "    return stride_tricks.as_strided(a, shape=shape, strides=strides)\n",
    "Z = rolling(np.arange(10), 3)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# no module: stride_tricks\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Nathaniel J. Smith\n",
    "\n",
    "Z = np.random.randint(0,2,100)\n",
    "np.logical_not(Z, out=Z)\n",
    "\n",
    "Z = np.random.uniform(-1.0,1.0,100)\n",
    "np.negative(Z, out=Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[100]\n",
      "[0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, ...]\n",
      "Numo::Int32#shape=[100]\n",
      "[1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, ...]\n",
      "Numo::DFloat#shape=[100]\n",
      "[-0.00105903, 0.140595, -0.671175, 0.782669, 0.406039, -0.54762, 0.86998, ...]\n",
      "Numo::DFloat#shape=[100]\n",
      "[0.00105903, -0.140595, 0.671175, -0.782669, -0.406039, 0.54762, -0.86998, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[100]\n",
       "[0.00105903, -0.140595, 0.671175, -0.782669, -0.406039, 0.54762, -0.86998, ...]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# todo: logical_not\n",
    "z = Numo::Int32.new(100).rand(2)\n",
    "p z\n",
    "z.inplace ^ 1\n",
    "p z\n",
    "\n",
    "z = Numo::DFloat.new(100).rand(-1,1)\n",
    "p z\n",
    "-z.inplace\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i  (P0\\[i\\],P1\\[i\\])? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "def distance(P0, P1, p):\n",
    "    T = P1 - P0\n",
    "    L = (T**2).sum(axis=1)\n",
    "    U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L\n",
    "    U = U.reshape(len(U),1)\n",
    "    D = P0 + U*T - p\n",
    "    return np.sqrt((D**2).sum(axis=1))\n",
    "\n",
    "P0 = np.random.uniform(-10,10,(10,2))\n",
    "P1 = np.random.uniform(-10,10,(10,2))\n",
    "p  = np.random.uniform(-10,10,( 1,2))\n",
    "print(distance(P0, P1, p))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[10]\n",
      "[4.40292, 8.02008, 14.77, 16.1336, 16.0784, 7.29705, 9.79877, 10.7509, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10]\n",
       "[4.40292, 8.02008, 14.77, 16.1336, 16.0784, 7.29705, 9.79877, 10.7509, ...]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def distance(p0, p1, p)\n",
    "  t = p1 - p0\n",
    "  l = (t**2).sum(1)\n",
    "  u = -((p0[true,0]-p[false,0])*t[true,0] + (p0[true,1]-p[false,1])*t[true,1]) / l\n",
    "  u = u.reshape(u.size,1)\n",
    "  d = p0 + u*t - p\n",
    "  return Numo::NMath.sqrt((d**2).sum(1))\n",
    "end\n",
    "\n",
    "p0 = Numo::DFloat.new(10,2).rand(-10,10)\n",
    "p1 = Numo::DFloat.new(10,2).rand(-10,10)\n",
    "p  = Numo::DFloat.new( 1,2).rand(-10,10)\n",
    "p distance(p0, p1, p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P\\[j\\]) to each line i (P0\\[i\\],P1\\[i\\])? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Italmassov Kuanysh\n",
    "\n",
    "# based on distance function from previous question\n",
    "P0 = np.random.uniform(-10, 10, (10,2))\n",
    "P1 = np.random.uniform(-10,10,(10,2))\n",
    "p = np.random.uniform(-10, 10, (10,2))\n",
    "print(np.array([distance(P0,P1,p_i) for p_i in p]))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Numo::DFloat#shape=[10]\n",
      "[4.02299, 3.7767, 7.22074, 2.65235, 4.03999, 0.0493129, 5.89712, 0.821607, ...], Numo::DFloat#shape=[10]\n",
      "[4.06287, 10.2757, 5.22386, 9.17308, 3.49263, 6.51912, 12.1396, 6.87145, ...], Numo::DFloat#shape=[10]\n",
      "[12.2731, 4.95469, 14.9322, 6.10746, 12.3248, 8.71038, 4.51429, 8.38228, ...], Numo::DFloat#shape=[10]\n",
      "[7.29208, 11.2986, 5.80238, 9.93285, 11.5081, 9.4783, 9.65492, 10.33, ...], Numo::DFloat#shape=[10]\n",
      "[7.42679, 14.6827, 6.16027, 13.4397, 8.20325, 10.8814, 16.0162, 11.3742, ...], Numo::DFloat#shape=[10]\n",
      "[8.90535, 14.3727, 6.22079, 12.941, 13.1026, 11.9889, 13.0645, 12.8597, ...], Numo::DFloat#shape=[10]\n",
      "[10.121, 14.4822, 11.257, 13.8115, 7.24394, 11.2893, 17.1786, 11.2277, ...], Numo::DFloat#shape=[10]\n",
      "[9.10298, 2.80676, 12.3148, 3.95976, 7.48986, 5.13894, 6.17195, 4.37329, ...], Numo::DFloat#shape=[10]\n",
      "[0.720696, 7.46437, 3.14777, 6.04, 4.88395, 4.03443, 8.16959, 4.84799, ...], Numo::DFloat#shape=[10]\n",
      "[2.82967, 10.3413, 0.872794, 8.92055, 6.33658, 6.80721, 10.889, 7.56309, ...]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Numo::DFloat#shape=[10]\n",
       "[4.02299, 3.7767, 7.22074, 2.65235, 4.03999, 0.0493129, 5.89712, 0.821607, ...], Numo::DFloat#shape=[10]\n",
       "[4.06287, 10.2757, 5.22386, 9.17308, 3.49263, 6.51912, 12.1396, 6.87145, ...], Numo::DFloat#shape=[10]\n",
       "[12.2731, 4.95469, 14.9322, 6.10746, 12.3248, 8.71038, 4.51429, 8.38228, ...], Numo::DFloat#shape=[10]\n",
       "[7.29208, 11.2986, 5.80238, 9.93285, 11.5081, 9.4783, 9.65492, 10.33, ...], Numo::DFloat#shape=[10]\n",
       "[7.42679, 14.6827, 6.16027, 13.4397, 8.20325, 10.8814, 16.0162, 11.3742, ...], Numo::DFloat#shape=[10]\n",
       "[8.90535, 14.3727, 6.22079, 12.941, 13.1026, 11.9889, 13.0645, 12.8597, ...], Numo::DFloat#shape=[10]\n",
       "[10.121, 14.4822, 11.257, 13.8115, 7.24394, 11.2893, 17.1786, 11.2277, ...], Numo::DFloat#shape=[10]\n",
       "[9.10298, 2.80676, 12.3148, 3.95976, 7.48986, 5.13894, 6.17195, 4.37329, ...], Numo::DFloat#shape=[10]\n",
       "[0.720696, 7.46437, 3.14777, 6.04, 4.88395, 4.03443, 8.16959, 4.84799, ...], Numo::DFloat#shape=[10]\n",
       "[2.82967, 10.3413, 0.872794, 8.92055, 6.33658, 6.80721, 10.889, 7.56309, ...]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p0 = Numo::DFloat.new(10,2).rand(-10,10)\n",
    "p1 = Numo::DFloat.new(10,2).rand(-10,10)\n",
    "p  = Numo::DFloat.new(10,2).rand(-10,10)\n",
    "p a = p.shape[0].times.map{|i| distance(p0, p1, p.slice(i,true))}\n",
    "# todo: concat narray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Nicolas Rougier\n",
    "\n",
    "Z = np.random.randint(0,10,(10,10))\n",
    "shape = (5,5)\n",
    "fill  = 0\n",
    "position = (1,1)\n",
    "\n",
    "R = np.ones(shape, dtype=Z.dtype)*fill\n",
    "P  = np.array(list(position)).astype(int)\n",
    "Rs = np.array(list(R.shape)).astype(int)\n",
    "Zs = np.array(list(Z.shape)).astype(int)\n",
    "\n",
    "R_start = np.zeros((len(shape),)).astype(int)\n",
    "R_stop  = np.array(list(shape)).astype(int)\n",
    "Z_start = (P-Rs//2)\n",
    "Z_stop  = (P+Rs//2)+Rs%2\n",
    "\n",
    "R_start = (R_start - np.minimum(Z_start,0)).tolist()\n",
    "Z_start = (np.maximum(Z_start,0)).tolist()\n",
    "R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()\n",
    "Z_stop = (np.minimum(Z_stop,Zs)).tolist()\n",
    "\n",
    "r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]\n",
    "z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]\n",
    "R[r] = Z[z]\n",
    "print(Z)\n",
    "print(R)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: minimum, maximum\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 81. Consider an array Z = \\[1,2,3,4,5,6,7,8,9,10,11,12,13,14\\], how to generate an array R = \\[\\[1,2,3,4\\], \\[2,3,4,5\\], \\[3,4,5,6\\], ..., \\[11,12,13,14\\]\\]? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Stefan van der Walt\n",
    "\n",
    "Z = np.arange(1,15,dtype=np.uint32)\n",
    "R = stride_tricks.as_strided(Z,(11,4),(4,4))\n",
    "print(R)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# no moudle: stride_tricks\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 82. Compute a matrix rank (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Stefan van der Walt\n",
    "\n",
    "Z = np.random.uniform(0,1,(10,10))\n",
    "U, S, V = np.linalg.svd(Z) # Singular Value Decomposition\n",
    "rank = np.sum(S > 1e-10)\n",
    "print(rank)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: svd\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 83. How to find the most frequent value in an array?\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.random.randint(0,10,50)\n",
    "print(np.bincount(Z).argmax())\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: bincount\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Chris Barker\n",
    "\n",
    "Z = np.random.randint(0,5,(10,10))\n",
    "n = 3\n",
    "i = 1 + (Z.shape[0]-3)\n",
    "j = 1 + (Z.shape[1]-3)\n",
    "C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)\n",
    "print(C)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# no module: stride_tricks\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 85. Create a 2D array subclass such that Z\\[i,j\\] == Z\\[j,i\\] (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Eric O. Lebigot\n",
    "# Note: only works for 2d array and value setting using indices\n",
    "\n",
    "class Symetric(np.ndarray):\n",
    "    def __setitem__(self, index, value):\n",
    "        i,j = index\n",
    "        super(Symetric, self).__setitem__((i,j), value)\n",
    "        super(Symetric, self).__setitem__((j,i), value)\n",
    "\n",
    "def symetric(Z):\n",
    "    return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)\n",
    "\n",
    "S = symetric(np.random.randint(0,10,(5,5)))\n",
    "S[2,3] = 42\n",
    "print(S)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[5,5]\n",
      "[[8, 6, 5, 8, 12], \n",
      " [6, 7, 9, 11, 10], \n",
      " [5, 9, 5, 42, 6], \n",
      " [8, 11, 42, 3, 16], \n",
      " [12, 10, 6, 16, 0]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[5,5]\n",
       "[[8, 6, 5, 8, 12], \n",
       " [6, 7, 9, 11, 10], \n",
       " [5, 9, 5, 42, 6], \n",
       " [8, 11, 42, 3, 16], \n",
       " [12, 10, 6, 16, 0]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "module Symetric\n",
    "  def []=(i,j,value)\n",
    "    super(i,j,value)\n",
    "    super(j,i,value) if i != j\n",
    "  end\n",
    "end\n",
    "\n",
    "def symetric(z)\n",
    "  y = z + z.transpose\n",
    "  y.diagonal.store(z.diagonal)\n",
    "  y.extend(Symetric)\n",
    "end\n",
    "\n",
    "s = symetric(Numo::Int32.new(5,5).rand(10))\n",
    "s[2,3] = 42\n",
    "p s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Stefan van der Walt\n",
    "\n",
    "p, n = 10, 20\n",
    "M = np.ones((p,n,n))\n",
    "V = np.ones((p,n,1))\n",
    "S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])\n",
    "print(S)\n",
    "\n",
    "# It works, because:\n",
    "# M is (p,n,n)\n",
    "# V is (p,n,1)\n",
    "# Thus, summing over the paired axes 0 and 0 (of M and V independently),\n",
    "# and 2 and 1, to remain with a (n,1) vector.\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat#shape=[20]\n",
      "[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, ...]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[20]\n",
       "[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, ...]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p, n = 10, 20\n",
    "m = Numo::DFloat.ones(p,n,n)\n",
    "v = Numo::DFloat.ones(p,n,1)\n",
    "s = m.transpose(0,2,1).mulsum(v,0,1)\n",
    "p s\n",
    "# todo: tensordot?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Robert Kern\n",
    "\n",
    "Z = np.ones((16,16))\n",
    "k = 4\n",
    "S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),\n",
    "                                       np.arange(0, Z.shape[1], k), axis=1)\n",
    "print(S)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[4,4]\n",
       "[[16, 16, 16, 16], \n",
       " [16, 16, 16, 16], \n",
       " [16, 16, 16, 16], \n",
       " [16, 16, 16, 16]]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n, k = 16, 4\n",
    "z = Numo::DFloat.ones(n,n)\n",
    "s = z.reshape(n/k,k,n/k,k).sum(1,3)\n",
    "# todo: reduceat?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 88. How to implement the Game of Life using numpy arrays? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Nicolas Rougier\n",
    "\n",
    "def iterate(Z):\n",
    "    # Count neighbours\n",
    "    N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +\n",
    "         Z[1:-1,0:-2]                + Z[1:-1,2:] +\n",
    "         Z[2:  ,0:-2] + Z[2:  ,1:-1] + Z[2:  ,2:])\n",
    "\n",
    "    # Apply rules\n",
    "    birth = (N==3) & (Z[1:-1,1:-1]==0)\n",
    "    survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)\n",
    "    Z[...] = 0\n",
    "    Z[1:-1,1:-1][birth | survive] = 1\n",
    "    return Z\n",
    "\n",
    "Z = np.random.randint(0,2,(50,50))\n",
    "for i in range(100): Z = iterate(Z)\n",
    "print(Z)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[50,50]\n",
      "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, ...], \n",
      " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
      " ...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[50,50]\n",
       "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, ...], \n",
       " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n",
       " ..."
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def iterate(z)\n",
    "  # Count neighbours\n",
    "  n = z[0..-3,0..-3] + z[0..-3,1..-2] + z[0..-3,2..-1] +\n",
    "      z[1..-2,0..-3]                  + z[1..-2,2..-1] +\n",
    "      z[2..-1,0..-3] + z[2..-1,1..-2] + z[2..-1,2..-1]\n",
    "\n",
    "  # Apply rules\n",
    "  birth = n.eq(3) & z[1..-2,1..-2].eq(0)\n",
    "  survive = (n.eq(2) | n.eq(3)) & z[1..-2,1..-2].eq(1)\n",
    "  z[] = 0\n",
    "  #z[1..-2,1..-2][birth | survive] = 1\n",
    "  y = z[0..-3,0..-3].copy\n",
    "  y[birth | survive] = 1\n",
    "  z[1..-2,1..-2] = y\n",
    "end\n",
    "\n",
    "z = Numo::Int32.new(50,50).rand(2)\n",
    "100.times{ iterate(z) }\n",
    "p z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 89. How to get the n largest values of an array (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.arange(10000)\n",
    "np.random.shuffle(Z)\n",
    "n = 5\n",
    "\n",
    "# Slow\n",
    "print (Z[np.argsort(Z)[-n:]])\n",
    "\n",
    "# Fast\n",
    "print (Z[np.argpartition(-Z,n)[:n]])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat(view)#shape=[5]\n",
      "[0.999617, 0.999781, 0.999866, 0.999873, 0.999884]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat(view)#shape=[5]\n",
       "[0.999617, 0.999781, 0.999866, 0.999873, 0.999884]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::DFloat.new(10000).rand\n",
    "n = 5\n",
    "p z[z.sort_index[-n..-1]]\n",
    "# todo: shuffle, argpartition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Stefan Van der Walt\n",
    "\n",
    "def cartesian(arrays):\n",
    "    arrays = [np.asarray(a) for a in arrays]\n",
    "    shape = (len(x) for x in arrays)\n",
    "\n",
    "    ix = np.indices(shape, dtype=int)\n",
    "    ix = ix.reshape(len(arrays), -1).T\n",
    "\n",
    "    for n, arr in enumerate(arrays):\n",
    "        ix[:, n] = arrays[n][ix[:, n]]\n",
    "\n",
    "    return ix\n",
    "\n",
    "print (cartesian(([1, 2, 3], [4, 5], [6, 7])))\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[12,3]\n",
      "[[1, 4, 6], \n",
      " [1, 4, 7], \n",
      " [1, 5, 6], \n",
      " [1, 5, 7], \n",
      " [2, 4, 6], \n",
      " [2, 4, 7], \n",
      " [2, 5, 6], \n",
      " [2, 5, 7], \n",
      " [3, 4, 6], \n",
      " [3, 4, 7], \n",
      " [3, 5, 6], \n",
      " [3, 5, 7]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[12,3]\n",
       "[[1, 4, 6], \n",
       " [1, 4, 7], \n",
       " [1, 5, 6], \n",
       " [1, 5, 7], \n",
       " [2, 4, 6], \n",
       " [2, 4, 7], \n",
       " [2, 5, 6], \n",
       " [2, 5, 7], \n",
       " [3, 4, 6], \n",
       " [3, 4, 7], \n",
       " [3, 5, 6], \n",
       " [3, 5, 7]]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cartesian(*arrays)\n",
    "  arrays = arrays.map{|a| Numo::Int32.cast(a)}\n",
    "  shape = arrays.map{|x| x.size}\n",
    "  asz = arrays.size\n",
    "\n",
    "  ix = Numo::Int32.zeros(*shape, asz)\n",
    "  arrays.each_with_index do |arr,n|\n",
    "    s = [1]*asz\n",
    "    s[n] = arr.size\n",
    "    ix[false,n] = arr.reshape(*s)\n",
    "  end\n",
    "  return ix.reshape(ix.size/asz,asz)\n",
    "end\n",
    "\n",
    "p cartesian([1, 2, 3], [4, 5], [6, 7])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 91. How to create a record array from a regular array? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "Z = np.array([(\"Hello\", 2.5, 3),\n",
    "              (\"World\", 3.6, 2)])\n",
    "R = np.core.records.fromarrays(Z.T,\n",
    "                               names='col1, col2, col3',\n",
    "                               formats = 'S8, f8, i8')\n",
    "print(R)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: record\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Ryan G.\n",
    "\n",
    "x = np.random.rand(5e7)\n",
    "\n",
    "%timeit np.power(x,3)\n",
    "%timeit x*x*x\n",
    "%timeit np.einsum('i,i,i->i',x,x,x)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[50000000]\n",
       "[0.000235508, 0.051923, 0.50211, 0.00812571, 0.00156255, 0.040719, ...]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Numo::DFloat.new(5e7).rand\n",
    "x**3 # probably fast"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Gabe Schwartz\n",
    "\n",
    "A = np.random.randint(0,5,(8,3))\n",
    "B = np.random.randint(0,5,(2,2))\n",
    "\n",
    "C = (A[..., np.newaxis, np.newaxis] == B)\n",
    "rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0]\n",
    "print(rows)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[6]\n",
      "[0, 1, 2, 5, 6, 7]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32#shape=[6]\n",
       "[0, 1, 2, 5, 6, 7]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Numo::Int32.new(8,3).rand(5)\n",
    "b = Numo::Int32.new(2,2).rand(5)\n",
    "c = a[false,:new,:new].eq b\n",
    "rows = (c.count_true(1,2,3) >= b.shape[1]).where\n",
    "p rows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. \\[2,2,3\\]) (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Robert Kern\n",
    "\n",
    "Z = np.random.randint(0,5,(10,3))\n",
    "E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)\n",
    "U = Z[~E]\n",
    "print(Z)\n",
    "print(U)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::Int32#shape=[10,3]\n",
      "[[0, 2, 1], \n",
      " [3, 1, 0], \n",
      " [3, 1, 2], \n",
      " [2, 4, 4], \n",
      " [0, 3, 0], \n",
      " [4, 3, 3], \n",
      " [4, 3, 2], \n",
      " [2, 0, 2], \n",
      " [2, 3, 2], \n",
      " [2, 3, 4]]\n",
      "Numo::Int32(view)#shape=[10,3]\n",
      "[[0, 2, 1], \n",
      " [3, 1, 0], \n",
      " [3, 1, 2], \n",
      " [2, 4, 4], \n",
      " [0, 3, 0], \n",
      " [4, 3, 3], \n",
      " [4, 3, 2], \n",
      " [2, 0, 2], \n",
      " [2, 3, 2], \n",
      " [2, 3, 4]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::Int32(view)#shape=[10,3]\n",
       "[[0, 2, 1], \n",
       " [3, 1, 0], \n",
       " [3, 1, 2], \n",
       " [2, 4, 4], \n",
       " [0, 3, 0], \n",
       " [4, 3, 3], \n",
       " [4, 3, 2], \n",
       " [2, 0, 2], \n",
       " [2, 3, 2], \n",
       " [2, 3, 4]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = Numo::Int32.new(10,3).rand(5)\n",
    "e = (z[true,1..-1].eq z[true,0..-2]).all?(1)\n",
    "u = z[(~e).where,true]\n",
    "p z\n",
    "p u"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 95. Convert a vector of ints into a matrix binary representation (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Warren Weckesser\n",
    "\n",
    "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])\n",
    "B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)\n",
    "print(B[:,::-1])\n",
    "\n",
    "# Author: Daniel T. McDonald\n",
    "\n",
    "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)\n",
    "print(np.unpackbits(I[:, np.newaxis], axis=1))\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: bit\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jaime Fernández del Río\n",
    "\n",
    "Z = np.random.randint(0,2,(6,3))\n",
    "T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))\n",
    "_, idx = np.unique(T, return_index=True)\n",
    "uZ = Z[idx]\n",
    "print(uZ)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: unique row\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Alex Riley\n",
    "# Make sure to read: http://ajcr.net/Basic-guide-to-einsum/\n",
    "\n",
    "A = np.random.uniform(0,1,10)\n",
    "B = np.random.uniform(0,1,10)\n",
    "\n",
    "np.einsum('i->', A)       # np.sum(A)\n",
    "np.einsum('i,i->i', A, B) # A * B\n",
    "np.einsum('i,i', A, B)    # np.inner(A, B)\n",
    "np.einsum('i,j', A, B)    # np.outer(A, B)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Numo::DFloat#shape=[10,10]\n",
       "[[0.418361, 0.252631, 0.168549, 0.367414, 0.0409535, 0.365022, 0.22084, ...], \n",
       " [0.369513, 0.223134, 0.148869, 0.324515, 0.0361717, 0.322402, 0.195055, ...], \n",
       " [0.575741, 0.347666, 0.231954, 0.505629, 0.0563594, 0.502337, 0.303916, ...], \n",
       " [0.423199, 0.255553, 0.170498, 0.371663, 0.0414271, 0.369243, 0.223394, ...], \n",
       " [0.0554262, 0.0334696, 0.02233, 0.0486766, 0.00542569, 0.0483596, ...], \n",
       " [0.285368, 0.172322, 0.114968, 0.250617, 0.0279347, 0.248985, 0.150637, ...], \n",
       " [0.398275, 0.240502, 0.160457, 0.349775, 0.0389873, 0.347497, 0.210238, ...], \n",
       " [0.21605, 0.130464, 0.0870418, 0.18974, 0.0211492, 0.188505, 0.114046, ...], \n",
       " [0.343856, 0.20764, 0.138532, 0.301982, 0.0336601, 0.300016, 0.181511, ...], \n",
       " [0.0467036, 0.0282024, 0.0188159, 0.0410162, 0.00457183, 0.0407492, ...]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# no method: einsum\n",
    "a = Numo::DFloat.new(10).rand(0,1)\n",
    "b = Numo::DFloat.new(10).rand(0,1)\n",
    "\n",
    "a.sum           # np.sum(A)\n",
    "a*b             # A * B\n",
    "a.mulsum(b)     # np.inner(A, B)\n",
    "a[false,:new]*b # np.outer(A, B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Bas Swinckels\n",
    "\n",
    "phi = np.arange(0, 10*np.pi, 0.1)\n",
    "a = 1\n",
    "x = a*phi*np.cos(phi)\n",
    "y = a*phi*np.sin(phi)\n",
    "\n",
    "dr = (np.diff(x)**2 + np.diff(y)**2)**.5 # segment lengths\n",
    "r = np.zeros_like(x)\n",
    "r[1:] = np.cumsum(dr)                # integrate path\n",
    "r_int = np.linspace(0, r.max(), 200) # regular spaced path\n",
    "x_int = np.interp(r_int, r, x)       # integrate path\n",
    "y_int = np.interp(r_int, r, y)\n",
    "```\n",
    "\n",
    "Ruby:\n",
    "```ruby\n",
    "# todo: interp\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Evgeni Burovski\n",
    "\n",
    "X = np.asarray([[1.0, 0.0, 3.0, 8.0],\n",
    "                [2.0, 0.0, 1.0, 1.0],\n",
    "                [1.5, 2.5, 1.0, 0.0]])\n",
    "n = 4\n",
    "M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)\n",
    "M &= (X.sum(axis=-1) == n)\n",
    "print(X[M])\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)\n",
    "\n",
    "\n",
    "Python:\n",
    "```python\n",
    "# Author: Jessica B. Hamrick\n",
    "\n",
    "X = np.random.randn(100) # random 1D array\n",
    "N = 1000 # number of bootstrap samples\n",
    "idx = np.random.randint(0, X.size, (N, X.size))\n",
    "means = X[idx].mean(1)\n",
    "confint = np.percentile(means, [2.5, 97.5])\n",
    "print(confint)\n",
    "```\n",
    "\n",
    "Ruby:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numo::DFloat(view)#shape=[2]\n",
      "[0.345385, 0.449567]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Numo::DFloat(view)#shape=[2]\n",
       "[0.345385, 0.449567]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Numo::DFloat.new(100).rand\n",
    "n = 1000 # number of bootstrap samples\n",
    "idx = Numo::Int32.new(n, x.size).rand(x.size)\n",
    "means = x[idx].mean(1)\n",
    "confint = means[means.sort_index[means.size/100.0*Numo::DFloat[2.5, 97.5]]]\n",
    "p confint\n",
    "# todo: percentile, rand_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}