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 (Show a, V.Vector v a) => Show (Vn v a) where
24 -- | Display vectors as ordinary tuples. This is poor practice, but
25 -- these results are primarily displayed interactively and
26 -- convenience trumps correctness (said the guy who insists his
27 -- vector lengths be statically checked at compile-time).
31 -- >>> let v1 = make2d (1,2)
36 "(" ++ (intercalate "," element_strings) ++ ")"
39 element_strings = Prelude.map show v1l
42 -- | We would really like to say, "anything that is a vector of
43 -- equatable things is itself equatable." The 'Vn' class
44 -- allows us to express this without a GHC battle.
48 -- >>> let v1 = make2d (1,2)
49 -- >>> let v2 = make2d (1,2)
50 -- >>> let v3 = make2d (3,4)
56 instance (Eq a, V.Vector v a, V.Vector v Bool) => Eq (Vn v a) where
57 (Vn v1) == (Vn v2) = V.foldl (&&) True (V.zipWith (==) v1 v2)
60 -- | The use of 'Num' here is of course incorrect (otherwise, we
61 -- wouldn't have to throw errors). But it's really nice to be able
62 -- to use normal addition/subtraction.
63 instance (Num a, V.Vector v a) => Num (Vn v a) where
64 -- | Componentwise addition.
68 -- >>> let v1 = make2d (1,2)
69 -- >>> let v2 = make2d (3,4)
73 (Vn v1) + (Vn v2) = Vn $ V.zipWith (+) v1 v2
75 -- | Componentwise subtraction.
79 -- >>> let v1 = make2d (1,2)
80 -- >>> let v2 = make2d (3,4)
84 (Vn v1) - (Vn v2) = Vn $ V.zipWith (-) v1 v2
86 -- | Create an n-vector whose components are all equal to the given
87 -- integer. The result type must be specified since otherwise the
88 -- length n would be unknown.
92 -- >>> let v1 = fromInteger 17 :: Vn Vec3 Int
95 fromInteger x = Vn $ V.replicate (fromInteger x)
96 (*) = error "multiplication of vectors is undefined"
97 abs = error "absolute value of vectors is undefined"
98 signum = error "signum of vectors is undefined"
101 -- | This is probably useless, since the vectors we usually contain
102 -- aren't functor instances.
103 instance (Functor v) => Functor (Vn v) where
104 fmap f (Vn v1) = Vn (f `fmap` v1)
107 instance (RealFloat a, Ord a, V.Vector v a) => Normed (Vn v a) where
108 -- | The infinity norm. We don't use V.maximum here because it
109 -- relies on a type constraint that the vector be non-empty and I
110 -- don't know how to pattern match it away.
114 -- >>> let v1 = make3d (1,5,2)
118 norm_infty (Vn v1) = fromRational $ toRational $ V.foldl max 0 v1
120 -- | Generic p-norms. The usual norm in R^n is (norm_p 2).
124 -- >>> let v1 = make2d (3,4)
131 fromRational $ toRational $ root $ V.sum $ V.map (exponentiate . abs) v1
133 exponentiate = (** (fromIntegral p))
134 root = (** (recip (fromIntegral p)))
136 -- | Dot (standard inner) product.
140 -- >>> let v1 = make3d (1,2,3)
141 -- >>> let v2 = make3d (4,5,6)
145 dot :: (Num a, V.Vector v a) => Vn v a -> Vn v a -> a
146 dot (Vn v1) (Vn v2) = V.sum $ V.zipWith (*) v1 v2
149 -- | The angle between @v1@ and @v2@ in Euclidean space.
153 -- >>> let v1 = make2d (1.0, 0.0)
154 -- >>> let v2 = make2d (0.0, 1.0)
155 -- >>> angle v1 v2 == pi/2.0
158 angle :: (RealFloat a, V.Vector v a) => Vn v a -> Vn v a -> a
162 theta = (v1 `dot` v2) / norms
163 norms = (norm v1) * (norm v2)
166 -- | The length of a vector.
170 -- >>> let v1 = make2d (1,2)
174 length :: (V.Vector v a) => Vn v a -> Int
175 length (Vn v1) = V.length v1
178 -- | Unsafe indexing.
182 -- >>> let v1 = make3d (1,2,3)
186 -- *** Exception: Data.Vector.Fixed.!: index out of range
188 (!) :: (V.Vector v a) => Vn v a -> Int -> a
189 (!) (Vn v1) idx = v1 V.! idx
196 -- >>> let v1 = make3d (1,2,3)
202 (!?) :: (V.Vector v a) => Vn v a -> Int -> Maybe a
204 | idx < 0 || idx >= V.length v2 = Nothing
205 | otherwise = Just $ v1 ! idx
208 -- | Convert vector to a list.
212 -- >>> let v1 = make2d (1,2)
216 toList :: (V.Vector v a) => Vn v a -> [a]
217 toList (Vn v1) = V.toList v1
220 -- | Convert a list to a vector.
224 -- >>> fromList [1,2] :: Vn Vec2D Int
227 fromList :: (V.Vector v a) => [a] -> Vn v a
228 fromList xs = Vn $ V.fromList xs
230 -- | Map a function over a vector.
234 -- >>> let v1 = make2d (1,2)
238 map :: (V.Vector v a, V.Vector v b) => (a -> b) -> Vn v a -> Vn v b
239 map f (Vn vs) = Vn $ V.map f vs
243 -- * Low-dimension vector wrappers.
245 -- These wrappers are instances of 'Vector', so they inherit all of
246 -- the userful instances defined above. But, they use fixed
247 -- constructors, so you can pattern match out the individual
250 data Vec2D a = Vec2D a a
251 type instance V.Dim Vec2D = V.N2
252 instance V.Vector Vec2D a where
253 inspect (Vec2D x y) (V.Fun f) = f x y
254 construct = V.Fun Vec2D
256 data Vec3D a = Vec3D a a a
257 type instance V.Dim Vec3D = V.N3
258 instance V.Vector Vec3D a where
259 inspect (Vec3D x y z) (V.Fun f) = f x y z
260 construct = V.Fun Vec3D
262 data Vec4D a = Vec4D a a a a
263 type instance V.Dim Vec4D = V.N4
264 instance V.Vector Vec4D a where
265 inspect (Vec4D w x y z) (V.Fun f) = f w x y z
266 construct = V.Fun Vec4D
269 -- | Convenience function for creating 2d vectors.
273 -- >>> let v1 = make2d (1,2)
276 -- >>> let Vn (Vec2D x y) = v1
280 make2d :: forall a. (a,a) -> Vn Vec2D a
281 make2d (x,y) = Vn (Vec2D x y)
284 -- | Convenience function for creating 3d vectors.
288 -- >>> let v1 = make3d (1,2,3)
291 -- >>> let Vn (Vec3D x y z) = v1
295 make3d :: forall a. (a,a,a) -> Vn Vec3D a
296 make3d (x,y,z) = Vn (Vec3D x y z)
299 -- | Convenience function for creating 4d vectors.
303 -- >>> let v1 = make4d (1,2,3,4)
306 -- >>> let Vn (Vec4D w x y z) = v1
310 make4d :: forall a. (a,a,a,a) -> Vn Vec4D a
311 make4d (w,x,y,z) = Vn (Vec4D w x y z)