Tuesday, January 3, 2012

Automatic Differentiation in Scala

Veronica has started to implement automatic differentiation in Scala, following Functional Differentiation of Computer Programs that shows how to do it in Haskell using type classes. A first version uses dual numbers to represent the value of a function and the value of the first derivative at a given point. By overloading the numeric operators and functions, the evaluation of a numeric function, including functions we define in a program, applied to a dual number gets us both results at once. To implement these ideas in Scala we followed Type Classes as Objects and Implicits. The paper shows how to use Scala to implement a hierarchy of type classes that build on previous classes and how to implement parameterized type classes, but it does not show how to combine these two. In our current implementation of dual numbers we combined both in an ad-hoc way that needs to be revised.