langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
12
Task/Matrix-multiplication/Nial/matrix-multiplication.nial
Normal file
12
Task/Matrix-multiplication/Nial/matrix-multiplication.nial
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|A := 4 4 reshape 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256
|
||||
=1 1 1 1
|
||||
=2 4 8 16
|
||||
=3 9 27 81
|
||||
=4 16 64 256
|
||||
|B := inverse A
|
||||
|
||||
|A innerproduct B
|
||||
=1. 0. 8.3e-17 -2.9e-16
|
||||
=1.3e-15 1. -4.4e-16 -3.3e-16
|
||||
=0. 0. 1. 4.4e-16
|
||||
=0. 0. 0. 1.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
let matrix_multiply x y =
|
||||
let x0 = Array.length x
|
||||
and y0 = Array.length y in
|
||||
let y1 = if y0 = 0 then 0 else Array.length y.(0) in
|
||||
let z = Array.make_matrix x0 y1 0 in
|
||||
for i = 0 to x0-1 do
|
||||
for j = 0 to y1-1 do
|
||||
for k = 0 to y0-1 do
|
||||
z.(i).(j) <- z.(i).(j) + x.(i).(k) * y.(k).(j)
|
||||
done
|
||||
done
|
||||
done;
|
||||
z
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(* equivalent to (apply map ...) *)
|
||||
let rec mapn f lists =
|
||||
assert (lists <> []);
|
||||
if List.mem [] lists then
|
||||
[]
|
||||
else
|
||||
f (List.map List.hd lists) :: mapn f (List.map List.tl lists)
|
||||
|
||||
let matrix_multiply m1 m2 =
|
||||
List.map
|
||||
(fun row ->
|
||||
mapn
|
||||
(fun column ->
|
||||
List.fold_left (+) 0
|
||||
(List.map2 ( * ) row column))
|
||||
m2)
|
||||
m1
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
a = zeros(4);
|
||||
% prepare the matrix
|
||||
% 1 1 1 1
|
||||
% 2 4 8 16
|
||||
% 3 9 27 81
|
||||
% 4 16 64 256
|
||||
for i = 1:4
|
||||
for j = 1:4
|
||||
a(i, j) = i^j;
|
||||
endfor
|
||||
endfor
|
||||
b = inverse(a);
|
||||
a * b
|
||||
113
Task/Matrix-multiplication/OxygenBasic/matrix-multiplication.oxy
Normal file
113
Task/Matrix-multiplication/OxygenBasic/matrix-multiplication.oxy
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
'Example of matrix layout mapped to an array of 4x4 cells
|
||||
'
|
||||
' 0 4 8 C
|
||||
' 1 5 9 D
|
||||
' 2 6 A E
|
||||
' 3 7 B F
|
||||
'
|
||||
|
||||
% MatrixType double
|
||||
|
||||
sub MatrixMul(MatrixType *A,*B,*C, sys n)
|
||||
'========================================
|
||||
'
|
||||
'
|
||||
#if leftmatch matrixtype single
|
||||
% OneStep 4
|
||||
% mtype single
|
||||
#endif
|
||||
'
|
||||
#if leftmatch matrixtype double
|
||||
% OneStep 8
|
||||
% mtype double
|
||||
#endif
|
||||
|
||||
sys pa=@A, pb=@B, pc=@C
|
||||
sys ColStep=OneStep*n
|
||||
|
||||
mov ecx,pa
|
||||
mov edx,pb
|
||||
mov eax,pc
|
||||
|
||||
mov esi,n
|
||||
(
|
||||
call column : dec esi : jg repeat
|
||||
)
|
||||
exit sub
|
||||
|
||||
column:
|
||||
'======
|
||||
|
||||
mov edi,n
|
||||
(
|
||||
call cell : dec edi : jg repeat
|
||||
)
|
||||
add edx,ColStep
|
||||
sub ecx,ColStep
|
||||
ret
|
||||
|
||||
cell: ' row A * column B
|
||||
'=======================
|
||||
|
||||
'matrix data is stored ascending vertically then horizontally
|
||||
'thus rows are minor, columns are major
|
||||
'
|
||||
push ecx
|
||||
push edx
|
||||
push eax
|
||||
mov eax,4
|
||||
fldz
|
||||
(
|
||||
fld mtype [ecx]
|
||||
fmul mtype [edx]
|
||||
faddp st1
|
||||
add ecx,ColStep 'next column of matrix A
|
||||
add edx,OneStep 'next row of matrix B
|
||||
dec eax
|
||||
jnz repeat
|
||||
)
|
||||
pop eax
|
||||
fstp mtype [eax] 'assign to next row of matrix C
|
||||
'
|
||||
pop edx
|
||||
pop ecx
|
||||
add eax,OneStep 'next cell in column of matrix C (columns then rows)
|
||||
add ecx,OneStep 'next row of matrix A
|
||||
ret
|
||||
'
|
||||
end sub
|
||||
|
||||
|
||||
function ShowMatrix(MatrixType*A,sys n) as string
|
||||
'================================================
|
||||
string cr=chr(13)+chr(10), tab=chr(9)
|
||||
function="MATRIX " n "x" n cr cr
|
||||
sys i,j,m
|
||||
'
|
||||
for i=1 to n
|
||||
m=0
|
||||
for j=1 to n
|
||||
function+=str( A[m+i] ) tab
|
||||
m+=n
|
||||
next
|
||||
function+=cr
|
||||
next
|
||||
end function
|
||||
|
||||
'TEST
|
||||
'====
|
||||
|
||||
% n 4
|
||||
MatrixType A[n*n],B[n*n],C[n*n]
|
||||
|
||||
|
||||
'reading vertically (minor) then left to right (major)
|
||||
|
||||
A <= 4,0,0,1, 0,4,0,0, 0,0,4,0, 0,0,0,4
|
||||
|
||||
B <= 2,0,0,2, 0,2,0,0, 0,0,2,0, 0,0,0,2
|
||||
|
||||
|
||||
MatrixMul A,B,C,n
|
||||
|
||||
Print ShowMatrix C,n
|
||||
|
|
@ -0,0 +1 @@
|
|||
M*N
|
||||
23
Task/Matrix-multiplication/PL-I/matrix-multiplication.pli
Normal file
23
Task/Matrix-multiplication/PL-I/matrix-multiplication.pli
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* Matrix multiplication of A by B, yielding C */
|
||||
MMULT: procedure (a, b, c);
|
||||
declare (a, b, c)(*,*) float controlled;
|
||||
declare (i, j, m, n, p) fixed binary;
|
||||
|
||||
if hbound(a,2) ^= hbound(b,1) then
|
||||
do;
|
||||
put skip list
|
||||
('Matrices are incompatible for matrix multiplication');
|
||||
signal error;
|
||||
end;
|
||||
|
||||
m = hbound(a, 1); p = hbound(b, 2);
|
||||
if allocation(c) > 0 then free c;
|
||||
|
||||
allocate c(m,p);
|
||||
|
||||
do i = 1 to m;
|
||||
do j = 1 to p;
|
||||
c(i,j) = sum(a(i,*) * b(*,j) );
|
||||
end;
|
||||
end;
|
||||
end MMULT;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
sub mmult(@a,@b) {
|
||||
my @p;
|
||||
for ^@a X ^@b[0] -> $r, $c {
|
||||
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b;
|
||||
}
|
||||
@p;
|
||||
}
|
||||
|
||||
my @a = [1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256];
|
||||
|
||||
my @b = [ 4 , -3 , 4/3, -1/4 ],
|
||||
[-13/3, 19/4, -7/3, 11/24],
|
||||
[ 3/2, -2 , 7/6, -1/4 ],
|
||||
[ -1/6, 1/4, -1/6, 1/24];
|
||||
|
||||
.say for mmult(@a,@b);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
sub mmult(@a,@b) {
|
||||
for ^@a -> $r {[
|
||||
for ^@b[0] -> $c {
|
||||
[+] (@a[$r][^@b] Z* @b[^@b]»[$c])
|
||||
}
|
||||
]}
|
||||
}
|
||||
23
Task/Matrix-multiplication/Pop11/matrix-multiplication.pop11
Normal file
23
Task/Matrix-multiplication/Pop11/matrix-multiplication.pop11
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
define matmul(a, b) -> c;
|
||||
lvars ba = boundslist(a), bb = boundslist(b);
|
||||
lvars i, i0 = ba(1), i1 = ba(2);
|
||||
lvars j, j0 = bb(1), j1 = bb(2);
|
||||
lvars k, k0 = bb(3), k1 = bb(4);
|
||||
if length(ba) /= 4 then
|
||||
throw([need_2d_array ^a])
|
||||
endif;
|
||||
if length(bb) /= 4 then
|
||||
throw([need_2d_array ^b])
|
||||
endif;
|
||||
if ba(3) /= j0 or ba(4) /= j1 then
|
||||
throw([dimensions_do_not_match ^a ^b]);
|
||||
endif;
|
||||
newarray([^i0 ^i1 ^k0 ^k1], 0) -> c;
|
||||
for i from i0 to i1 do
|
||||
for k from k0 to k1 do
|
||||
for j from j0 to j1 do
|
||||
c(i, k) + a(i, j)*b(j, k) -> c(i, k);
|
||||
endfor;
|
||||
endfor;
|
||||
endfor;
|
||||
enddefine;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Procedure multiplyMatrix(Array a(2), Array b(2), Array prd(2))
|
||||
Protected ar = ArraySize(a()) ;#rows for matrix a
|
||||
Protected ac = ArraySize(a(), 2) ;#cols for matrix a
|
||||
Protected br = ArraySize(b()) ;#rows for matrix b
|
||||
Protected bc = ArraySize(b(), 2) ;#cols for matrix b
|
||||
|
||||
If ac = br
|
||||
Dim prd(ar, bc)
|
||||
|
||||
Protected i, j, k
|
||||
For i = 0 To ar
|
||||
For j = 0 To bc
|
||||
For k = 0 To br ;ac
|
||||
prd(i, j) = prd(i, j) + (a(i, k) * b(k, j))
|
||||
Next
|
||||
Next
|
||||
Next
|
||||
|
||||
ProcedureReturn #True ;multiplication performed, product in prd()
|
||||
Else
|
||||
ProcedureReturn #False ;multiplication not performed, dimensions invalid
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
DataSection
|
||||
Data.i 2,3 ;matrix a (#rows, #cols)
|
||||
Data.i 1,2,3, 4,5,6 ;elements by row
|
||||
|
||||
Data.i 3,1 ;matrix b (#rows, #cols)
|
||||
Data.i 1, 5, 9 ;elements by row
|
||||
EndDataSection
|
||||
|
||||
Procedure displayMatrix(Array a(2), text.s)
|
||||
Protected i, j
|
||||
Protected columns = ArraySize(a(), 2), rows = ArraySize(a(), 1)
|
||||
|
||||
PrintN(text + ": (" + Str(rows + 1) + ", " + Str(columns + 1) + ")")
|
||||
For i = 0 To rows
|
||||
For j = 0 To columns
|
||||
Print(LSet(Str(a(i, j)), 4, " "))
|
||||
Next
|
||||
PrintN("")
|
||||
Next
|
||||
PrintN("")
|
||||
EndProcedure
|
||||
|
||||
Procedure loadMatrix(Array a(2))
|
||||
Protected rows, columns, i, j
|
||||
Read.i rows
|
||||
Read.i columns
|
||||
|
||||
Dim a(rows - 1, columns - 1)
|
||||
|
||||
For i = 0 To rows - 1
|
||||
For j = 0 To columns - 1
|
||||
Read.i a(i, j)
|
||||
Next
|
||||
Next
|
||||
EndProcedure
|
||||
|
||||
Dim a(0,0)
|
||||
Dim b(0,0)
|
||||
Dim c(0,0)
|
||||
|
||||
If OpenConsole()
|
||||
loadMatrix(a()): displayMatrix(a(), "matrix a")
|
||||
loadMatrix(b()): displayMatrix(b(), "matrix b")
|
||||
|
||||
If multiplyMatrix(a(), b(), c())
|
||||
displayMatrix(c(), "product of a * b")
|
||||
Else
|
||||
PrintN("product of a * b is undefined")
|
||||
EndIf
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
|
||||
Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
public rel[real, real, real] matrixMultiplication(rel[real x, real y, real v] matrix1, rel[real x, real y, real v] matrix2){
|
||||
if (max(matrix1.x) == max(matrix2.y)){
|
||||
p = {<x1,y1,x2,y2, v1*v2> | <x1,y1,v1> <- matrix1, <x2,y2,v2> <- matrix2};
|
||||
|
||||
result = {};
|
||||
for (y <- matrix1.y){
|
||||
for (x <- matrix2.x){
|
||||
v = (0.0 | it + v | <x1, y1, x2, y2, v> <- p, x==x2 && y==y1, x1==y2 && y2==x1);
|
||||
result += <x,y,v>;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else throw "Matrix sizes do not match.";
|
||||
|
||||
//a matrix, given by a relation of the x-coordinate, y-coordinate and value.
|
||||
public rel[real x, real y, real v] matrixA = {
|
||||
<0.0,0.0,12.0>, <0.0,1.0, 6.0>, <0.0,2.0,-4.0>,
|
||||
<1.0,0.0,-51.0>, <1.0,1.0,167.0>, <1.0,2.0,24.0>,
|
||||
<2.0,0.0,4.0>, <2.0,1.0,-68.0>, <2.0,2.0,-41.0>
|
||||
};
|
||||
20
Task/Matrix-multiplication/SQL/matrix-multiplication.sql
Normal file
20
Task/Matrix-multiplication/SQL/matrix-multiplication.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
CREATE TABLE a (x integer, y integer, e real);
|
||||
CREATE TABLE b (x integer, y integer, e real);
|
||||
|
||||
-- test data
|
||||
-- A is a 2x2 matrix
|
||||
INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2);
|
||||
INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4);
|
||||
|
||||
-- B is a 2x3 matrix
|
||||
INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3);
|
||||
INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4);
|
||||
|
||||
-- C is 2x2 * 2x3 so will be a 2x3 matrix
|
||||
SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b
|
||||
WHERE a.y = lhs.y
|
||||
AND b.x = rhs.x
|
||||
AND a.x = b.y)
|
||||
INTO TABLE c
|
||||
FROM a AS lhs, b AS rhs
|
||||
WHERE lhs.x = 0 AND rhs.y = 0;
|
||||
26
Task/Matrix-multiplication/Seed7/matrix-multiplication.seed7
Normal file
26
Task/Matrix-multiplication/Seed7/matrix-multiplication.seed7
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
const type: matrix is array array float;
|
||||
|
||||
const func matrix: (in matrix: left) * (in matrix: right) is func
|
||||
result
|
||||
var matrix: result is matrix.value;
|
||||
local
|
||||
var integer: i is 0;
|
||||
var integer: j is 0;
|
||||
var integer: k is 0;
|
||||
var float: accumulator is 0.0;
|
||||
begin
|
||||
if length(left[1]) <> length(right) then
|
||||
raise RANGE_ERROR;
|
||||
else
|
||||
result := length(left) times length(right[1]) times 0.0;
|
||||
for i range 1 to length(left) do
|
||||
for j range 1 to length(right) do
|
||||
accumulator := 0.0;
|
||||
for k range 1 to length(left) do
|
||||
accumulator +:= left[i][k] * right[k][j];
|
||||
end for;
|
||||
result[i][j] := accumulator;
|
||||
end for;
|
||||
end for;
|
||||
end if;
|
||||
end func;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Disp [A]*[B]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[1,2; 3,4; 5,6; 7,8] → m1
|
||||
[1,2,3; 4,5,6] → m2
|
||||
m1 * m2
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1,2; 3,4; 5,6; 7,8] * [1,2,3; 4,5,6]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[[9,12,15][19,26,33][29,40,51][39,54,69]]
|
||||
183
Task/Matrix-multiplication/UNIX-Shell/matrix-multiplication.sh
Normal file
183
Task/Matrix-multiplication/UNIX-Shell/matrix-multiplication.sh
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
#!/bin/bash
|
||||
|
||||
DELAY=0 # increase this if printing of matrices should be slower
|
||||
|
||||
echo "This script takes two matrices, henceforth called A and B,
|
||||
and returns their product, AB.
|
||||
|
||||
For the time being, matrices can have integer components only.
|
||||
|
||||
"
|
||||
|
||||
read -p "Number of rows of matrix A: " arows
|
||||
read -p "Number of columns of matrix A: " acols
|
||||
brows="$acols"
|
||||
echo
|
||||
echo "Number of rows of matrix B: "$brows
|
||||
read -p "Number of columns of matrix B: " bcols
|
||||
|
||||
crows="$arows"
|
||||
ccols="$bcols"
|
||||
echo
|
||||
|
||||
echo "Number of rows of matrix AB: " $crows
|
||||
echo "Number of columns of matrix AB: " $ccols
|
||||
echo
|
||||
echo
|
||||
|
||||
matrixa=( )
|
||||
matrixb=( )
|
||||
|
||||
# input matrix A
|
||||
|
||||
maxlengtha=0
|
||||
for ((row=1; row<=arows; row++)); do
|
||||
for ((col=1; col<=acols; col++)); do
|
||||
checkentry="false"
|
||||
while [ "$checkentry" != "true" ]; do
|
||||
read -p "Enter component A[$row, $col]: " number
|
||||
index=$(((row-1)*acols+col))
|
||||
matrixa[$index]="$number"
|
||||
[ "${matrixa[$index]}" -eq "$number" ] && checkentry="true"
|
||||
echo
|
||||
done
|
||||
entry="${matrixa[$index]}"
|
||||
[ "${#entry}" -gt "$maxlengtha" ] && maxlengtha="${#entry}"
|
||||
done
|
||||
echo
|
||||
done
|
||||
|
||||
# print matrix A to guard against errors
|
||||
|
||||
if [ "$maxlengtha" -le "5" ]; then
|
||||
width=8
|
||||
else
|
||||
width=$((maxlengtha + 3))
|
||||
fi
|
||||
|
||||
echo "This is matrix A:
|
||||
|
||||
"
|
||||
|
||||
for ((row=1; row<=arows; row++)); do
|
||||
for ((col=1; col<=acols; col++)); do
|
||||
|
||||
index=$(((row-1)*acols+col))
|
||||
printf "%${width}d" "${matrixa[$index]}"
|
||||
sleep "$DELAY"
|
||||
|
||||
done
|
||||
echo; echo # printf %s "\n\n" does not work...
|
||||
done
|
||||
|
||||
echo
|
||||
echo
|
||||
|
||||
# input matrix B
|
||||
|
||||
maxlengthb=0
|
||||
for ((row=1; row<=brows; row++)); do
|
||||
for ((col=1; col<=bcols; col++)); do
|
||||
checkentry="false"
|
||||
while [ "$checkentry" != "true" ]; do
|
||||
read -p "Enter component B[$row, $col]: " number
|
||||
index=$(((row-1)*bcols+col))
|
||||
matrixb[$index]="$number"
|
||||
[ "${matrixb[$index]}" -eq "$number" ] && checkentry="true"
|
||||
echo
|
||||
done
|
||||
entry="${matrixb[$index]}"
|
||||
[ "${#entry}" -gt "$maxlengthb" ] && maxlengthb="${#entry}"
|
||||
done
|
||||
echo
|
||||
done
|
||||
|
||||
# print matrix B to guard against errors
|
||||
|
||||
if [ "$maxlengthb" -le "5" ]; then
|
||||
width=8
|
||||
else
|
||||
width=$((maxlengthb + 3))
|
||||
fi
|
||||
|
||||
echo "This is matrix B:
|
||||
|
||||
"
|
||||
|
||||
for ((row=1; row<=brows; row++)); do
|
||||
for ((col=1; col<=bcols; col++)); do
|
||||
|
||||
index=$(((row-1)*bcols+col))
|
||||
printf "%${width}d" "${matrixb[$index]}"
|
||||
sleep "$DELAY"
|
||||
|
||||
done
|
||||
echo; echo # printf %s "\n\n" does not work...
|
||||
done
|
||||
|
||||
read -p "Hit enter to continue"
|
||||
|
||||
# calculate matrix C := AB
|
||||
|
||||
maxlengthc=0
|
||||
time for ((row=1; row<=crows; row++)); do
|
||||
for ((col=1; col<=ccols; col++)); do
|
||||
|
||||
# calculate component C[$row, $col]
|
||||
|
||||
runningtotal=0
|
||||
for ((j=1; j<=acols; j++)); do
|
||||
rowa="$row"
|
||||
cola="$j"
|
||||
indexa=$(((rowa-1)*acols+cola))
|
||||
rowb="$j"
|
||||
colb="$col"
|
||||
indexb=$(((rowb-1)*bcols+colb))
|
||||
|
||||
entry_from_A=${matrixa[$indexa]}
|
||||
entry_from_B=${matrixb[$indexb]}
|
||||
|
||||
subtotal=$((entry_from_A * entry_from_B))
|
||||
((runningtotal+=subtotal))
|
||||
done
|
||||
|
||||
number="$runningtotal"
|
||||
|
||||
# store component in the result array
|
||||
index=$(((row-1)*ccols+col))
|
||||
matrixc[$index]="$number"
|
||||
|
||||
entry="${matrixc[$index]}"
|
||||
[ "${#entry}" -gt "$maxlengthc" ] && maxlengthc="${#entry}"
|
||||
done
|
||||
done
|
||||
|
||||
echo
|
||||
read -p "Hit enter to continue"
|
||||
echo
|
||||
|
||||
# print the matrix C
|
||||
|
||||
if [ "$maxlengthc" -le "5" ]; then
|
||||
width=8
|
||||
else
|
||||
width=$((maxlengthc + 3))
|
||||
fi
|
||||
|
||||
echo "The product matrix is:
|
||||
|
||||
"
|
||||
|
||||
for ((row=1; row<=crows; row++)); do
|
||||
for ((col=1; col<=ccols; col++)); do
|
||||
|
||||
index=$(((row-1)*ccols+col))
|
||||
printf "%${width}d" "${matrixc[$index]}"
|
||||
sleep "$DELAY"
|
||||
|
||||
done
|
||||
echo; echo # printf %s "\n\n" does not work...
|
||||
done
|
||||
|
||||
echo
|
||||
echo
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#import rat
|
||||
|
||||
a =
|
||||
|
||||
<
|
||||
<1/1, 1/1, 1/1, 1/1>,
|
||||
<2/1, 4/1, 8/1, 16/1>,
|
||||
<3/1, 9/1, 27/1, 81/1>,
|
||||
<4/1, 16/1, 64/1, 256/1>>
|
||||
|
||||
b =
|
||||
|
||||
<
|
||||
< 4/1, -3/1, 4/3, -1/4>,
|
||||
<-13/3, 19/4, -7/3, 11/24>,
|
||||
< 3/2, -2/1, 7/6, -1/4>,
|
||||
< -1/6, 1/4, -1/6, 1/24>>
|
||||
|
||||
mmult = *rK7lD *rlD sum:-0.+ product*p
|
||||
|
||||
#cast %qLL
|
||||
|
||||
test = mmult(a,b)
|
||||
27
Task/Matrix-multiplication/XPL0/matrix-multiplication.xpl0
Normal file
27
Task/Matrix-multiplication/XPL0/matrix-multiplication.xpl0
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
proc Mat4x1Mul(M, V); \Multiply matrix M times column vector V
|
||||
real M, \4x4 matrix [M] * [V] -> [V]
|
||||
V; \column vector
|
||||
real W(4); \working copy of column vector
|
||||
int R; \row
|
||||
[for R:= 0 to 4-1 do
|
||||
W(R):= M(R,0)*V(0) + M(R,1)*V(1) + M(R,2)*V(2) + M(R,3)*V(3);
|
||||
for R:= 0 to 4-1 do V(R):= W(R);
|
||||
];
|
||||
|
||||
proc Mat4x4Mul(M, N); \Multiply matrix M times matrix N
|
||||
real M, N; \4x4 matrices [M] * [N] -> [N]
|
||||
real W(4,4); \working copy of matrix N
|
||||
int C; \column
|
||||
[for C:= 0 to 4-1 do
|
||||
[W(0,C):= M(0,0)*N(0,C) + M(0,1)*N(1,C) + M(0,2)*N(2,C) + M(0,3)*N(3,C);
|
||||
W(1,C):= M(1,0)*N(0,C) + M(1,1)*N(1,C) + M(1,2)*N(2,C) + M(1,3)*N(3,C);
|
||||
W(2,C):= M(2,0)*N(0,C) + M(2,1)*N(1,C) + M(2,2)*N(2,C) + M(2,3)*N(3,C);
|
||||
W(3,C):= M(3,0)*N(0,C) + M(3,1)*N(1,C) + M(3,2)*N(2,C) + M(3,3)*N(3,C);
|
||||
];
|
||||
for C:= 0 to 4-1 do
|
||||
[N(0,C):= W(0,C);
|
||||
N(1,C):= W(1,C);
|
||||
N(2,C):= W(2,C);
|
||||
N(3,C):= W(3,C);
|
||||
];
|
||||
];
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?xml-stylesheet href="matmul.templ.xsl" type="text/xsl"?>
|
||||
<mult>
|
||||
<A>
|
||||
<r><c>1</c><c>2</c></r>
|
||||
<r><c>3</c><c>4</c></r>
|
||||
<r><c>5</c><c>6</c></r>
|
||||
<r><c>7</c><c>8</c></r>
|
||||
</A>
|
||||
<B>
|
||||
<r><c>1</c><c>2</c><c>3</c></r>
|
||||
<r><c>4</c><c>5</c><c>6</c></r>
|
||||
</B>
|
||||
</mult>
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
<xsl:stylesheet version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
>
|
||||
<xsl:output method="html"/>
|
||||
|
||||
<xsl:template match="/mult">
|
||||
<table>
|
||||
<tr><td>╭</td><td colspan="{count(*[2]/*[1]/*)}"/><td>╮</td></tr>
|
||||
<xsl:call-template name="prodMM">
|
||||
<xsl:with-param name="A" select="*[1]/*"/>
|
||||
<xsl:with-param name="B" select="*[2]/*"/>
|
||||
</xsl:call-template>
|
||||
<tr><td>╰</td><td colspan="{count(*[2]/*[1]/*)}"/><td>╯</td></tr>
|
||||
</table>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template name="prodMM">
|
||||
<xsl:param name="A"/>
|
||||
<xsl:param name="B"/>
|
||||
|
||||
<xsl:if test="$A/*">
|
||||
<tr>
|
||||
<td>│</td>
|
||||
<xsl:call-template name="prodVM">
|
||||
<xsl:with-param name="a" select="$A[1]/*"/>
|
||||
<xsl:with-param name="B" select="$B"/>
|
||||
</xsl:call-template>
|
||||
<td>│</td>
|
||||
</tr>
|
||||
|
||||
<xsl:call-template name="prodMM">
|
||||
<xsl:with-param name="A" select="$A[position()>1]"/>
|
||||
<xsl:with-param name="B" select="$B"/>
|
||||
</xsl:call-template>
|
||||
</xsl:if>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template name="prodVM">
|
||||
<xsl:param name="a"/>
|
||||
<xsl:param name="B"/>
|
||||
<xsl:param name="col" select="1"/>
|
||||
|
||||
<xsl:if test="$B/*[$col]">
|
||||
<td align="right">
|
||||
<xsl:call-template name="prod">
|
||||
<xsl:with-param name="a" select="$a"/>
|
||||
<xsl:with-param name="b" select="$B/*[$col]"/>
|
||||
</xsl:call-template>
|
||||
</td>
|
||||
|
||||
<xsl:call-template name="prodVM">
|
||||
<xsl:with-param name="a" select="$a"/>
|
||||
<xsl:with-param name="B" select="$B"/>
|
||||
<xsl:with-param name="col" select="$col+1"/>
|
||||
</xsl:call-template>
|
||||
</xsl:if>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template name="prod">
|
||||
<xsl:param name="a"/>
|
||||
<xsl:param name="b"/>
|
||||
|
||||
<xsl:if test="not($a)">0</xsl:if>
|
||||
|
||||
<xsl:if test="$a">
|
||||
<xsl:variable name="res">
|
||||
<xsl:call-template name="prod">
|
||||
<xsl:with-param name="a" select="$a[position()>1]"/>
|
||||
<xsl:with-param name="b" select="$b[position()>1]"/>
|
||||
</xsl:call-template>
|
||||
</xsl:variable>
|
||||
|
||||
<xsl:value-of select="$a[1] * $b[1] + $res"/>
|
||||
</xsl:if>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
141
Task/Matrix-multiplication/ZPL/matrix-multiplication.zpl
Normal file
141
Task/Matrix-multiplication/ZPL/matrix-multiplication.zpl
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
program matmultSUMMA;
|
||||
|
||||
prototype GetSingleDim(infile:file):integer;
|
||||
prototype GetInnerDim(infile1:file; infile2:file):integer;
|
||||
|
||||
config var
|
||||
Afilename: string = "";
|
||||
Bfilename: string = "";
|
||||
|
||||
Afile: file = open(Afilename,file_read);
|
||||
Bfile: file = open(Bfilename,file_read);
|
||||
|
||||
default_size:integer = 4;
|
||||
m:integer = GetSingleDim(Afile);
|
||||
n:integer = GetInnerDim(Afile,Bfile);
|
||||
p:integer = GetSingleDim(Bfile);
|
||||
|
||||
iters: integer = 1;
|
||||
|
||||
printinput: boolean = false;
|
||||
verbose: boolean = true;
|
||||
dotiming: boolean = false;
|
||||
|
||||
region
|
||||
RA = [1..m,1..n];
|
||||
RB = [1..n,1..p];
|
||||
RC = [1..m,1..p];
|
||||
FCol = [1..m,*];
|
||||
FRow = [*,1..p];
|
||||
|
||||
var
|
||||
A : [RA] double;
|
||||
B : [RB] double;
|
||||
C : [RC] double;
|
||||
Aflood : [FCol] double;
|
||||
Bflood : [FRow] double;
|
||||
|
||||
|
||||
procedure ReadA();
|
||||
var step:double;
|
||||
[RA] begin
|
||||
if (Afile != znull) then
|
||||
read(Afile,A);
|
||||
else
|
||||
step := 1.0/(m*n);
|
||||
A := ((Index1-1)*n + Index2)*step + 1.0;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure ReadB();
|
||||
var step:double;
|
||||
[RB] begin
|
||||
if (Bfile != znull) then
|
||||
read(Bfile,B);
|
||||
else
|
||||
step := 1.0/(n*p);
|
||||
B := ((Index1-1)*p + Index2)*step + 1.0;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure matmultSUMMA();
|
||||
var
|
||||
i: integer;
|
||||
it: integer;
|
||||
runtime: double;
|
||||
[RC] begin
|
||||
ReadA();
|
||||
ReadB();
|
||||
|
||||
if (printinput) then
|
||||
[RA] writeln("A is:\n",A);
|
||||
[RB] writeln("B is:\n",B);
|
||||
end;
|
||||
|
||||
ResetTimer();
|
||||
|
||||
for it := 1 to iters do
|
||||
|
||||
C := 0.0; -- zero C
|
||||
|
||||
for i := 1 to n do
|
||||
[FCol] Aflood := >>[,i] A; -- flood A col
|
||||
[FRow] Bflood := >>[i,] B; -- flood B row
|
||||
|
||||
C += (Aflood * Bflood); -- multiply
|
||||
end;
|
||||
end;
|
||||
|
||||
runtime := CheckTimer();
|
||||
|
||||
if (verbose) then
|
||||
writeln("C is:\n",C);
|
||||
end;
|
||||
|
||||
if (dotiming) then
|
||||
writeln("total runtime = %12.6f":runtime);
|
||||
writeln("actual runtime = %12.6f":runtime/iters);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure GetSingleDim(infile:file):integer;
|
||||
var dim:integer;
|
||||
begin
|
||||
if (infile != znull) then
|
||||
read(infile,dim);
|
||||
else
|
||||
dim := default_size;
|
||||
end;
|
||||
return dim;
|
||||
end;
|
||||
|
||||
|
||||
procedure GetInnerDim(infile1:file; infile2:file):integer;
|
||||
var
|
||||
col:integer;
|
||||
row:integer;
|
||||
retval:integer;
|
||||
begin
|
||||
retval := -1;
|
||||
if (infile1 != znull) then
|
||||
read(infile1,col);
|
||||
retval := col;
|
||||
end;
|
||||
if (infile2 != znull) then
|
||||
read(infile2,row);
|
||||
if (retval = -1) then
|
||||
retval := row;
|
||||
else
|
||||
if (row != col) then
|
||||
halt("ERROR: Inner dimensions don't match");
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
if (retval = -1) then
|
||||
retval := default_size;
|
||||
end;
|
||||
return retval;
|
||||
end;
|
||||
Loading…
Add table
Add a link
Reference in a new issue