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 as V
12 import Data.Vector.Fixed.Boxed
16 -- | The Vn newtype simply wraps (Vector v a) so that we avoid
17 -- undecidable instances.
21 instance (Show a, Vector v a) => Show (Vn (v a)) where
22 -- | Display vectors as ordinary tuples. This is poor practice, but
23 -- these results are primarily displayed interactively and
24 -- convenience trumps correctness (said the guy who insists his
25 -- vector lengths be statically checked at compile-time).
29 -- >>> let v1 = make2d (1,2)
34 "(" ++ (intercalate "," element_strings) ++ ")"
37 element_strings = Prelude.map show v1l
40 -- | We would really like to say, "anything that is a vector of
41 -- equatable things is itself equatable." The 'Vn' class
42 -- allows us to express this without a GHC battle.
46 -- >>> let v1 = make2d (1,2)
47 -- >>> let v2 = make2d (1,2)
48 -- >>> let v3 = make2d (3,4)
54 instance (Eq a, Vector v a, Vector v Bool) => Eq (Vn (v a)) where
55 (Vn v1) == (Vn v2) = V.foldl (&&) True (V.zipWith (==) v1 v2)
58 -- | The use of 'Num' here is of course incorrect (otherwise, we
59 -- wouldn't have to throw errors). But it's really nice to be able
60 -- to use normal addition/subtraction.
61 instance (Num a, Vector v a) => Num (Vn (v a)) where
62 -- | Componentwise addition.
66 -- >>> let v1 = make2d (1,2)
67 -- >>> let v2 = make2d (3,4)
71 (Vn v1) + (Vn v2) = Vn $ V.zipWith (+) v1 v2
73 -- | Componentwise subtraction.
77 -- >>> let v1 = make2d (1,2)
78 -- >>> let v2 = make2d (3,4)
82 (Vn v1) - (Vn v2) = Vn $ V.zipWith (-) v1 v2
84 -- | Create an n-vector whose components are all equal to the given
85 -- integer. The result type must be specified since otherwise the
86 -- length n would be unknown.
90 -- >>> let v1 = fromInteger 17 :: Vn (Vec3 Int)
93 fromInteger x = Vn $ V.replicate (fromInteger x)
94 (*) = error "multiplication of vectors is undefined"
95 abs = error "absolute value of vectors is undefined"
96 signum = error "signum of vectors is undefined"
98 instance Functor Vn where
99 fmap f (Vn v1) = Vn (f v1)
101 instance (RealFloat a, Ord a, Vector v a) => Normed (Vn (v a)) where
102 -- | The infinity norm. We don't use V.maximum here because it
103 -- relies on a type constraint that the vector be non-empty and I
104 -- don't know how to pattern match it away.
108 -- >>> let v1 = make3d (1,5,2)
112 norm_infty (Vn v1) = fromRational $ toRational $ V.foldl max 0 v1
114 -- | Generic p-norms. The usual norm in R^n is (norm_p 2).
118 -- >>> let v1 = make2d (3,4)
125 fromRational $ toRational $ root $ V.sum $ V.map (exponentiate . abs) v1
127 exponentiate = (** (fromIntegral p))
128 root = (** (recip (fromIntegral p)))
130 -- | Dot (standard inner) product.
134 -- >>> let v1 = make3d (1,2,3)
135 -- >>> let v2 = make3d (4,5,6)
139 dot :: (Num a, Vector v a) => Vn (v a) -> Vn (v a) -> a
140 dot (Vn v1) (Vn v2) = V.sum $ V.zipWith (*) v1 v2
143 -- | The angle between @v1@ and @v2@ in Euclidean space.
147 -- >>> let v1 = make2d (1.0, 0.0)
148 -- >>> let v2 = make2d (0.0, 1.0)
149 -- >>> angle v1 v2 == pi/2.0
152 angle :: (RealFloat a, Vector v a) => Vn (v a) -> Vn (v a) -> a
156 theta = (v1 `dot` v2) / norms
157 norms = (norm_p (2 :: Integer) v1) * (norm_p (2 :: Integer) v2)
160 -- | Convenience function for creating 2d vectors.
164 -- >>> let v1 = make2d (1,2)
168 make2d :: forall a. (a,a) -> Vn (Vec2 a)
172 v1 = vec $ con |> x |> y :: Vec2 a
175 -- | Convenience function for creating 3d vectors.
179 -- >>> let v1 = make3d (1,2,3)
183 make3d :: forall a. (a,a,a) -> Vn (Vec3 a)
187 v1 = vec $ con |> x |> y |> z :: Vec3 a