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)
179 -- >>> let v1 = make3d (1,2,3)
185 (!?) :: (V.Vector v a) => v a -> Int -> Maybe a
187 | idx < 0 || idx >= V.length v1 = Nothing
188 | otherwise = Just $ v1 V.! idx
193 -- * Low-dimension vector wrappers.
195 -- These wrappers are instances of 'Vector', so they inherit all of
196 -- the userful instances defined above. But, they use fixed
197 -- constructors, so you can pattern match out the individual
200 data Vec2D a = Vec2D a a
201 type instance V.Dim Vec2D = V.N2
202 instance V.Vector Vec2D a where
203 inspect (Vec2D x y) (V.Fun f) = f x y
204 construct = V.Fun Vec2D
206 data Vec3D a = Vec3D a a a
207 type instance V.Dim Vec3D = V.N3
208 instance V.Vector Vec3D a where
209 inspect (Vec3D x y z) (V.Fun f) = f x y z
210 construct = V.Fun Vec3D
212 data Vec4D a = Vec4D a a a a
213 type instance V.Dim Vec4D = V.N4
214 instance V.Vector Vec4D a where
215 inspect (Vec4D w x y z) (V.Fun f) = f w x y z
216 construct = V.Fun Vec4D
219 -- | Convenience function for creating 2d vectors.
223 -- >>> let v1 = make2d (1,2)
226 -- >>> let Vn (Vec2D x y) = v1
230 make2d :: forall a. (a,a) -> Vn Vec2D a
231 make2d (x,y) = Vn (Vec2D x y)
234 -- | Convenience function for creating 3d vectors.
238 -- >>> let v1 = make3d (1,2,3)
241 -- >>> let Vn (Vec3D x y z) = v1
245 make3d :: forall a. (a,a,a) -> Vn Vec3D a
246 make3d (x,y,z) = Vn (Vec3D x y z)
249 -- | Convenience function for creating 4d vectors.
253 -- >>> let v1 = make4d (1,2,3,4)
256 -- >>> let Vn (Vec4D w x y z) = v1
260 make4d :: forall a. (a,a,a,a) -> Vn Vec4D a
261 make4d (w,x,y,z) = Vn (Vec4D w x y z)