{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.MonadicStreamFunction.Instances.VectorSpace where
import Control.Arrow ((>>^))
import Data.VectorSpace (VectorSpace (..))
import Control.Arrow.Util (constantly, elementwise2)
import Data.MonadicStreamFunction.Core (MSF)
instance (Monad m, Eq s, Num s, VectorSpace v s, Floating (MSF m a s))
=> VectorSpace (MSF m a v) (MSF m a s)
where
zeroVector :: MSF m a v
zeroVector = forall (a :: * -> * -> *) b c. Arrow a => b -> a c b
constantly forall v a. VectorSpace v a => v
zeroVector
*^ :: MSF m a s -> MSF m a v -> MSF m a v
(*^) = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall v a. VectorSpace v a => a -> v -> v
(*^)
^/ :: MSF m a v -> MSF m a s -> MSF m a v
(^/) = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall v a. VectorSpace v a => v -> a -> v
(^/)
^+^ :: MSF m a v -> MSF m a v -> MSF m a v
(^+^) = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall v a. VectorSpace v a => v -> v -> v
(^+^)
^-^ :: MSF m a v -> MSF m a v -> MSF m a v
(^-^) = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall v a. VectorSpace v a => v -> v -> v
(^-^)
negateVector :: MSF m a v -> MSF m a v
negateVector = (forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall v a. VectorSpace v a => v -> v
negateVector)
dot :: MSF m a v -> MSF m a v -> MSF m a s
dot = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall v a. VectorSpace v a => v -> v -> a
dot
normalize :: MSF m a v -> MSF m a v
normalize MSF m a v
v = forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 forall {a} {v}. (Eq a, Num a, VectorSpace v a) => v -> a -> v
f MSF m a v
v (forall v a. VectorSpace v a => v -> a
norm MSF m a v
v)
where
f :: v -> a -> v
f v
v' a
nv'
| a
nv' forall a. Eq a => a -> a -> Bool
/= a
0 = v
v' forall v a. VectorSpace v a => v -> a -> v
^/ a
nv'
| Bool
otherwise = forall a. HasCallStack => [Char] -> a
error [Char]
"normalize: zero vector"