Matrix multiplication: Difference between revisions
Content added Content deleted
m (Matrix Multiplication moved to Matrix multiplication: for consistency) |
m (→[[Matrix_multiplication#ALGOL 68|ALGOL 68]]: highlight task part.) |
||
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 * = ( |
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 * = ( |
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 * = ( |
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 # |
||
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)); |
||
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)); |
||
MAT prod = a * b; # actual multiplication example of A x B # |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
PROC real matrix printf= (FORMAT real fmt, MAT m)VOID:( |
|||
⚫ | |||
⚫ | |||
# finally print the result # |
|||
⚫ | |||
); |
|||
⚫ | |||
# finally print the result # |
# finally print the result # |
||
⚫ | |||
⚫ | |||
real matrix printf(real fmt, prod) |
|||
EXIT |
EXIT |
||