lua-users home
lua-l archive

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


On Fri, Dec 17, 2010 at 06:39:55PM +0200, en dator wrote:
 
> I'm working on a little pet project of mine and for it I need to do some
> linear algebra :(  . Now lua is probably not the best choice for language for
> this, but I happen to like it.
>
Lua is an excellent choice for this.  With the aid of metatables and
metamethods you can write functions enabling you to say:

point={}        -- namespace for functions involving points
-- omitting details here: metamethods for __call, __add, __mod etc.
a=point(1,1,1); b=point(4,5,6)
print(a:norm()) --> 1.7320508075689 i.e. sqrt(3)
print(b-a)      --> (3,4,5) 
print(b%a)      --> (-1,0,1), component of b orthogonal to a

And if someone else has built such a basic module 'point', Lua allows you
to add your own functions to it:

function point.dist(a,b) return (a-b):norm() end  

function point.proj_onto(a,b) return a-(a%b) end  -- use as a:proj_onto(b)   

> So does anyone know of a library or similar that I can use, as of right now
> the only thing I will need is a way to do point to plane projections to
> calculate the distance between points in a 3d environment.
> 
The trouble with linear algebra libraries is that they all assume
you are an expert in linear algebra who want code optimized for huge
matrices (well, bigger than 3x3 anyway).  If you are quite sure that
you will only work in 3D, they overkill by some margin.

So even though LuaMatrix and Numeric Lua do all you want and more, 
you need Linear Algebra 101 if not 201 to understand how to use them.

So my recommendation is: write your own, and enjoy the feeling of
power you get from having mastered metatables and metamethods!

Dirk