# ekmett/linear

1 parent 7df0285 commit a489507f4d08aef598747b507bedbcdb7386a3d8 committed Sep 14, 2012
Showing with 20 additions and 4 deletions.
1. +3 −0 CHANGELOG.markdown
2. +3 −1 src/Linear/Conjugate.hs
3. +3 −0 src/Linear/Epsilon.hs
4. +10 −2 src/Linear/Metric.hs
5. +1 −1 src/Linear/Quaternion.hs
3 CHANGELOG.markdown
 @@ -0,0 +1,3 @@ +0.2 +--- +* Initial hackage release
4 src/Linear/Conjugate.hs
 @@ -4,7 +4,9 @@ module Linear.Conjugate import Data.Complex hiding (conjugate) -class Conjugate a where +-- | An involutive ring +class Num a => Conjugate a where + -- | Conjugate a value. This defaults to the trivial involution. conjugate :: a -> a conjugate = id
3 src/Linear/Epsilon.hs
 @@ -2,11 +2,14 @@ module Linear.Epsilon ( Epsilon(..) ) where +-- | Provides a fairly subjective test to see if a quantity is near zero. class Num a => Epsilon a where nearZero :: a -> Bool +-- | @'abs' a '<=' 1e-6@ instance Epsilon Float where nearZero a = abs a <= 1e-6 +-- | @'abs' a '<=' 1e-12@ instance Epsilon Double where nearZero a = abs a <= 1e-12
12 src/Linear/Metric.hs
 @@ -5,27 +5,35 @@ module Linear.Metric import Control.Applicative import Linear.Epsilon --- free inner product/metric space +-- | A free inner product/metric space class Applicative f => Metric f where + -- | Compute the inner product of two vectors or (equivalently) + -- convert a vector @f a@ into a covector @f a -> a@. dot :: Num a => f a -> f a -> a + -- | Compute the squared norm. The name quadrance arises from + -- Norman J. Wildberger's rational trigonometry. quadrance :: Num a => f a -> a quadrance v = dot v v + -- | Compute the quadrance of the difference qd :: Num a => f a -> f a -> a qd f g = quadrance (liftA2 (-) f g) + -- | Compute the distance between two vectors in a metric space distance :: Floating a => f a -> f a -> a distance f g = norm (liftA2 (-) f g) + -- | Compute the norm of a vector in a metric space norm :: Floating a => f a -> a norm v = sqrt (dot v v) + -- | Convert a non-zero vector to unit vector. signorm :: Floating a => f a -> f a signorm v = fmap (/m) v where m = norm v --- |Normalize a 'Metric' functor to have unit 'norm'. This function +-- | Normalize a 'Metric' functor to have unit 'norm'. This function -- does not change the functor if its 'norm' is 0 or 1. normalize :: (Floating a, Metric f, Epsilon a) => f a -> f a normalize v = if nearZero l || nearZero (1-l) then v else fmap (/sqrt l) v
2 src/Linear/Quaternion.hs
 @@ -148,7 +148,7 @@ instance Hamiltonian Quaternion where instance Distributive Quaternion where distribute = distributeRep -instance (Conjugate a, Num a) => Conjugate (Quaternion a) where +instance (Conjugate a, RealFloat a) => Conjugate (Quaternion a) where conjugate (Quaternion e v) = Quaternion (conjugate e) (negate v) reimagine :: RealFloat a => a -> a -> Quaternion a -> Quaternion a