Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
37
Task/Matrix-multiplication/ERRE/matrix-multiplication.erre
Normal file
37
Task/Matrix-multiplication/ERRE/matrix-multiplication.erre
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
PROGRAM MAT_PROD
|
||||
|
||||
DIM A[3,1],B[1,2],ANS[3,2]
|
||||
|
||||
BEGIN
|
||||
|
||||
DATA(1,2,3,4,5,6,7,8)
|
||||
DATA(1,2,3,4,5,6)
|
||||
|
||||
FOR I=0 TO 3 DO
|
||||
FOR J=0 TO 1 DO
|
||||
READ(A[I,J])
|
||||
END FOR
|
||||
END FOR
|
||||
|
||||
FOR I=0 TO 1 DO
|
||||
FOR J=0 TO 2 DO
|
||||
READ(B[I,J])
|
||||
END FOR
|
||||
END FOR
|
||||
|
||||
FOR I=0 TO UBOUND(ANS,1) DO
|
||||
FOR J=0 TO UBOUND(ANS,2) DO
|
||||
FOR K=0 TO UBOUND(A,2) DO
|
||||
ANS[I,J]=ANS[I,J]+(A[I,K]*B[K,J])
|
||||
END FOR
|
||||
END FOR
|
||||
END FOR
|
||||
! print answer
|
||||
FOR I=0 TO UBOUND(ANS,1) DO
|
||||
FOR J=0 TO UBOUND(ANS,2) DO
|
||||
PRINT(ANS[I,J],)
|
||||
END FOR
|
||||
PRINT
|
||||
END FOR
|
||||
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
fun main(x: [n][m]int, y: [m][p]int): [n][p]int =
|
||||
map (fn xr => map (fn yc => reduce (+) 0 (zipWith (*) xr yc))
|
||||
(transpose y))
|
||||
x
|
||||
14
Task/Matrix-multiplication/Idris/matrix-multiplication.idris
Normal file
14
Task/Matrix-multiplication/Idris/matrix-multiplication.idris
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Data.Vect
|
||||
|
||||
Matrix : Nat -> Nat -> Type -> Type
|
||||
Matrix m n t = Vect m (Vect n t)
|
||||
|
||||
multiply : Num t => Matrix m1 n t -> Matrix n m2 t -> Matrix m1 m2 t
|
||||
multiply a b = multiply' a (transpose b)
|
||||
where
|
||||
dot : Num t => Vect n t -> Vect n t -> t
|
||||
dot v1 v2 = sum $ map (\(s1, s2) => (s1 * s2)) (zip v1 v2)
|
||||
|
||||
multiply' : Num t => Matrix m1 n t -> Matrix m2 n t -> Matrix m1 m2 t
|
||||
multiply' (a::as) b = map (dot a) b :: multiply' as b
|
||||
multiply' [] _ = []
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defun matrix* (matrix-1 matrix-2)
|
||||
(list-comp
|
||||
((<- a matrix-1))
|
||||
(list-comp
|
||||
((<- b (transpose matrix-2)))
|
||||
(lists:foldl #'+/2 0
|
||||
(lists:zipwith #'*/2 a b)))))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
> (set ma '((1 2)
|
||||
(3 4)
|
||||
(5 6)
|
||||
(7 8)))
|
||||
((1 2) (3 4) (5 6) (7 8))
|
||||
> (set mb (transpose ma))
|
||||
((1 3 5 7) (2 4 6 8))
|
||||
> (matrix* ma mb)
|
||||
((5 11 17 23) (11 25 39 53) (17 39 61 83) (23 53 83 113))
|
||||
31
Task/Matrix-multiplication/Nim/matrix-multiplication.nim
Normal file
31
Task/Matrix-multiplication/Nim/matrix-multiplication.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import strfmt
|
||||
|
||||
type Matrix[M,N: static[int]] = array[M, array[N, float]]
|
||||
|
||||
let a = [[1.0, 1.0, 1.0, 1.0],
|
||||
[2.0, 4.0, 8.0, 16.0],
|
||||
[3.0, 9.0, 27.0, 81.0],
|
||||
[4.0, 16.0, 64.0, 256.0]]
|
||||
|
||||
let b = [[ 4.0 , -3.0 , 4/3.0, -1/4.0 ],
|
||||
[-13/3.0, 19/4.0, -7/3.0, 11/24.0],
|
||||
[ 3/2.0, -2.0 , 7/6.0, -1/4.0 ],
|
||||
[ -1/6.0, 1/4.0, -1/6.0, 1/24.0]]
|
||||
|
||||
proc `$`(m: Matrix): string =
|
||||
result = "(["
|
||||
for r in m:
|
||||
if result.len > 2: result.add "]\n ["
|
||||
for val in r: result.add val.format("8.2f")
|
||||
result.add "])"
|
||||
|
||||
proc `*`[M,P,N](a: Matrix[M,P]; b: Matrix[P,N]): Matrix[M,N] =
|
||||
for i in result.low .. result.high:
|
||||
for j in result[0].low .. result[0].high:
|
||||
for k in a[0].low .. a[0].high:
|
||||
result[i][j] += a[i][k] * b[k][j]
|
||||
|
||||
echo a
|
||||
echo b
|
||||
echo a * b
|
||||
echo b * a
|
||||
16
Task/Matrix-multiplication/Phix/matrix-multiplication.phix
Normal file
16
Task/Matrix-multiplication/Phix/matrix-multiplication.phix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function matrix_mul(sequence a, sequence b)
|
||||
sequence c
|
||||
if length(a[1]) != length(b) then
|
||||
return 0
|
||||
else
|
||||
c = repeat(repeat(0,length(b[1])),length(a))
|
||||
for i=1 to length(a) do
|
||||
for j=1 to length(b[1]) do
|
||||
for k=1 to length(a[1]) do
|
||||
c[i][j] += a[i][k]*b[k][j]
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
return c
|
||||
end if
|
||||
end function
|
||||
18
Task/Matrix-multiplication/Ring/matrix-multiplication.ring
Normal file
18
Task/Matrix-multiplication/Ring/matrix-multiplication.ring
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
load "stdlib.ring"
|
||||
n = 3
|
||||
C = newlist(n,n)
|
||||
A = [[1,2,3], [4,5,6], [7,8,9]]
|
||||
B = [[1,0,0], [0,1,0], [0,0,1]]
|
||||
for i = 1 to n
|
||||
for j = 1 to n
|
||||
for k = 1 to n
|
||||
C[i][k] += A[i][j] * B[j][k]
|
||||
next
|
||||
next
|
||||
next
|
||||
for i = 1 to n
|
||||
for j = 1 to n
|
||||
see C[i][j] + " "
|
||||
next
|
||||
see nl
|
||||
next
|
||||
26
Task/Matrix-multiplication/SPAD/matrix-multiplication.spad
Normal file
26
Task/Matrix-multiplication/SPAD/matrix-multiplication.spad
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(1) -> A:=matrix [[1,2],[3,4],[5,6],[7,8]]
|
||||
|
||||
+1 2+
|
||||
| |
|
||||
|3 4|
|
||||
(1) | |
|
||||
|5 6|
|
||||
| |
|
||||
+7 8+
|
||||
Type: Matrix(Integer)
|
||||
(2) -> B:=matrix [[1,2,3],[4,5,6]]
|
||||
|
||||
+1 2 3+
|
||||
(2) | |
|
||||
+4 5 6+
|
||||
Type: Matrix(Integer)
|
||||
(3) -> A*B
|
||||
|
||||
+9 12 15+
|
||||
| |
|
||||
|19 26 33|
|
||||
(3) | |
|
||||
|29 40 51|
|
||||
| |
|
||||
+39 54 69+
|
||||
Type: Matrix(Integer)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
matmul(A(2), B(2)) [i,j] :=
|
||||
let k := 1...size(B);
|
||||
in sum( A[i,k] * B[k,j] );
|
||||
|
||||
//Example Use
|
||||
a := [[1, 2],
|
||||
[3, 4]];
|
||||
|
||||
b := [[-3, -8, 3],
|
||||
[-2, 1, 4]];
|
||||
|
||||
test := matmul(a, b);
|
||||
|
|
@ -0,0 +1 @@
|
|||
matmul(A(2), B(2)) [i,j] := sum( A[i,all] * B[all,j] );
|
||||
27
Task/Matrix-multiplication/Sidef/matrix-multiplication.sidef
Normal file
27
Task/Matrix-multiplication/Sidef/matrix-multiplication.sidef
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
func matrix_multi(a, b) {
|
||||
var m = [[]]
|
||||
for r in ^a {
|
||||
for c in ^b[0] {
|
||||
for i in ^b {
|
||||
m[r][c] := 0 += (a[r][i] * b[i][c])
|
||||
}
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
var a = [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6],
|
||||
[7, 8]
|
||||
]
|
||||
|
||||
var b = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6]
|
||||
]
|
||||
|
||||
for line in matrix_multi(a, b) {
|
||||
say line.map{|i|'%3d' % i }.join(', ')
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
LOCAL ARRAY a[4,2], b[2,3], c[4,3]
|
||||
CLOSE DATABASES ALL
|
||||
*!* The arrays could be created directly but I prefer to do this:
|
||||
CREATE CURSOR mat1 (c1 I, c2 I)
|
||||
CREATE CURSOR mat2 (c1 I, c2 I, c3 I)
|
||||
*!* Since matrix multiplication of integer arrays
|
||||
*!* involves only multiplication and addition,
|
||||
*!* the result will contain integers
|
||||
CREATE CURSOR result (c1 I, c2 I, c3 I)
|
||||
INSERT INTO mat1 VALUES (1, 2)
|
||||
INSERT INTO mat1 VALUES (3, 4)
|
||||
INSERT INTO mat1 VALUES (5, 6)
|
||||
INSERT INTO mat1 VALUES (7, 8)
|
||||
SELECT * FROM mat1 INTO ARRAY a
|
||||
|
||||
INSERT INTO mat2 VALUES (1, 2, 3)
|
||||
INSERT INTO mat2 VALUES (4, 5, 6)
|
||||
SELECT * FROM mat2 INTO ARRAY b
|
||||
STORE 0 TO c
|
||||
MatMult(@a,@b,@c)
|
||||
SELECT result
|
||||
APPEND FROM ARRAY c
|
||||
BROWSE
|
||||
|
||||
|
||||
PROCEDURE MatMult(aa, bb, cc)
|
||||
LOCAL n As Integer, m As Integer, p As Integer, i As Integer, j As Integer, k As Integer
|
||||
IF ALEN(aa,2) = ALEN(bb,1)
|
||||
n = ALEN(aa,2)
|
||||
m = ALEN(aa,1)
|
||||
p = ALEN(bb,2)
|
||||
FOR i = 1 TO m
|
||||
FOR j = 1 TO p
|
||||
FOR k = 1 TO n
|
||||
cc[i,j] = cc[i,j] + aa[i,k]*bb[k,j]
|
||||
ENDFOR
|
||||
ENDFOR
|
||||
ENDFOR
|
||||
ELSE
|
||||
? "Invalid dimensions"
|
||||
ENDIF
|
||||
ENDPROC
|
||||
18
Task/Matrix-multiplication/jq/matrix-multiplication.jq
Normal file
18
Task/Matrix-multiplication/jq/matrix-multiplication.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def dot_product(a; b):
|
||||
a as $a | b as $b
|
||||
| reduce range(0;$a|length) as $i (0; . + ($a[$i] * $b[$i]) );
|
||||
|
||||
# transpose/0 expects its input to be a rectangular matrix (an array of equal-length arrays)
|
||||
def transpose:
|
||||
if (.[0] | length) == 0 then []
|
||||
else [map(.[0])] + (map(.[1:]) | transpose)
|
||||
end ;
|
||||
|
||||
# A and B should both be numeric matrices, A being m by n, and B being n by p.
|
||||
def multiply(A; B):
|
||||
A as $A | B as $B
|
||||
| ($B[0]|length) as $p
|
||||
| ($B|transpose) as $BT
|
||||
| reduce range(0; $A|length) as $i
|
||||
([]; reduce range(0; $p) as $j
|
||||
(.; .[$i][$j] = dot_product( $A[$i]; $BT[$j] ) )) ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue