Matrix multiplication: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: added comment, changed sep character, changed indentation of DO loops. -- ~~~~
Line 2,311: Line 2,311:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program multiplies two matrixes together, shows matrixes & result*/
<lang rexx>
x.=
/*REXX program multiplies two matrixes together, shows matrixes & result*/

x.=''
x.1='1 2'
x.1='1 2'
x.2='3 4'
x.2='3 4'
x.3='5 6'
x.3='5 6'
x.4='7 8'
x.4='7 8'
do r=1 while x.r\=='' /*build the "A" matric from X. numbers */
do r=1 while x.r\=='' /*build the "A" matric from X. #s*/
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end
do c=1 while x.r\==''; parse var x.r a.r.c x.r; end
end
end
Arows=r-1; Acols=c-1
Arows=r-1; Acols=c-1
y.=

y.=''
y.1='1 2 3'
y.1='1 2 3'
y.2='4 5 6'
y.2='4 5 6'
do r=1 while y.r\=='' /*build the "B" matric from Y. numbers */
do r=1 while y.r\=='' /*build the "B" matric from Y. #s*/
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end
do c=1 while y.r\==''; parse var y.r b.r.c y.r; end
end
end
Brows=r-1; Bcols=c-1
Brows=r-1; Bcols=c-1


c.=0; L=0 /*L is the maximum width element value.*/
c.=0; L=0 /*L is the ma width element value*/
do i=1 for Arows /*multiply matrix A & B ---> matrix C.*/
do i =1 for Arows /*multiply matrix A & B ──► C */
do j=1 for Bcols
do j =1 for Bcols
do k=1 for Acols
do k=1 for Acols
c.i.j=c.i.j+a.i.k*b.k.j; L=max(L,length(c.i.j))
c.i.j = c.i.j + a.i.k * b.k.j; L=max(L,length(c.i.j))
end /*k*/
end /*k*/
end /*j*/
end /*j*/
Line 2,344: Line 2,341:
call showMat 'B',Brows,Bcols
call showMat 'B',Brows,Bcols
call showMat 'C',Arows,Bcols
call showMat 'C',Arows,Bcols
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────SHOWMAT subroutine──────────────────*/

showMat: parse arg mat,rows,cols; say

say center(mat 'matrix',cols*(L+1)+4,"")
/*─────────────────────────────────────SHOWMAT subroutine───────────────*/
do r =1 for rows; _=
showMat: parse arg mat,rows,cols; say
do c=1 for cols; _=_ right(value(mat'.'r'.'c),L); end; say _
say center(mat 'matrix',cols*(L+1)+4,"-")
do r=1 for rows
end
return</lang>
_=''; do c=1 for cols; _=_ right(value(mat'.'r'.'c),L); end; say _
'''output'''
end
<pre style="overflow:scroll">
return
─A matrix─
</lang>
Output:
<pre style="height:35ex;overflow:scroll">
-A matrix-
1 2
1 2
3 4
3 4
Line 2,363: Line 2,357:
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
19 26 33