# Questions from a newcomer...

3 messages
Open this post in threaded view
|

## Questions from a newcomer...

 ```I'm a relatively new user of lua, but let me say from the start that it seems like a very well done and useful bit of software. I've done some mild experimenation with it, and in spite of (or maybe because of) its small size, it has been remarkably easy to extend an application I've been working on to use it. I do have a couple of questions about how to use lua most effectively. I have a computer graphics application which passes vectors (4 floats) and homogeneous matrices (4x4 matrix of floats) around. I'd like the user to be able to be able to create and manipulate these data structures in lua as well as in C. For instance, I'd like to be able to write code that looks like: m = matrix{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0} ; v = vector{10.0, 3.0, 1.0} ; newvec = m * v ; Would it be best to make m and v user data objects, and tag them with types, then override the tag methods to check for these types prior to calling the original tag methods? Or is there another, better way to go about this? Thanks in advance... Mark <[hidden email]> ```
 ```>From [hidden email] Fri May 14 19:31:45 1999 > >m = matrix{1.0, 0.0, 0.0, 0.0, > 0.0, 1.0, 0.0, 0.0, > 0.0, 0.0, 1.0, 0.0, > 0.0, 0.0, 0.0, 1.0} ; > > >v = vector{10.0, 3.0, 1.0} ; > >newvec = m * v ; > >Would it be best to make m and v user data objects, and tag them with types, then override the tag methods to check for these types prior to calling the original tag methods? try something like this: MATVEC=newtag() function matrix(t) settag(t,MATVEC) t.type="matrix" return t end function vector(t) settag(t,MATVEC) t.type="vector" return t end function do_mul(a,b) -- test a.type and b.type and do the right thing end settagmethod(MATVEC, "mul", do_mul) --lhf ```
 In reply to this post by mark-2 ```> Would it be best to make m and v user data objects, [...] That depends on how ofter you will use these data in Lua and in C. If in Lua, then it is better to represent your data as Lua tables, and to write your C code so that it accesses those tables when it needs the numbers. Otherwise, you can have them represented as userdata, plus two Cfunctions registered in Lua to set and get their values. Then you can use 'settable' and 'gettable' to manipulate the data using Lua syntax: settagmethod(MATVEC, 'gettable', getCvalue) settagmethod(MATVEC, 'settable', setCvalue) print(v[3]) -- calls 'getCvalue' to get value from array in C -- Roberto PS: maybe you should change your matrix to something like m = matrix{{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}} ; with explicit rows and columns. ```