packages feed

vector-space 0.12 → 0.19

raw patch · 7 files changed

Files

src/Data/AdditiveGroup.hs view
@@ -8,15 +8,15 @@ -- Module      :   Data.AdditiveGroup -- Copyright   :  (c) Conal Elliott and Andy J Gill 2008 -- License     :  BSD3--- +-- -- Maintainer  :  conal@conal.net, andygill@ku.edu -- Stability   :  experimental--- +-- -- Groups: zero, addition, and negation (additive inverse) ----------------------------------------------------------------------  module Data.AdditiveGroup-  ( +  (     AdditiveGroup(..), sumV   , Sum(..), inSum, inSum2   ) where@@ -31,6 +31,9 @@ import Data.Foldable (foldr) import Data.Complex hiding (magnitude) import Data.Ratio+#if !(MIN_VERSION_base(4,11,0))+import Data.Semigroup (Semigroup(..))+#endif import Foreign.C.Types (CSChar, CInt, CShort, CLong, CLLong, CIntMax, CFloat, CDouble)  import Data.MemoTrie@@ -47,14 +50,17 @@   zeroV :: v   default zeroV :: (Generic v, AdditiveGroup (VRep v)) => v   zeroV = Gnrx.to (zeroV :: VRep v)+  {-# INLINE zeroV #-}   -- | Add vectors   (^+^) :: v -> v -> v   default (^+^) :: (Generic v, AdditiveGroup (VRep v)) => v -> v -> v   v ^+^ v' = Gnrx.to (Gnrx.from v ^+^ Gnrx.from v' :: VRep v)+  {-# INLINE (^+^) #-}   -- | Additive inverse   negateV :: v -> v   default negateV :: (Generic v, AdditiveGroup (VRep v)) => v -> v   negateV v = Gnrx.to (negateV $ Gnrx.from v :: VRep v)+  {-# INLINE negateV #-}   -- | Group subtraction   (^-^) :: v -> v -> v   v ^-^ v' = v ^+^ negateV v'@@ -62,6 +68,7 @@ -- | Sum over several vectors sumV :: (Foldable f, AdditiveGroup v) => f v -> v sumV = foldr (^+^) zeroV+{-# INLINE sumV #-}  instance AdditiveGroup () where   zeroV     = ()@@ -69,7 +76,7 @@   negateV   = id  -- For 'Num' types:--- +-- -- instance AdditiveGroup n where {zeroV=0; (^+^) = (+); negateV = negate}  #define ScalarTypeCon(con,t) \@@ -178,6 +185,7 @@  instance Functor Sum where   fmap f (Sum a) = Sum (f a)+  {-# INLINE fmap #-}  -- instance Applicative Sum where --   pure a = Sum a@@ -185,32 +193,40 @@  instance Applicative Sum where   pure  = Sum+  {-# INLINE pure #-}   (<*>) = inSum2 ($)+  {-# INLINE (<*>) #-} +instance AdditiveGroup a => Semigroup (Sum a) where+  (<>) = liftA2 (^+^)+  {-# INLINE (<>) #-}+ instance AdditiveGroup a => Monoid (Sum a) where   mempty  = Sum zeroV-  mappend = liftA2 (^+^)-+#if !(MIN_VERSION_base(4,11,0))+  mappend = (<>)+#endif  -- | Application a unary function inside a 'Sum' inSum :: (a -> b) -> (Sum a -> Sum b) inSum = getSum ~> Sum+{-# INLINE inSum #-}  -- | Application a binary function inside a 'Sum' inSum2 :: (a -> b -> c) -> (Sum a -> Sum b -> Sum c) inSum2 = getSum ~> inSum-+{-# INLINE inSum2 #-}  instance AdditiveGroup a => AdditiveGroup (Sum a) where-  zeroV   = mempty-  (^+^)   = mappend+  zeroV   = Sum zeroV+  (^+^)   = (<>)   negateV = inSum negateV - ---- to go elsewhere  (~>) :: (a' -> a) -> (b -> b') -> ((a -> b) -> (a' -> b')) (i ~> o) f = o . f . i+{-# INLINE (~>) #-}  -- result :: (b -> b') -> ((a -> b) -> (a -> b')) -- result = (.)@@ -224,16 +240,28 @@  instance AdditiveGroup a => AdditiveGroup (Gnrx.Rec0 a s) where   zeroV = Gnrx.K1 zeroV+  {-# INLINE zeroV #-}   negateV (Gnrx.K1 v) = Gnrx.K1 $ negateV v+  {-# INLINE negateV #-}   Gnrx.K1 v ^+^ Gnrx.K1 w = Gnrx.K1 $ v ^+^ w+  {-# INLINE (^+^) #-}   Gnrx.K1 v ^-^ Gnrx.K1 w = Gnrx.K1 $ v ^-^ w+  {-# INLINE (^-^) #-} instance AdditiveGroup (f p) => AdditiveGroup (Gnrx.M1 i c f p) where   zeroV = Gnrx.M1 zeroV+  {-# INLINE zeroV #-}   negateV (Gnrx.M1 v) = Gnrx.M1 $ negateV v+  {-# INLINE negateV #-}   Gnrx.M1 v ^+^ Gnrx.M1 w = Gnrx.M1 $ v ^+^ w+  {-# INLINE (^+^) #-}   Gnrx.M1 v ^-^ Gnrx.M1 w = Gnrx.M1 $ v ^-^ w+  {-# INLINE (^-^) #-} instance (AdditiveGroup (f p), AdditiveGroup (g p)) => AdditiveGroup ((f :*: g) p) where   zeroV = zeroV :*: zeroV+  {-# INLINE zeroV #-}   negateV (x:*:y) = negateV x :*: negateV y+  {-# INLINE negateV #-}   (x:*:y) ^+^ (ξ:*:υ) = (x^+^ξ) :*: (y^+^υ)+  {-# INLINE (^+^) #-}   (x:*:y) ^-^ (ξ:*:υ) = (x^-^ξ) :*: (y^-^υ)+  {-# INLINE (^-^) #-}
src/Data/AffineSpace.hs view
@@ -21,8 +21,9 @@   (     AffineSpace(..), (.-^), distanceSq, distance, alerp, affineCombo   ) where-+#if !MIN_VERSION_base(4,10,0) import Control.Applicative (liftA2)+#endif import Data.Ratio import Foreign.C.Types (CSChar, CInt, CShort, CLong, CLLong, CIntMax, CFloat, CDouble) import Control.Arrow(first)@@ -149,7 +150,7 @@  instance AdditiveGroup (Diff (VRep p)) => AdditiveGroup (GenericDiff p) instance VectorSpace (Diff (VRep p)) => VectorSpace (GenericDiff p)-instance InnerSpace (Diff (VRep p)) => InnerSpace (GenericDiff p)+instance (AdditiveGroup (Scalar (Diff (VRep p))), InnerSpace (Diff (VRep p))) => InnerSpace (GenericDiff p) instance HasBasis (Diff (VRep p)) => HasBasis (GenericDiff p)  data AffineDiffProductSpace f g p = AffineDiffProductSpace@@ -160,7 +161,8 @@          , VectorSpace (Diff (f p)), VectorSpace (Diff (g p))          , Scalar (Diff (f p)) ~ Scalar (Diff (g p)) )     => VectorSpace (AffineDiffProductSpace f g p)-instance ( AffineSpace (f p), AffineSpace (g p)+instance ( AdditiveGroup (Scalar (Diff (g p)))+         , AffineSpace (f p), AffineSpace (g p)          , InnerSpace (Diff (f p)), InnerSpace (Diff (g p))          , Scalar (Diff (f p)) ~ Scalar (Diff (g p))          , Num (Scalar (Diff (f p))) )
src/Data/Basis.hs view
@@ -23,6 +23,7 @@ import Control.Arrow (first) import Data.Ratio import Foreign.C.Types (CFloat, CDouble)+import Data.Kind -- import Data.Either  import Data.VectorSpace@@ -36,7 +37,7 @@  class VectorSpace v => HasBasis v where   -- | Representation of the canonical basis for @v@-  type Basis v :: *+  type Basis v :: Type   type Basis v = Basis (VRep v)   -- | Interpret basis rep as a vector   basisValue   :: Basis v -> v
src/Data/Cross.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE FlexibleInstances, FlexibleContexts, TypeOperators-           , TypeFamilies, TypeSynonymInstances  #-}+           , TypeFamilies, TypeSynonymInstances +           , UndecidableInstances  #-} {-# OPTIONS_GHC -Wall #-} ---------------------------------------------------------------------- -- |
src/Data/LinearMap.hs view
@@ -25,9 +25,9 @@   where  #if !(MIN_VERSION_base(4,8,0))-import Control.Applicative (Applicative)+import Control.Applicative (Applicative, liftA2) #endif-import Control.Applicative (liftA2, liftA3)+import Control.Applicative (liftA3) import Control.Arrow       (first,second)  import Data.MemoTrie      (HasTrie(..),(:->:))
src/Data/VectorSpace.hs view
@@ -10,12 +10,12 @@ -- Module      :   Data.VectorSpace -- Copyright   :  (c) Conal Elliott and Andy J Gill 2008 -- License     :  BSD3--- +-- -- Maintainer  :  conal@conal.net, andygill@ku.edu -- Stability   :  experimental--- +-- -- Vector spaces--- +-- -- This version uses associated types instead of fundeps and -- requires ghc-6.10 or later ----------------------------------------------------------------------@@ -23,7 +23,7 @@ -- NB: I'm attempting to replace fundeps with associated types.  See -- NewVectorSpace.hs.  Ran into trouble with type equality constraints not -- getting propagated.  Manuel Ch is looking into it.--- +-- -- Blocking bug: http://hackage.haskell.org/trac/ghc/ticket/2448  module Data.VectorSpace@@ -32,11 +32,13 @@   , InnerSpace(..)   , lerp, linearCombo, magnitudeSq, magnitude, normalized, project   ) where-+#if !(MIN_VERSION_base(4,8,0)) import Control.Applicative (liftA2)+#endif import Data.Complex hiding (magnitude) import Foreign.C.Types (CSChar, CInt, CShort, CLong, CLLong, CIntMax, CFloat, CDouble) import Data.Ratio+import Data.Kind  import Data.AdditiveGroup import Data.MemoTrie@@ -49,13 +51,14 @@  -- | Vector space @v@. class AdditiveGroup v => VectorSpace v where-  type Scalar v :: *+  type Scalar v :: Type   type Scalar v = Scalar (VRep v)   -- | Scale a vector   (*^) :: Scalar v -> v -> v   default (*^) :: (Generic v, VectorSpace (VRep v), Scalar (VRep v) ~ Scalar v)                     => Scalar v -> v -> v   μ *^ v = Gnrx.to (μ *^ Gnrx.from v :: VRep v)+  {-# INLINE (*^) #-}  infixr 7 <.> @@ -66,45 +69,54 @@   default (<.>) :: (Generic v, InnerSpace (VRep v), Scalar (VRep v) ~ Scalar v)                     => v -> v -> Scalar v   v<.>w = (Gnrx.from v :: VRep v) <.> Gnrx.from w+  {-# INLINE (<.>) #-}  infixr 7 ^/ infixl 7 ^*  -- | Vector divided by scalar (^/) :: (VectorSpace v, s ~ Scalar v, Fractional s) => v -> s -> v-v ^/ s = (1/s) *^ v+v ^/ s = recip s *^ v+{-# INLINE (^/) #-}  -- | Vector multiplied by scalar (^*) :: (VectorSpace v, s ~ Scalar v) => v -> s -> v (^*) = flip (*^)+{-# INLINE (^*) #-}  -- | Linear interpolation between @a@ (when @t==0@) and @b@ (when @t==1@).  -- lerp :: (VectorSpace v, s ~ Scalar v, Num s) => v -> v -> s -> v lerp :: VectorSpace v => v -> v -> Scalar v -> v lerp a b t = a ^+^ t *^ (b ^-^ a)+{-# INLINE lerp #-}  -- | Linear combination of vectors linearCombo :: VectorSpace v => [(v,Scalar v)] -> v linearCombo ps = sumV [v ^* s | (v,s) <- ps]+{-# INLINE linearCombo #-}  -- | Square of the length of a vector.  Sometimes useful for efficiency. -- See also 'magnitude'. magnitudeSq :: (InnerSpace v, s ~ Scalar v) => v -> s magnitudeSq v = v <.> v+{-# INLINE magnitudeSq #-}  -- | Length of a vector.   See also 'magnitudeSq'. magnitude :: (InnerSpace v, s ~ Scalar v, Floating s) =>  v -> s magnitude = sqrt . magnitudeSq+{-# INLINE magnitude #-} --- | Vector in same direction as given one but with length of one.  If--- given the zero vector, then return it.+-- | Vector in same direction as given one but with length of one.+-- Divides by zero for the zero vector. normalized :: (InnerSpace v, s ~ Scalar v, Floating s) =>  v -> v normalized v = v ^/ magnitude v+{-# INLINE normalized #-}  -- | @project u v@ computes the projection of @v@ onto @u@. project :: (InnerSpace v, s ~ Scalar v, Fractional s) => v -> v -> v project u v = ((v <.> u) / magnitudeSq u) *^ u+{-# INLINE project #-}  #define ScalarType(t) \   instance VectorSpace (t) where \@@ -234,19 +246,25 @@ instance VectorSpace a => VectorSpace (Gnrx.Rec0 a s) where   type Scalar (Gnrx.Rec0 a s) = Scalar a   μ *^ Gnrx.K1 v = Gnrx.K1 $ μ*^v+  {-# INLINE (*^) #-} instance VectorSpace (f p) => VectorSpace (Gnrx.M1 i c f p) where   type Scalar (Gnrx.M1 i c f p) = Scalar (f p)   μ *^ Gnrx.M1 v = Gnrx.M1 $ μ*^v+  {-# INLINE (*^) #-} instance (VectorSpace (f p), VectorSpace (g p), Scalar (f p) ~ Scalar (g p))          => VectorSpace ((f :*: g) p) where   type Scalar ((f:*:g) p) = Scalar (f p)   μ *^ (x:*:y) = μ*^x :*: μ*^y+  {-# INLINE (*^) #-}  instance InnerSpace a => InnerSpace (Gnrx.Rec0 a s) where   Gnrx.K1 v <.> Gnrx.K1 w = v<.>w+  {-# INLINE (<.>) #-} instance InnerSpace (f p) => InnerSpace (Gnrx.M1 i c f p) where   Gnrx.M1 v <.> Gnrx.M1 w = v<.>w+  {-# INLINE (<.>) #-} instance ( InnerSpace (f p), InnerSpace (g p)          , Scalar (f p) ~ Scalar (g p), Num (Scalar (f p)) )          => InnerSpace ((f :*: g) p) where   (x:*:y) <.> (ξ:*:υ) = x<.>ξ + y<.>υ+  {-# INLINE (<.>) #-}
vector-space.cabal view
@@ -1,6 +1,6 @@ Name:                vector-space-Version:             0.12-Cabal-Version:       >= 1.6+Version:             0.19+Cabal-Version:       >= 1.10 Synopsis:            Vector & affine spaces, linear maps, and derivatives Category:            math Description:@@ -29,6 +29,7 @@   location: git://github.com/conal/vector-space.git  Library+  default-language:  Haskell2010   hs-Source-Dirs:      src   Extensions:             Build-Depends:       base<5, MemoTrie >= 0.5@@ -47,15 +48,8 @@   Other-Modules:                           Data.VectorSpace.Generic -   -- This library relies on type families working as well as in 6.10.-  if impl(ghc < 6.10) {-    buildable: False-  }-  if !impl(ghc >= 7.9) {-    Build-Depends: void >= 0.4-  }-  ghc-options:         -Wall -O2---  ghc-prof-options:    -prof -auto-all ---- For ghc-options: -ddump-simpl-stats -ddump-rules -ddump-simpl -ddump-simpl-phases+  if  impl(ghc < 6.10) { buildable: False }+  if !impl(ghc >= 7.6) { Build-Depends: ghc-prim >= 0.2 }+  if !impl(ghc >= 7.9) { Build-Depends: void >= 0.4 }+  if !impl(ghc >= 8.0) { Build-Depends: semigroups >= 0.16 }