1. Lua¶
Lua is a powerful and fast scripting language that is easy to learn. In UniPlot R2016 Lua can be used parallel with UniScript. UniPlot uses LuaJIT (http://www.luajit.org) from Mike Pall.
1.1. More Informationen¶
The first edition of “Programming in Lua” by Roberto Ierusalimschy, Lua.org, December 2003, ISBN 8590379817 is available online: http://www.lua.org/pil/contents.html
Here is the link for the reference manual 5.1: http://www.lua.org/manual/5.1/
Good tutorials can be found here: http://lua-users.org/wiki/TutorialDirectory
1.2. Short comparison of Lua and UniScript¶
Here is an example in UniScipt. The function finds the index of the maximum element in a vector:
 1// Maximum and index of a vector
 2def maximum(a)
 3{
 4    mi = 1;
 5    m = a[mi];
 6
 7    for (i in 1:len(a)) {
 8        if (a[i] > m) {
 9            mi = i;
10            m = a[i];
11        }
12    }
13    return [m, mi];
14}
To create the function, we use a UniPlot-Editor. The file must be saved with
the extension .ic. The UniScript=>Save/Execute command or the function
key F4 translates the function. To invoke the function, open the command
window (View=>Command Window). First we create a vector with 1,000,000 random
numbers in the range 0 to 1.
* r = rand(1,1e6)
* tic(); maximum(r); toc()
   56.1462
tic() starts a timer and toc() returns the time since the timer has been
started in milliseconds.
Here is the same function in Lua.
 1-- Maximum and index of a vector
 2function maximum(a)
 3    local mi = 1
 4    local m = a[mi]
 5
 6    for i = 1, #a do
 7        if a[i] > m then
 8            mi = i
 9            m = a[i]
10        end
11    end
12    return m, mi
13end
The function must be saved in a file with the extension .lua.
The first line is a comment. In Lua, comments start with a doublehyphen --
and run until the end of the line.
Instead of def the Lua function is created with the function keyword.
In UniScript statements are enclosed in braces { ... }. Lua uses the
end keyword.
function ... end
for ... do ... end
if ... then ... end
The first statement in UniScript is mi = 1; and in Lua local mi = 1.
In UniScript all variables inside a function are local variables. To create
a global variable, it must be declared at the beginning of the function using
the global keyword.
In Lua a local variable must be declared before using it with the keyword
local. Otherwise the variable is global. See also
http://lua-users.org/wiki/LocalByDefault.
To compile the Lua function press F4 in the editor. To execute the function
open the the command window. To switch into the Lua mode type in .lua.
Now you can execute the following statements:
1> a = {}; for i=1,1e6 do a[i] = math.random() end
2> up = require"uniplot"
3> up.tic(); maximum(a); print(up.toc())
40.95537533378229
The first command creates a Lua table with 1 million elements. The
random() function from the Lua math library will create a random number.
The second statement loads the uniplot library for the functions tic()
and toc(). They use the Windows QueryPerformanceCounter function to
measure the execution time.
The Lua function takes approx. one millisecond. The UniScript function takes approx. 50 milliseconds. The reasons are: 1.) LuaJIT is a Just-In-Time compiler, 2.) UniScript is an interpreter.
In practice, UniScript will call a C function to calculate the maximum.
Exit the Lua command window with the exit command.  In the UniScript
command window the maxi function can be invoked. This function is even
a bit faster than the Lua function.
* a = rand(1e6,1);
* tic(); maxi(a); toc()
    0.7638
1.3. UniScript Interface¶
The r = us.call(us_func_name, p1, ...) function is used to call an
UniScript function. The interface is loaded from the lua_uniscript.dll dll.
Example:
us = require "lua-uniscript"   -- load lua-uniscript.dll
m = us.call("sin", 1)
print(m)
0.8414709848079
If a UniScript function returns a scalar number, it will be converted to a
Lua number. In all other cases the UniScript value will be converted to a
Lua userdata element.
x = us.call("linspace", 0, 2*math.pi, 1e5)
print(x)
matrix: 0x22e70660
print(type(x))
userdata
x is a UniScript vector with 100,000 points.
y = us.call("sin", x)
us.call("plot", x, y)
Normally us.call() will not be invoked directly. A wrapper function is
used instead. Here are some examples of the uniplot module:
-- uniplot.lua
local us = require "lua-uniscript"
local mat = require "uniplot.matrix"
local u = {}
u.abs = function(a1)
    return mat:new(us.call("abs", a1))
end
u.acos = function(a1)
    return mat:new(us.call("acos", a1))
end
u.acosh = function(a1)
    return mat:new(us.call("acosh", a1))
end
return u
With the following two packages
up = require"uniplot"
mat = require"uniplot.matrix"
the following UniScript operators can be executed:
UniScript  | 
Lua  | 
Comment  | 
|---|---|---|
  | 
  | 
The matrices must have the same size or must be scalars.  | 
  | 
  | 
Subtraction.  | 
  | 
  | 
Matrix multiplication  | 
  | 
  | 
Multiplication element wise  | 
  | 
  | 
|
  | 
  | 
Disision element wise  | 
  | 
||
  | 
  | 
Transponse.  | 
  | 
  | 
|
  | 
  | 
Number of rows.  | 
  | 
  | 
Number of columns.  | 
  | 
  | 
Number of elements of a vector.  | 
  | 
  | 
Inverse of a matrix.  | 
  | 
  | 
Row vector  | 
  | 
  | 
Column vector.  | 
  | 
  | 
Matrix  | 
  | 
  | 
Concatenate matrices horizontally  | 
  | 
  | 
Concatenate matrices vertical  | 
  | 
  | 
Compare operators for matrices and vectors.  | 
  | 
  | 
Bit wise and, or, exclusiv or, not, shift left, shift right  | 
  | 
  | 
Sequence   | 
  | 
  | 
|
  | 
  | 
|
  | 
reshape(a, 1, #a)  | 
|
  | 
  | 
Create complex numbers and matrices.  | 
1.4. Lua Debugger¶
A Lua-Debugger will be available in one of the next releases.
1.5. Remarks¶
The LuaJIT version is 2.1-beta, 2016-01-18 with
-DLUAJIT_ENABLE_LUA52COMPATenabled. The DLLluajit.dllis compiled using the Microsoft Visual Studio 2015 kompiliert with the C-Runtime-Librarymsvcr120.dll.The LuaJIT DLL can be replaced by an owner build DLL.
id-1010283