AC-Vector 2.2.0 → 2.3.0
raw patch · 6 files changed
+59/−15 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Vector.Class: (/|) :: (Vector v) => Scalar -> v -> v
+ Data.Vector.Class: (|/) :: (Vector v) => v -> Scalar -> v
+ Data.Vector.Class: class BasicVector v
+ Data.Vector.Class: vpromote :: (BasicVector v) => Scalar -> v
+ Data.Vector.V1: instance BasicVector Vector1
+ Data.Vector.V2: instance BasicVector Vector2
+ Data.Vector.V3: instance BasicVector Vector3
+ Data.Vector.V4: instance BasicVector Vector4
- Data.Vector.Class: class Vector v
+ Data.Vector.Class: class (BasicVector v, Num v, Fractional v) => Vector v
- Data.Vector.Class: vfold :: (Vector v) => (Scalar -> Scalar -> Scalar) -> (v -> Scalar)
+ Data.Vector.Class: vfold :: (BasicVector v) => (Scalar -> Scalar -> Scalar) -> (v -> Scalar)
- Data.Vector.Class: vlinear :: (Num v, Vector v) => Scalar -> v -> v -> v
+ Data.Vector.Class: vlinear :: (Vector v) => Scalar -> v -> v -> v
- Data.Vector.Class: vmap :: (Vector v) => (Scalar -> Scalar) -> (v -> v)
+ Data.Vector.Class: vmap :: (BasicVector v) => (Scalar -> Scalar) -> (v -> v)
- Data.Vector.Class: vpack :: (Vector v) => [Scalar] -> Maybe v
+ Data.Vector.Class: vpack :: (BasicVector v) => [Scalar] -> Maybe v
- Data.Vector.Class: vunpack :: (Vector v) => v -> [Scalar]
+ Data.Vector.Class: vunpack :: (BasicVector v) => v -> [Scalar]
- Data.Vector.Class: vzip :: (Vector v) => (Scalar -> Scalar -> Scalar) -> (v -> v -> v)
+ Data.Vector.Class: vzip :: (BasicVector v) => (Scalar -> Scalar -> Scalar) -> (v -> v -> v)
Files
- AC-Vector.cabal +4/−2
- Data/Vector/Class.hs +29/−3
- Data/Vector/V1.hs +5/−1
- Data/Vector/V2.hs +7/−3
- Data/Vector/V3.hs +7/−3
- Data/Vector/V4.hs +7/−3
AC-Vector.cabal view
@@ -1,6 +1,6 @@ Cabal-Version: >= 1.6 Name: AC-Vector -Version: 2.2.0 +Version: 2.3.0 Stability: Experimental Synopsis: Efficient geometric vectors and transformations. @@ -14,7 +14,9 @@ just contains data structures that are useful for graphics work.) - Removed gather modules. Some name changes for bounding boxes. + The @Vector@ class now implies @Num@ and @Fractional@. + Hopefully this will be the last breaking change for a while. + Also added @vpromote@, @(\/|)@ and @(|\/)@. Category: Data, Math, Numerical, Graphics License: BSD3
Data/Vector/Class.hs view
@@ -1,5 +1,5 @@ {- | - General function applicable to all vector types. + General functions applicable to all vector types. -} module Data.Vector.Class where @@ -10,7 +10,7 @@ {- | All vector types belong to this class. Aside from 'vpack' and 'vunpack', these methods aren't especially useful to end-users; they're used internally by the vector arithmetic implementations. -} -class Vector v where +class BasicVector v where -- | Apply a function to all vector fields. vmap :: (Scalar -> Scalar) -> (v -> v) @@ -26,7 +26,15 @@ -- | Unpack a vector into a list of values. (Always succeeds.) vunpack :: v -> [Scalar] + -- | Convert a 'Scalar' to a vector (with all components the same). + vpromote :: Scalar -> v + {- | + Dummy class that enables you to request a vector in a type signature without needing to explicitly list 'Num' or 'Fractional' as well. +-} +class (BasicVector v, Num v, Fractional v) => Vector v where + +{- | Scale a vector (i.e., change its length but not its direction). This operator has the same precedence as the usual @(*)@ operator. The @(*|)@ and @(|*)@ operators are identical, but with their argument flipped. Just remember that the \'@|@\' denotes the scalar part. @@ -42,8 +50,26 @@ (|*) :: Vector v => v -> Scalar -> v v |* k = vmap (k*) v +{- | + Scale a vector (i.e., change its length but not its direction). This operator has the same precedence as the usual @(/)@ operator. + + The @(/|)@ and @(|/)@ operators are identical, but with their argument flipped. Just remember that the \'@|@\' denotes the scalar part. +-} +(|/) :: Vector v => v -> Scalar -> v +v |/ k = v |* (1/k) + +{- | + Scale a vector (i.e., change its length but not its direction). This operator has the same precedence as the usual @(/)@ operator. + + The @(/|)@ and @(|/)@ operators are identical, but with their argument flipped. Just remember that the \'@|@\' denotes the scalar part. +-} +(/|) :: Vector v => Scalar -> v -> v +k /| v = (1/k) *| v + infixl 7 *| infixl 7 |* +infixl 7 /| +infixl 7 |/ -- | Take the /dot product/ of two vectors. This is a scalar equal to the cosine of the angle between the two vectors multiplied by the length of each vectors. vdot :: Vector v => v -> v -> Scalar @@ -68,5 +94,5 @@ * @vlinear 0.5 a b@ would give a point exactly half way between @a@ and @b@ in a straight line. -} -vlinear :: (Num v, Vector v) => Scalar -> v -> v -> v +vlinear :: (Vector v) => Scalar -> v -> v -> v vlinear t a b = (1-t) *| a + t *| b
Data/Vector/V1.hs view
@@ -17,7 +17,7 @@ -} newtype Vector1 = Vector1 {v1x :: Scalar} deriving (Eq, Ord, Enum, Show, Num, Fractional) -instance Vector Vector1 where +instance BasicVector Vector1 where vmap f (Vector1 x ) = Vector1 (f x) vzip f (Vector1 x1) (Vector1 x2) = Vector1 (f x1 x2) vfold _ (Vector1 x ) = x @@ -26,3 +26,7 @@ vpack _ = Nothing vunpack (Vector1 x) = [x] + + vpromote x = Vector1 x + +instance Vector Vector1 where
Data/Vector/V2.hs view
@@ -8,7 +8,7 @@ data Vector2 = Vector2 {v2x, v2y :: {-# UNPACK #-} !Scalar} deriving (Eq, Show) -instance Vector Vector2 where +instance BasicVector Vector2 where vmap f (Vector2 x y ) = Vector2 (f x) (f y) vzip f (Vector2 x1 y1) (Vector2 x2 y2) = Vector2 (f x1 x2) (f y1 y2) vfold f (Vector2 x y ) = f x y @@ -18,15 +18,19 @@ vunpack (Vector2 x y) = [x,y] + vpromote x = Vector2 x x + instance Num Vector2 where (+) = vzip (+) (-) = vzip (-) (*) = vzip (*) abs = vmap abs signum = vmap signum - fromInteger x = let x' = fromInteger x in Vector2 x' x' + fromInteger = vpromote . fromInteger instance Fractional Vector2 where (/) = vzip (/) recip = vmap recip - fromRational x = let x' = fromRational x in Vector2 x' x' + fromRational = vpromote . fromRational + +instance Vector Vector2 where
Data/Vector/V3.hs view
@@ -8,7 +8,7 @@ data Vector3 = Vector3 {v3x, v3y, v3z :: {-# UNPACK #-} !Scalar} deriving (Eq, Show) -instance Vector Vector3 where +instance BasicVector Vector3 where vmap f (Vector3 x y z ) = Vector3 (f x) (f y) (f z) vzip f (Vector3 x1 y1 z1) (Vector3 x2 y2 z2) = Vector3 (f x1 x2) (f y1 y2) (f z1 z2) vfold f (Vector3 x y z ) = f x (f y z) @@ -18,18 +18,22 @@ vunpack (Vector3 x y z) = [x,y,z] + vpromote x = Vector3 x x x + instance Num Vector3 where (+) = vzip (+) (-) = vzip (-) (*) = vzip (*) abs = vmap abs signum = vmap signum - fromInteger x = let x' = fromInteger x in Vector3 x' x' x' + fromInteger = vpromote . fromInteger instance Fractional Vector3 where (/) = vzip (/) recip = vmap recip - fromRational x = let x' = fromRational x in Vector3 x' x' x' + fromRational = vpromote . fromRational + +instance Vector Vector3 where {- | Take the /cross product/ of two 3D vectors. This produces a new 3D vector that is perpendicular to the plane of the first two vectors, and who's length is equal to the sine of the angle between those vectors multiplied by their lengths.
Data/Vector/V4.hs view
@@ -8,7 +8,7 @@ data Vector4 = Vector4 {v4x, v4y, v4z, v4w :: {-# UNPACK #-} !Scalar} deriving (Eq, Show) -instance Vector Vector4 where +instance BasicVector Vector4 where vmap f (Vector4 x y z w ) = Vector4 (f x) (f y) (f z) (f w) vzip f (Vector4 x1 y1 z1 w1) (Vector4 x2 y2 z2 w2) = Vector4 (f x1 x2) (f y1 y2) (f z1 z2) (f w1 w2) vfold f (Vector4 x y z w ) = f (f x y) (f z w) @@ -18,15 +18,19 @@ vunpack (Vector4 x y z w) = [x,y,z,w] + vpromote x = Vector4 x x x x + instance Num Vector4 where (+) = vzip (+) (-) = vzip (-) (*) = vzip (*) abs = vmap abs signum = vmap signum - fromInteger x = let x' = fromInteger x in Vector4 x' x' x' x' + fromInteger = vpromote . fromInteger instance Fractional Vector4 where (/) = vzip (/) recip = vmap recip - fromRational x = let x' = fromRational x in Vector4 x' x' x' x' + fromRational = vpromote . fromRational + +instance Vector Vector4 where