问题
- How to numerically integrate complex, complex-valued functions in Haskell?
- Are there any existing libraries for it? numeric-tools operates only on reals.
I am aware that on complex plane there's only line integrals, so the interface I am interested in is something like this:
i = integrate f x a b precision
to calculate integral along straight line from a
to b
of function f
on point x
.
i
, x
, a
, b
are all of Complex Double
or better Num a => Complex a
type.
Please... :)
回答1:
You can make something like this yourself. Suppose you have a function realIntegrate
of type (Double -> Double) -> (Double,Double) -> Double
, taking a function and a tuple containing the lower and upper bounds, returning the result to some fixed precision. You could define realIntegrate f (lo,hi) = quadRomberg defQuad (lo,hi) f
using numeric-tools, for example.
Then we can make your desired function as follows - I'm ignoring the precision for now (and I don't understand what your x
parameter is for!):
integrate :: (Complex Double -> Complex Double) -> Complex Double -> Complex Double -> Complex Double
integrate f a b = r :+ i where
r = realIntegrate realF (0,1)
i = realIntegrate imagF (0,1)
realF t = realPart (f (interpolate t)) -- or realF = realPart . f . interpolate
imagF t = imagPart (f (interpolate t))
interpolate t = a + (t :+ 0) * (b - a)
So we express the path from a
to b
as a function on the real interval from 0 to 1 by linear interpolation, take the value of f
along that path, integrate the real and imaginary parts separately (I don't know if this can give numerically badly behaving results, though) and reassemble them into the final answer.
I haven't tested this code as I don't have numeric-tools installed, but at least it typechecks :-)
来源:https://stackoverflow.com/questions/17038693/are-there-any-haskell-libraries-for-integrating-complex-functions