{ "metadata": { "language": "lua", "name": "", "signature": "sha256:f3ddbf4d6bccf8e356162a2248cf98b27c64b3105c5d37864c5f13c6e54fb769" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Demo for IPyLua" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can declare multi-line functions using the Notebook or the qtconsole with ctrl+intro." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function fib(n)\n", " if n <= 2 then return 1 end\n", " return fib(n-1) + fib(n-2)\n", "end" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "for i=1,10 do print(\"fib(\"..i..\") =\",fib(i)) end" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
fib(1) =\t1" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(1) =\t1\n" ] }, { "html": [ "
fib(2) =\t1" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(2) =\t1\n" ] }, { "html": [ "
fib(3) =\t2" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(3) =\t2\n" ] }, { "html": [ "
fib(4) =\t3" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(4) =\t3\n" ] }, { "html": [ "
fib(5) =\t5" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(5) =\t5\n" ] }, { "html": [ "
fib(6) =\t8" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(6) =\t8\n" ] }, { "html": [ "
fib(7) =\t13" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(7) =\t13\n" ] }, { "html": [ "
fib(8) =\t21" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(8) =\t21\n" ] }, { "html": [ "
fib(9) =\t34" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(9) =\t34\n" ] }, { "html": [ "
fib(10) =\t55" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "fib(10) =\t55\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is possible to declare tables as usual." ] }, { "cell_type": "code", "collapsed": false, "input": [ "tbl = {\n", " a = 1,\n", " b = 2,\n", " \"Hello\", \"World\", \"!\"\n", "}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the table can be shown as usual or using the fancy `show()` command." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(tbl)\n", "show(tbl)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
table: 0x1428010" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "table: 0x1428010\n" ] }, { "html": [ "
{\n", "\t[1] = \"Hello\",\n", "\t[2] = \"World\",\n", "\t[3] = \"!\",\n", "\t[\"a\"] = 1,\n", "\t[\"b\"] = 2,\n", "}\n", "-- table: 0x1428010 with 3 array part, 2 hash part" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "{\n", "\t[1] = \"Hello\",\n", "\t[2] = \"World\",\n", "\t[3] = \"!\",\n", "\t[\"a\"] = 1,\n", "\t[\"b\"] = 2,\n", "}\n", "-- table: 0x1428010 with 3 array part, 2 hash part\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can even show multiple objects by columns because `show()` accepts a variable number of arguments." ] }, { "cell_type": "code", "collapsed": false, "input": [ "show(tbl, {1,2,3,4})" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
{\n", "\t[1] = \"Hello\",\n", "\t[2] = \"World\",\n", "\t[3] = \"!\",\n", "\t[\"a\"] = 1,\n", "\t[\"b\"] = 2,\n", "}\n", "-- table: 0x1428010 with 3 array part, 2 hash part
{\n", "\t[1] = 1,\n", "\t[2] = 2,\n", "\t[3] = 3,\n", "\t[4] = 4,\n", "}\n", "-- table: 0x13a3f40 with 4 array part, 0 hash part
{\n", "\t[\"fib\"] = function: 0x14286d0,\n", "\t[\"tbl\"] = table: 0x1428010,\n", "}\n", "-- table: 0x140d240 with 0 array part, 2 hash part" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "{\n", "\t[\"fib\"] = function: 0x14286d0,\n", "\t[\"tbl\"] = table: 0x1428010,\n", "}\n", "-- table: 0x140d240 with 0 array part, 2 hash part\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "APRIL-ANN, a toolkit for pattern recognition tasks, has been connected with IPyLua and it is possible to show matrices, images and plots." ] }, { "cell_type": "code", "collapsed": false, "input": [ "require \"aprilann\"\n", "x = matrix(300,300):linspace(0,0.5)\n", "x = x + x:t()\n", "x_img = Image(x)\n", "show(x_img, x)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
0 0.00167224 0.00334448 0.00501672 ... 0.5 \n", " 0.00167224 0.00334448 0.00501672 0.00668896 ... 0.501672 \n", " 0.00334448 0.00501672 0.00668896 0.0083612 ... 0.503344 \n", " 0.00501672 0.00668896 0.0083612 0.0100334 ... 0.505017 \n", " 0.00668896 0.0083612 0.0100334 0.0117057 ... 0.506689 \n", "...\n", " 0.5 0.501672 0.503344 0.505017 ... 1 \n", "# Matrix of size [300,300] stride [300,1] ref [0x1647560 data= 0x1705a00]\n", "