This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pearsonsCorrelationCoefficient xys = | |
let n = Array.length xys in | |
let ∑xy, ∑x, ∑y, ∑x2, ∑y2 = | |
Array.fold [(∑xy, ∑x, ∑y, ∑x2, ∑y2), (x, y) → | |
∑xy+x*y, ∑x+x, ∑y+y, ∑x2+x*x, ∑y2+y*y] | |
(0, 0, 0, 0, 0) xys in | |
(n*∑xy-∑x*∑y)/(√(n*∑x2-∑x*∑x)*√(n*∑y2-∑y*∑y)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primes = | |
let a = {2} in | |
let grow() = | |
let p0 = Array.get a (Array.length a - 1) + 1 in | |
let b = Array.init p0 [_ → True] in | |
let () = | |
Array.fold [(), di → | |
let rec loop i = | |
if i >= Array.length b then () else | |
let () = Array.Unsafe.set b i False in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primeFactors x = | |
let rec loop fs c p = | |
if p < c * c then Array.Unsafe.append fs p else | |
if mod(p, c) = 0 then | |
loop (Array.Unsafe.append fs c) c (p / c) | |
else loop fs (c + 1) p in | |
loop {} 2 x | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec fixedPoint f x = | |
let fx = f x in | |
if fx = x then x else fixedPoint f fx | |
let ∂ fxs f xs (i, xi) = | |
let () = xi + δ @ Array.Unsafe.set xs i in | |
let f2 = (f xs - fxs) / δ in | |
let () = xi @ Array.Unsafe.set xs i in | |
f2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let integrate (x0, x1) f = | |
let integrateAux f (x0, x2) = | |
let δ = ε in | |
let simpsonsRule dx fx0 fx1 fx2 = dx * (fx0 + 4*fx1 + fx2) / 6 in | |
let rec aux x0 fx0 x2 fx2 x4 fx4 = | |
let dx = x2 - x0 in | |
let x1 = (x0 + x2) / 2 in | |
let x3 = (x2 + x4) / 2 in | |
if x0 = x1 || x2 = x3 then 0 else | |
let fx1 = f x1 in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let lagrange xys = | |
let n = # xys in | |
let xys = Array.sortBy [x, _ → x] xys in | |
let lagrange j x = | |
for 0 1 (n-1) 1 [y, i → | |
if i = j then y else | |
let xi, _ = Array.get xys i in | |
let xj, _ = Array.get xys j in | |
y * (x - xi) / (xj - xi)] in | |
[ x → |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let δ = ∛ ε | |
let d f x = (f(x+δ) - f(x-δ))/(2*δ) | |
let rec fixedPoint f x = | |
let fx = f x in | |
if fx = x then x else fixedPoint f fx | |
let newtonRaphson f df x = fixedPoint [x → x - f x / df x] x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type rec Tree = Tree((Number, Number), Stack Tree) | |
let mkFree n = | |
for 0 1 (n-1) (Set.empty()) [ps, x -> | |
for 0 1 (n-1) ps [ps, y -> | |
Set.add (x, y) ps]] | |
let rec search free (i, j as ij) = | |
let moves = | |
Random.next 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let trajectory p dt n = | |
let s, t, u = 10, 8/3, 28 in | |
Array.unfold [(x, y, z), i -> | |
if i=n then None else | |
let p = x+s*(y-x)*dt, y+(x*(u-z)-y)*dt, z+(x*y-t*z)*dt in | |
Some(p, (p, i+1))] (p, 0) | |
let () = | |
trajectory (10, 0, 20) (1/200) 2000 | |
@ Array.map [x, y, z -> x, y] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let qr mA = | |
let m, n = Matrix.dimensions mA in | |
let mI = Matrix.identity m in | |
for 0 1 (n-1) (mI, mI, mA) [(mQ, mR, mQA), k -> | |
let x = Matrix.column 0 mQA in | |
let xb = Vector.norm x in | |
let u = Array.mapi [i, xi -> if i=0 then xi - xb else xi] x in | |
let v = Vector.scale (1 / Vector.norm u) u in | |
let q k (i, j) = | |
let x = if i=j then 1 else 0 in |