Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,27 @@
(lib 'list)
(lib 'matrix)
;; adapted from Racket
(define (permanent M)
(let (( n (matrix-row-num M)))
(for/sum ([σ (in-permutations n)])
(for/product ([i n] [σi σ])
(array-ref M i σi)))))
;; output
(define A (list->array '(1 2 3 4) 2 2))
(array-print A)
1 2
3 4
(determinant A) → -2
(permanent A) → 10
(define M (list->array (iota 25) 5 5))
(array-print M)
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
(determinant M) → 0
(permanent M) → 6778800

View file

@ -0,0 +1,5 @@
def sgn( p ) = product( (if s(0) < s(1) xor i(0) < i(1) then -1 else 1) | (s, i) <- p.combinations(2).zip( (0:p.length()).combinations(2) ) )
def perm( m ) = sum( product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() )
def det( m ) = sum( sgn(sigma)*product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() )

View file

@ -0,0 +1,7 @@
def perm( m )
| m.length() == 1 and m(0).length() == 1 = m(0, 0)
| otherwise = sum( m(i, 0)*perm(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() )
def det( m )
| m.length() == 1 and m(0).length() == 1 = m(0, 0)
| otherwise = sum( (-1)^i*m(i, 0)*det(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() )

View file

@ -0,0 +1,18 @@
matrices = [
( (1, 2),
(3, 4)),
( (-2, 2, -3),
(-1, 1, 3),
( 2, 0, -1)),
( ( 1, 2, 3, 4),
( 4, 5, 6, 7),
( 7, 8, 9, 10),
(10, 11, 12, 13)),
( ( 0, 1, 2, 3, 4),
( 5, 6, 7, 8, 9),
(10, 11, 12, 13, 14),
(15, 16, 17, 18, 19),
(20, 21, 22, 23, 24)) ]
for m <- matrices
println( m, 'perm: ' + perm(m), 'det: ' + det(m) )

View file

@ -0,0 +1,37 @@
import sequtils, permutationsswap
type Matrix[M,N: static[int]] = array[M, array[N, float]]
proc det[M,N](a: Matrix[M,N]): float =
let n = toSeq 0..a.high
for sigma, sign in n.permutations:
var x = sign.float
for i in n: x *= a[i][sigma[i]]
result += x
proc perm[M,N](a: Matrix[M,N]): float =
let n = toSeq 0..a.high
for sigma, sign in n.permutations:
var x = 1.0
for i in n: x *= a[i][sigma[i]]
result += x
const
a = [ [1.0, 2.0]
, [3.0, 4.0]
]
b = [ [ 1.0, 2, 3, 4]
, [ 4.0, 5, 6, 7]
, [ 7.0, 8, 9, 10]
, [10.0, 11, 12, 13]
]
c = [ [ 0.0, 1, 2, 3, 4]
, [ 5.0, 6, 7, 8, 9]
, [10.0, 11, 12, 13, 14]
, [15.0, 16, 17, 18, 19]
, [20.0, 21, 22, 23, 24]
]
echo "perm: ", a.perm, " det: ", a.det
echo "perm: ", b.perm, " det: ", b.det
echo "perm: ", c.perm, " det: ", c.det

View file

@ -0,0 +1,16 @@
(1) -> M:=matrix [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
+2 9 4+
| |
(1) |7 5 3|
| |
+6 1 8+
Type: Matrix(Integer)
(2) -> determinant M
(2) - 360
Type: Integer
(3) -> permanent M
(3) 900
Type: PositiveInteger

View file

@ -0,0 +1,18 @@
# Eliminate row i and row j
def except(i;j):
reduce del(.[i])[] as $row ([]; . + [$row | del(.[j]) ] );
def det:
def parity(i): if i % 2 == 0 then 1 else -1 end;
if length == 1 and (.[0] | length) == 1 then .[0][0]
else . as $m
| reduce range(0; length) as $i
(0; . + parity($i) * $m[0][$i] * ( $m | except(0;$i) | det) )
end ;
def perm:
if length == 1 and (.[0] | length) == 1 then .[0][0]
else . as $m
| reduce range(0; length) as $i
(0; . + $m[0][$i] * ( $m | except(0;$i) | perm) )
end ;

View file

@ -0,0 +1,22 @@
def matrices:
[ [1, 2],
[3, 4]],
[ [-2, 2, -3],
[-1, 1, 3],
[ 2, 0, -1]],
[ [ 1, 2, 3, 4],
[ 4, 5, 6, 7],
[ 7, 8, 9, 10],
[10, 11, 12, 13]],
[ [ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
;
"Determinants: ", (matrices | det),
"Permanents: ", (matrices | perm)

View file

@ -0,0 +1,11 @@
$ jq -n -r -f Matrix_arithmetic.jq
Determinants:
-2
18
0
0
Permanents:
10
10
29556
6778800

View file

@ -0,0 +1,8 @@
# Requires lup/0
def det:
def product_diagonal:
. as $m | reduce range(0;length) as $i (1; . * $m[$i][$i]);
def tidy: if . == -0 then 0 else . end;
lup
| (.[0]|product_diagonal) as $l
| if $l == 0 then 0 else $l * (.[1]|product_diagonal) | tidy end ;

View file

@ -0,0 +1 @@
matrices | det