How to differentiate integrals with a vector space library (haskell)

When using vector-space package for derivative towers (see derivative towers ). I am faced with the need to differentiate the integrals. From mathematics it’s very clear how to achieve this:

f(x) = int g(y) dy from 0 to x

with function

g : R -> R

eg.

The derivative with respect to x will be:

f'(x) = g(x)

I tried to get this behavior by first defining the Integration class

class Integration a b where
--standard integration function
integrate :: (a -> b) -> a -> a -> b

base instance

instance  Integration Double Double where
  integrate f a b = fst $ integrateQAGS prec 1000 f a b

with integrateQAGSfrom hmatrix

the problem arises with the values ​​of b, which are towers of derivatives:

instance Integration Double (Double :> (NC.T Double)) where
  integrate = integrateD

NC.T- from Numeric.Complex (prelude number). The function is integrateDdefined as follows (but incorrectly):

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)

, , , . , , f u. a :> b :

data a :> b = D { powVal :: b, derivative :: a :-* (a :> b) }

, derivative. ,

:

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

, : ,

f(x) = exp(2*x)*sin(x)

>let f = \x -> (Prelude.exp ((pureD 2.0) AR.* (idD x))) * (sin (idD x)) :: Double :> Double 

(AR. *) Algebra.Ring(-)

integrateD:

>integrateD f 0 1 :: Double :> Double
D 1.888605715258933 ...

f:

f'(x) = 2*exp(2*x)*sin(x)+exp(2*x)*cos(x)

0 pi/2 1 :

> derivAtBasis (f 0.0) ()
D 1.0 ...

> derivAtBasis (f (pi AF./ 2)) ()
D 46.281385265558534 ...

, , f

> derivAtBasis (integrate f 0 (pi AF./ 2)) ()
D 46.281385265558534 ...

:

> f (pi AF./ 2)
D 23.140692632779267 ...
+5
3

- . >-< vector-space, .

, integrateD' :

integrateD' :: (Integration a b, HasTrie (Basis a), HasBasis a, AdditiveGroup b , b ~ Scalar b, VectorSpace b) => (a -> a :> b) -> a -> a -> (a:>b) -> (a :> b)
integrateD' f l u d_one =  ((\_ -> integrate (powVal . f) l  (u)) >-< (\_ ->  f u)) (d_one)

the d_one , 1.

instance Integration Double (Double :> Double) where
integrate f l u = integrateD' f l u (idD 1)

instance Integration ( Double) (Double :> (NC.T Double)) where
integrate f l u = liftD2 (NC.+:) (integrateD' (\x -> NC.real <$>> f x) l u (idD 1.0 :: Double :> Double)) (integrateD' (\x -> NC.imag <$>> f x) l u (idD 1.0 :: Double :> Double))

, integrateD , liftD2. idD, , .

, :

*Main> derivAtBasis (integrateD' f 0 (pi AF./ 2) (idD 1.0 :: Double :> Double )) ()
D 23.140692632779267 ...

:

*Main> derivAtBasis (integrate f 0 (pi AF./ 2)) ()
D 23.140692632779267 ...
0

AD , , AD, , " ". . ( , .)

import Numeric.AD
import Data.Complex

intIcky :: (Integral a, Fractional b) => a -> (b -> b) -> b -> b -> b
intIcky n f a b = c/n' * sum [f (a+fromIntegral i*c/(n'-1)) | i<-[0..n-1]]
  where n' = fromIntegral n
        c = b-a

sinIcky t = intIcky 1000 cos 0 t
cosIcky t = diff sinIcky t

test1 = map sinIcky [0,pi/2..2*pi::Float]
 -- [0.0,0.9997853,-4.4734867e-7,-0.9966421,6.282018e-3]
test2 = map sin [0,pi/2..2*pi::Float]
 -- [0.0,1.0,-8.742278e-8,-1.0,-3.019916e-7]
test3 = map cosIcky [0,pi/2..2*pi::Float]
 -- [1.0,-2.8568506e-4,-0.998999,2.857402e-3,0.999997]
test4 = map cos [0,pi/2..2*pi::Float]
 -- [1.0,-4.371139e-8,-1.0,1.1924881e-8,1.0]
test5 = diffs sinIcky (2*pi::Float)
 -- [6.282019e-3,0.99999696,-3.143549e-3,-1.0004976,3.1454563e-3,1.0014982,-3.1479746e-3,...]
test6 = diffs sinIcky (2*pi::Complex Float)
 -- [6.282019e-3 :+ 0.0,0.99999696 :+ 0.0,(-3.143549e-3) :+ 0.0,(-1.0004976) :+ 0.0,...]

, AD, . - ,

intIcky' dx f x0 x1 = dx * sum [f x|x<-[x0,x0+dx..x1]]

, , Enum , , , - :

Prelude> last [0..9.5]
10.0
+1

'hmatrix' Double. , " " "".

0

All Articles