Symbolic Differentiation Symbolic Derivatives ---------------------------------------------------------------------- > module Derive ( derive ) > where ---------------------------------------------------------------------- ---------------------------------------------------------------------- > import Prim > import Expr > import Function ( Prim (..), Ident, Function (..), > constant, > minus, zero, one, mone, two, mtwo, half, mhalf ) ---------------------------------------------------------------------- Differentiation ---------------------------------------------------------------------- > derive :: Function -> Function > derive (Const c) = zero > derive (Ratio n) = zero > derive Id = one > derive (Prim f) = case f of > Sin -> Prim Cos > Cos -> minus (Prim Sin) > Tan -> Prim Cos :^: mtwo > Cot -> minus (Prim Sin :^: mtwo) > Exp -> Prim Exp > Ln -> Id :^: mone > Arcsin -> Summ [one, minus (Id :^: two)] :^: mhalf > Arccos -> minus (Summ [one, minus (Id :^: two)] :^: mhalf) > Arctan -> Summ [one, Id :^: two] :^: mone > Arccot -> minus (Summ [one, Id :^: two] :^: mone) > Sinh -> Prim Cosh > Cosh -> Prim Sinh > Tanh -> Prim Cosh :^: mtwo > Coth -> minus (Prim Sinh :^: mtwo) > Abs -> Derive 1 (Prim Abs) > derive (Fun f) = Derive 1 (Fun f) > derive (Derive n f) = Derive (n + 1) f > derive (f :.: g) = Prod [derive f :.: g, derive g] > derive (Summ fs) = Summ [ derive f | f<-fs ] > derive (Prod fs) = Summ [ Prod fs' | fs'<-rotate derive fs ] > derive (f :^: n) > | constant n = Prod [n, f :^: (Summ [n, mone]), derive f] > derive (f :^: g) = derive (Prim Exp :.: Prod [g, Prim Ln :.: f]) ---------------------------------------------------------------------- Helper functions ---------------------------------------------------------------------- > rotate f [] = [] > rotate f (a:x) = (f a:x) : [ a:x' | x'<-rotate f x ] ----------------------------------------------------------------------