{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TupleSections #-}
module Language.Drasil.Chunk.DifferentialModel (
DifferentialModel(..), ODESolverFormat(..), InitialValueProblem(..),
($^^), ($*), ($+),
makeAODESolverFormat, makeAIVP, makeASystemDE, makeASingleDE,
formEquations
) where
import Control.Lens (makeLenses, (^.), view)
import Language.Drasil.Chunk.Concept (ConceptChunk, dccWDS)
import Language.Drasil.UID (HasUID(uid))
import Language.Drasil.Classes (Express(..),
ConceptDomain(..), Definition(..), Idea(..), NamedIdea(..))
import Language.Drasil.ModelExpr.Lang (ModelExpr)
import Language.Drasil.NounPhrase.Core (NP)
import Language.Drasil.Sentence (Sentence)
import Language.Drasil.Expr.Lang (Expr(..))
import Language.Drasil.Chunk.Unital (UnitalChunk)
import Language.Drasil.ModelExpr.Class (ModelExprC(nthderiv, equiv))
import Language.Drasil.Expr.Class (ExprC(..), columnVec)
import Language.Drasil.Chunk.Constrained (ConstrConcept)
import Language.Drasil.Chunk.Quantity (qw)
import Language.Drasil.Literal.Class (LiteralC(exactDbl, int))
import Data.List (find)
import Language.Drasil.WellTyped (RequiresChecking (requiredChecks))
import Language.Drasil.Space (Space, HasSpace (..))
type Unknown = Integer
data Term = T{
Term -> Expr
_coeff :: Expr,
Term -> Unknown
_unk :: Unknown
}
makeLenses ''Term
type LHS = [Term]
($^^) :: ConstrConcept -> Integer -> Unknown
$^^ :: ConstrConcept -> Unknown -> Unknown
($^^) ConstrConcept
_ Unknown
unk' = Unknown
unk'
($*) :: Expr -> Unknown -> Term
$* :: Expr -> Unknown -> Term
($*) = Expr -> Unknown -> Term
T
($+) :: [Term] -> Term -> LHS
$+ :: [Term] -> Term -> [Term]
($+) [Term]
xs Term
x = [Term]
xs forall a. [a] -> [a] -> [a]
++ [Term
x]
data DifferentialModel = SystemOfLinearODEs {
DifferentialModel -> UnitalChunk
_indepVar :: UnitalChunk,
DifferentialModel -> ConstrConcept
_depVar :: ConstrConcept,
DifferentialModel -> [[Expr]]
_coefficients :: [[Expr]],
DifferentialModel -> [Unknown]
_unknowns :: [Unknown],
DifferentialModel -> [Expr]
_dmConstants :: [Expr],
DifferentialModel -> ConceptChunk
_dmconc :: ConceptChunk
}
makeLenses ''DifferentialModel
data InitialValueProblem = IVP{
InitialValueProblem -> Expr
initTime :: Expr,
InitialValueProblem -> Expr
finalTime :: Expr,
InitialValueProblem -> [Expr]
initValues :: [Expr]
}
data ODESolverFormat = X'{
ODESolverFormat -> [[Expr]]
coeffVects :: [[Expr]],
ODESolverFormat -> [Unknown]
unknownVect :: [Integer],
ODESolverFormat -> [Expr]
constantVect :: [Expr]
}
instance HasUID DifferentialModel where uid :: Lens' DifferentialModel UID
uid = Lens' DifferentialModel ConceptChunk
dmconc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
instance Eq DifferentialModel where DifferentialModel
a == :: DifferentialModel -> DifferentialModel -> Bool
== DifferentialModel
b = (DifferentialModel
a forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (DifferentialModel
b forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid)
instance NamedIdea DifferentialModel where term :: Lens' DifferentialModel NP
term = Lens' DifferentialModel ConceptChunk
dmconc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
instance Idea DifferentialModel where getA :: DifferentialModel -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' DifferentialModel ConceptChunk
dmconc
instance Definition DifferentialModel where defn :: Lens' DifferentialModel Sentence
defn = Lens' DifferentialModel ConceptChunk
dmconc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Definition c => Lens' c Sentence
defn
instance ConceptDomain DifferentialModel where cdom :: DifferentialModel -> [UID]
cdom = forall c. ConceptDomain c => c -> [UID]
cdom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' DifferentialModel ConceptChunk
dmconc
instance Express DifferentialModel where express :: DifferentialModel -> ModelExpr
express = DifferentialModel -> ModelExpr
formStdODE
instance RequiresChecking DifferentialModel Expr Space where
requiredChecks :: DifferentialModel -> [(Expr, Space)]
requiredChecks DifferentialModel
dmo = forall a b. (a -> b) -> [a] -> [b]
map (, DifferentialModel
dmo forall s a. s -> Getting a s a -> a
^. (Lens' DifferentialModel ConstrConcept
depVar forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ)) forall a b. (a -> b) -> a -> b
$ [[Expr]] -> [Unknown] -> [Expr] -> ConstrConcept -> [Expr]
formEquations (ODESolverFormat -> [[Expr]]
coeffVects ODESolverFormat
dm) (ODESolverFormat -> [Unknown]
unknownVect ODESolverFormat
dm) (ODESolverFormat -> [Expr]
constantVect ODESolverFormat
dm) (DifferentialModel -> ConstrConcept
_depVar DifferentialModel
dmo)
where dm :: ODESolverFormat
dm = DifferentialModel -> ODESolverFormat
makeAODESolverFormat DifferentialModel
dmo
formStdODE :: DifferentialModel -> ModelExpr
formStdODE :: DifferentialModel -> ModelExpr
formStdODE DifferentialModel
d
| Int
size forall a. Eq a => a -> a -> Bool
== Int
1 = [Expr] -> [ModelExpr] -> [Expr] -> ModelExpr
formASingleODE (forall a. [a] -> a
head (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [[Expr]]
coefficients)) [ModelExpr]
unknownVec (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [Expr]
dmConstants)
| Bool
otherwise = forall r. ModelExprC r => [r] -> r
equiv (ModelExpr
coeffsMatix forall r. ExprC r => r -> r -> r
$. forall r. ExprC r => [r] -> r
columnVec [ModelExpr]
unknownVec forall a. a -> [a] -> [a]
: [ModelExpr]
constantVec)
where size :: Int
size = forall (t :: * -> *) a. Foldable t => t a -> Int
length (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [[Expr]]
coefficients)
coeffsMatix :: ModelExpr
coeffsMatix = forall c. Express c => c -> ModelExpr
express(forall r. ExprC r => [[r]] -> r
matrix (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [[Expr]]
coefficients))
unknownVec :: [ModelExpr]
unknownVec = [Unknown] -> ConstrConcept -> UnitalChunk -> [ModelExpr]
formAllUnknown (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [Unknown]
unknowns) (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel ConstrConcept
depVar) (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel UnitalChunk
indepVar)
constantVec :: [ModelExpr]
constantVec = [forall c. Express c => c -> ModelExpr
express (forall r. ExprC r => [r] -> r
columnVec (DifferentialModel
d forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [Expr]
dmConstants))]
formASingleODE :: [Expr] -> [ModelExpr] -> [Expr] -> ModelExpr
formASingleODE :: [Expr] -> [ModelExpr] -> [Expr] -> ModelExpr
formASingleODE [Expr]
coeffs [ModelExpr]
unks [Expr]
consts = forall r. ModelExprC r => [r] -> r
equiv (ModelExpr
lhs forall a. a -> [a] -> [a]
: [ModelExpr]
rhs)
where lhs :: ModelExpr
lhs = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 forall r. ExprC r => r -> r -> r
addRe (forall a b. (a -> b) -> [a] -> [b]
map (\(Expr, ModelExpr)
x-> forall c. Express c => c -> ModelExpr
express (forall a b. (a, b) -> a
fst (Expr, ModelExpr)
x) forall r. ExprC r => r -> r -> r
`mulRe` forall a b. (a, b) -> b
snd (Expr, ModelExpr)
x) forall a b. (a -> b) -> a -> b
$ [Expr] -> [ModelExpr] -> [(Expr, ModelExpr)]
filterZeroCoeff [Expr]
coeffs [ModelExpr]
unks)
rhs :: [ModelExpr]
rhs = forall a b. (a -> b) -> [a] -> [b]
map forall c. Express c => c -> ModelExpr
express [Expr]
consts
filterZeroCoeff :: [Expr] -> [ModelExpr] -> [(Expr, ModelExpr)]
filterZeroCoeff :: [Expr] -> [ModelExpr] -> [(Expr, ModelExpr)]
filterZeroCoeff [Expr]
es [ModelExpr]
mes = forall a. (a -> Bool) -> [a] -> [a]
filter (\(Expr, ModelExpr)
x -> forall a b. (a, b) -> a
fst (Expr, ModelExpr)
x forall a. Eq a => a -> a -> Bool
/= forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0) forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip [Expr]
es [ModelExpr]
mes
formAllUnknown :: [Unknown] -> ConstrConcept -> UnitalChunk -> [ModelExpr]
formAllUnknown :: [Unknown] -> ConstrConcept -> UnitalChunk -> [ModelExpr]
formAllUnknown [Unknown]
unks ConstrConcept
dep UnitalChunk
ind = forall a b. (a -> b) -> [a] -> [b]
map (\Unknown
x -> Unknown -> ConstrConcept -> UnitalChunk -> ModelExpr
formAUnknown Unknown
x ConstrConcept
dep UnitalChunk
ind) [Unknown]
unks
formAUnknown :: Unknown -> ConstrConcept-> UnitalChunk -> ModelExpr
formAUnknown :: Unknown -> ConstrConcept -> UnitalChunk -> ModelExpr
formAUnknown Unknown
unk'' ConstrConcept
dep = forall r c.
(ModelExprC r, HasUID c, HasSymbol c) =>
Unknown -> r -> c -> r
nthderiv (forall a. Integral a => a -> Unknown
toInteger Unknown
unk'') (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw ConstrConcept
dep))
makeASystemDE :: UnitalChunk -> ConstrConcept -> [[Expr]] -> [Unknown] -> [Expr]-> String -> NP -> Sentence -> DifferentialModel
makeASystemDE :: UnitalChunk
-> ConstrConcept
-> [[Expr]]
-> [Unknown]
-> [Expr]
-> String
-> NP
-> Sentence
-> DifferentialModel
makeASystemDE UnitalChunk
indepVar' ConstrConcept
depVar' [[Expr]]
coeffs [Unknown]
unks [Expr]
const' String
id' NP
term' Sentence
defn'
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Expr]]
coeffs forall a. Eq a => a -> a -> Bool
/= forall (t :: * -> *) a. Foldable t => t a -> Int
length [Expr]
const' =
forall a. HasCallStack => String -> a
error String
"Length of coefficients matrix should equal to the length of the constant vector"
| Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ [[Expr]] -> [Unknown] -> Bool
isCoeffsMatchUnknowns [[Expr]]
coeffs [Unknown]
unks =
forall a. HasCallStack => String -> a
error String
"The length of each row vector in coefficients need to equal to the length of unknowns vector"
| Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ [Unknown] -> Bool
isUnknownDescending [Unknown]
unks =
forall a. HasCallStack => String -> a
error String
"The order of giving unknowns need to be descending"
| Bool
otherwise = UnitalChunk
-> ConstrConcept
-> [[Expr]]
-> [Unknown]
-> [Expr]
-> ConceptChunk
-> DifferentialModel
SystemOfLinearODEs UnitalChunk
indepVar' ConstrConcept
depVar' [[Expr]]
coeffs [Unknown]
unks [Expr]
const'(String -> NP -> Sentence -> ConceptChunk
dccWDS String
id' NP
term' Sentence
defn')
makeASingleDE :: UnitalChunk -> ConstrConcept -> LHS -> Expr-> String -> NP -> Sentence -> DifferentialModel
makeASingleDE :: UnitalChunk
-> ConstrConcept
-> [Term]
-> Expr
-> String
-> NP
-> Sentence
-> DifferentialModel
makeASingleDE UnitalChunk
indepVar'' ConstrConcept
depVar'' [Term]
lhs Expr
const'' String
id'' NP
term'' Sentence
defn''
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Expr]]
coeffs forall a. Eq a => a -> a -> Bool
/= forall (t :: * -> *) a. Foldable t => t a -> Int
length [Expr
const''] =
forall a. HasCallStack => String -> a
error String
"Length of coefficients matrix should equal to the length of the constant vector"
| Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ [[Expr]] -> [Unknown] -> Bool
isCoeffsMatchUnknowns [[Expr]]
coeffs [Unknown]
unks =
forall a. HasCallStack => String -> a
error String
"The length of each row vector in coefficients need to equal to the length of unknowns vector"
| Bool
otherwise = UnitalChunk
-> ConstrConcept
-> [[Expr]]
-> [Unknown]
-> [Expr]
-> ConceptChunk
-> DifferentialModel
SystemOfLinearODEs UnitalChunk
indepVar'' ConstrConcept
depVar'' [[Expr]]
coeffs [Unknown]
unks [Expr
const''](String -> NP -> Sentence -> ConceptChunk
dccWDS String
id'' NP
term'' Sentence
defn'')
where unks :: [Unknown]
unks = Unknown -> ConstrConcept -> [Unknown]
createAllUnknowns([Term] -> Term
findHighestOrder [Term]
lhs forall s a. s -> Getting a s a -> a
^. Lens' Term Unknown
unk) ConstrConcept
depVar''
coeffs :: [[Expr]]
coeffs = [[Term] -> [Unknown] -> [Expr]
createCoefficients [Term]
lhs [Unknown]
unks]
isCoeffsMatchUnknowns :: [[Expr]] -> [Unknown] -> Bool
isCoeffsMatchUnknowns :: [[Expr]] -> [Unknown] -> Bool
isCoeffsMatchUnknowns [] [Unknown]
_ = forall a. HasCallStack => String -> a
error String
"Coefficients matrix can not be empty"
isCoeffsMatchUnknowns [[Expr]]
_ [] = forall a. HasCallStack => String -> a
error String
"Unknowns column vector can not be empty"
isCoeffsMatchUnknowns [[Expr]]
coeffs [Unknown]
unks = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ [Expr]
x -> Bool -> Bool -> Bool
(&&) (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Expr]
x forall a. Eq a => a -> a -> Bool
== forall (t :: * -> *) a. Foldable t => t a -> Int
length [Unknown]
unks)) Bool
True [[Expr]]
coeffs
isUnknownDescending :: [Unknown] -> Bool
isUnknownDescending :: [Unknown] -> Bool
isUnknownDescending [] = Bool
True
isUnknownDescending [Unknown
_] = Bool
True
isUnknownDescending (Unknown
x:Unknown
y:[Unknown]
xs) = Unknown
x forall a. Ord a => a -> a -> Bool
> Unknown
y Bool -> Bool -> Bool
&& [Unknown] -> Bool
isUnknownDescending [Unknown]
xs
findHighestOrder :: LHS -> Term
findHighestOrder :: [Term] -> Term
findHighestOrder = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\Term
x Term
y -> if Term
x forall s a. s -> Getting a s a -> a
^. Lens' Term Unknown
unk forall a. Ord a => a -> a -> Bool
>= Term
y forall s a. s -> Getting a s a -> a
^. Lens' Term Unknown
unk then Term
x else Term
y)
createAllUnknowns :: Unknown -> ConstrConcept -> [Unknown]
createAllUnknowns :: Unknown -> ConstrConcept -> [Unknown]
createAllUnknowns Unknown
highestUnk ConstrConcept
depv
| Unknown
highestUnk forall a. Eq a => a -> a -> Bool
== Unknown
0 = [Unknown
highestUnk]
| Bool
otherwise = Unknown
highestUnk forall a. a -> [a] -> [a]
: Unknown -> ConstrConcept -> [Unknown]
createAllUnknowns (Unknown
highestUnk forall a. Num a => a -> a -> a
- Unknown
1) ConstrConcept
depv
createCoefficients :: LHS -> [Unknown] -> [Expr]
createCoefficients :: [Term] -> [Unknown] -> [Expr]
createCoefficients [] [Unknown]
_ = forall a. HasCallStack => String -> a
error String
"Left hand side is an empty list"
createCoefficients [Term]
_ [] = []
createCoefficients [Term]
lhs (Unknown
x:[Unknown]
xs) = Maybe Term -> Expr
genCoefficient (Unknown -> [Term] -> Maybe Term
findCoefficient Unknown
x [Term]
lhs) forall a. a -> [a] -> [a]
: [Term] -> [Unknown] -> [Expr]
createCoefficients [Term]
lhs [Unknown]
xs
genCoefficient :: Maybe Term -> Expr
genCoefficient :: Maybe Term -> Expr
genCoefficient Maybe Term
Nothing = forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0
genCoefficient (Just Term
x) = Term
x forall s a. s -> Getting a s a -> a
^. Lens' Term Expr
coeff
findCoefficient :: Unknown -> LHS -> Maybe Term
findCoefficient :: Unknown -> [Term] -> Maybe Term
findCoefficient Unknown
u = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find(\Term
x -> Term
x forall s a. s -> Getting a s a -> a
^. Lens' Term Unknown
unk forall a. Eq a => a -> a -> Bool
== Unknown
u)
transUnknowns :: [Unknown] -> [Unknown]
transUnknowns :: [Unknown] -> [Unknown]
transUnknowns = forall a. [a] -> [a]
tail
transCoefficients :: [Expr] -> [Expr]
transCoefficients :: [Expr] -> [Expr]
transCoefficients [Expr]
es
| forall a. [a] -> a
head [Expr]
es forall a. Eq a => a -> a -> Bool
== forall r. LiteralC r => Unknown -> r
exactDbl Unknown
1 = [Expr] -> [Expr]
mapNeg forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail [Expr]
es
| Bool
otherwise = [Expr] -> [Expr]
mapNeg forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall r. ExprC r => r -> r -> r
$/ forall a. [a] -> a
head [Expr]
es) [Expr]
es
where mapNeg :: [Expr] -> [Expr]
mapNeg = forall a b. (a -> b) -> [a] -> [b]
map (\Expr
x -> if Expr
x forall a. Eq a => a -> a -> Bool
== forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0 then forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0 else forall r. ExprC r => r -> r
neg Expr
x)
addIdentityCoeffs :: [[Expr]] -> Int -> Int -> [[Expr]]
addIdentityCoeffs :: [[Expr]] -> Int -> Int -> [[Expr]]
addIdentityCoeffs [[Expr]]
es Int
len Int
index
| Int
len forall a. Eq a => a -> a -> Bool
== Int
index forall a. Num a => a -> a -> a
+ Int
1 = [[Expr]]
es
| Bool
otherwise = [[Expr]] -> Int -> Int -> [[Expr]]
addIdentityCoeffs (Int -> Int -> [Expr]
constIdentityRowVect Int
len Int
index forall a. a -> [a] -> [a]
: [[Expr]]
es) Int
len (Int
index forall a. Num a => a -> a -> a
+ Int
1)
constIdentityRowVect :: Int -> Int -> [Expr]
constIdentityRowVect :: Int -> Int -> [Expr]
constIdentityRowVect Int
len Int
index = Int -> [Expr] -> [Expr]
addIdentityValue Int
index forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate Int
len forall a b. (a -> b) -> a -> b
$ forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0
addIdentityValue :: Int -> [Expr] -> [Expr]
addIdentityValue :: Int -> [Expr] -> [Expr]
addIdentityValue Int
n [Expr]
es = forall a b. (a, b) -> a
fst ([Expr], [Expr])
splits forall a. [a] -> [a] -> [a]
++ [forall r. LiteralC r => Unknown -> r
exactDbl Unknown
1] forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [a]
tail (forall a b. (a, b) -> b
snd ([Expr], [Expr])
splits)
where splits :: ([Expr], [Expr])
splits = forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [Expr]
es
addIdentityConsts :: [Expr] -> Int -> [Expr]
addIdentityConsts :: [Expr] -> Int -> [Expr]
addIdentityConsts [Expr]
expr Int
len = forall a. Int -> a -> [a]
replicate (Int
len forall a. Num a => a -> a -> a
- Int
1) (forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0) forall a. [a] -> [a] -> [a]
++ [Expr]
expr
divideConstant :: Expr -> Expr -> Expr
divideConstant :: Expr -> Expr -> Expr
divideConstant Expr
a Expr
b
| Expr
b forall a. Eq a => a -> a -> Bool
== forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0 = forall a. HasCallStack => String -> a
error String
"Divisor can't be zero"
| Expr
b forall a. Eq a => a -> a -> Bool
== forall r. LiteralC r => Unknown -> r
exactDbl Unknown
1 = Expr
a
| Bool
otherwise = Expr
a forall r. ExprC r => r -> r -> r
$/ Expr
b
makeAODESolverFormat :: DifferentialModel -> ODESolverFormat
makeAODESolverFormat :: DifferentialModel -> ODESolverFormat
makeAODESolverFormat DifferentialModel
dm = [[Expr]] -> [Unknown] -> [Expr] -> ODESolverFormat
X' [[Expr]]
transEs [Unknown]
transUnks [Expr]
transConsts
where transUnks :: [Unknown]
transUnks = [Unknown] -> [Unknown]
transUnknowns forall a b. (a -> b) -> a -> b
$ DifferentialModel
dm forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [Unknown]
unknowns
transEs :: [[Expr]]
transEs = [[Expr]] -> Int -> Int -> [[Expr]]
addIdentityCoeffs [[Expr] -> [Expr]
transCoefficients forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head (DifferentialModel
dm forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [[Expr]]
coefficients)] (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Unknown]
transUnks) Int
0
transConsts :: [Expr]
transConsts = [Expr] -> Int -> [Expr]
addIdentityConsts [forall a. [a] -> a
head (DifferentialModel
dm forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [Expr]
dmConstants) Expr -> Expr -> Expr
`divideConstant` forall a. [a] -> a
head (forall a. [a] -> a
head (DifferentialModel
dm forall s a. s -> Getting a s a -> a
^. Lens' DifferentialModel [[Expr]]
coefficients))] (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Unknown]
transUnks)
formEquations :: [[Expr]] -> [Unknown] -> [Expr] -> ConstrConcept-> [Expr]
formEquations :: [[Expr]] -> [Unknown] -> [Expr] -> ConstrConcept -> [Expr]
formEquations [] [Unknown]
_ [Expr]
_ ConstrConcept
_ = []
formEquations [[Expr]]
_ [] [Expr]
_ ConstrConcept
_ = []
formEquations [[Expr]]
_ [Unknown]
_ [] ConstrConcept
_ = []
formEquations ([Expr]
ex:[[Expr]]
exs) [Unknown]
unks (Expr
y:[Expr]
ys) ConstrConcept
depVa =
(if Expr
y forall a. Eq a => a -> a -> Bool
== forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0 then Expr
finalExpr else Expr
finalExpr forall r. ExprC r => r -> r -> r
`addRe` Expr
y) forall a. a -> [a] -> [a]
: [[Expr]] -> [Unknown] -> [Expr] -> ConstrConcept -> [Expr]
formEquations [[Expr]]
exs [Unknown]
unks [Expr]
ys ConstrConcept
depVa
where indexUnks :: [Expr]
indexUnks = forall a b. (a -> b) -> [a] -> [b]
map (forall r. ExprC r => r -> r -> r
idx (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy ConstrConcept
depVa) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. LiteralC r => Unknown -> r
int) [Unknown]
unks
filteredExprs :: [(Expr, Expr)]
filteredExprs = forall a. (a -> Bool) -> [a] -> [a]
filter (\(Expr, Expr)
x -> forall a b. (a, b) -> a
fst (Expr, Expr)
x forall a. Eq a => a -> a -> Bool
/= forall r. LiteralC r => Unknown -> r
exactDbl Unknown
0) (forall a b. [a] -> [b] -> [(a, b)]
zip [Expr]
ex [Expr]
indexUnks)
termExprs :: [Expr]
termExprs = forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall r. ExprC r => r -> r -> r
mulRe) [(Expr, Expr)]
filteredExprs
finalExpr :: Expr
finalExpr = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 forall r. ExprC r => r -> r -> r
addRe [Expr]
termExprs
makeAIVP :: Expr -> Expr -> [Expr] -> InitialValueProblem
makeAIVP :: Expr -> Expr -> [Expr] -> InitialValueProblem
makeAIVP = Expr -> Expr -> [Expr] -> InitialValueProblem
IVP