Anonymous user
Matrix multiplication: Difference between revisions
m
→[[Matrix_multiplication#ALGOL 68|ALGOL 68]]: highlight task part.
m (Matrix Multiplication moved to Matrix multiplication: for consistency) |
m (→[[Matrix_multiplication#ALGOL 68|ALGOL 68]]: highlight task part.) |
||
Line 36:
An example of user defined Vector and Matrix Multiplication Operators:
MODE FIELD = LONG REAL; # field type is LONG REAL #
INT default upb:=3;
MODE VEC = [default upb]FIELD;
MODE MAT = [default upb,default upb]FIELD;
# crude exception handling #
Line 41 ⟶ 44:
# define the vector/matrix operators #
OP * = (
FIELD result:=0;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
Line 48 ⟶ 51:
);
OP * = (
[2 LWB b:2 UPB b]FIELD result;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
Line 54 ⟶ 57:
result
);
<pre style="background-color:#ffe">
# this is the task portion #
OP * = (
[LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
IF 2 LWB a/=LWB b OR 2 UPB a/=UPB b THEN raise index error FI;
Line 61 ⟶ 65:
result
);
</pre>
# Some sample matrices to test #
(2, 4, 8, 16),
(3, 9, 27, 81),
(4, 16, 64, 256));
(-13/3, 19/4, -7/3, 11/24),
( 3/2, -2 , 7/6, -1/4 ),
( -1/6, 1/4, -1/6, 1/24));
FORMAT field fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #▼
FORMAT vec fmt = $"("n(2 UPB prod-1)(f(field fmt)",")f(field fmt)")"$;▼
FORMAT matrix fmt = $x"("n(UPB prod-1)(f(vec fmt)","lxx)f(vec fmt)");"$;▼
FORMAT result fmt = $x"Product of a and b: "lf(matrix fmt)l$;▼
PROC real matrix printf= (FORMAT real fmt, MAT m)VOID:(
# finally print the result #
);
# finally print the result #
▲ printf((result fmt,prod))
real matrix printf(real fmt, prod)
EXIT
|