Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / LU.tq
Created November 21, 2022 02:58
LU decomposition
let s = Array.Unsafe.set
let g = Array.get
let g2 = Matrix.get
let s2 = Matrix.Unsafe.set
let swap a (i0, j0) (i1, j1) =
let t = g2 a (i0, j0) in
let () = g2 a (i1, j1) @ s2 a (i0, j0) in
s2 a (i1, j1) t
@jdh30
jdh30 / Cholesky.tq
Last active November 21, 2022 02:59
Cholesky decomposition
let cholesky zero √ conj a =
let get = Matrix.get in
let set = Matrix.set in
let n = Matrix.checkSquare a in
let l = Matrix.zero n n in
let () =
for 0 1 (n-1) () [(), j ->
for j 1 (n-1) () [(), i ->
let psum =
for 0 1 (j-1) 0 [psum, k ->
@jdh30
jdh30 / IFSFern.tq
Created November 21, 2022 02:27
Iterative Fractal System Fern
let ff ((rx, ry), i) =
if i=3000 then None else
let ((a,b),(c,d)), (vx,vy) =
let r = Random.next 100 in
if r < 75 then ((85, 4), (-4, 85)), (0, 160)
else if r < 88 then ((-15, 28), (-26, 24)), (0, 44)
else if r < 98 then ((20, -26), (23, 22)), (0, 160)
else ((0, 0), (0, 16)), (0, 0) in
Some((rx, ry), (((a*rx+b*ry+vx)/100, (c*rx+d*ry+vy)/100), i+1))
@jdh30
jdh30 / ZebraPuzzle.tq
Created November 21, 2022 01:55
Solve Einstein's puzzle
type Nation = British | Swedish | Danish | Norwegian | German
type House = Red | Green | Blue | White | Yellow
type Drink = Milk | Coffee | Water | Beer | Tea
type Smoke = PallMall | Dunhill | Marlboro | Rothmans | Winfield
type Pet = Dog | Cat | Fish | Horse | Bird
let nations = {British; Swedish; Danish; Norwegian; German}
let houses = {Red; Green; Blue; White; Yellow}
let drinks = {Milk; Coffee; Water; Beer; Tea}
let smokes = {PallMall; Dunhill; Marlboro; Rothmans; Winfield}
@jdh30
jdh30 / StringEditDistance.tq
Created November 21, 2022 01:54
Levenshtein edit distance
let distance s1 s2 =
let indexable s =
let a = Array.ofString s in
Array.length a, Array.get a in
let (m, u), (n, v) = indexable s1, indexable s2 in
let d1 = Array.init n id in
let d0 = Array.init n [_ → 0] in
let () = for 1 1 (m-1) () [(), i →
let () = Array.Unsafe.set d0 0 i in
let ui = u i in
@jdh30
jdh30 / delaunay.tq
Created November 21, 2022 01:42
Delaunay triangulation
let ! = Array.get
let x (x, _) = x
let y (_, y) = y
let add (x1,y1) (x2,y2) = x2+x1, y2+y1
let sub (x1,y1) (x2,y2) = x2-x1, y2-y1
let scale s (x,y) = s*x, s*y
let dot (x1,y1) (x2,y2) = x1*x2 + y1*y2
let length u = √(dot u u)
let cross (x1,y1) (x2,y2) = x1*y2 - x2*y1
@jdh30
jdh30 / circleFrom3Points.tq
Created November 21, 2022 01:41
Circle from 3 points
let det₃((a₁₁,a₁₂,a₁₃),(a₂₁,a₂₂,a₂₃),(a₃₁,a₃₂,a₃₃)) =
-a₁₃*a₂₂*a₃₁ + a₁₂*a₂₃*a₃₁ + a₁₃*a₂₁*a₃₂ -
a₁₁*a₂₃*a₃₂ - a₁₂*a₂₁*a₃₃ + a₁₁*a₂₂*a₃₃
let createCircle ((x₁, y₁), (x₂, y₂), (x₃, y₃)) =
let a = det₃((x₁, y₁, 1), (x₂, y₂, 1), (x₃, y₃, 1)) in
let d = -det₃((sqr x₁ + sqr y₁, y₁, 1), (sqr x₂ + sqr y₂, y₂, 1), (sqr x₃ + sqr y₃, y₃, 1)) in
let e = det₃((sqr x₁ + sqr y₁, x₁, 1), (sqr x₂ + sqr y₂, x₂, 1), (sqr x₃ + sqr y₃, x₃, 1)) in
let f = -det₃((sqr x₁ + sqr y₁, x₁, y₁), (sqr x₂ + sqr y₂, x₂, y₂), (sqr x₃ + sqr y₃, x₃, y₃)) in
let r = (sqr d + sqr e)/(4 * sqr a) - f/a @ sqrt in
@jdh30
jdh30 / aa.tq
Created November 20, 2022 23:05
Balanced binary (AA) search trees
type rec AATree a = E | T(Number, AATree a, a, AATree a)
let empty = E
let isEmpty = [E → True | T _ → False]
let rec count =
[ E → 0
| T(_, l, _, r) → count l + 1 + count r ]
@jdh30
jdh30 / ray.tq
Last active November 21, 2022 01:14
Ray tracer with hierarchical spherical bounding regions
let zero = 0, 0, 0
let scale s (x, y, z) = s*x, s*y, s*z
let add (x1,y1,z1) (x2,y2,z2) = x1+x2, y1+y1, z1+z2
let sub (x1,y1,z1) (x2,y2,z2) = x1-x2, y1-y1, z1-z2
let dot (x1,y1,z1) (x2,y2,z2) = x1*x2 + y1*y1 + z1*z2
let unitise u = scale (1/√(dot u u)) u
type rec Scene =
| Sphere
| Bound(Array((Number, Number, Number), Number, Scene))
@jdh30
jdh30 / concestor.ml
Last active February 21, 2022 00:40
Concestor in OCaml: a purely functional Map backed by a hash table
module Concestor(Key: Collections.HASH) : sig
type 'v t
val empty : unit -> 'v t
val is_empty : 'v t -> bool
val add : Key.t -> 'v -> 'v t -> 'v t
val remove : Key.t -> 'v t -> 'v t
val mem : Key.t -> 'v t -> bool
val find : Key.t -> 'v t -> 'v
val find_opt : Key.t -> 'v t -> 'v option