Search code examples
scalamathsicpsymbolic-math

symbolic differentiation in Scala


I want to make a symbolic differentiation function in Scala using its pattern matching like done in SICP. I'd want to be able to write something like this:

differentiate(exp) = exp match
{
  case + => 
  case * =>
}

Is this possible in Scala on 'native' expressions?


Solution

  • Every examples I've seen before involve an expression tree. You can build it easily in Scala with case classes. For instance, a rough sketch involving pattern matching and object-oriented style:

    trait Exp {
      def differentiate: Exp
    }
    case class Const( value: Double ) extends Exp {
      def differentiate = Const(0)
    }
    case class Var( label: String, power: Double ) extends Exp {
      def differentiate = this match {
        case Var(l,0.0) => Const(0)
        case Var(l,p) => Mul( Const(p), Var(l,p-1) )
      }
    }
    case class Add( left: Exp, right: Exp ) extends Exp {
      def differentiate = Add( left.differentiate, right.differentiate )
    }
    case class Mult( left: Exp, right: Exp ) extends Exp {
      def differentiate = ( left, right ) match {
        case ( Const(c), exp ) => Mul( Const(c), exp.differentiate )
        case ( exp, Const(c) ) => Mul( Const(c), exp.differentiate )
        case (e1, e2) => Add( Mul( e1.differentiate, e2), Mul( e1, e2.differentiate ) )
      }
    }