Anonymous user
Matrix multiplication: Difference between revisions
m
→[[ALGOL 68]]
m (→[[ALGOL 68]]: use the bold form of the if ~ then ~ else ~ fi syntax. - easier to read blocks.) |
m (→[[ALGOL 68]]) |
||
Line 37:
MODE FIELD = LONG REAL; # field type is LONG REAL #
INT default upb:=3;
MODE
MODE
# crude exception handling #
Line 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 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 59:
<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 67:
</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 real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
PROC real matrix printf= (FORMAT real fmt,
FORMAT
FORMAT matrix fmt = $x"("n(UPB m-1)(f(
# finally print the result #
printf((matrix fmt,m))
Line 102:
Alternatively, for multicore CPUs, the following recursive algorithm can be used:
<u>int</u> default upb :=
<u>mode</u> <u>field</u> = <u>long</u> <u>real</u>;
<u>mode</u> <u>
<u>mode</u> <u>
# crude exception handling #
Line 113:
# define an operator to slice array into quarters #
<u>op</u> <u>top</u> = (<u>
<u>bot</u> = (<u>
<u>left</u> = (<u>
<u>right</u> = (<u>
<u>left</u> = (<u>
<u>right</u> = (<u>
<u>prio</u> <u>top</u> = 8, <u>bot</u> = 8, <u>left</u> = 8, <u>right</u> = 8; # Operator priority - same as LWB & UPB #
<u>op</u> × = (<u>
<u>if</u>
<u>if</u> ⌊a = ⌈a <u>then</u>
a[⌈a] × b[⌈b]
<u>else</u>
<u>field</u> begin, end;
[]<u>proc</u> <u>void</u>
<u>void</u>: begin:=a[:<u>left</u> a] × b[:<u>left</u> b],
<u>void</u>: end :=a[<u>right</u> a:] × b[<u>right</u> b:]
);
<u>if</u> <u>level</u> idle cpus = 0 <u>then</u> # use current CPU #
<u>for</u> thread <u>to</u>
<u>else</u>
( ↓idle cpus;
)
<u>fi</u>;
Line 143:
);
<u>op</u> × = (<u>
<u>if</u> (⌊a, 2⌊b) = (⌈a,
a[⌊a, ] × b[,
<u>else</u>
[⌈a,
<u>if</u>
[]<u>struct</u>(<u>bool</u> required, <u>proc</u> <u>void</u> thread) schedule = (
( <u>true</u>, # calculate top left corner #
Line 154:
( ⌊a ≠ ⌈a, # calculate bottom left corner #
<u>void</u>: out[<u>bot</u> a:, :<u>left</u> b] := a[<u>bot</u> a:, ] × b[, :<u>left</u> b]),
(
<u>void</u>: out[:<u>top</u> a, <u>right</u> b:] := a[:<u>top</u> a, ] × b[, <u>right</u> b:]),
(
<u>void</u>: out[<u>bot</u> a:, <u>right</u> b:] := a[<u>bot</u> a:, ] × b[, <u>right</u> b:])
);
<u>if</u> <u>level</u> idle cpus = 0 <u>then</u> # use current CPU #
<u>for</u> thread <u>to</u> ⌈schedule <u>do</u> (
<u>else</u>
(
# try to do opposite corners of matrix in parallel if CPUs are limited #
(
(
)
<u>fi</u>;
Line 174:
<u>format</u> real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
<u>proc</u> real matrix printf= (<u>format</u> real fmt, <u>
<u>format</u>
<u>format</u> matrix fmt = $x"("n(⌈m-1)(f(
# finally print the result #
printf((matrix fmt,m))
Line 182:
# Some sample matrices to test #
<u>
(2, 4, 8, 16),
(3, 9, 27, 81),
(4, 16, 64, 256));
<u>
(-13/3, 19/4, -7/3, 11/24),
( 3/2, -2 , 7/6, -1/4 ),
( -1/6, 1/4, -1/6, 1/24));
<u>
print((" A x B =",new line));
Line 199:
exception index error:
putf(stand error, $x"Exception: index error."l$)
Note: In the ALGOL 68 Revised Report the keywords, types and operators were permitted in a different typeface. Hence the
▲Note: In the ALGOL 68 Revised Report the keywords, types and operators were permitted in a different typeface. Hence the underling of these identifiers above. Effectively this puts these identifiers into a different name space.
=={{header|BASIC}}==
|