{ "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": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 2, "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.2\"\n" ] }, { "data": { "text/plain": [ "\"0.9.0.2\"" ] }, "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": [ "#### 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": 2, "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": 2, "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": 3, "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": 3, "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": 4, "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": 4, "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": 5, "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": 5, "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": 6, "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": 6, "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": 7, "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": 7, "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": 8, "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": 8, "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": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.002701878465693965\n", "0.995590771731077\n" ] }, { "data": { "text/plain": [ "[0.002701878465693965, 0.995590771731077]" ] }, "execution_count": 10, "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": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.35905260384501714\n" ] }, { "data": { "text/plain": [ "0.35905260384501714" ] }, "execution_count": 3, "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": 6, "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": 6, "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": [ "#### 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": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 7, "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": 8, "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": 8, "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": 9, "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": 9, "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": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] }, { "data": { "text/plain": [ "100" ] }, "execution_count": 10, "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": [ "#### 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": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Numo::DFloat#shape=[5,5]\n", "[[0.403835, 0.315208, 1, 0.906438, 0.103955], \n", " [0, 0.187591, 0.0340522, 0.950887, 0.52957], \n", " [0.303649, 0.307339, 0.99628, 0.458339, 0.515865], \n", " [0.525582, 0.876358, 0.0981913, 0.682091, 0.120115], \n", " [0.715885, 0.215871, 0.941261, 0.187328, 0.393366]]\n" ] }, { "data": { "text/plain": [ "Numo::DFloat#shape=[5,5]\n", "[[0.403835, 0.315208, 1, 0.906438, 0.103955], \n", " [0, 0.187591, 0.0340522, 0.950887, 0.52957], \n", " [0.303649, 0.307339, 0.99628, 0.458339, 0.515865], \n", " [0.525582, 0.876358, 0.0981913, 0.682091, 0.120115], \n", " [0.715885, 0.215871, 0.941261, 0.187328, 0.393366]]" ] }, "execution_count": 11, "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": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 12, "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": 13, "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": 13, "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": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Numo::Int32#shape=[5]\n", "[-4, -5, -6, -7, -8]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = Numo::Int32.new(11).seq\n", "z[(3 < z) & (z <= 8)] *= -1" ] }, { "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", "ZZ\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 `
'\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 `'\u001b[0m", "\u001b[37m/usr/local/bin/iruby:22:in `load'\u001b[0m", "\u001b[37m/usr/local/bin/iruby:22:in `
'\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", "zz" ] }, { "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 `
'\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 `'\u001b[0m", "\u001b[37m/usr/local/bin/iruby:22:in `load'\u001b[0m", "\u001b[37m/usr/local/bin/iruby:22:in `
'\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": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.1.5", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.1.5" } }, "nbformat": 4, "nbformat_minor": 0 }