Matrix multiplication: Difference between revisions
Content added Content deleted
(Add S-Lang) |
m (→{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.) |
||
Line 3,925: | Line 3,925: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program multiplies two matrices together, displays the matrices and the results. */ |
<lang rexx>/*REXX program multiplies two matrices together, displays the matrices and the results. */ |
||
x.=; x.1=1 2 |
x.=; x.1= 1 2 /*╔═══════════════════════════════════╗*/ |
||
x.2=3 4 |
x.2= 3 4 /*║ As none of the matrix values have ║*/ |
||
x.3=5 6 |
x.3= 5 6 /*║ a sign, quotes aren't needed. ║*/ |
||
x.4=7 8 |
x.4= 7 8 /*╚═══════════════════════════════════╝*/ |
||
do r=1 while x.r\=='' /*build the "A" matrix from X. numbers.*/ |
do r=1 while x.r\=='' /*build the "A" matrix from X. numbers.*/ |
||
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end /*c*/ |
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end /*c*/ |
||
end /*r*/ |
end /*r*/ |
||
Arows=r- |
Arows= r - 1 /*adjust the number of rows (DO loop).*/ |
||
Acols=c- |
Acols= c - 1 /* " " " " cols " " .*/ |
||
y.=; y.1=1 2 3 |
y.=; y.1= 1 2 3 |
||
y.2=4 5 6 |
y.2= 4 5 6 |
||
do r=1 while y.r\=='' /*build the "B" matrix from Y. numbers.*/ |
do r=1 while y.r\=='' /*build the "B" matrix from Y. numbers.*/ |
||
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end /*c*/ |
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end /*c*/ |
||
end /*r*/ |
end /*r*/ |
||
Brows=r- |
Brows= r - 1 /*adjust the number of rows (DO loop).*/ |
||
Bcols=c- |
Bcols= c - 1 /* " " " " cols " " */ |
||
w= 0 /*W is max width of an matrix element.*/ |
|||
c.= 0; do i=1 for Arows /*multiply matrix A and B ───► C */ |
|||
do j=1 for Bcols |
|||
do k=1 for Acols; c.i.j= c.i.j + a.i.k*b.k.j; w= max(w, length(c.i.j) ) |
|||
end /*k*/ /* ↑ */ |
|||
end /*j*/ /* | */ |
|||
end /*i*/ /*max width of ┴ the matrix elements.*/ |
|||
end /*i*/ |
|||
call showMatrix 'A', Arows, Acols /*display matrix A ───► the terminal.*/ |
call showMatrix 'A', Arows, Acols /*display matrix A ───► the terminal.*/ |
||
Line 3,955: | Line 3,954: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
showMatrix: parse arg mat,rows,cols; say; say center(mat 'matrix', cols*(w+1) + |
showMatrix: parse arg mat,rows,cols; say; say center(mat 'matrix', cols*(w+1) +9, "─") |
||
do r=1 for rows; _= |
do r=1 for rows; _= ' ' |
||
do c=1 for cols; _=_ right(value(mat'.'r"."c), w); end; say _ |
do c=1 for cols; _=_ right(value(mat'.'r"."c), w); end; say _ |
||
end /*r*/ |
end /*r*/ |
||
return</lang> |
return</lang> |
||
{{out|output|text= when using the internal default input:}} |
|||
'''output''' |
|||
<pre> |
<pre> |
||
───A matrix──── |
|||
─A matrix─ |
|||
1 2 |
1 2 |
||
3 4 |
3 4 |
||
5 6 |
5 6 |
||
7 8 |
7 8 |
||
─────B matrix───── |
|||
──B matrix─── |
|||
1 2 3 |
1 2 3 |
||
4 5 6 |
4 5 6 |
||
─────C matrix───── |
|||
──C matrix─── |
|||
9 12 15 |
9 12 15 |
||
19 26 33 |
|||
29 40 51 |
|||
39 54 69 |
|||
</pre> |
</pre> |
||