[Haskell-cafe] Hit a wall with the type system
lemming at henning-thielemann.de
Thu Nov 29 03:19:15 EST 2007
On Thu, 29 Nov 2007, Henning Thielemann wrote:
> On Wed, 28 Nov 2007, Chris Smith wrote:
> > > diffNum :: Num b => (forall a. Num a => a -> a) -> b -> b
> > > diffFractional :: Fractional b => (forall a. Fractional a => a -> a) -> b -> b
> > > diffFloating :: Floating b => (forall a. Floating a => a -> a) -> b -> b
> > >
> > > diffNum f x = let AD y dy = f (AD x 1) in dy
> > > diffFractional f x = let AD y dy = f (AD x 1) in dy
> > > diffFloating f x = let AD y dy = f (AD x 1) in dy
> Why do the functions have different number types after differentiation?
> I thought, that just 'diff'
> diff :: (AD a -> AD a) -> (a -> a)
> diff f x = let AD y dy = f (AD x 1) in dy
> must work. What you do, looks like numbers with errors, but I suspect you
> are right that 'automatic differentiation' is the term used for those
> kinds of computations.
I like to add that I have written some code for working with Taylor
expansions of functions, that is, with all derivatives of a function. You
can get the derivatives of composed functions by composing the Taylor
series of the elementary functions:
*PowerSeries.Taylor> take 10 $ exp `compose` sin (pi/2) :: [Double]
Computes the (truncated) Taylor series for (exp . sin) at pi/2.
More information about the Haskell-Cafe