module Symtegration.Differentiation (differentiate) where
import Data.Text (Text)
import Numeric.AD.Rank1.Forward
import Symtegration.Symbolic
import Symtegration.Symbolic.Simplify
differentiate ::
Text ->
Expression ->
Expression
differentiate :: Text -> Expression -> Expression
differentiate Text
v Expression
e = Expression -> Expression
tidy (Expression -> Expression) -> Expression -> Expression
forall a b. (a -> b) -> a -> b
$ Text -> Expression -> Expression
simplifyForVariable Text
v (Expression -> Expression) -> Expression -> Expression
forall a b. (a -> b) -> a -> b
$ (Forward Expression -> Forward Expression)
-> Expression -> Expression
forall a. Num a => (Forward a -> Forward a) -> a -> a
diff Forward Expression -> Forward Expression
f (Expression -> Expression) -> Expression -> Expression
forall a b. (a -> b) -> a -> b
$ Text -> Expression
Symbol Text
v
where
f :: Forward Expression -> Forward Expression
f = Expression
-> (Text -> Forward Expression -> Forward Expression)
-> Forward Expression
-> Forward Expression
forall b a. Floating b => Expression -> (Text -> a -> b) -> a -> b
toFunction Expression
e Text -> Forward Expression -> Forward Expression
forall {a}. (Scalar a ~ Expression, Mode a) => Text -> a -> a
assign
assign :: Text -> a -> a
assign Text
x
| Text
v Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
x = a -> a
forall a. a -> a
id
| Bool
otherwise = a -> a -> a
forall a b. a -> b -> a
const (a -> a -> a) -> a -> a -> a
forall a b. (a -> b) -> a -> b
$ Scalar a -> a
forall t. Mode t => Scalar t -> t
auto (Scalar a -> a) -> Scalar a -> a
forall a b. (a -> b) -> a -> b
$ Text -> Expression
Symbol Text
x