AC-Vector (empty) → 1.1.1
raw patch · 4 files changed
+181/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- AC-Vector.cabal +24/−0
- Data/Vector.hs +144/−0
- Licence.txt +10/−0
- Setup.hs +3/−0
+ AC-Vector.cabal view
@@ -0,0 +1,24 @@+Cabal-Version: >= 1.6 +Name: AC-Vector +Version: 1.1.1 +Stability: Experimental +Synopsis: Efficient geometric vectors. + +Description: + + This Haskell library implements several small vectors types + with @Double@ fields, with seperate types for each size of + vector, and a type class for handling vectors generally. + +Category: Data, Math, Numerical +License: BSD3 +License-file: Licence.txt +Author: Andrew Coppin +Maintainer: MathematicalOrchid@hotmail.com +Build-Type: Simple +Tested-With: GHC == 6.10.3 + +Library + Exposed-modules: Data.Vector + Build-Depends: base >= 4 && < 5 + HS-Source-Dirs: .
+ Data/Vector.hs view
@@ -0,0 +1,144 @@+{- | + This module provides several small vectors over @Double@ values. + All fields are strict and unpacked, so using these should be + fairly efficient. Each size of vector is a seperate type. It also + provides a few vector constants to save you some typing now and + then. +-} + +module Data.Vector where + +{- | The type of @Vector@ fields. -} +type Scalar = Double + +{- | + The @Vector@ class. All vectors are members of this class, + and it provides ways to apply functions over vectors. + Typically this methods aren't used directly; rather, the + other class instances for each vector are implemented + in terms of these. +-} +class Vector v where + fromScalar :: Scalar -> v + vmap :: (Scalar -> Scalar) -> v -> v + vzip :: (Scalar -> Scalar -> Scalar) -> v -> v -> v + vfold :: (Scalar -> Scalar -> Scalar) -> v -> Scalar + +{- | + Takes the /dot product/ of two vectors [of the same dimension]. + If you remember your highschool linear algebra, the dot product + of two vectors V and W is equal to |V| * |W| * cos k, where + |V| is the length of vector V, and k is the minimum angle + between the two vectors. +-} +vdot :: Vector v => v -> v -> Scalar +vdot v w = vfold (+) $ vzip (*) v w + +{- | + Returns the /magnitude/ of a vector (that is, it's length). + Note that this is always positive or zero (never negative). +-} +vmag :: Vector v => v -> Scalar +vmag v = sqrt $ v `vdot` v + +{- | + Multiply a vector by a scalar. This scales the magnitude + (length) of the vector, but leaves its length unchanged. + (Except in the case of a negative scalar, in which case + the vector's direction is reversed.) +-} +(*<>) :: Vector v => Scalar -> v -> v +s *<> v = vmap (s*) v + + + +{- | + The type of 2-dimensional vectors. It provides various + class instances such as 'Eq', 'Num', 'Show', etc. +-} +data Vector2 = Vector2 {v2x, v2y :: {-# UNPACK #-} !Scalar} + deriving (Eq, Ord, Show) + +instance Vector Vector2 where + fromScalar x = Vector2 x x + vmap f (Vector2 x1 y1) = Vector2 (f x1) (f y1) + vzip f (Vector2 x1 y1) (Vector2 x2 y2) = Vector2 (f x1 x2) (f y1 y2) + vfold f (Vector2 x1 y1) = f x1 y1 + +instance Num Vector2 where + (+) = vzip (+) + (-) = vzip (-) + (*) = vzip (*) + negate = vmap negate + abs = vmap abs + signum = vmap signum + fromInteger n = let s = fromInteger n in fromScalar s + +instance Fractional Vector2 where + (/) = vzip (/) + recip = vmap recip + fromRational r = let s = fromRational r in fromScalar s + +-- | Constant: The unit-length X vector, (1, 0). +vector2X :: Vector2 +vector2X = Vector2 1 0 + +-- | Constant: The unit-length Y vector, (0, 1). +vector2Y :: Vector2 +vector2Y = Vector2 0 1 + + + +{- | + The type of 3-dimensional vectors. Similar to 'Vector2'. +-} +data Vector3 = Vector3 {v3x, v3y, v3z :: {-# UNPACK #-} !Scalar} + deriving (Eq, Ord, Show) + +instance Vector Vector3 where + fromScalar x = Vector3 x x x + vmap f (Vector3 x1 y1 z1) = Vector3 (f x1) (f y1) (f z1) + vzip f (Vector3 x1 y1 z1) (Vector3 x2 y2 z2) = Vector3 (f x1 x2) (f y1 y2) (f z1 z2) + vfold f (Vector3 x1 y1 z1) = f x1 (f y1 z1) + +instance Num Vector3 where + (+) = vzip (+) + (-) = vzip (-) + (*) = vzip (*) + negate = vmap negate + abs = vmap abs + signum = vmap signum + fromInteger n = let s = fromInteger n in fromScalar s + +instance Fractional Vector3 where + (/) = vzip (/) + recip = vmap recip + fromRational r = let s = fromRational r in fromScalar s + +{- | + Takes the /cross product/ of two [3D] vectors. Again, from highschool + linear algebra, the cross product of vector V and W is a new vector + P such that |P| = |V| * |W| * sin k (where k is the minimum angle + between V and W), and the direction of P is perpendicular to both + V and W. For example, @vcross 'vector3X' 'vector3Y' = 'vector3Z'@. + Note also that @vcross w v = negate (vcross v w)@. +-} +vcross :: Vector3 -> Vector3 -> Vector3 +vcross (Vector3 x1 y1 z1) (Vector3 x2 y2 z2) = Vector3 + { + v3x = y1 * z2 - y2 * z1, + v3y = x1 * z2 - x2 * z1, + v3z = x1 * y2 - x2 * y1 + } + +-- | Constant: The unit-length X vector, (1, 0, 0). +vector3X :: Vector3 +vector3X = Vector3 1 0 0 + +-- | Constant: The unit-length Y vector, (0, 1, 0). +vector3Y :: Vector3 +vector3Y = Vector3 0 1 0 + +-- | Constant: The unit-length Z vector, (0, 0, 1). +vector3Z :: Vector3 +vector3Z = Vector3 0 0 1
+ Licence.txt view
@@ -0,0 +1,10 @@+Copyright (c) 2009, Andrew Coppin +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the name of Andrew Coppin nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple + +main = defaultMain