{-# LANGUAGE Arrows #-}
module FRP.BearRiver.Integration
(
integral
, imIntegral
, trapezoidIntegral
, impulseIntegral
, count
, derivative
, iterFrom
)
where
import Control.Arrow (returnA, (***), (>>^))
import Data.VectorSpace (VectorSpace, zeroVector, (*^), (^+^), (^-^), (^/))
import Control.Monad.Trans.MSF (ask)
import Data.MonadicStreamFunction (accumulateWith, constM, iPre)
import Data.MonadicStreamFunction.InternalCore (MSF (MSF))
import FRP.BearRiver.Event (Event)
import FRP.BearRiver.Hybrid (accumBy, accumHoldBy)
import FRP.BearRiver.InternalCore (DTime, SF)
integral :: (Monad m, Fractional s, VectorSpace a s) => SF m a a
integral :: forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
SF m a a
integral = a -> SF m a a
forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
a -> SF m a a
integralFrom a
forall v a. VectorSpace v a => v
zeroVector
integralFrom :: (Monad m, Fractional s, VectorSpace a s) => a -> SF m a a
integralFrom :: forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
a -> SF m a a
integralFrom a
a0 = proc a
a -> do
DTime
dt <- ClockInfo m DTime -> MSF (ClockInfo m) () DTime
forall (m :: * -> *) b a. Monad m => m b -> MSF m a b
constM ClockInfo m DTime
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask -< ()
(a -> a -> a) -> a -> MSF (ClockInfo m) a a
forall (m :: * -> *) a s.
Monad m =>
(a -> s -> s) -> s -> MSF m a s
accumulateWith a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
(^+^) a
a0 -< DTime -> s
forall a b. (Real a, Fractional b) => a -> b
realToFrac DTime
dt s -> a -> a
forall v a. VectorSpace v a => a -> v -> v
*^ a
a
imIntegral :: (Fractional s, VectorSpace a s, Monad m)
=> a -> SF m a a
imIntegral :: forall s a (m :: * -> *).
(Fractional s, VectorSpace a s, Monad m) =>
a -> SF m a a
imIntegral = ((\a
_ a
a' DTime
dt a
v -> a
v a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
^+^ DTime -> s
forall a b. (Real a, Fractional b) => a -> b
realToFrac DTime
dt s -> a -> a
forall v a. VectorSpace v a => a -> v -> v
*^ a
a') (a -> a -> DTime -> a -> a) -> a -> SF m a a
forall (m :: * -> *) a b.
Monad m =>
(a -> a -> DTime -> b -> b) -> b -> SF m a b
`iterFrom`)
trapezoidIntegral :: (Fractional s, VectorSpace a s, Monad m) => SF m a a
trapezoidIntegral :: forall s a (m :: * -> *).
(Fractional s, VectorSpace a s, Monad m) =>
SF m a a
trapezoidIntegral =
(a -> a -> DTime -> a -> a) -> a -> SF m a a
forall (m :: * -> *) a b.
Monad m =>
(a -> a -> DTime -> b -> b) -> b -> SF m a b
iterFrom (\a
a a
a' DTime
dt a
v -> a
v a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
^+^ (DTime -> s
forall a b. (Real a, Fractional b) => a -> b
realToFrac DTime
dt s -> s -> s
forall a. Fractional a => a -> a -> a
/ s
2) s -> a -> a
forall v a. VectorSpace v a => a -> v -> v
*^ (a
a a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
^+^ a
a')) a
forall v a. VectorSpace v a => v
zeroVector
impulseIntegral :: (Fractional k, VectorSpace a k, Monad m)
=> SF m (a, Event a) a
impulseIntegral :: forall k a (m :: * -> *).
(Fractional k, VectorSpace a k, Monad m) =>
SF m (a, Event a) a
impulseIntegral = (SF m a a
forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
SF m a a
integral SF m a a
-> MSF (ClockInfo m) (Event a) a
-> MSF (ClockInfo m) (a, Event a) (a, a)
forall b c b' c'.
MSF (ClockInfo m) b c
-> MSF (ClockInfo m) b' c' -> MSF (ClockInfo m) (b, b') (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** (a -> a -> a) -> a -> MSF (ClockInfo m) (Event a) a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SF m (Event a) b
accumHoldBy a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
(^+^) a
forall v a. VectorSpace v a => v
zeroVector) MSF (ClockInfo m) (a, Event a) (a, a)
-> ((a, a) -> a) -> MSF (ClockInfo m) (a, Event a) a
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (a -> a -> a) -> (a, a) -> a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
(^+^)
count :: (Integral b, Monad m) => SF m (Event a) (Event b)
count :: forall b (m :: * -> *) a.
(Integral b, Monad m) =>
SF m (Event a) (Event b)
count = (b -> a -> b) -> b -> SF m (Event a) (Event b)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SF m (Event a) (Event b)
accumBy (\b
n a
_ -> b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1) b
0
derivative :: (Monad m, Fractional s, VectorSpace a s) => SF m a a
derivative :: forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
SF m a a
derivative = a -> SF m a a
forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
a -> SF m a a
derivativeFrom a
forall v a. VectorSpace v a => v
zeroVector
derivativeFrom :: (Monad m, Fractional s, VectorSpace a s) => a -> SF m a a
derivativeFrom :: forall (m :: * -> *) s a.
(Monad m, Fractional s, VectorSpace a s) =>
a -> SF m a a
derivativeFrom a
a0 = proc a
a -> do
DTime
dt <- ClockInfo m DTime -> MSF (ClockInfo m) () DTime
forall (m :: * -> *) b a. Monad m => m b -> MSF m a b
constM ClockInfo m DTime
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask -< ()
a
aOld <- a -> MSF (ClockInfo m) a a
forall (m :: * -> *) a. Monad m => a -> MSF m a a
iPre a
a0 -< a
a
MSF (ClockInfo m) a a
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< (a
a a -> a -> a
forall v a. VectorSpace v a => v -> v -> v
^-^ a
aOld) a -> s -> a
forall v a. VectorSpace v a => v -> a -> v
^/ DTime -> s
forall a b. (Real a, Fractional b) => a -> b
realToFrac DTime
dt
iterFrom :: Monad m => (a -> a -> DTime -> b -> b) -> b -> SF m a b
iterFrom :: forall (m :: * -> *) a b.
Monad m =>
(a -> a -> DTime -> b -> b) -> b -> SF m a b
iterFrom a -> a -> DTime -> b -> b
f b
b = (a -> ClockInfo m (b, MSF (ReaderT DTime m) a b))
-> MSF (ReaderT DTime m) a b
forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF ((a -> ClockInfo m (b, MSF (ReaderT DTime m) a b))
-> MSF (ReaderT DTime m) a b)
-> (a -> ClockInfo m (b, MSF (ReaderT DTime m) a b))
-> MSF (ReaderT DTime m) a b
forall a b. (a -> b) -> a -> b
$ \a
a -> do
DTime
dt <- ReaderT DTime m DTime
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
let b' :: b
b' = a -> a -> DTime -> b -> b
f a
a a
a DTime
dt b
b
(b, MSF (ReaderT DTime m) a b)
-> ClockInfo m (b, MSF (ReaderT DTime m) a b)
forall a. a -> ReaderT DTime m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> a -> DTime -> b -> b) -> b -> MSF (ReaderT DTime m) a b
forall (m :: * -> *) a b.
Monad m =>
(a -> a -> DTime -> b -> b) -> b -> SF m a b
iterFrom a -> a -> DTime -> b -> b
f b
b')