Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / CorrelationCoefficient.tq
Created November 21, 2022 12:00
Computes the Pearson's rank correlation coefficient
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))
@jdh30
jdh30 / Primes.tq
Created November 21, 2022 11:59
Generate prime numbers using an online Sieve of Eratosthenes
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
@jdh30
jdh30 / Factorize
Created November 21, 2022 11:59
Compute the prime factors of the given number
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
@jdh30
jdh30 / GradientDescent.tq
Created November 21, 2022 11:57
Gradient descent function minimization
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
@jdh30
jdh30 / NumericalIntegration.tq
Created November 21, 2022 11:52
Numerical integration using adaptive Simpson's rule with substitution of variables to handle infinite limits
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
@jdh30
jdh30 / FunctionInterpolation
Created November 21, 2022 11:50
Linear, cubic and Lagrange function interpolation and extrapolation
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 →
@jdh30
jdh30 / NewtonRaphson.tq
Created November 21, 2022 11:47
Newton-Raphson root finder
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
@jdh30
jdh30 / Maze.tq
Created November 21, 2022 11:35
Maze generator
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
@jdh30
jdh30 / LorenzAttractor.tq
Created November 21, 2022 10:54
Lorenz attractor
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]
@jdh30
jdh30 / QR.tq
Created November 21, 2022 02:59
QR decomposition
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