{-# LANGUAGE FunctionalDependencies, FlexibleInstances #-}
{-# LANGUAGE DefaultSignatures #-}
module Data.VectorSpace where
infixr *^
infixl ^/
infix 7 `dot`
infixl 6 ^+^, ^-^
class VectorSpace v a | v -> a where
zeroVector :: v
(*^) :: a -> v -> v
(^/) :: v -> a -> v
default (^/) :: Fractional a => v -> a -> v
v
v ^/ a
a = (a
1forall a. Fractional a => a -> a -> a
/a
a) forall v a. VectorSpace v a => a -> v -> v
*^ v
v
(^+^) :: v -> v -> v
(^-^) :: v -> v -> v
v
v1 ^-^ v
v2 = v
v1 forall v a. VectorSpace v a => v -> v -> v
^+^ forall v a. VectorSpace v a => v -> v
negateVector v
v2
negateVector :: v -> v
default negateVector :: Num a => v -> v
negateVector v
v = (-a
1) forall v a. VectorSpace v a => a -> v -> v
*^ v
v
dot :: v -> v -> a
norm :: v -> a
default norm :: Floating a => v -> a
norm v
v = forall a. Floating a => a -> a
sqrt (v
v forall v a. VectorSpace v a => v -> v -> a
`dot` v
v)
normalize :: v -> v
default normalize :: (Eq a, Floating a) => v -> v
normalize v
v = if a
nv forall a. Eq a => a -> a -> Bool
/= a
0 then v
v forall v a. VectorSpace v a => v -> a -> v
^/ a
nv else forall a. HasCallStack => [Char] -> a
error [Char]
"normalize: zero vector"
where nv :: a
nv = forall v a. VectorSpace v a => v -> a
norm v
v
instance VectorSpace Float Float where
zeroVector :: Float
zeroVector = Float
0
Float
a *^ :: Float -> Float -> Float
*^ Float
x = Float
a forall a. Num a => a -> a -> a
* Float
x
Float
x ^/ :: Float -> Float -> Float
^/ Float
a = Float
x forall a. Fractional a => a -> a -> a
/ Float
a
negateVector :: Float -> Float
negateVector Float
x = (-Float
x)
Float
x1 ^+^ :: Float -> Float -> Float
^+^ Float
x2 = Float
x1 forall a. Num a => a -> a -> a
+ Float
x2
Float
x1 ^-^ :: Float -> Float -> Float
^-^ Float
x2 = Float
x1 forall a. Num a => a -> a -> a
- Float
x2
Float
x1 dot :: Float -> Float -> Float
`dot` Float
x2 = Float
x1 forall a. Num a => a -> a -> a
* Float
x2
instance VectorSpace Double Double where
zeroVector :: Double
zeroVector = Double
0
Double
a *^ :: Double -> Double -> Double
*^ Double
x = Double
a forall a. Num a => a -> a -> a
* Double
x
Double
x ^/ :: Double -> Double -> Double
^/ Double
a = Double
x forall a. Fractional a => a -> a -> a
/ Double
a
negateVector :: Double -> Double
negateVector Double
x = (-Double
x)
Double
x1 ^+^ :: Double -> Double -> Double
^+^ Double
x2 = Double
x1 forall a. Num a => a -> a -> a
+ Double
x2
Double
x1 ^-^ :: Double -> Double -> Double
^-^ Double
x2 = Double
x1 forall a. Num a => a -> a -> a
- Double
x2
Double
x1 dot :: Double -> Double -> Double
`dot` Double
x2 = Double
x1 forall a. Num a => a -> a -> a
* Double
x2
instance (Eq a, Floating a) => VectorSpace (a,a) a where
zeroVector :: (a, a)
zeroVector = (a
0,a
0)
a
a *^ :: a -> (a, a) -> (a, a)
*^ (a
x,a
y) = (a
a forall a. Num a => a -> a -> a
* a
x, a
a forall a. Num a => a -> a -> a
* a
y)
(a
x,a
y) ^/ :: (a, a) -> a -> (a, a)
^/ a
a = (a
x forall a. Fractional a => a -> a -> a
/ a
a, a
y forall a. Fractional a => a -> a -> a
/ a
a)
negateVector :: (a, a) -> (a, a)
negateVector (a
x,a
y) = (-a
x, -a
y)
(a
x1,a
y1) ^+^ :: (a, a) -> (a, a) -> (a, a)
^+^ (a
x2,a
y2) = (a
x1 forall a. Num a => a -> a -> a
+ a
x2, a
y1 forall a. Num a => a -> a -> a
+ a
y2)
(a
x1,a
y1) ^-^ :: (a, a) -> (a, a) -> (a, a)
^-^ (a
x2,a
y2) = (a
x1 forall a. Num a => a -> a -> a
- a
x2, a
y1 forall a. Num a => a -> a -> a
- a
y2)
(a
x1,a
y1) dot :: (a, a) -> (a, a) -> a
`dot` (a
x2,a
y2) = a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2
instance (Eq a, Floating a) => VectorSpace (a,a,a) a where
zeroVector :: (a, a, a)
zeroVector = (a
0,a
0,a
0)
a
a *^ :: a -> (a, a, a) -> (a, a, a)
*^ (a
x,a
y,a
z) = (a
a forall a. Num a => a -> a -> a
* a
x, a
a forall a. Num a => a -> a -> a
* a
y, a
a forall a. Num a => a -> a -> a
* a
z)
(a
x,a
y,a
z) ^/ :: (a, a, a) -> a -> (a, a, a)
^/ a
a = (a
x forall a. Fractional a => a -> a -> a
/ a
a, a
y forall a. Fractional a => a -> a -> a
/ a
a, a
z forall a. Fractional a => a -> a -> a
/ a
a)
negateVector :: (a, a, a) -> (a, a, a)
negateVector (a
x,a
y,a
z) = (-a
x, -a
y, -a
z)
(a
x1,a
y1,a
z1) ^+^ :: (a, a, a) -> (a, a, a) -> (a, a, a)
^+^ (a
x2,a
y2,a
z2) = (a
x1forall a. Num a => a -> a -> a
+a
x2, a
y1forall a. Num a => a -> a -> a
+a
y2, a
z1forall a. Num a => a -> a -> a
+a
z2)
(a
x1,a
y1,a
z1) ^-^ :: (a, a, a) -> (a, a, a) -> (a, a, a)
^-^ (a
x2,a
y2,a
z2) = (a
x1forall a. Num a => a -> a -> a
-a
x2, a
y1forall a. Num a => a -> a -> a
-a
y2, a
z1forall a. Num a => a -> a -> a
-a
z2)
(a
x1,a
y1,a
z1) dot :: (a, a, a) -> (a, a, a) -> a
`dot` (a
x2,a
y2,a
z2) = a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2 forall a. Num a => a -> a -> a
+ a
z1 forall a. Num a => a -> a -> a
* a
z2
instance (Eq a, Floating a) => VectorSpace (a,a,a,a) a where
zeroVector :: (a, a, a, a)
zeroVector = (a
0,a
0,a
0,a
0)
a
a *^ :: a -> (a, a, a, a) -> (a, a, a, a)
*^ (a
x,a
y,a
z,a
u) = (a
a forall a. Num a => a -> a -> a
* a
x, a
a forall a. Num a => a -> a -> a
* a
y, a
a forall a. Num a => a -> a -> a
* a
z, a
a forall a. Num a => a -> a -> a
* a
u)
(a
x,a
y,a
z,a
u) ^/ :: (a, a, a, a) -> a -> (a, a, a, a)
^/ a
a = (a
x forall a. Fractional a => a -> a -> a
/ a
a, a
y forall a. Fractional a => a -> a -> a
/ a
a, a
z forall a. Fractional a => a -> a -> a
/ a
a, a
u forall a. Fractional a => a -> a -> a
/ a
a)
negateVector :: (a, a, a, a) -> (a, a, a, a)
negateVector (a
x,a
y,a
z,a
u) = (-a
x, -a
y, -a
z, -a
u)
(a
x1,a
y1,a
z1,a
u1) ^+^ :: (a, a, a, a) -> (a, a, a, a) -> (a, a, a, a)
^+^ (a
x2,a
y2,a
z2,a
u2) = (a
x1forall a. Num a => a -> a -> a
+a
x2, a
y1forall a. Num a => a -> a -> a
+a
y2, a
z1forall a. Num a => a -> a -> a
+a
z2, a
u1forall a. Num a => a -> a -> a
+a
u2)
(a
x1,a
y1,a
z1,a
u1) ^-^ :: (a, a, a, a) -> (a, a, a, a) -> (a, a, a, a)
^-^ (a
x2,a
y2,a
z2,a
u2) = (a
x1forall a. Num a => a -> a -> a
-a
x2, a
y1forall a. Num a => a -> a -> a
-a
y2, a
z1forall a. Num a => a -> a -> a
-a
z2, a
u1forall a. Num a => a -> a -> a
-a
u2)
(a
x1,a
y1,a
z1,a
u1) dot :: (a, a, a, a) -> (a, a, a, a) -> a
`dot` (a
x2,a
y2,a
z2,a
u2) = a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2 forall a. Num a => a -> a -> a
+ a
z1 forall a. Num a => a -> a -> a
* a
z2 forall a. Num a => a -> a -> a
+ a
u1 forall a. Num a => a -> a -> a
* a
u2
instance (Eq a, Floating a) => VectorSpace (a,a,a,a,a) a where
zeroVector :: (a, a, a, a, a)
zeroVector = (a
0,a
0,a
0,a
0,a
0)
a
a *^ :: a -> (a, a, a, a, a) -> (a, a, a, a, a)
*^ (a
x,a
y,a
z,a
u,a
v) = (a
a forall a. Num a => a -> a -> a
* a
x, a
a forall a. Num a => a -> a -> a
* a
y, a
a forall a. Num a => a -> a -> a
* a
z, a
a forall a. Num a => a -> a -> a
* a
u, a
a forall a. Num a => a -> a -> a
* a
v)
(a
x,a
y,a
z,a
u,a
v) ^/ :: (a, a, a, a, a) -> a -> (a, a, a, a, a)
^/ a
a = (a
x forall a. Fractional a => a -> a -> a
/ a
a, a
y forall a. Fractional a => a -> a -> a
/ a
a, a
z forall a. Fractional a => a -> a -> a
/ a
a, a
u forall a. Fractional a => a -> a -> a
/ a
a, a
v forall a. Fractional a => a -> a -> a
/ a
a)
negateVector :: (a, a, a, a, a) -> (a, a, a, a, a)
negateVector (a
x,a
y,a
z,a
u,a
v) = (-a
x, -a
y, -a
z, -a
u, -a
v)
(a
x1,a
y1,a
z1,a
u1,a
v1) ^+^ :: (a, a, a, a, a) -> (a, a, a, a, a) -> (a, a, a, a, a)
^+^ (a
x2,a
y2,a
z2,a
u2,a
v2) = (a
x1forall a. Num a => a -> a -> a
+a
x2, a
y1forall a. Num a => a -> a -> a
+a
y2, a
z1forall a. Num a => a -> a -> a
+a
z2, a
u1forall a. Num a => a -> a -> a
+a
u2, a
v1forall a. Num a => a -> a -> a
+a
v2)
(a
x1,a
y1,a
z1,a
u1,a
v1) ^-^ :: (a, a, a, a, a) -> (a, a, a, a, a) -> (a, a, a, a, a)
^-^ (a
x2,a
y2,a
z2,a
u2,a
v2) = (a
x1forall a. Num a => a -> a -> a
-a
x2, a
y1forall a. Num a => a -> a -> a
-a
y2, a
z1forall a. Num a => a -> a -> a
-a
z2, a
u1forall a. Num a => a -> a -> a
-a
u2, a
v1forall a. Num a => a -> a -> a
-a
v2)
(a
x1,a
y1,a
z1,a
u1,a
v1) dot :: (a, a, a, a, a) -> (a, a, a, a, a) -> a
`dot` (a
x2,a
y2,a
z2,a
u2,a
v2) =
a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2 forall a. Num a => a -> a -> a
+ a
z1 forall a. Num a => a -> a -> a
* a
z2 forall a. Num a => a -> a -> a
+ a
u1 forall a. Num a => a -> a -> a
* a
u2 forall a. Num a => a -> a -> a
+ a
v1 forall a. Num a => a -> a -> a
* a
v2