module Symtegration.Integration.Parts (integrate) where
import Control.Applicative (asum, (<|>))
import Data.Text (Text)
import Symtegration.Differentiation
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
x :*: Expression
y) = [Text -> Expression -> Maybe Expression]
-> Text -> Expression -> Expression -> Maybe Expression
integrate' [Text -> Expression -> Maybe Expression]
fs Text
v Expression
x Expression
y Maybe Expression -> Maybe Expression -> Maybe Expression
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Text -> Expression -> Maybe Expression]
-> Text -> Expression -> Expression -> Maybe Expression
integrate' [Text -> Expression -> Maybe Expression]
fs Text
v Expression
y Expression
x
integrate [Text -> Expression -> Maybe Expression]
_ Text
_ Expression
_ = Maybe Expression
forall a. Maybe a
Nothing
integrate' ::
[Text -> Expression -> Maybe Expression] ->
Text ->
Expression ->
Expression ->
Maybe Expression
integrate' :: [Text -> Expression -> Maybe Expression]
-> Text -> Expression -> Expression -> Maybe Expression
integrate' [Text -> Expression -> Maybe Expression]
fs Text
v Expression
x Expression
y = do
ix <- Expression -> Maybe Expression
integrate'' Expression
x
iixdy <- integrate'' $ simplifyForVariable v $ ix * differentiate v y
return $ ix * y - iixdy
where
integrate'' :: Expression -> Maybe Expression
integrate'' Expression
z = [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 -> Text -> Expression -> Maybe Expression
f Text
v Expression
z) [Text -> Expression -> Maybe Expression]
fs