1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
10 import Data.List (intercalate)
11 import Data.Vector.Fixed (
24 import qualified Data.Vector.Fixed as V (
35 -- | The Vn newtype simply wraps (Vector v a) so that we avoid
36 -- undecidable instances.
37 newtype Vn v a = Vn (v a)
39 -- | Declare the dimension of the wrapper to be the dimension of what
41 type instance Dim (Vn v) = Dim v
43 instance (Vector v a) => Vector (Vn v) a where
44 -- | Fortunately, 'Fun' is an instance of 'Functor'. The
45 -- 'construct' defined on our contained type will return a
46 -- 'Fun', and we simply slap our constructor on top with fmap.
47 construct = fmap Vn construct
49 -- | Defer to the inspect defined on the contained type.
50 inspect (Vn v1) = inspect v1
52 instance (Show a, Vector v a) => Show (Vn v a) where
53 -- | Display vectors as ordinary tuples. This is poor practice, but
54 -- these results are primarily displayed interactively and
55 -- convenience trumps correctness (said the guy who insists his
56 -- vector lengths be statically checked at compile-time).
60 -- >>> let v1 = make2d (1,2)
65 "(" ++ (intercalate "," element_strings) ++ ")"
68 element_strings = Prelude.map show v1l
71 -- | We would really like to say, "anything that is a vector of
72 -- equatable things is itself equatable." The 'Vn' class
73 -- allows us to express this without a GHC battle.
77 -- >>> let v1 = make2d (1,2)
78 -- >>> let v2 = make2d (1,2)
79 -- >>> let v3 = make2d (3,4)
85 instance (Eq a, Vector v a, Vector v Bool) => Eq (Vn v a) where
86 (Vn v1) == (Vn v2) = V.foldl (&&) True (V.zipWith (==) v1 v2)
89 -- | The use of 'Num' here is of course incorrect (otherwise, we
90 -- wouldn't have to throw errors). But it's really nice to be able
91 -- to use normal addition/subtraction.
92 instance (Num a, Vector v a) => Num (Vn v a) where
93 -- | Componentwise addition.
97 -- >>> let v1 = make2d (1,2)
98 -- >>> let v2 = make2d (3,4)
102 (Vn v1) + (Vn v2) = Vn $ V.zipWith (+) v1 v2
104 -- | Componentwise subtraction.
108 -- >>> let v1 = make2d (1,2)
109 -- >>> let v2 = make2d (3,4)
113 (Vn v1) - (Vn v2) = Vn $ V.zipWith (-) v1 v2
115 -- | Create an n-vector whose components are all equal to the given
116 -- integer. The result type must be specified since otherwise the
117 -- length n would be unknown.
121 -- >>> let v1 = fromInteger 17 :: Vn Vec3 Int
124 fromInteger x = Vn $ V.replicate (fromInteger x)
125 (*) = error "multiplication of vectors is undefined"
126 abs = error "absolute value of vectors is undefined"
127 signum = error "signum of vectors is undefined"
130 -- | This is probably useless, since the vectors we usually contain
131 -- aren't functor instances.
132 instance (Functor v) => Functor (Vn v) where
133 fmap f (Vn v1) = Vn (f `fmap` v1)
136 instance (RealFloat a, Ord a, Vector v a) => Normed (Vn v a) where
137 -- | The infinity norm. We don't use V.maximum here because it
138 -- relies on a type constraint that the vector be non-empty and I
139 -- don't know how to pattern match it away.
143 -- >>> let v1 = make3d (1,5,2)
147 norm_infty (Vn v1) = fromRational $ toRational $ V.foldl max 0 v1
149 -- | Generic p-norms. The usual norm in R^n is (norm_p 2).
153 -- >>> let v1 = make2d (3,4)
160 fromRational $ toRational $ root $ V.sum $ V.map (exponentiate . abs) v1
162 exponentiate = (** (fromIntegral p))
163 root = (** (recip (fromIntegral p)))
165 -- | Dot (standard inner) product.
169 -- >>> let v1 = make3d (1,2,3)
170 -- >>> let v2 = make3d (4,5,6)
174 dot :: (Num a, Vector v a) => Vn v a -> Vn v a -> a
175 dot (Vn v1) (Vn v2) = V.sum $ V.zipWith (*) v1 v2
178 -- | The angle between @v1@ and @v2@ in Euclidean space.
182 -- >>> let v1 = make2d (1.0, 0.0)
183 -- >>> let v2 = make2d (0.0, 1.0)
184 -- >>> angle v1 v2 == pi/2.0
187 angle :: (RealFloat a, Vector v a) => Vn v a -> Vn v a -> a
191 theta = (v1 `dot` v2) / norms
192 norms = (norm v1) * (norm v2)
199 -- >>> let v1 = make3d (1,2,3)
205 (!?) :: (Vector v a) => v a -> Int -> Maybe a
207 | idx < 0 || idx >= V.length v1 = Nothing
208 | otherwise = Just $ v1 ! idx
213 -- * Low-dimension vector wrappers.
215 -- These wrappers are instances of 'Vector', so they inherit all of
216 -- the userful instances defined above. But, they use fixed
217 -- constructors, so you can pattern match out the individual
220 data Vec2D a = Vec2D a a
221 type instance Dim Vec2D = N2
222 instance Vector Vec2D a where
223 inspect (Vec2D x y) (Fun f) = f x y
224 construct = Fun Vec2D
226 data Vec3D a = Vec3D a a a
227 type instance Dim Vec3D = N3
228 instance Vector Vec3D a where
229 inspect (Vec3D x y z) (Fun f) = f x y z
230 construct = Fun Vec3D
232 data Vec4D a = Vec4D a a a a
233 type instance Dim Vec4D = N4
234 instance Vector Vec4D a where
235 inspect (Vec4D w x y z) (Fun f) = f w x y z
236 construct = Fun Vec4D
239 -- | Convenience function for creating 2d vectors.
243 -- >>> let v1 = make2d (1,2)
246 -- >>> let Vn (Vec2D x y) = v1
250 make2d :: forall a. (a,a) -> Vn Vec2D a
251 make2d (x,y) = Vn (Vec2D x y)
254 -- | Convenience function for creating 3d vectors.
258 -- >>> let v1 = make3d (1,2,3)
261 -- >>> let Vn (Vec3D x y z) = v1
265 make3d :: forall a. (a,a,a) -> Vn Vec3D a
266 make3d (x,y,z) = Vn (Vec3D x y z)
269 -- | Convenience function for creating 4d vectors.
273 -- >>> let v1 = make4d (1,2,3,4)
276 -- >>> let Vn (Vec4D w x y z) = v1
280 make4d :: forall a. (a,a,a,a) -> Vn Vec4D a
281 make4d (w,x,y,z) = Vn (Vec4D w x y z)