module Symtegration.Integration.Term (integrate) where
import Data.Foldable (asum)
import Data.Text (Text)
import Symtegration.Integration.Factor
import Symtegration.Symbolic
import Symtegration.Symbolic.Simplify
integrate ::
[Text -> Expression -> Maybe Expression] ->
Text ->
Expression ->
Maybe Expression
integrate :: [Text -> Expression -> Maybe Expression]
-> Text -> Expression -> Maybe Expression
integrate [Text -> Expression -> Maybe Expression]
fs Text
v Expression
e = [Maybe Expression] -> Maybe Expression
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ([Maybe Expression] -> Maybe Expression)
-> [Maybe Expression] -> Maybe Expression
forall a b. (a -> b) -> a -> b
$ ((Text -> Expression -> Maybe Expression) -> Maybe Expression)
-> [Text -> Expression -> Maybe Expression] -> [Maybe Expression]
forall a b. (a -> b) -> [a] -> [b]
map (\Text -> Expression -> Maybe Expression
f -> Expression -> Expression -> Expression
(:*:) Expression
c (Expression -> Expression) -> Maybe Expression -> Maybe Expression
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Expression -> Maybe Expression
f Text
v Expression
u) [Text -> Expression -> Maybe Expression]
fs
where
e' :: Expression
e' = Text -> Expression -> Expression
simplifyForVariable Text
v Expression
e
(Expression
c, Expression
u) = Text -> Expression -> (Expression, Expression)
factor Text
v Expression
e'