packages feed

numhask 0.0.1 → 0.0.2

raw patch · 5 files changed

+22/−96 lines, 5 files

Files

numhask.cabal view
@@ -1,7 +1,7 @@ name:   numhask version:-  0.0.1+  0.0.2 synopsis:   A numeric prelude description:@@ -54,7 +54,6 @@     NumHask.HasShape,     NumHask.Vector,     NumHask.Matrix,-    NumHask.Num,     NumHask.Tensor   build-depends:     base >= 4.7 && < 4.10,
src/NumHask/Algebra/Integral.hs view
@@ -14,10 +14,8 @@   ) where  import qualified Protolude as P-import Protolude (Double, Float, Int, Integer, Functor(..), ($), (.), Foldable(..), fst, snd, foldr, const, Ord(..))+import Protolude (Double, Float, Int, Integer, Functor(..), (.), fst, snd) import Data.Functor.Rep-import NumHask.Algebra.Additive-import NumHask.Algebra.Multiplicative import NumHask.Algebra.Ring  -- | Integral@@ -53,12 +51,6 @@ -- | fromInteger class (Ring a) => FromInteger a where     fromInteger :: Integer -> a-    fromInteger = slowFromInteger--slowFromInteger :: (Ring r) => Integer -> r-slowFromInteger i = if i > zero-                    then foldr (+) zero $ fmap (const one) [one..i]-                    else negate $ foldr (+) zero $ fmap (const one) [one..negate i]  -- | This splitting away of fromInteger from the 'Ring' instance tends to increase constraint boier-plate fromIntegral :: (ToInteger a, FromInteger b) => a -> b
src/NumHask/Examples.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedLists #-}+{-# LANGUAGE DataKinds #-}  -- | NumHask usage examples @@ -18,7 +19,7 @@      ) where -import NumHask.Prelude()+import NumHask.Prelude  -- $imports -- NumHask.Prelude is a complete replacement for the standard prelude.@@ -148,7 +149,6 @@ -- >>> c / d -- [0.33333334,1.0,NaN] ----- >>> :set -XFlexibleContexts -- >>> size c :: Float -- 2.236068 --
− src/NumHask/Num.hs
@@ -1,60 +0,0 @@-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE PolyKinds #-}---- | Orphan instances for conversion between Num and NumHask classes.--module NumHask.Num (-  ) where--import Protolude-import qualified NumHask.Algebra as N-import Data.Functor.Rep---- | NumHask instances for Num instanced classes--- not compatible with most other NumHask modules-instance (Num a) => N.AdditiveMagma a where plus = (+)-instance (Num a) => N.AdditiveUnital a where zero = 0-instance (Num a) => N.AdditiveAssociative a-instance (Num a) => N.AdditiveCommutative a-instance (Num a) => N.AdditiveInvertible a where negate = negate-instance (Num a) => N.Additive a-instance (Num a) => N.AdditiveGroup a-instance (Num a) => N.MultiplicativeMagma a where times = (*)-instance (Num a) => N.MultiplicativeUnital a where one = 1-instance (Num a) => N.MultiplicativeCommutative a-instance (Num a) => N.MultiplicativeAssociative a-instance (Fractional a) => N.MultiplicativeInvertible a where recip = recip-instance (Num a) => N.Multiplicative a-instance (Fractional a) => N.MultiplicativeGroup a-instance (Num a) => N.Distribution a-instance (Num a) => N.Semiring a-instance (Num a) => N.Ring a-instance (Num a) => N.CRing a-instance (Fractional a) => N.Field a-instance (Num a) => N.Normed a a where size = abs---- | Num instance for something built with NumHask-instance ( N.Additive a-         , N.Signed a-         , N.FromInteger a) =>-         Num a where-    (+) = (N.+)-    (-) = (N.-)-    (*) = (N.-)-    negate = N.negate-    signum = N.sign-    abs = N.abs-    fromInteger = N.fromInteger---- | Num instance for a Representable-instance ( Representable r-         , Num a ) =>-         Num (r a) where-    (+) = liftR2 (+)-    (-) = liftR2 (-)-    (*) = liftR2 (*)-    negate = fmapRep negate-    signum = fmapRep signum-    abs = fmapRep abs-    fromInteger = pureRep . fromInteger-
src/NumHask/Tensor.hs view
@@ -66,7 +66,7 @@ shapeT _ =     case (sing :: Sing r) of       SNil -> []-      (SCons x xs) -> fmap P.fromIntegral (fromSing x: fromSing xs)+      (SCons x xs) -> fromIntegral <$> (fromSing x: fromSing xs)  -- not sure how to combine this with HasShape newtype ShapeT = ShapeT {unshapeT :: [Int]} deriving (Show, Eq)@@ -107,7 +107,7 @@   where     sh' = case (sing :: Sing r) of             SNil -> []-            (SCons x xs) -> fmap P.fromIntegral (fromSing x: fromSing xs)+            (SCons x xs) -> fromIntegral <$> (fromSing x: fromSing xs)  -- | convert the top layer of a SomeTensor to a [SomeTensor] flatten1 :: SomeTensor a -> [SomeTensor a]@@ -130,45 +130,41 @@ unind :: [Int] -> Int -> [Int] unind ns x= fst $ unfoldI ns x -instance forall (r :: [Nat]). (SingI r) => Distributive (Tensor r) where+instance forall r. (SingI r) => Distributive (Tensor (r::[Nat])) where     distribute f = Tensor $ V.generate n         $ \i -> fmap (\(Tensor v) -> V.unsafeIndex v i) f       where-        ns = case (sing :: Sing r) of-          SNil -> []-          (SCons x xs) -> fmap P.fromInteger (fromSing x: fromSing xs)-        n = P.foldr (*) one ns+        n = case (sing :: Sing r) of+          SNil -> one+          (SCons x xs) -> product $ fromInteger <$> (fromSing x: fromSing xs)  instance forall (r :: [Nat]). (SingI r) => Representable (Tensor r) where     type Rep (Tensor r) = [Int]-    tabulate f = Tensor $ V.generate n (f . unind ns)+    tabulate f = Tensor $ V.generate (product ns) (f . unind ns)       where         ns = case (sing :: Sing r) of           SNil -> []-          (SCons x xs) -> fmap P.fromIntegral (fromSing x: fromSing xs)-        n = P.foldr (*) one ns+          (SCons x xs) -> fromIntegral <$> (fromSing x: fromSing xs)     index (Tensor xs) rs = xs V.! ind ns rs       where         ns = case (sing :: Sing r) of           SNil -> []-          (SCons x xs') -> fmap P.fromIntegral (fromSing x: fromSing xs')+          (SCons x xs') -> fromIntegral <$> (fromSing x: fromSing xs')  -- | from flat list instance (SingI r, AdditiveUnital a) => IsList (Tensor (r::[Nat]) a) where     type Item (Tensor r a) = a     fromList l = Tensor $ V.fromList $ P.take n $ l P.++ P.repeat zero       where-        ns = case (sing :: Sing r) of-          SNil -> []-          (SCons x xs') -> fmap P.fromIntegral (fromSing x: fromSing xs')-        n = product ns+        n = case (sing :: Sing r) of+          SNil -> one+          (SCons x xs') -> product $ fromIntegral <$> (fromSing x: fromSing xs')     toList (Tensor v) = V.toList v --- | not sure if an arbitraryly-nested list can be converted to a 'SomeTensor'+-- | arbitraryly-nested list conversion to fit in with OverloadedLists needs some complex parsing fromListSomeTensor :: forall a. (AdditiveUnital a) => [Int] -> [a] -> SomeTensor a-fromListSomeTensor ns l = SomeTensor ns (V.fromList $ P.take n $ l P.++ P.repeat zero)-  where-    n = P.foldr (*) one ns+fromListSomeTensor ns l =+    SomeTensor ns (V.fromList $ P.take (product ns) $ l P.++ P.repeat zero)  instance Arbitrary ShapeT where     arbitrary = frequency@@ -187,10 +183,9 @@         , (9, fromList <$> vector n)         ]       where-        ns = case (sing :: Sing r) of-               SNil -> []-               (SCons x xs) -> fmap P.fromInteger (fromSing x: fromSing xs)-        n = P.foldr (*) one ns+        n = case (sing :: Sing r) of+               SNil -> one+               (SCons x xs) -> product $ fromInteger <$> (fromSing x: fromSing xs)  instance forall a. (Arbitrary a, AdditiveUnital a) => Arbitrary (SomeTensor a) where     arbitrary = frequency