Automatic Differentiation in 38 lines of Haskell
gist.github.com
Automatic Differentiation in 38 lines of Haskell
1–10 of 60 posts
Re: Automatic Differentiation in 38 lines of Haskell
#2Re: Automatic Differentiation in 38 lines of Haskell
#3Re: Automatic Differentiation in 38 lines of Haskell
#4It's a neat design pattern. I bet it'd work in Julia too.
Re: Automatic Differentiation in 38 lines of Haskell
#5Curious: I don't see the `^` op defined, or is it translated inti `exp` im guessing?
Re: Automatic Differentiation in 38 lines of Haskell
#6Being purely functional makes this quite easy, still a beauty to see
If you did (f 'x) for instance, you'd end up with things like (* 2 'x) which would blow up, since Lisp would try to compute the answer instead giving you '(* 2 x) back.
Re: Automatic Differentiation in 38 lines of Haskell
#7Curious: I don't see the `^` op defined, or is it translated inti `exp` im guessing?
EDIT: As for the differentiation, it works for ^ since it is just multiplication (https://hackage.haskell.org/package/base-4.17.0.0/docs/src/G...) for which the derivative was defined using the product rule.
[1]: https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelu... [2]: https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelu...
Re: Automatic Differentiation in 38 lines of Haskell
#8Being purely functional makes this quite easy, still a beauty to see
it's not just the functionalness. you couldn't do it this way in Lisp/Scheme (I think?) because of the lack of multiple dispatch. If you did (f 'x) for instance, you'd end up with things like (* 2 'x) which would blow up, since Lisp would try to compute the answer instead giving you '(* 2 x) back.
Re: Automatic Differentiation in 38 lines of Haskell
#9Curious: I don't see the `^` op defined, or is it translated inti `exp` im guessing?
The neat thing with this approach is that ^ works for any numeric type, including user-defined types like Dual in this example. Since the Dual type can handle calculating derivatives for *, it gets derivatives for ^ for free.
[1]: https://hackage.haskell.org/package/base-4.17.0.0/docs/src/G...
Re: Automatic Differentiation in 38 lines of Haskell
#10This is an interesting approach. Haskell is not a symbolic language, but you take advantage of the abstractness of type parameters in function definitions to thread your implementation of "D x" through, and pattern match on that. It's a neat design pattern. I bet it'd work in Julia too.