-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathVector2.hs
74 lines (57 loc) · 1.71 KB
/
Vector2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE TypeFamilies, FlexibleContexts, UndecidableInstances #-}
module Vector2 ( V2(..) ) where
import Control.Applicative hiding ((*>))
import Basis
import VectorSpace
import InnerSpace
data V2 a = V2 !a !a deriving (Eq,Ord)
instance Show a => Show (V2 a) where
showsPrec _ (V2 x y) =
showChar '(' . shows x . showString ", " . shows y . showChar ')'
instance Functor V2 where
fmap f (V2 x y) = V2 (f x) (f y)
instance Applicative V2 where
pure a = V2 a a
V2 f g <*> V2 x y = V2 (f x) (g y)
instance Num a => Num (V2 a) where
fromInteger = pure . fromInteger
(+) = liftA2 (+)
(-) = liftA2 (-)
(*) = liftA2 (*)
negate = fmap negate
signum = fmap signum
abs = fmap abs
instance Fractional a => Fractional (V2 a) where
fromRational = pure . fromRational
(/) = liftA2 (/)
recip = fmap recip
instance Floating a => Floating (V2 a) where
pi = pure pi
exp = liftA exp
log = liftA log
sqrt = liftA sqrt
sin = liftA sin
cos = liftA cos
asin = liftA asin
acos = liftA acos
atan = liftA atan
sinh = liftA sinh
cosh = liftA cosh
asinh = liftA asinh
acosh = liftA acosh
atanh = liftA atanh
instance Num a => AdditiveGroup (V2 a) where
zeroV = V2 0 0
(<+>) = liftA2 (+)
negateV = liftA negate
instance Num a => VectorSpace (V2 a) where
type Scalar (V2 a) = a
s *> V2 a b = V2 (s * a) (s * b)
instance Num a => InnerSpace (V2 a) where
V2 a b `dot` V2 a' b' = a * a' + b * b'
instance Num a => Enumerable (V2 a) where
enumerate = [ V2 1 0, V2 0 1 ]
instance Num a => HasBasis (V2 a) where
type Basis (V2 a) = V2 a
basisValue = id
decompose = dot