Matrix multiplication: Difference between revisions

(add RPL)
(→‎{{header|REXX}}: refurbished)
Line 5,500:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program multipliescalculates twothe matricesKronecker together,product displays the matricesof and the resultstwo arbitrary size matrices. */
x.=0
x.=; x.1= 1 2 /*╔═══════════════════════════════════╗*/
amat=4X2 1 2 3 4 5 6 7 x.2= 3 4 8 /* Asdefine noneA ofmatrix the matrixsize valuesand haveelements */
bmat=2X3 1 2 3 4 5 6 /* " B " x.3= 5 6 " " /*║ a sign, quotes aren't" needed. */
Call makeMat 'A',amat /* construct A matrix from elements */
x.4= 7 8 /*╚═══════════════════════════════════╝*/
Call makeMat 'B',bmat /* do" r=1 while x.r\=='' B " /*build the "A " matrix from X. numbers. */
If cols.A<>rows.B Then
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end /*c*/
Call exit 'Matrices are incompatible for matrix multiplication',
end /*r*/
'cols.A='cols.A'<>rows.B='rows.B
Arows= r - 1 /*adjust the number of rows (DO loop).*/
Acols= c - 1 Call multMat /* multiply A " x B " " " cols " " .*/
Call showMat 'A',amat /* display matrix A y.=; y.1= 1 2 3 */
Call showMat 'B',bmat /* " " B y.2= 4 5 6 */
Call showMat 'C',mm do /* " r=1 while y.r\=='' " C /*build the "B" matrix from Y. numbers.*/
Exit
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end /*c*/
/*--------------------------------------------------------------------*/
end /*r*/
makeMat:
Brows= r - 1 /*adjust the number of rows (DO loop).*/
Parse Arg what,size elements /*elements: e.1.1 e.1.2 - e.rows cols*/
Bcols= c - 1 /* " " " " cols " " */
Parse Var size rows 'X' cols
w= 0 /*W is max width of an matrix element.*/
x.what.shape=rows cols
c.= 0; do i=1 for Arows /*multiply matrix A and B ───► C */
Parse Value rows cols With rows.what cols.what
do j=1 for Bcols
n=0
do k=1 for Acols; c.i.j= c.i.j + a.i.k*b.k.j; w= max(w, length(c.i.j) )
Do r=1 To rows
end /*k*/ /* ↑ */
Do c=1 To cols
end /*j*/ /* └────────◄───────────┐ */
n=n+1
end /*i*/ /*max width of the matrix elements─►─┘ */
element=word(elements,n)
 
x.what.r.c=element
call showMat 'A', Arows, Acols /*display matrix A ───► the terminal.*/
End
call showMat 'B', Brows, Bcols /* " " B ───► " " */
End
call showMat 'C', Arows, Bcols /* " " C ───► " " */
Return
exit /*stick a fork in it, we're all done. */
/*--------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
multMat:
showMat: parse arg mat,rows,cols; say; say center(mat 'matrix', cols*(w+1) + 9, "─")
/* x.C.*.* = x.A.*.* x x.B.*.* do r=1 for rows; _= ' '*/
Do i=1 To rows.A
do c=1 for cols; _= _ right( value(mat'.'r"."c), w); end; say _
Do j=1 To cols.B
end /*r*/; return</syntaxhighlight>
Do k=1 To cols.A
x.C.i.j=x.C.i.j+x.A.i.k*x.B.k.j
End /*k*/
End /*j*/
End /*i*/
mm=rows.A||'X'||cols.B
Do i=1 To rows.A
Do j=1 To cols.B
mm=mm x.C.i.j
End /*j*/
End /*i*/
Call makeMat 'C',mm
Return
/*--------------------------------------------------------------------*/
showMat:
Parse Arg what,size .
Parse Var size rows 'X' cols
z='+'
b6=left('',6)
Say ''
Say b6 copies('-',7) 'matrix' what copies('-',7)
w=0
Do r=1 To rows
Do c=1 To cols
w=max(w,length(x.what.r.c))
End
End
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+' /* top border */
Do r=1 To rows
line='|' right(x.what.r.1,w) /* element of first colsumn */ /* start with long vertical bar */
Do c=2 To cols /* loop for other columns */
line=line right(x.what.r.c,w) /* append the elements */
End /* c */
Say b6 b6 line '|' /* append a long vertical bar. */
End /* r */
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+' /* bottom border */
Return
exit:
Say arg(1)
Exit</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
------- matrix A -------
───A matrix────
1 2 +-----+
3 4 | 1 2 |
5 6 | 3 4 |
7 8 | 5 6 |
| 7 8 |
+-----+
 
------- matrix B -------
─────B matrix─────
1 2 3 +-------+
4 5 6 | 1 2 3 |
| 4 5 6 |
+-------+
 
------- matrix C -------
─────C matrix─────
9 12 15 +----------+
19 26 33 | 9 12 15 |
29 40 51 | 19 26 33 |
39 54 69 | 29 40 51 |
| 39 54 69 |
+----------+
</pre>
 
2,295

edits