fork/numbat
git clone https://git.julia.blue/fork/numbat
db3d95d
master
1let eps = 1e-10 2 3fn diff(f: Fn[(Scalar) -> Scalar], x: Scalar) -> Scalar = 4 (f(x + eps) - f(x)) / eps 5 6assert_eq(diff(log, 2.0), 1 / 2, 1e-5) 7assert_eq(diff(sin, 0.0), 1.0, 1e-5) 8 9fn f(x: Scalar) -> Scalar = x * x + 4 * x + 1 10 11assert_eq(diff(f, 2.0), 8.0, 1e-5) 12 13# TODO: It would be so cool if we could write it in a generic way: 14# 15# fn diff<X, Y>(f: Fn[(X) -> Y], x: X) -> Y / X = 16# (f(x + eps · unit_of(x)) - f(x)) / (eps · unit_of(x))