When using the vector-space package for derivative towers (see derivative towers) I come across the need to differentiate integrals. From math it is quite clear how to achieve this:
f(x) = int g(y) dy from 0 to x
with a function
g : R -> R
for example.
The derivative with respect to x would be:
f'(x) = g(x)
I tried to get this behaviour by first defining a class "Integration"
class Integration a b where
--standard integration function
integrate :: (a -> b) -> a -> a -> b
a basic instance is
instance Integration Double Double where
integrate f a b = fst $ integrateQAGS prec 1000 f a b
with integrateQAGS
from hmatrix
the problem comes with values b which represent towers of derivatives:
instance Integration Double (Double :> (NC.T Double)) where
integrate = integrateD
NC.T
is from Numeric.Complex (numeric-prelude).
The function integrateD
is defined as follows (but wrong):
integrateD ::(Integration a b, HasTrie (Basis a), HasBasis a, AdditiveGroup b) => (a -> a :> b) -> a -> a -> (a :> b)
integrateD f l u = D (integrate (powVal . f) l u) (derivative $ f u)
The function does not return what I want, it derives the integrand, but not the integral. The problem is, that I need a linear map which returns f u
. The a :> b
is defined as follows:
data a :> b = D { powVal :: b, derivative :: a :-* (a :> b) }
I don't know how to define derivative
. Any help will be appreciated, thanks
edit:
I forgot to provide the instance for Integration Double (NC.T Double)
:
instance Integration Double (NC.T Double) where
integrate f a b = bc $ (\g -> integrate g a b) <$> [NC.real . f, NC.imag . f]
where bc (x:y:[]) = x NC.+: y
and I can give an example of what I mean: Let's say I have a function
f(x) = exp(2*x)*sin(x)
>let f = \x -> (Prelude.exp ((pureD 2.0) AR.* (idD x))) * (sin (idD x)) :: Double :> Double
(AR.*) means multiplication from Algebra.Ring (numeric-prelude)
I can easily integrate this function with the above function integrateD
:
>integrateD f 0 1 :: Double :> Double
D 1.888605715258933 ...
When I take a look at the derivative of f:
f'(x) = 2*exp(2*x)*sin(x)+exp(2*x)*cos(x)
and evaluate this at 0
and pi/2
I get 1
and some value:
> derivAtBasis (f 0.0) ()
D 1.0 ...
> derivAtBasis (f (pi AF./ 2)) ()
D 46.281385265558534 ...
Now, when deriving the integral, I get the derivation of the function f
not its value at the upper bound
> derivAtBasis (integrate f 0 (pi AF./ 2)) ()
D 46.281385265558534 ...
But I expect:
> f (pi AF./ 2)
D 23.140692632779267 ...