This file contains hidden or 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 hidden or 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 hidden or 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 -> |
This file contains hidden or 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 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)) |
This file contains hidden or 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 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} |
This file contains hidden or 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 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 |
This file contains hidden or 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 ! = 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 |
This file contains hidden or 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 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 |