Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / MaxSumSubseq.tq
Created November 21, 2022 16:09
Maximum sum subsequence
let maxSumSubseq a =
let maxm = Array.fold [a, b -> max a b] 0 a in
let rec loop sum i =
if i = # a then sum else
let ai = Array.get a i in
if ai > 0 then loop (sum + ai) (i+1) else loop sum (i+1) in
loop 0 0
@jdh30
jdh30 / PythagorasTree.tq
Created November 21, 2022 15:25
Pythagoras' Tree
let branches m =
{ { Vec2.rotate(π/2 - asin(4/5));
Vec2.scale(4/5, 4/5);
Vec2.translate(0, -1) };
{ Vec2.translate(1, -1);
Vec2.rotate(-π/2 + asin(3/5));
Vec2.scale(3/5, 3/5);
Vec2.translate(1/3, 0) } }
@ Array.map [m2 → Array.concat{m; m2}]
@jdh30
jdh30 / InfinitePowerSeries.tq
Created November 21, 2022 15:15
Infinite power series
let toHtml f =
let x = Html "𝑥" in
let pow = (* xⁿ *)
[ 0 → {Html "1"}
| 1 → {x}
| n → {x; Html.tag "sup" {"style", "font-size:70%"} (Html.ofNumber n)} ] in
let append(xs, x) = Array.Unsafe.append xs x in
let appends(xs, xss) = Array.fold append xs xss in
let minus = Html.of " - " in
let ellipses = Html " + …" in
@jdh30
jdh30 / NQueens.tq
Created November 21, 2022 12:43
Find solutions to the n-queens problem
let safe (x1, y1) (x2, y2) =
x1 <> x2 && y1 <> y2 && x2 - x1 <> y2 - y1 && x1 - y2 <> x2 - y1
let rec next n s =
Stack.pop s @
[ None -> None
| Some((qs, ps), s) ->
Stack.pop ps @
[ None -> if Stack.length qs = n then Some(qs, s) else next n s
| Some(q, ps) ->
@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 →