While I was watching the Ohio State / Penn State football game, I thought I’d write a multidimensional matrix multiply. It wasn’t as bad or as big as I thought it would be. Just create 2 multidimensional matrices. Each matrix will display. The requirement is the number of columns in matrix A must equal the number of rows in matrix B. Don’t know if anyone can use this but it gave me something to do while watching the game. Depending on the matrix size, you might have to adjust the WIDTH values in draw() as to where each matrix displays.
PS. Matrix C is created in the same format as matrices A and B.
viewer.mode = FULLSCREEN
function setup()
-- number of columns in A must equal number of rows in B
A={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}}
B={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}}
if #A[1]==#B then
ok=true
C=matrixMultiply(A,B)
end
fill(255)
end
function draw()
background(0)
matrixPrint(A,1,HEIGHT/2)
matrixPrint(B,WIDTH*.3,HEIGHT/2)
if ok then
matrixPrint(C,WIDTH*.65,HEIGHT/2)
else
text("Column/Row mismatch",WIDTH/2,HEIGHT-100)
end
end
function matrixMultiply(a,b)
local m={}
for x=1,#a do
m[x]={}
for y=1,#b[1] do
total=0
for z=1,#b do
total=total+a[x][z]*b[z][y]
end
m[x][y]=total
end
end
return m
end
function matrixPrint(m,w,h)
for y=1,#m[1] do
for x=1,#m do
text(m[x][y],w+y*40,h-x*40)
end
end
end