{-# LANGUAGE FlexibleInstances, FlexibleContexts, TypeOperators
, TypeFamilies, TypeSynonymInstances #-}
{-# OPTIONS_GHC -Wall #-}
module Data.Cross
(
HasNormal(..), normal
, One, Two, Three
, HasCross2(..), HasCross3(..)
) where
import Data.VectorSpace
import Data.MemoTrie
import Data.Basis
import Data.Derivative
class HasNormal v where normalVec :: v -> v
normal :: (HasNormal v, InnerSpace v, Floating (Scalar v)) => v -> v
normal = normalized . normalVec
type One s = s
type Two s = (s,s)
type Three s = (s,s,s)
class HasCross2 v where cross2 :: v -> v
instance AdditiveGroup u => HasCross2 (u,u) where
cross2 (x,y) = (negateV y,x)
instance (HasTrie (Basis a), HasCross2 v) => HasCross2 (a:>v) where
cross2 = fmapD cross2
instance (HasBasis s, HasTrie (Basis s), Basis s ~ ()) =>
HasNormal (One s :> Two s) where
normalVec v = cross2 (v `derivAtBasis` ())
instance (VectorSpace s, HasBasis s, HasTrie (Basis s), Basis s ~ ())
=> HasNormal (Two (One s :> s)) where
normalVec = unpairD . normalVec . pairD
class HasCross3 v where cross3 :: v -> v -> v
instance Num s => HasCross3 (s,s,s) where
(ax,ay,az) `cross3` (bx,by,bz) = ( ay * bz - az * by
, az * bx - ax * bz
, ax * by - ay * bx )
instance (HasBasis a, HasTrie (Basis a), VectorSpace v, HasCross3 v) => HasCross3 (a:>v) where
cross3 = distrib cross3
instance (Num s, HasTrie (Basis (s, s)), HasBasis s, Basis s ~ ()) =>
HasNormal (Two s :> Three s) where
normalVec v = d (Left ()) `cross3` d (Right ())
where
d = derivAtBasis v
instance ( VectorSpace s, HasBasis s, HasTrie (Basis s)
, HasNormal (Two s :> Three s) )
=> HasNormal (Three (Two s :> s)) where
normalVec = untripleD . normalVec . tripleD