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,67 @@
module Main
import Data.Vect
Matrix : Nat -> Nat -> Type -> Type
Matrix m n t = Vect m (Vect n t)
zeros : (m : Nat) -> (n : Nat) -> Matrix m n Double
zeros m n = replicate m (replicate n 0.0)
indexM : (Fin m, Fin n) -> Matrix m n t -> t
indexM (i, j) a = index j (index i a)
replaceAtM : (Fin m, Fin n) -> t -> Matrix m n t -> Matrix m n t
replaceAtM (i, j) e a = replaceAt i (replaceAt j e (index i a)) a
get : Matrix m m Double -> Matrix m m Double -> (Fin m, Fin m) -> Double
get a l (i, j) {m} = if i == j then sqrt $ indexM (j, j) a - dot
else if i > j then (indexM (i, j) a - dot) / indexM (j, j) l
else 0.0
where
-- Obtain indicies 0 to j -1
ks : List (Fin m)
ks = case (findIndices (\_ => True) a) of
[] => []
(x::xs) => init (x::xs)
dot : Double
dot = sum [(indexM (i, k) l) * (indexM (j, k) l) | k <- ks]
updateL : Matrix m m Double -> Matrix m m Double -> (Fin m, Fin m) -> Matrix m m Double
updateL a l idx = replaceAtM idx (get a l idx) l
cholesky : Matrix m m Double -> Matrix m m Double
cholesky a {m} =
foldl (\l',i =>
foldl (\l'',j => updateL a l'' (i, j)) l' (js i))
l is
where l = zeros m m
is : List (Fin m)
is = findIndices (\_ => True) a
js : Fin m -> List (Fin m)
js n = filter (<= n) is
ex1 : Matrix 3 3 Double
ex1 = cholesky [[25.0, 15.0, -5.0], [15.0, 18.0, 0.0], [-5.0, 0.0, 11.0]]
ex2 : Matrix 4 4 Double
ex2 = cholesky [[18.0, 22.0, 54.0, 42.0], [22.0, 70.0, 86.0, 62.0],
[54.0, 86.0, 174.0, 134.0], [42.0, 62.0, 134.0, 106.0]]
main : IO ()
main = do
print ex1
putStrLn "\n"
print ex2
putStrLn "\n"

View file

@ -0,0 +1,28 @@
import math, strutils
proc cholesky[T](a: T): T =
for i in 0 .. < a[0].len:
for j in 0 .. i:
var s = 0.0
for k in 0 .. < j:
s += result[i][k] * result[j][k]
result[i][j] = if i == j: sqrt(a[i][i]-s)
else: (1.0 / result[j][j] * (a[i][j] - s))
proc `$`(a): string =
result = ""
for b in a:
for c in b:
result.add c.formatFloat(ffDecimal, 5) & " "
result.add "\n"
let m1 = [[25.0, 15.0, -5.0],
[15.0, 18.0, 0.0],
[-5.0, 0.0, 11.0]]
echo cholesky(m1)
let m2 = [[18.0, 22.0, 54.0, 42.0],
[22.0, 70.0, 86.0, 62.0],
[54.0, 86.0, 174.0, 134.0],
[42.0, 62.0, 134.0, 106.0]]
echo cholesky(m2)

View file

@ -0,0 +1,13 @@
func cholesky(matrix) {
var chol = matrix.len.of { matrix.len.of(0) }
for row in ^matrix {
for col in (0..row) {
var x = matrix[row][col]
for i in (0..col) {
x -= (chol[row][i] * chol[col][i])
}
chol[row][col] = (row == col ? x.sqrt : x/chol[col][col])
}
}
return chol
}

View file

@ -0,0 +1,18 @@
var example1 = [ [ 25, 15, -5 ],
[ 15, 18, 0 ],
[ -5, 0, 11 ] ];
say "Example 1:";
cholesky(example1).each { |row|
say row.map {'%7.4f' % _}.join(' ');
}
var example2 = [ [ 18, 22, 54, 42],
[ 22, 70, 86, 62],
[ 54, 86, 174, 134],
[ 42, 62, 134, 106] ];
say "\nExample 2:";
cholesky(example2).each { |row|
say row.map {'%7.4f' % _}.join(' ');
}

View file

@ -0,0 +1,38 @@
# Create an m x n matrix
def matrix(m; n; init):
if m == 0 then []
elif m == 1 then [range(0; n)] | map(init)
elif m > 0 then
matrix(1; n; init) as $row
| [range(0; m)] | map( $row )
else error("matrix\(m);_;_) invalid")
end ;
# Print a matrix neatly, each cell ideally occupying n spaces,
# but without truncation
def neatly(n):
def right: tostring | ( " " * (n-length) + .);
. as $in
| length as $length
| reduce range (0; $length) as $i
(""; . + reduce range(0; $length) as $j
(""; "\(.) \($in[$i][$j] | right )" ) + "\n" ) ;
def is_square:
type == "array" and (map(type == "array") | all) and
length == 0 or ( (.[0]|length) as $l | map(length == $l) | all) ;
# This implementation of is_symmetric/0 uses a helper function that circumvents
# limitations of jq 1.4:
def is_symmetric:
# [matrix, i,j, len]
def test:
if .[1] > .[3] then true
elif .[1] == .[2] then [ .[0], .[1] + 1, 0, .[3]] | test
elif .[0][.[1]][.[2]] == .[0][.[2]][.[1]]
then [ .[0], .[1], .[2]+1, .[3]] | test
else false
end;
if is_square|not then false
else [ ., 0, 0, length ] | test
end ;

View file

@ -0,0 +1,22 @@
def cholesky_factor:
if is_symmetric then
length as $length
| . as $self
| reduce range(0; $length) as $k
( matrix(length; length; 0); # the matrix that will hold the answer
reduce range(0; $length) as $i
(.;
if $i == $k
then (. as $lower
| reduce range(0; $k) as $j
(0; . + ($lower[$k][$j] | .*.) )) as $sum
| .[$k][$k] = (($self[$k][$k] - $sum) | sqrt)
elif $i > $k
then (. as $lower
| reduce range(0; $k) as $j
(0; . + $lower[$i][$j] * $lower[$k][$j])) as $sum
| .[$i][$k] = (($self[$k][$i] - $sum) / .[$k][$k] )
else .
end ))
else error( "cholesky_factor: matrix is not symmetric" )
end ;

View file

@ -0,0 +1,4 @@
4.242640687119285 0 0 0
5.185449728701349 6.565905201197403 0 0
12.727922061357857 3.0460384954008553 1.6497422479090704 0
9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924