lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

I would like to share with you my enthusiasm about an algorithm for
plotting contour curves with lua  by using gsl shell module for
graphics.

At the beginning I began to code the algorithm in C but at some points
I've realised that Lua was perfectly fine for the task and when I've
implemented it the code ran at a quite acceptable speed even if the
algorithm was complicated and with a lot of FP calculations.

So it was amazing for me to discover that I was able to code the whole
algorithm entirely in Lua and still having acceptable performance in
term of speed.

I'm also specially proud of the algorithm because it is absolutely
accurate and produces beautiful smooth curves. As far as I know the
algorithm is not described in the literature and I was willing to
write an article about it. For the other side the algorithm is
computation intensive and requires the jacobian of the function.

If you want to give a look to some examples you can look at the
following images:

http://www.nongnu.org/gsl-shell/contour/demo1-search.PNG
http://www.nongnu.org/gsl-shell/contour/demo2-two-peaks.PNG

and the code samples is available here:

http://www.nongnu.org/gsl-shell/contour/contour.lua
http://www.nongnu.org/gsl-shell/contour/test.lua (please note that it
is copyrighted under GPL).

Otherwise the code is already available in the SVN repository at
savannah. I've also implemented the interface to the multi variables
minimisation algorithm of GSL and I'm planning to publish this stuff
with the next release of gsl shell.

If you want to give a look at the code and give me some suggestions
about the style or the technique it will be nice, I'm not used to
write complex routines in Lua.

For the other side I've discovered some shortcomings of Lua:
1) the lack of a debugger to step through the code is really painful.
I'm used to to so in C/C++ with gdb and not having that with lua was
painful.
2) in lua all the arguments are passed by values except when they are
objects because in this latter case you can have only reference
(pointer) to the objects. When writing complex algorithm is confusing
because you have always to keep trace in your mind if a routine modify
an object or if it does creates a new one. For examples if I write:
p = q
or
p = q + z
where these are objects the first statement will just point p to the
same object of q while the seconde statement will create a new object
for 'p'. If you later change 'p' you may or not changing the original
object 'q' depending on the assignment that you've done. I'm always
unconfortable with this kind of behaviour and I need to stay alert
about object assignment and side effects.
3) the lack of any typing system and mandatory variable declarations
in Lua prevent the detection of many errors that could be otherwise
detected by the compiler. For example  took me a while to find this
bug: I wrote
if i1 == i2 then bla bla...
instead of
if g.i1 == g.i2 then bla bla...
The interpreter did not raise any error not even at run time because
i1 and i2 was supposed to be global variables and as both of them were
nil they were actually equal!

I've discovered this shortcoming but I don't mean that they are
specific to Lua because I guess these kind of problems are common to
any dynamic typed object oriented programming language like python for
example.

For the other side ML languages like OCaml are excellent and much
better for coding complex algorithms because their strong typing
system detect many many error at compile time and because there are
never unexpected side effects because object cannot be changed by a
subroutines because you did not pay enough attention.

I was willing to share with you guys these reflections, I hope it will
be useful or interesting for someone.

Best regards,
Francesco