Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / Simplify.mx
Last active February 16, 2017 05:55
Symbolic simplification rules written in Mathematica's language
0 + x_ := x
x_ + 0 := x
f_ + (g_ + h_) := f + g + h
0 x_ := 0
x_ 0 := 0
1 x_ := x
x_ 1 := x
f_ (g_ h_) := f g h
@jdh30
jdh30 / Derivative
Last active February 25, 2017 10:49
Rewrite rules for simple differentiation in Mathematica syntax
D[_, _] := 0
D[x_, x_] := 1
D[f_ + g_, x_] := D[f, x] + D[g, x]
D[f_ - g_, x_] := D[f, x] - D[g, x]
D[f_ g_, x_] := f D[g, x] + g D[f, x]
D[f_ / g_, x_] := f D[1/g, x] + 1/g D[f, x]
D[f_^g_, x_] := f^(g - 1) (g D[f, x] + f Log[f] D[g, x])
D[Log[f_], x_] := D[f, x] / f
@jdh30
jdh30 / derivative.ml
Last active February 25, 2017 22:09
A symbolic derivative written in OCaml using pattern matching over code quotations
let d x = function
| <:expr< $y$ >> when x=y -> <:expr< 1 >>
| <:expr< $f$ + $g$ >> -> <:expr< $d f x$ + $d g x$ >>
| <:expr< $f$ * $g$ >> -> <:expr< $f$ * $d g x$ + $g$ * $d f x$ >>
| _ -> <:expr< 0 >>
@jdh30
jdh30 / derivative.rw
Created February 25, 2017 22:35
A symbolic derivative written in my own term rewrite language
D(x, y) -> 0
D(x, x) -> 1
D(f+g, x) -> D(f, x) + D(g, x)
D(f*g, x) -> f*D(g, x) + g*D(f, x)
@jdh30
jdh30 / ReversePolishCalculator.fs
Created March 25, 2017 13:58
Reverse polish calculator in F#, derived from this 35-line Racket metaprogramming example http://beautifulracket.com/stacker/source-listing.html
let (|Float|_|) s =
let mutable x = 0.0
if System.Double.TryParse(s, &x) then Some x else None
let binop f x y stack =
let z = f x y
printfn "%A" z
z::stack
let rec loop stack =
@jdh30
jdh30 / ReversePolishCompiler1.fs
Created March 26, 2017 00:18
Reverse polish calculator compiler in F# using quotations, derived from this 35-line Racket example http://beautifulracket.com/stacker/source-listing.html Raw
let (|Float|_|) s =
let mutable x = 0.0
if System.Double.TryParse(s, &x) then Some x else None
let rec (|Parse|) = function
| Float x::t -> <@ x @>, t
| "+"::Parse(x, Parse(y, t)) -> <@ %x + %y @>, t
| "*"::Parse(x, Parse(y, t)) -> <@ %x * %y @>, t
let (Parse(f, _)) = stdin.ReadToEnd().Split '\n' |> List.ofSeq |> List.rev
@jdh30
jdh30 / ReversePolishCompiler2.fs
Created March 26, 2017 00:34
Reverse polish compiler in F# using LLVM, derived from this 35-line Racket example http://beautifulracket.com/stacker/source-listing.html
let (|Int|_|) s =
let mutable x = 0
if System.Int32.TryParse(s, &x) then Some x else None
LLVM.NativeLibrary.LLVMDLL.Load()
LLVM.Target.InitializeNative()
let context = LLVM.Context.Global
let i32 = LLVM.IntegerType.GetInt32 context
@jdh30
jdh30 / dfs.fs
Created April 4, 2017 16:56
Imperative and purely functional depth-first searches in F#
open System.Collections.Generic
/// Imperative depth-first search
let dfs (V, E) =
let visited = HashSet(HashIdentity.Structural)
let stack = Stack[V]
while stack.Count > 0 do
let u = stack.Pop()
if not(visited.Contains u) then
for v in E u do
@jdh30
jdh30 / bf.bf
Created April 6, 2017 01:03
BF interpreter written in BF from "A very short self-interpreter" https://arxiv.org/html/cs/0311032
>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<]
<
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<<
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]
@jdh30
jdh30 / deriv.fs
Created December 26, 2017 04:21
F# code to compute the nth derivative of x^x
let timer = System.Diagnostics.Stopwatch.StartNew()
type expr =
| Int of int
| Var of string
| Add of expr * expr
| Mul of expr * expr
| Pow of expr * expr
| Ln of expr