# Demo for IPyLua¶

You can declare multi-line functions using the Notebook or the qtconsole with ctrl+intro.

In [1]:
function fib(n)
if n <= 2 then return 1 end
return fib(n-1) + fib(n-2)
end

In [2]:
for i=1,10 do print("fib("..i..") =",fib(i)) end

Out[2]:
fib(1) =	1
Out[2]:
fib(2) =	1
Out[2]:
fib(3) =	2
Out[2]:
fib(4) =	3
Out[2]:
fib(5) =	5
Out[2]:
fib(6) =	8
Out[2]:
fib(7) =	13
Out[2]:
fib(8) =	21
Out[2]:
fib(9) =	34
Out[2]:
fib(10) =	55

It is possible to declare tables as usual.

In [3]:
tbl = {
a = 1,
b = 2,
"Hello", "World", "!"
}


And the table can be shown as usual or using the fancy show() command.

In [4]:
print(tbl)
show(tbl)

Out[4]:
table: 0x1428010
Out[4]:
{
[1] = "Hello",
[2] = "World",
[3] = "!",
["a"] = 1,
["b"] = 2,
}
-- table: 0x1428010 with 3 array part, 2 hash part

You can even show multiple objects by columns because show() accepts a variable number of arguments.

In [5]:
show(tbl, {1,2,3,4})

Out[5]:
{
[1] = "Hello",
[2] = "World",
[3] = "!",
["a"] = 1,
["b"] = 2,
}
-- table: 0x1428010 with 3 array part, 2 hash part
{
[1] = 1,
[2] = 2,
[3] = 3,
[4] = 4,
}
-- table: 0x13a3f40 with 4 array part, 0 hash part

The function vars() allow shows all the variables declared by the user (global variables).

In [6]:
vars()

Out[6]:
{
["fib"] = function: 0x14286d0,
["tbl"] = table: 0x1428010,
}
-- table: 0x140d240 with 0 array part, 2 hash part

APRIL-ANN, a toolkit for pattern recognition tasks, has been connected with IPyLua and it is possible to show matrices, images and plots.

In [7]:
require "aprilann"
x = matrix(300,300):linspace(0,0.5)
x = x + x:t()
x_img = Image(x)
show(x_img, x)

Out[7]:
 0             0.00167224    0.00334448    0.00501672   ...  0.5
0.00167224    0.00334448    0.00501672    0.00668896   ...  0.501672
0.00334448    0.00501672    0.00668896    0.0083612    ...  0.503344
0.00501672    0.00668896    0.0083612     0.0100334    ...  0.505017
0.00668896    0.0083612     0.0100334     0.0117057    ...  0.506689
...
0.5           0.501672      0.503344      0.505017     ...  1
# Matrix of size [300,300] stride [300,1] ref [0x1647560 data= 0x1705a00]


Finally, it is possible to perform basic plots (currently: points, bars, lines, hist2d). Plots receive several series, which can be plain Lua tables (with numeric indices or array tables) or APRIL-ANN matrices.

In [10]:
local x = matrix(120,1):linspace(-10,10)
local y = stats.dist.normal(0,2):logpdf(x):exp()
local fig = bokeh.figure{ width=600, height=500 }
-- bars using Lua arrays
fig:bars{ x=0, y=0.1, height=0.2, width=2, alpha=0.3 }
-- bars with APRIL-ANN matrix
fig:bars{ x=x, height=y, y = y/2, width=0.01, color=fig:color_num(1), alpha=0.3 }
-- lines with APRIL-ANN matrix
fig:lines{ x=x, y=y, width=3, color=fig:color_num(1), alpha=0.3, legend="Normal" }
-- points with APRIL-ANN matrix
fig:points{ x=x, y=y, color=fig:color_num(1) }
show(fig)

Out[10]: