1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
10 import Data.List (intercalate)
11 import qualified Data.Vector.Fixed as V
15 -- | The Vn newtype simply wraps (Vector v a) so that we avoid
16 -- undecidable instances.
17 newtype Vn v a = Vn (v a)
19 -- | Declare the dimension of the wrapper to be the dimension of what
21 type instance V.Dim (Vn v) = V.Dim v
23 instance (V.Vector v a) => V.Vector (Vn v) a where
24 -- | Fortunately, 'Fun' is an instance of 'Functor'. The
25 -- 'V.construct' defined on our contained type will return a
26 -- 'Fun', and we simply slap our constructor on top with fmap.
27 construct = fmap Vn V.construct
29 -- | Defer to the V.inspect defined on the contained type.
30 inspect (Vn v1) = V.inspect v1
32 instance (Show a, V.Vector v a) => Show (Vn v a) where
33 -- | Display vectors as ordinary tuples. This is poor practice, but
34 -- these results are primarily displayed interactively and
35 -- convenience trumps correctness (said the guy who insists his
36 -- vector lengths be statically checked at compile-time).
40 -- >>> let v1 = make2d (1,2)
45 "(" ++ (intercalate "," element_strings) ++ ")"
48 element_strings = Prelude.map show v1l
51 -- | We would really like to say, "anything that is a vector of
52 -- equatable things is itself equatable." The 'Vn' class
53 -- allows us to express this without a GHC battle.
57 -- >>> let v1 = make2d (1,2)
58 -- >>> let v2 = make2d (1,2)
59 -- >>> let v3 = make2d (3,4)
65 instance (Eq a, V.Vector v a, V.Vector v Bool) => Eq (Vn v a) where
66 (Vn v1) == (Vn v2) = V.foldl (&&) True (V.zipWith (==) v1 v2)
69 -- | The use of 'Num' here is of course incorrect (otherwise, we
70 -- wouldn't have to throw errors). But it's really nice to be able
71 -- to use normal addition/subtraction.
72 instance (Num a, V.Vector v a) => Num (Vn v a) where
73 -- | Componentwise addition.
77 -- >>> let v1 = make2d (1,2)
78 -- >>> let v2 = make2d (3,4)
82 (Vn v1) + (Vn v2) = Vn $ V.zipWith (+) v1 v2
84 -- | Componentwise subtraction.
88 -- >>> let v1 = make2d (1,2)
89 -- >>> let v2 = make2d (3,4)
93 (Vn v1) - (Vn v2) = Vn $ V.zipWith (-) v1 v2
95 -- | Create an n-vector whose components are all equal to the given
96 -- integer. The result type must be specified since otherwise the
97 -- length n would be unknown.
101 -- >>> let v1 = fromInteger 17 :: Vn Vec3 Int
104 fromInteger x = Vn $ V.replicate (fromInteger x)
105 (*) = error "multiplication of vectors is undefined"
106 abs = error "absolute value of vectors is undefined"
107 signum = error "signum of vectors is undefined"
110 -- | This is probably useless, since the vectors we usually contain
111 -- aren't functor instances.
112 instance (Functor v) => Functor (Vn v) where
113 fmap f (Vn v1) = Vn (f `fmap` v1)
116 instance (RealFloat a, Ord a, V.Vector v a) => Normed (Vn v a) where
117 -- | The infinity norm. We don't use V.maximum here because it
118 -- relies on a type constraint that the vector be non-empty and I
119 -- don't know how to pattern match it away.
123 -- >>> let v1 = make3d (1,5,2)
127 norm_infty (Vn v1) = fromRational $ toRational $ V.foldl max 0 v1
129 -- | Generic p-norms. The usual norm in R^n is (norm_p 2).
133 -- >>> let v1 = make2d (3,4)
140 fromRational $ toRational $ root $ V.sum $ V.map (exponentiate . abs) v1
142 exponentiate = (** (fromIntegral p))
143 root = (** (recip (fromIntegral p)))
145 -- | Dot (standard inner) product.
149 -- >>> let v1 = make3d (1,2,3)
150 -- >>> let v2 = make3d (4,5,6)
154 dot :: (Num a, V.Vector v a) => Vn v a -> Vn v a -> a
155 dot (Vn v1) (Vn v2) = V.sum $ V.zipWith (*) v1 v2
158 -- | The angle between @v1@ and @v2@ in Euclidean space.
162 -- >>> let v1 = make2d (1.0, 0.0)
163 -- >>> let v2 = make2d (0.0, 1.0)
164 -- >>> angle v1 v2 == pi/2.0
167 angle :: (RealFloat a, V.Vector v a) => Vn v a -> Vn v a -> a
171 theta = (v1 `dot` v2) / norms
172 norms = (norm v1) * (norm v2)
175 -- | The length of a vector.
179 -- >>> let v1 = make2d (1,2)
183 length :: (V.Vector v a) => Vn v a -> Int
184 length (Vn v1) = V.length v1
187 -- | Unsafe indexing.
191 -- >>> let v1 = make3d (1,2,3)
195 -- *** Exception: Data.Vector.Fixed.!: index out of range
197 (!) :: (V.Vector v a) => Vn v a -> Int -> a
198 (!) (Vn v1) idx = v1 V.! idx
205 -- >>> let v1 = make3d (1,2,3)
211 (!?) :: (V.Vector v a) => Vn v a -> Int -> Maybe a
213 | idx < 0 || idx >= V.length v2 = Nothing
214 | otherwise = Just $ v1 ! idx
217 -- | Convert vector to a list.
221 -- >>> let v1 = make2d (1,2)
225 toList :: (V.Vector v a) => Vn v a -> [a]
226 toList (Vn v1) = V.toList v1
229 -- | Convert a list to a vector.
233 -- >>> fromList [1,2] :: Vn Vec2D Int
236 fromList :: (V.Vector v a) => [a] -> Vn v a
237 fromList xs = Vn $ V.fromList xs
239 -- | Map a function over a vector.
243 -- >>> let v1 = make2d (1,2)
247 map :: (V.Vector v a, V.Vector v b) => (a -> b) -> Vn v a -> Vn v b
248 map f (Vn vs) = Vn $ V.map f vs
252 -- * Low-dimension vector wrappers.
254 -- These wrappers are instances of 'Vector', so they inherit all of
255 -- the userful instances defined above. But, they use fixed
256 -- constructors, so you can pattern match out the individual
259 data Vec2D a = Vec2D a a
260 type instance V.Dim Vec2D = V.N2
261 instance V.Vector Vec2D a where
262 inspect (Vec2D x y) (V.Fun f) = f x y
263 construct = V.Fun Vec2D
265 data Vec3D a = Vec3D a a a
266 type instance V.Dim Vec3D = V.N3
267 instance V.Vector Vec3D a where
268 inspect (Vec3D x y z) (V.Fun f) = f x y z
269 construct = V.Fun Vec3D
271 data Vec4D a = Vec4D a a a a
272 type instance V.Dim Vec4D = V.N4
273 instance V.Vector Vec4D a where
274 inspect (Vec4D w x y z) (V.Fun f) = f w x y z
275 construct = V.Fun Vec4D
278 -- | Convenience function for creating 2d vectors.
282 -- >>> let v1 = make2d (1,2)
285 -- >>> let Vn (Vec2D x y) = v1
289 make2d :: forall a. (a,a) -> Vn Vec2D a
290 make2d (x,y) = Vn (Vec2D x y)
293 -- | Convenience function for creating 3d vectors.
297 -- >>> let v1 = make3d (1,2,3)
300 -- >>> let Vn (Vec3D x y z) = v1
304 make3d :: forall a. (a,a,a) -> Vn Vec3D a
305 make3d (x,y,z) = Vn (Vec3D x y z)
308 -- | Convenience function for creating 4d vectors.
312 -- >>> let v1 = make4d (1,2,3,4)
315 -- >>> let Vn (Vec4D w x y z) = v1
319 make4d :: forall a. (a,a,a,a) -> Vn Vec4D a
320 make4d (w,x,y,z) = Vn (Vec4D w x y z)