packages feed

ad 4.2.1.1 → 4.2.2

raw patch · 34 files changed

+93/−79 lines, 34 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

.travis.yml view
@@ -8,7 +8,7 @@  install:   - cabal install packunused packdeps-  - cabal configure -flib-Werror $mode+  - cabal configure $mode   - cabal build --ghc-options=-ddump-minimal-imports  script:
CHANGELOG.markdown view
@@ -1,3 +1,8 @@+4.2.2+-----+* *Major* bug fix for `grads`, `jacobians`, and anything that uses `Sparse` mode in `Numeric.AD`. Derivatives after the first two+  were previously incorrect.+ 4.2.1.1 ------- * Support `nats` version 1
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2010, Edward Kmett+Copyright (c) 2010-2015, Edward Kmett           (c) 2008-2009 Barak A. Pearlmutter and Jeffrey Mark Siskind  All rights reserved.
ad.cabal view
@@ -1,8 +1,8 @@ name:         ad-version:      4.2.1.1+version:      4.2.2 license:      BSD3 license-File: LICENSE-copyright:    (c) Edward Kmett 2010-2014,+copyright:    (c) Edward Kmett 2010-2015,               (c) Barak Pearlmutter and Jeffrey Mark Siskind 2008-2009 author:       Edward Kmett maintainer:   ekmett@gmail.com@@ -71,10 +71,6 @@     .     * @0@ means that the resulting derivative list is padded with 0s at the end. -flag lib-Werror-  default: False-  manual: True- source-repository head   type: git   location: git://github.com/ekmett/ad.git@@ -151,10 +147,7 @@   other-modules:     Numeric.AD.Internal.Combinators -  if flag(lib-Werror)-    ghc-options: -Werror-  else-    ghc-options: -Wall+  ghc-options: -Wall    ghc-options: -fspec-constr -fdicts-cheap -O2 
src/Numeric/AD.hs view
@@ -4,7 +4,7 @@ {-# LANGUAGE PatternGuards #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Halley.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE ScopedTypeVariables #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Combinators.hs view
@@ -2,7 +2,7 @@ {-# OPTIONS_HADDOCK not-home #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Dense.hs view
@@ -11,7 +11,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental
src/Numeric/AD/Internal/Forward.hs view
@@ -11,7 +11,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Forward/Double.hs view
@@ -10,7 +10,7 @@  ----------------------------------------------------------------------------- ---- |----- Copyright   :  (c) Edward Kmett 2010-2014+---- Copyright   :  (c) Edward Kmett 2010-2015 ---- License     :  BSD3 ---- Maintainer  :  ekmett@gmail.com ---- Stability   :  experimental
src/Numeric/AD/Internal/Identity.hs view
@@ -8,7 +8,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Kahn.hs view
@@ -12,7 +12,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/On.hs view
@@ -10,7 +10,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Or.hs view
@@ -10,7 +10,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2014+-- Copyright   :  (c) Edward Kmett 2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Internal/Reverse.hs view
@@ -14,7 +14,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2012-2014+-- Copyright   :  (c) Edward Kmett 2012-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental@@ -24,7 +24,7 @@ -- -- This version uses @Data.Reflection@ to find and update the tape. ----- This is asymptotically faster than using @Reverse@, which+-- This is asymptotically faster than using @Kahn@, which -- is forced to reify and topologically sort the graph, but it requires -- a fairly expensive rendezvous during construction when updated using -- multiple threads.
src/Numeric/AD/Internal/Sparse.hs view
@@ -10,7 +10,18 @@ {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-} {-# OPTIONS_HADDOCK not-home #-}-+-----------------------------------------------------------------------------+-- |+-- Copyright   :  (c) Edward Kmett 2010-2015+-- License     :  BSD3+-- Maintainer  :  ekmett@gmail.com+-- Stability   :  experimental+-- Portability :  GHC only+--+-- Unsafe and often partial combinators intended for internal usage.+--+-- Handle with care.+----------------------------------------------------------------------------- module Numeric.AD.Internal.Sparse   ( Index(..)   , emptyIndex@@ -75,10 +86,10 @@ times :: Num a => Sparse a -> Int -> Sparse a -> Sparse a times Zero _ _ = Zero times _ _ Zero = Zero-times (Sparse a as) n (Sparse b bs) = Sparse (a * b) $+times a@(Sparse pa da) n b@(Sparse pb db) = Sparse (pa * pb) $   unionWith (+)-    (fmap (^* b) (dropMap n as))-    (fmap (a *^) (dropMap n bs))+    (fmap (* b) (dropMap n da))+    (fmap (a *) (dropMap n db)) {-# INLINE times #-}  vars :: (Traversable f, Num a) => f a -> f (Sparse a)@@ -112,32 +123,6 @@   go ix i = partial (indices ix') as :< (go ix' <$> fns) where     ix' = addToIndex i ix {-# INLINE ds #-}--{--vvars :: Num a => Vector a -> Vector (AD Sparse a)-vvars = Vector.imap (\n a -> AD $ Sparse a $ singleton n $ auto 1)-{-# INLINE vvars #-}--vapply :: Num a => (Vector (AD Sparse a) -> b) -> Vector a -> b-vapply f = f . vvars-{-# INLINE vapply #-}---vd :: Num a => Int -> AD Sparse a -> Vector a-vd n (AD (Sparse _ da)) = Vector.generate n $ \i -> maybe 0 primal $ lookup i da-{-# INLINE vd #-}--vd' :: Num a => Int -> AD Sparse a -> (a, Vector a)-vd' n (AD (Sparse a da)) = (a , Vector.generate n $ \i -> maybe 0 primal $ lookup i da)-{-# INLINE vd' #-}--vds :: Num a => Int -> AD Sparse a -> Cofree Vector a-vds n (AD as@(Sparse a _)) = a :< Vector.generate n (go emptyIndex)-    where-        go ix i = partial (indices ix') as :< Vector.generate n (go ix')-            where ix' = addToIndex i ix-{-# INLINE vds #-}--}  partial :: Num a => [Int] -> Sparse a -> a partial []     (Sparse a _)  = a
src/Numeric/AD/Internal/Tower.hs view
@@ -11,7 +11,7 @@  ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental
src/Numeric/AD/Internal/Type.hs view
@@ -3,7 +3,7 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} ----------------------------------------------------------------------------- ---- |----- Copyright   :  (c) Edward Kmett 2010-2014+---- Copyright   :  (c) Edward Kmett 2010-2015 ---- License     :  BSD3 ---- Maintainer  :  ekmett@gmail.com ---- Stability   :  experimental
src/Numeric/AD/Jacobian.hs view
@@ -11,7 +11,7 @@ {-# LANGUAGE UndecidableInstances #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Jet.hs view
@@ -8,7 +8,7 @@ #endif ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Mode.hs view
@@ -10,7 +10,7 @@ {-# LANGUAGE UndecidableInstances #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Mode/Forward.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE Rank2Types #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Mode/Forward/Double.hs view
@@ -1,4 +1,17 @@ {-# LANGUAGE Rank2Types #-}+-----------------------------------------------------------------------------+-- |+-- Copyright   :  (c) Edward Kmett 2010-2015+-- License     :  BSD3+-- Maintainer  :  ekmett@gmail.com+-- Stability   :  experimental+-- Portability :  GHC only+--+-- Forward Mode AD specialized to `Double`. This enables the entire structure+-- to be unboxed.+--+-----------------------------------------------------------------------------+ module Numeric.AD.Mode.Forward.Double   ( AD   , ForwardDouble
src/Numeric/AD/Mode/Kahn.hs view
@@ -7,7 +7,7 @@ {-# LANGUAGE UndecidableInstances #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Mode/Reverse.hs view
@@ -8,7 +8,7 @@ {-# LANGUAGE ScopedTypeVariables #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Mode/Sparse.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Rank2Types #-} ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental
src/Numeric/AD/Mode/Tower.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Rank2Types #-} ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental
src/Numeric/AD/Newton.hs view
@@ -5,7 +5,7 @@ {-# LANGUAGE TypeFamilies #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Rank1/Forward.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Rank1/Halley.hs view
@@ -1,6 +1,6 @@ ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Rank1/Kahn.hs view
@@ -7,7 +7,7 @@ {-# LANGUAGE UndecidableInstances #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Rank1/Newton.hs view
@@ -4,7 +4,7 @@ {-# LANGUAGE TypeFamilies #-} ----------------------------------------------------------------------------- -- |--- Copyright   :  (c) Edward Kmett 2010-2014+-- Copyright   :  (c) Edward Kmett 2010-2015 -- License     :  BSD3 -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental
src/Numeric/AD/Rank1/Sparse.hs view
@@ -1,6 +1,6 @@ ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental
src/Numeric/AD/Rank1/Tower.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE BangPatterns #-} ----------------------------------------------------------------------------- -- |--- Copyright   : (c) Edward Kmett 2010-2014+-- Copyright   : (c) Edward Kmett 2010-2015 -- License     : BSD3 -- Maintainer  : ekmett@gmail.com -- Stability   : experimental@@ -29,89 +29,107 @@   , diffsF  -- answer and all derivatives of (a -> f a)   , diffs0F -- zero padded derivatives of (a -> f a)   -- * Directional Derivatives-  , du      -- directional derivative of (a -> a)-  , du'     -- answer and directional derivative of (a -> a)-  , dus     -- answer and all directional derivatives of (a -> a)-  , dus0    -- answer and all zero padded directional derivatives of (a -> a)-  , duF     -- directional derivative of (a -> f a)-  , duF'    -- answer and directional derivative of (a -> f a)-  , dusF    -- answer and all directional derivatives of (a -> f a)-  , dus0F   -- answer and all zero padded directional derivatives of (a -> a)+  , du      -- directional derivative of (f a -> a)+  , du'     -- answer and directional derivative of (f a -> a)+  , dus     -- answer and all directional derivatives of (f a -> a)+  , dus0    -- answer and all zero padded directional derivatives of (f a -> a)+  , duF     -- directional derivative of (f a -> g a)+  , duF'    -- answer and directional derivative of (f a -> g a)+  , dusF    -- answer and all directional derivatives of (f a -> g a)+  , dus0F   -- answer and all zero padded directional derivatives of (f a -> g a)   ) where  import Control.Applicative ((<$>)) import Numeric.AD.Internal.Tower import Numeric.AD.Mode +-- | Compute the answer and all derivatives of a function @(a -> a)@ diffs :: Num a => (Tower a -> Tower a) -> a -> [a] diffs f a = getADTower $ apply f a {-# INLINE diffs #-} +-- | Compute the zero-padded derivatives of a function @(a -> a)@ diffs0 :: Num a => (Tower a -> Tower a) -> a -> [a] diffs0 f a = zeroPad (diffs f a) {-# INLINE diffs0 #-} +-- | Compute the answer and all derivatives of a function @(a -> f a)@ diffsF :: (Functor f, Num a) => (Tower a -> f (Tower a)) -> a -> f [a] diffsF f a = getADTower <$> apply f a {-# INLINE diffsF #-} +-- | Compute the zero-padded derivatives of a function @(a -> f a)@ diffs0F :: (Functor f, Num a) => (Tower a -> f (Tower a)) -> a -> f [a] diffs0F f a = (zeroPad . getADTower) <$> apply f a {-# INLINE diffs0F #-} +-- | @taylor f x@ compute the Taylor series of @f@ around @x@. taylor :: Fractional a => (Tower a -> Tower a) -> a -> a -> [a] taylor f x dx = go 1 1 (diffs f x) where   go !n !acc (a:as) = a * acc : go (n + 1) (acc * dx / n) as   go _ _ [] = [] +-- | @taylor0 f x@ compute the Taylor series of @f@ around @x@, zero-padded. taylor0 :: Fractional a => (Tower a -> Tower a) -> a -> a -> [a] taylor0 f x dx = zeroPad (taylor f x dx) {-# INLINE taylor0 #-} +-- | @maclaurin f@ compute the Maclaurin series of @f@ maclaurin :: Fractional a => (Tower a -> Tower a) -> a -> [a] maclaurin f = taylor f 0 {-# INLINE maclaurin #-} +-- | @maclaurin f@ compute the Maclaurin series of @f@, zero-padded maclaurin0 :: Fractional a => (Tower a -> Tower a) -> a -> [a] maclaurin0 f = taylor0 f 0 {-# INLINE maclaurin0 #-} +-- | Compute the first derivative of a function @(a -> a)@ diff :: Num a => (Tower a -> Tower a) -> a -> a diff f = d . diffs f {-# INLINE diff #-} +-- | Compute the answer and first derivative of a function @(a -> a)@ diff' :: Num a => (Tower a -> Tower a) -> a -> (a, a) diff' f = d' . diffs f {-# INLINE diff' #-} +-- | Compute a directional derivative of a function @(f a -> a)@ du :: (Functor f, Num a) => (f (Tower a) -> Tower a) -> f (a, a) -> a du f = d . getADTower . f . fmap withD {-# INLINE du #-} +-- | Compute the answer and a directional derivative of a function @(f a -> a)@ du' :: (Functor f, Num a) => (f (Tower a) -> Tower a) -> f (a, a) -> (a, a) du' f = d' . getADTower . f . fmap withD {-# INLINE du' #-} +-- | Compute a directional derivative of a function @(f a -> g a)@ duF :: (Functor f, Functor g, Num a) => (f (Tower a) -> g (Tower a)) -> f (a, a) -> g a duF f = fmap (d . getADTower) . f . fmap withD {-# INLINE duF #-} +-- | Compute the answer and a directional derivative of a function @(f a -> g a)@ duF' :: (Functor f, Functor g, Num a) => (f (Tower a) -> g (Tower a)) -> f (a, a) -> g (a, a) duF' f = fmap (d' . getADTower) . f . fmap withD {-# INLINE duF' #-} +-- | Given a function @(f a -> a)@, and a tower of derivatives, compute the corresponding directional derivatives. dus :: (Functor f, Num a) => (f (Tower a) -> Tower a) -> f [a] -> [a] dus f = getADTower . f . fmap tower {-# INLINE dus #-} +-- | Given a function @(f a -> a)@, and a tower of derivatives, compute the corresponding directional derivatives, zero-padded dus0 :: (Functor f, Num a) => (f (Tower a) -> Tower a) -> f [a] -> [a] dus0 f = zeroPad . getADTower . f . fmap tower {-# INLINE dus0 #-} +-- | Given a function @(f a -> g a)@, and a tower of derivatives, compute the corresponding directional derivatives dusF :: (Functor f, Functor g, Num a) => (f (Tower a) -> g (Tower a)) -> f [a] -> g [a] dusF f = fmap getADTower . f . fmap tower {-# INLINE dusF #-} +-- | Given a function @(f a -> g a)@, and a tower of derivatives, compute the corresponding directional derivatives, zero-padded dus0F :: (Functor f, Functor g, Num a) => (f (Tower a) -> g (Tower a)) -> f [a] -> g [a] dus0F f = fmap getADTower . f . fmap tower {-# INLINE dus0F #-}