Matrix multiplication: Difference between revisions

Content added Content deleted
m (Matrix Multiplication moved to Matrix multiplication: for consistency)
Line 36: Line 36:
An example of user defined Vector and Matrix Multiplication Operators:
An example of user defined Vector and Matrix Multiplication Operators:
MODE FIELD = LONG REAL; # field type is LONG REAL #
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 #
# crude exception handling #
Line 41: Line 44:
# define the vector/matrix operators #
# define the vector/matrix operators #
OP * = (FLEX []FIELD a,b)FIELD: ( # basically the dot product #
OP * = (VEC a,b)FIELD: ( # basically the dot product #
FIELD result:=0;
FIELD result:=0;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
Line 48: Line 51:
);
);
OP * = ([]FIELD a, [,]FIELD b)[]FIELD: ( # overload vec times matrix #
OP * = (VEC a, MAT b)VEC: ( # overload vec times matrix #
[2 LWB b:2 UPB b]FIELD result;
[2 LWB b:2 UPB b]FIELD result;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
Line 54: Line 57:
result
result
);
);
<pre style="background-color:#ffe">
# this is the task portion #
OP * = ([,]FIELD a, b)[,]FIELD: ( # overload matrix times matrix #
OP * = (MAT a, b)MAT: ( # overload matrix times matrix #
[LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
[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;
IF 2 LWB a/=LWB b OR 2 UPB a/=UPB b THEN raise index error FI;
Line 61: Line 65:
result
result
);
);
</pre>
# Some sample matrices to test #
# Some sample matrices to test #
[,]FIELD a=((1, 1, 1, 1), # matrix A #
MAT a=((1, 1, 1, 1), # matrix A #
(2, 4, 8, 16),
(2, 4, 8, 16),
(3, 9, 27, 81),
(3, 9, 27, 81),
(4, 16, 64, 256));
(4, 16, 64, 256));
[,]FIELD b=(( 4 , -3 , 4/3, -1/4 ), # matrix B #
MAT b=(( 4 , -3 , 4/3, -1/4 ), # matrix B #
(-13/3, 19/4, -7/3, 11/24),
(-13/3, 19/4, -7/3, 11/24),
( 3/2, -2 , 7/6, -1/4 ),
( 3/2, -2 , 7/6, -1/4 ),
( -1/6, 1/4, -1/6, 1/24));
( -1/6, 1/4, -1/6, 1/24));
[,]FIELD prod = a * b; # actual multiplication example of A x B #
MAT prod = a * b; # actual multiplication example of A x B #
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$;
FORMAT real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
PROC real matrix printf= (FORMAT real fmt, MAT m)VOID:(
FORMAT vec fmt = $"("n(2 UPB m-1)(f(real fmt)",")f(real fmt)")"$;
FORMAT matrix fmt = $x"("n(UPB m-1)(f(vec fmt)","lxx)f(vec fmt)");"$;
# finally print the result #
printf((matrix fmt,m))
);
# finally print the result #
# finally print the result #
print(("Product of a and b: ",new line));
printf((result fmt,prod))
real matrix printf(real fmt, prod)
EXIT
EXIT