Matrix multiplication: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Phix}}: crash(), added examples)
Line 3,596: Line 3,596:


=={{header|Phix}}==
=={{header|Phix}}==
Copy of [[Matrix_multiplication#Euphoria|Euphoria]]
<lang Phix>function matrix_mul(sequence a, sequence b)
<lang Phix>function matrix_mul(sequence a, sequence b)
sequence c
if length(a[1]) != length(b) then
if length(a[1]) != length(b) then
return 0
crash("invalid aguments")
else
end if
c = repeat(repeat(0,length(b[1])),length(a))
sequence c = repeat(repeat(0,length(b[1])),length(a))
for i=1 to length(a) do
for i=1 to length(a) do
for j=1 to length(b[1]) do
for j=1 to length(b[1]) do
for k=1 to length(a[1]) do
for k=1 to length(a[1]) do
c[i][j] += a[i][k]*b[k][j]
c[i][j] += a[i][k]*b[k][j]
end for
end for
end for
end for
end for
return c
end for
end if
return c
end function</lang>
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 E = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}},
F = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}}
pp(matrix_mul(E,F))

constant G = {{1,2},
{3,4}},
H = {{5,6},
{7,8}}
pp(matrix_mul(G,H))

constant r = sqrt(2)/2,
R = {{ r,r},
{-r,r}}
pp(matrix_mul(R,R))</lang>
{{out}}
<pre>
{{ 9, 12, 15},
{ 19, 26, 33},
{ 29, 40, 51},
{ 39, 54, 69}}
{{ 1, 0, 0, 0},
{ 0, 1, 0, 0},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1}}
{{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}}
{{ 19, 22},
{ 43, 50}}
{{ 0, 1},
{ -1, 0}}
</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==