lua-users home
lua-l archive

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


On Mon, Feb 6, 2012 at 1:47 PM, steve donovan <steve.j.donovan@gmail.com> wrote:
> I'll do some experiments and come up with a shorter notation.

This does the trick:

local M = require 'macro'
local indexes = {
'11','21','31','41',
'12','22','32','42',
'13','23','33','43',
'14','24','34','44'}
local nix = #indexes

M.define('mat4',function(get)
    local names = get:names '\n'
    for _,name in ipairs(names) do
        M.define(name,function(get,put)
            local out = {}
            for i = 1,nix do
                out[i] = name..indexes[i]
            end
            return put:names(out)
        end)
    end
end)

M.define('add4(a,b)',function(a,b)
    a,b = tostring(a), tostring(b)
    local out = {}
    for i = 1,nix do
        out[i] = a..indexes[i]..'+'..b..indexes[i]
    end
    return table.concat(out,',')
end)

This program does the desired thing using luam (say luam -d to see the
expanded source)

require_ 'mat'

mat4 A,B

A = 0, 0, 0, 0,
    0, 0, 0, 0,
    0, 0, 0, 0,
    0, 0, 0, 0

B = 1, 2, 3, 4,
    0.1, 0.2, 0.3, 0.4,
    1,-2,-3,-4,
    1.1,1.2,1.3,1.4

A = add4(A,B)

print(A)

This inlines additions, but multiplications should probably be done as
function, like your mat4add.

A point about this general technique: it would be easy to hit the
maximum number of locals for a given function!

Also, these matrices are rather hard to index ;)

Expressions would be hard in this framework, since LuaMacro just does
lexical substitution.  Metalua would then be the preferred route,
except that it directly compiles to 5.1 bytecode.

steve d.