(phixonline)-->
with javascript_semantics
function matrix_mul(sequence a, b)
integer {ha,wa,hb,wb} = apply({a,a[1],b,b[1]},length)
if wa!=hb then crash("invalid aguments") end if
sequence c = repeat(repeat(0,wb),ha)
for i=1 to ha do
for j=1 to wb do
for k=1 to wa do
c[i][j] += a[i][k]*b[k][j]
end for
end for
end for
return c
end function
ppOpt({pp_Nest,1,pp_IntFmt,"%3d",pp_FltFmt,"%3.0f",pp_IntCh,false})
constant A = { { 1, 2 },
{ 3, 4 },
{ 5, 6 },
{ 7, 8 }},
B = { { 1, 2, 3 },
{ 4, 5, 6 }}
pp(matrix_mul(A,B))
constant C = { { 1, 1, 1, 1 },
{ 2, 4, 8, 16 },
{ 3, 9, 27, 81 },
{ 4, 16, 64, 256 }},
D = { { 4, -3, 4/3, -1/ 4 },
{-13/3, 19/4, -7/3, 11/24 },
{ 3/2, -2, 7/6, -1/ 4 },
{ -1/6, 1/4, -1/6, 1/24 }}
pp(matrix_mul(C,D))
constant F = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}},
G = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}}
pp(matrix_mul(F,G))
constant H = {{1,2},
{3,4}},
I = {{5,6},
{7,8}}
pp(matrix_mul(H,I))
constant r = sqrt(2)/2,
R = {{ r,r},
{-r,r}}
pp(matrix_mul(R,R))
-- large matrix example from OI:
function row(integer i, l) return tagset(i+l,i) end function
constant J = apply(true,row,{tagset(16,0),371}),
K = apply(true,row,{tagset(371,0),16})
pp(shorten(apply(true,shorten,{matrix_mul(J,K),{""},2}),"",2))