@Ignatz perhaps this might give some insight into matrix multiplication
Using the example from @Andrew_Stacey’s tutorial on matrices:
http://loopspace.mathforge.org/discussion/13/matrices-in-codea
-- rotate y axis
rotate(90,0,1,0)
mb = modelMatrix()
resetMatrix()
-- rotate x axis
rotate(90,1,0,0)
ma = modelMatrix()
resetMatrix()
-- combined transform
rotate(90,0,1,0)
rotate(90,1,0,0)
mc = modelMatrix()
print("Rotate")
print("mc=")
print(mc)
print("ma*mb")
print(ma*mb)
print("mb*ma")
print(mb*ma)
This does the same multiplication manually and outputs what’s happening at each iteration to show how the result is calculated to get to (ma*mb)=mc
-- matrix A equivalent to (ma)
-- resetMatrix()
-- rotate(90,1,0,0)
-- ma = modelMatrix()
mta={{1.00, 0.00, 0.00, 0.00},
{0.00, -0.00, 1.00, 0.00},
{0.00, -1.00, -0.00, 0.00},
{0.00, 0.00, 0.00, 1.00}}
-- matrix B equivalent to (mb)
-- resetMatrix()
-- rotate(90,0,1,0)
-- mb = modelMatrix()
mtb={{-0.00, 0.00, -1.00, 0.00},
{0.00, 1.00, 0.00, 0.00},
{1.00, 0.00, -0.00, 0.00},
{0.00, 0.00, 0.00, 1.00}}
-- Result matrix (mc)
-- will be equivalent to (ma*mb) or (mb*ma) (in this case)
mr={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}}
-- output vars
local mtstr =""
local index =""
local map =""
for row=1,4 do
for col =1,4 do
-- create output
index = index.."mr["..row.."]["..col.."]="..mr[row][col].."\
"
for cur = 1,4 do
-- capture before for output
local before = mr[row][col]
-- multiply: for each row in Martix A multiply left side across with each column from the top down in Matrix B and add to the current result / row and column being calculated
mr[row][col] = mr[row][col]+(mta[row][cur]*mtb[cur][col])
-- more output
index = index .. "mr["..row.."]["..col.."]+=(mta["..row.."]["..cur.."]*mtb["..cur.."]["..col.."])"
index = index .. "=\
"..before.."+("..mta[row][cur].."*"
..mtb[cur][col].."="..(mta[row][cur]*mtb[cur][col])..")="..mr[row][col].."\
"
end
-- more output
mtstr=mtstr..string.format("%.2f",tostring(mr[row][col]))..","
map = map.."mr["..row.."]["..col.."] "
index = index.."mr["..row.."]["..col.."]="..mr[row][col].."\
\
"
-- column moves one down
end
-- more output
index = index.."\
"
mtstr = mtstr.."\
"
map = map.."\
"
-- row moves one down
end
print("Mapping:\
"..map)
print(index)
print("Results:")
print(mtstr)
print("==")
print(mc)