ad-delcont 0.1.0.0 → 0.2.0.0
raw patch · 3 files changed
+18/−16 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Numeric.AD.DelCont: op1Num :: (Num da, Num db) => (a -> (b, db -> da)) -> AD s a da -> AD s b db
+ Numeric.AD.DelCont: op2Num :: (Num da, Num db, Num dc) => (a -> b -> (c, dc -> da, dc -> db)) -> AD s a da -> AD s b db -> AD s c dc
- Numeric.AD.DelCont: auto :: a -> da -> AD s a da
+ Numeric.AD.DelCont: auto :: a -> AD s a da
Files
- ad-delcont.cabal +1/−1
- src/Numeric/AD/DelCont.hs +4/−3
- src/Numeric/AD/DelCont/Internal.hs +13/−12
ad-delcont.cabal view
@@ -1,5 +1,5 @@ name: ad-delcont-version: 0.1.0.0+version: 0.2.0.0 synopsis: Reverse-mode automatic differentiation with delimited continuations description: Reverse-mode automatic differentiation using delimited continuations (@shift@/@reset@), inspired by the papers .
src/Numeric/AD/DelCont.hs view
@@ -57,12 +57,13 @@ rad1, rad2 , auto -- * Advanced usage- , rad1g, rad2g,+ , rad1g, rad2g -- ** Lift functions into AD- op1, op2+ , op1, op2 -- *** Num instances+ , op1Num, op2Num -- * Types , AD, AD') where -import Numeric.AD.DelCont.Internal (rad1, rad2, auto, rad1g, rad2g, op1, op2, AD, AD')+import Numeric.AD.DelCont.Internal (rad1, rad2, auto, rad1g, rad2g, op1, op2, op1Num, op2Num, AD, AD')
src/Numeric/AD/DelCont/Internal.hs view
@@ -43,12 +43,13 @@ var :: a -> da -> ST s (DVar s a da) var x dx = newSTRef (D x dx) --- | Lift a constant into 'AD'-auto :: a -- ^ primal- -> da -- ^ adjoint (in most cases this can be set to (@0 :: a@))- -> AD s a da-auto x dx = AD $ lift $ var x dx+-- | Lift a constant value into 'AD'+--+-- As one expects from a constant, its value will be used for computing the result, but it will be discarded when computing the sensitivities.+auto :: a -> AD s a da+auto x = AD $ lift $ var x undefined + -- | Mutable references to dual numbers in the continuation monad -- -- Here the @a@ and @da@ type parameters are respectively the /primal/ and /dual/ quantities tracked by the AD computation.@@ -104,9 +105,9 @@ -> AD s b db op1 z plusa f (AD ioa) = AD $ op1_ z plusa f ioa --- | Helper for constructing Num instances (= 'op1' specialized to Num)+-- | Helper for constructing unary functions that operate on Num instances (i.e. 'op1' specialized to Num) op1Num :: (Num da, Num db) =>- (a -> (b, db -> da))+ (a -> (b, db -> da)) -- ^ returns : (function result, pullback) -> AD s a da -> AD s b db op1Num = op1 0 (+)@@ -143,9 +144,9 @@ -> (AD s a da -> AD s b db -> AD s c dc) op2 z plusa plusb f (AD ioa) (AD iob) = AD $ op2_ z plusa plusb f ioa iob --- | Helper for constructing Num instances (= 'op2' specialized to Num)+-- | Helper for constructing binary functions that operate on Num instances (i.e. 'op2' specialized to Num) op2Num :: (Num da, Num db, Num dc) =>- (a -> b -> (c, dc -> da, dc -> db))+ (a -> b -> (c, dc -> da, dc -> db)) -- ^ returns : (function result, pullback) -> AD s a da -> AD s b db -> AD s c dc@@ -156,17 +157,17 @@ (+) = op2Num $ \x y -> (x + y, id, id) (-) = op2Num $ \x y -> (x - y, id, negate) (*) = op2Num $ \x y -> (x*y, (y *), (x *))- fromInteger x = auto (fromInteger x) 0+ fromInteger x = auto (fromInteger x) abs = op1Num $ \x -> (abs x, (* signum x)) signum = op1Num $ \x -> (signum x, const 0) instance (Fractional a) => Fractional (AD s a a) where (/) = op2Num $ \x y -> (x / y, (/ y), (\g -> -g*x/(y*y) ))- fromRational x = auto (fromRational x) 0+ fromRational x = auto (fromRational x) recip = op1Num $ \x -> (recip x, (/(x*x)) . negate) instance Floating a => Floating (AD s a a) where- pi = auto pi 0+ pi = auto pi exp = op1Num $ \x -> (exp x, (exp x *)) log = op1Num $ \x -> (log x, (/x)) sqrt = op1Num $ \x -> (sqrt x, (/ (2 * sqrt x)))