packages feed

algebra 0.3.0 → 0.4.0

raw patch · 9 files changed

+185/−16 lines, 9 filesdep +reflectionPVP ok

version bump matches the API change (PVP)

Dependencies added: reflection

API changes (from Hackage documentation)

- Numeric.Algebra.Free: antipode :: Hopf r h => (h -> r) -> h -> r
- Numeric.Algebra.Free: class Semiring r => FreeAlgebra r a
- Numeric.Algebra.Free: class Semiring r => FreeCoalgebra r c
- Numeric.Algebra.Free: class FreeCoalgebra r c => FreeCounitalCoalgebra r c
- Numeric.Algebra.Free: class (Unital r, FreeAlgebra r a) => FreeUnitalAlgebra r a
- Numeric.Algebra.Free: class (FreeUnitalAlgebra r h, FreeCounitalCoalgebra r h) => Hopf r h
- Numeric.Algebra.Free: cojoin :: FreeCoalgebra r c => (c -> r) -> c -> c -> r
- Numeric.Algebra.Free: counit :: FreeCounitalCoalgebra r c => (c -> r) -> r
- Numeric.Algebra.Free: join :: FreeAlgebra r a => (a -> a -> r) -> a -> r
- Numeric.Algebra.Free: unit :: FreeUnitalAlgebra r a => r -> a -> r
- Numeric.Algebra.Free.Unital: instance FreeUnitalAlgebra r m => FreeCounitalCoalgebra r (m -> r)
- Numeric.Functional.Linear: (*.) :: RightModule r m => m -> r -> m
- Numeric.Functional.Linear: (.*) :: LeftModule r m => r -> m -> m
+ Numeric.Algebra.Free.Class: instance Semiring r => FreeCoalgebra r (Seq a)
+ Numeric.Algebra.Free.Class: instance Semiring r => FreeCoalgebra r [a]
+ Numeric.Algebra.Free.Unital: instance (Unital r, FreeUnitalAlgebra r m) => FreeCounitalCoalgebra r (m -> r)
+ Numeric.Algebra.Free.Unital: instance Semiring r => FreeCounitalCoalgebra r (Seq a)
+ Numeric.Algebra.Free.Unital: instance Semiring r => FreeCounitalCoalgebra r [a]
+ Numeric.Polynomial.Basis.Power: (^:) :: x -> n -> x :^ n
+ Numeric.Polynomial.Basis.Power: Power :: n -> :^ x n
+ Numeric.Polynomial.Basis.Power: W :: W
+ Numeric.Polynomial.Basis.Power: X :: X
+ Numeric.Polynomial.Basis.Power: Y :: Y
+ Numeric.Polynomial.Basis.Power: Z :: Z
+ Numeric.Polynomial.Basis.Power: at :: (Unital r, Whole n) => Linear r (x :^ n) -> r -> r
+ Numeric.Polynomial.Basis.Power: coef :: (Rig r, Eq n) => n -> Linear r (x :^ n) -> r
+ Numeric.Polynomial.Basis.Power: data W
+ Numeric.Polynomial.Basis.Power: data X
+ Numeric.Polynomial.Basis.Power: data Y
+ Numeric.Polynomial.Basis.Power: data Z
+ Numeric.Polynomial.Basis.Power: delta :: (Rig r, Eq a) => a -> a -> r
+ Numeric.Polynomial.Basis.Power: instance (Semiring r, Additive n) => FreeCoalgebra r (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance (Semiring r, AdditiveMonoid n) => FreeCounitalCoalgebra r (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance (Semiring r, AdditiveMonoid r, Unital r, DecidableZero n, Partitionable n) => FreeUnitalAlgebra r (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance (Semiring r, Partitionable n) => FreeAlgebra r (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance (Show t, Reifies x t, Show n) => Show (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Additive n => Multiplicative (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance AdditiveGroup n => MultiplicativeGroup (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance AdditiveMonoid n => Unital (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance DecidableZero n => DecidableUnits (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Eq n => Eq (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Foldable ((:^) x)
+ Numeric.Polynomial.Basis.Power: instance Foldable1 ((:^) x)
+ Numeric.Polynomial.Basis.Power: instance Functor ((:^) x)
+ Numeric.Polynomial.Basis.Power: instance HasTrie n => HasTrie (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Ord n => Ord (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Partitionable n => Factorable (x :^ n)
+ Numeric.Polynomial.Basis.Power: instance Reifies W W
+ Numeric.Polynomial.Basis.Power: instance Reifies X X
+ Numeric.Polynomial.Basis.Power: instance Reifies Y Y
+ Numeric.Polynomial.Basis.Power: instance Reifies Z Z
+ Numeric.Polynomial.Basis.Power: instance Show W
+ Numeric.Polynomial.Basis.Power: instance Show X
+ Numeric.Polynomial.Basis.Power: instance Show Y
+ Numeric.Polynomial.Basis.Power: instance Show Z
+ Numeric.Polynomial.Basis.Power: instance Traversable ((:^) x)
+ Numeric.Polynomial.Basis.Power: instance Traversable1 ((:^) x)
+ Numeric.Polynomial.Basis.Power: logPower :: :^ x n -> n
+ Numeric.Polynomial.Basis.Power: newtype (:^) x n
+ Numeric.Polynomial.Basis.Power: x :: Unital n => Linear r (x :^ n)
- Numeric.Algebra.Free.Unital: class (Unital r, FreeAlgebra r a) => FreeUnitalAlgebra r a
+ Numeric.Algebra.Free.Unital: class FreeAlgebra r a => FreeUnitalAlgebra r a
- Numeric.Functional.Linear: unitVector :: FreeUnitalAlgebra r a => a -> r
+ Numeric.Functional.Linear: unitVector :: (FreeUnitalAlgebra r a, Unital r) => a -> r

Files

Numeric/Algebra/Free.hs view
@@ -1,9 +1,7 @@ module Numeric.Algebra.Free -  ( FreeAlgebra(..)-  , FreeUnitalAlgebra(..)-  , FreeCoalgebra(..)-  , FreeCounitalCoalgebra(..)-  , Hopf(..)+  ( module Numeric.Algebra.Free.Class+  , module Numeric.Algebra.Free.Unital+  , module Numeric.Algebra.Free.Hopf   ) where  import Numeric.Algebra.Free.Class
Numeric/Algebra/Free/Class.hs view
@@ -5,6 +5,8 @@   ) where  import Numeric.Semiring.Internal+import Data.Sequence+import Data.Monoid (mappend) import Prelude ()  -- A coassociative coalgebra over a semiring using@@ -36,3 +38,9 @@  instance (FreeCoalgebra r a, FreeCoalgebra r b, FreeCoalgebra r c, FreeCoalgebra r d, FreeCoalgebra r e) => FreeCoalgebra r (a, b, c, d, e) where   cojoin f (a1,b1,c1,d1,e1) (a2,b2,c2,d2,e2) = cojoin (\a -> cojoin (\b -> cojoin (\c -> cojoin (\d -> cojoin (\e -> f (a,b,c,d,e)) e1 e2) d1 d2) c1 c2) b1 b2) a1 a2++instance Semiring r => FreeCoalgebra r [a] where+  cojoin f as bs = f (mappend as bs)++instance Semiring r => FreeCoalgebra r (Seq a) where+  cojoin f as bs = f (mappend as bs)
Numeric/Algebra/Free/Hopf.hs view
@@ -5,9 +5,9 @@  import Numeric.Algebra.Free.Unital --- | a Hopf algebra on a semiring, where the module is a free.+-- | A Hopf algebra on a semiring, where the module is free. ----- If @antipode . antipode = id@ then we are Involutive+-- If @antipode . antipode = id@ then we are 'Involutive'  class (FreeUnitalAlgebra r h, FreeCounitalCoalgebra r h) => Hopf r h where   -- > convolve id antipode = convolve antipode id = unit . counit
Numeric/Algebra/Free/Unital.hs view
@@ -6,13 +6,16 @@  import Numeric.Algebra.Free.Class import Numeric.Monoid.Multiplicative.Internal+import Data.Sequence (Seq)+import Numeric.Semiring.Internal+import qualified Data.Sequence as Seq import Prelude (($))  -- A coassociative counital coalgebra over a semiring, where the module is free class FreeCoalgebra r c => FreeCounitalCoalgebra r c where   counit :: (c -> r) -> r -instance FreeUnitalAlgebra r m => FreeCounitalCoalgebra r (m -> r) where+instance (Unital r, FreeUnitalAlgebra r m) => FreeCounitalCoalgebra r (m -> r) where   counit k = k one  instance (FreeUnitalAlgebra r a, FreeCounitalCoalgebra r c) => FreeCounitalCoalgebra (a -> r) c where @@ -32,3 +35,9 @@  instance (FreeCounitalCoalgebra r a, FreeCounitalCoalgebra r b, FreeCounitalCoalgebra r c, FreeCounitalCoalgebra r d, FreeCounitalCoalgebra r e) => FreeCounitalCoalgebra r (a, b, c, d, e) where   counit k = counit $ \a -> counit $ \b -> counit $ \c -> counit $ \d -> counit $ \e -> k (a,b,c,d,e)++instance Semiring r => FreeCounitalCoalgebra r [a] where+  counit k = k []++instance Semiring r => FreeCounitalCoalgebra r (Seq a) where+  counit k = k (Seq.empty)
Numeric/Functional/Linear.hs view
@@ -1,7 +1,6 @@ {-# LANGUAGE ImplicitParams, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} module Numeric.Functional.Linear    ( Linear(..)-  , (.*), (*.)   -- * Vectors   , Vector   , unitVector@@ -85,7 +84,7 @@ instance (Rng r, FreeCounitalCoalgebra r m) => Rng (Linear r m) instance (Ring r, FreeCounitalCoalgebra r m) => Ring (Linear r m) -unitVector :: FreeUnitalAlgebra r a => a -> r+unitVector :: (FreeUnitalAlgebra r a, Unital r) => a -> r unitVector = unit one  counitCovector :: FreeCounitalCoalgebra r c => Linear r c
Numeric/Monoid/Multiplicative/Internal.hs view
@@ -8,8 +8,11 @@ import Data.Foldable hiding (product) import Data.Int import Data.Word+import Data.Sequence (Seq)+import qualified Data.Sequence as Seq import Prelude hiding ((*), foldr, product) import Numeric.Semiring.Internal+import Numeric.Monoid.Additive import Numeric.Natural.Internal  infixr 8 `pow`@@ -60,10 +63,10 @@   one = (one,one,one,one,one)  -- | An associative unital algebra over a semiring, built using a free module-class (Unital r, FreeAlgebra r a) => FreeUnitalAlgebra r a where+class (FreeAlgebra r a) => FreeUnitalAlgebra r a where   unit :: r -> a -> r -instance (FreeUnitalAlgebra r a) => Unital (a -> r) where+instance (Unital r, FreeUnitalAlgebra r a) => Unital (a -> r) where   one = unit one  instance FreeUnitalAlgebra () a where@@ -83,3 +86,11 @@  instance (FreeUnitalAlgebra r a, FreeUnitalAlgebra r b, FreeUnitalAlgebra r c, FreeUnitalAlgebra r d, FreeUnitalAlgebra r e) => FreeUnitalAlgebra r (a,b,c,d,e) where   unit r (a,b,c,d,e) = unit r a * unit r b * unit r c * unit r d * unit r e++instance (AdditiveMonoid r, Semiring r) => FreeUnitalAlgebra r [a] where+  unit r [] = r+  unit _ _ = zero++instance (AdditiveMonoid r, Semiring r) => FreeUnitalAlgebra r (Seq a) where+  unit r a | Seq.null a = r+           | otherwise = zero
+ Numeric/Polynomial/Basis/Power.hs view
@@ -0,0 +1,126 @@+{-# LANGUAGE TypeOperators, FlexibleInstances, MultiParamTypeClasses, UndecidableInstances, TypeFamilies #-}+module Numeric.Polynomial.Basis.Power +  ( +  -- * Power basis+    (:^)(Power, logPower)+  , (^:)+  -- * Variables+  , W(..), X(..), Y(..), Z(..)+  , x+  , at+  , delta+  , coef+  ) where++import Control.Applicative+import Data.Foldable+import Data.Function (on)+import Data.Proxy+import Data.Reflection+import Data.Functor.Representable.Trie+import Data.Semigroup.Foldable+import Data.Semigroup.Traversable+import Data.Traversable+import Numeric.Addition+import Numeric.Algebra.Free+import Numeric.Multiplication+import Numeric.Decidable.Zero+import Numeric.Decidable.Units+import Numeric.Semiring.Class+import Numeric.Rig.Class+import Numeric.Functional.Linear+import Numeric.Natural.Internal+import Prelude hiding ((^),(+),(-),(*),negate, replicate,subtract)++infixr 8 :^,^:++newtype x:^n = Power { logPower :: n } deriving (Eq,Ord)++-- convenient constructor +-- X ^: 12+(^:) :: x -> n -> x :^ n+_ ^: n = Power n++data W = W deriving Show; instance Reifies W W where reflect _ = W+  +data X = X deriving Show; instance Reifies X X where reflect _ = X++data Y = Y deriving Show; instance Reifies Y Y where reflect _ = Y++data Z = Z deriving Show; instance Reifies Z Z where reflect _ = Z++instance (Show t, Reifies x t, Show n) => Show (x:^n) where+  showsPrec d p = showParen (d > 8) $+   showsPrec 9 (reflect (proxyX p)) . showString "^:" . showsPrec 8 (logPower p) where+      proxyX :: x:^n -> Proxy x+      proxyX _ = Proxy++instance Functor ((:^) x) where+  fmap f (Power n) = Power (f n)++instance Foldable ((:^) x) where+  foldMap f (Power n) = f n++instance Traversable ((:^) x) where+  traverse f (Power n) = Power <$> f n++instance Foldable1 ((:^) x) where+  foldMap1 f (Power n) = f n++instance Traversable1 ((:^) x) where+  traverse1 f (Power n) = Power <$> f n++instance HasTrie n => HasTrie (x :^ n) where+  type BaseTrie (x :^ n) = BaseTrie n+  embedKey = embedKey . logPower+  projectKey = Power . projectKey++instance Additive n => Multiplicative (x :^ n) where+  Power n * Power m = Power (n + m)+  pow1p (Power n) m = Power (replicate1p m n)++instance AdditiveMonoid n => Unital (x :^ n) where+  one = Power zero+  pow (Power n) m = Power (replicate m n)++instance AdditiveGroup n => MultiplicativeGroup (x :^ n) where+  Power n / Power m = Power (n - m)+  recip (Power n) = Power (negate n)+  Power n \\ Power m = Power (subtract n m)+  Power n ^ m = Power (times m n)++instance DecidableZero n => DecidableUnits (x :^ n) where+  recipUnit (Power n) | isZero n  = Just (Power n)+                      | otherwise = Nothing++instance Partitionable n => Factorable (x :^ n) where+  factorWith f = partitionWith (f `on` Power) . logPower ++instance (Semiring r, Additive n) => FreeCoalgebra r (x :^ n) where+  cojoin f i j = f $ i * j++instance (Semiring r, AdditiveMonoid n) => FreeCounitalCoalgebra r (x :^ n) where+  counit f = f one++instance (Semiring r, Partitionable n) => FreeAlgebra r (x :^ n) where+  join f = sum1 . partitionWith (f `on` Power) . logPower++instance (Semiring r, AdditiveMonoid r, Unital r, DecidableZero n, Partitionable n) => FreeUnitalAlgebra r (x :^ n) where+  unit r (Power n) | isZero n  = r+                   | otherwise = zero++x :: Unital n => Linear r (x:^n)+x = Linear $ \k -> k $ Power one++-- the price of this approach is the loss of Horner's scheme+at :: (Unital r, Whole n) => Linear r (x:^n) -> r -> r+m `at` r = m $* pow r . logPower++delta :: (Rig r, Eq a) => a -> a -> r+delta i j | i == j = one+          | otherwise = zero++-- extract the nth coefficient of a polynomial+coef :: (Rig r, Eq n) => n -> Linear r (x:^n) -> r+coef n m = m $* delta (Power n)+
Numeric/Semiring/Internal.hs view
@@ -15,6 +15,8 @@ import Data.Foldable hiding (sum, concat) import Data.Semigroup.Foldable import Data.Int+import Data.Sequence hiding (reverse)+import qualified Data.Sequence as Seq import Data.Word import Prelude hiding ((*), (+), negate, subtract,(-), recip, (/), foldr, sum, product, replicate, concat) import qualified Prelude@@ -169,10 +171,19 @@ instance FreeAlgebra () a where   join _ _ = () --- TODO: check this-instance (FreeAlgebra r b, FreeAlgebra r a) => FreeAlgebra (b -> r) a where-  join f a b = join (\a1 a2 -> f a1 a2 b) a+-- | The tensor algebra+instance Semiring r => FreeAlgebra r [a] where+  join f = go [] where+    go ls rrs@(r:rs) = f (reverse ls) rrs + go (r:ls) rs+    go ls [] = f (reverse ls) [] +-- | The tensor algebra+instance Semiring r => FreeAlgebra r (Seq a) where+  join f = go Seq.empty where+    go ls s = case viewl s of+       EmptyL -> f ls s +       r :< rs -> f ls s + go (ls |> r) rs+ instance (FreeAlgebra r a, FreeAlgebra r b) => FreeAlgebra r (a,b) where   join f (a,b) = join (\a1 a2 -> join (\b1 b2 -> f (a1,b1) (a2,b2)) b) a @@ -184,3 +195,8 @@  instance (FreeAlgebra r a, FreeAlgebra r b, FreeAlgebra r c, FreeAlgebra r d, FreeAlgebra r e) => FreeAlgebra r (a,b,c,d,e) where   join f (a,b,c,d,e) = join (\a1 a2 -> join (\b1 b2 -> join (\c1 c2 -> join (\d1 d2 -> join (\e1 e2 -> f (a1,b1,c1,d1,e1) (a2,b2,c2,d2,e2)) e) d) c) b) a++-- TODO: check this+instance (FreeAlgebra r b, FreeAlgebra r a) => FreeAlgebra (b -> r) a where+  join f a b = join (\a1 a2 -> f a1 a2 b) a+
algebra.cabal view
@@ -1,6 +1,6 @@ name:          algebra category:      Math, Algebra-version:       0.3.0+version:       0.4.0 license:       BSD3 cabal-version: >= 1.6 license-file:  LICENSE@@ -27,6 +27,7 @@     mtl >= 2.0 && < 2.1,     semigroups >= 0.5 && < 0.6,     semigroupoids >= 1.2.2 && < 1.3,+    reflection >= 0.4 && < 0.5,     representable-tries >= 1.8 && < 1.9,     void >= 0.5.4 && < 0.6 @@ -67,6 +68,7 @@     Numeric.Order     Numeric.Order.Additive     Numeric.Order.Class+    Numeric.Polynomial.Basis.Power     Numeric.Rig     Numeric.Rig.Class     Numeric.Rig.Ordered