Question

I am trying to integrate an complex logarithm function in R, but I am not sure how to define it.

Also when I write say x*i I get i is not defined ... but it is square root of opposite 1, isn't it? If I write say 5i I get no problem but 5*i gives error.

I define with some numbers x and y. In general, following code gives me an error

fun <- function(x + y*i){1/(x + y*i)}

integrate(fun,5+0i,10+0i)

Following code works

lower = 5

upper = 10

z <- complex(real = x, imaginary = y)

fun <- function(z){1/z}

integrate(fun,lower,upper) 

But how do calculate contour integrals? This is double integral, really, and I have no idea how to implement it here.

More importantly, I want to take a right hand limit (i.e. lim Y -> 0+) involving a complex logarithm, equal to (1), where Y decreases to 0 while X is fixed. Here, I could not define my complex function and variable without error.

Was it helpful?

Solution

Complex integration is integration along a path. You can parametrize the path, by arc length, i.e., using a real interval. (The result can actually depend on the path.)

One would expect to be able to compute the integral of 1/z along a straight line between -2-1i and 2-1i as follows (note the use of 1i rather than i).

f <- function(s) {
  z <- s-1i
  1/z
}
integrate( f, -2, 2 ) # Fails

But integrate only works with real numbers. You need to compute separately the real and imaginary parts.

integrate( function(s) Re(f(s)), -2, 2 )$value +
1i * integrate( function(s) Im(f(s)), -2, 2 )$value
# [1] 0+2.214297i

OTHER TIPS

There are functions in packages elliptic and pracma to compute a complex integral along a path directly, i.e. they do the splitting for you. For instance,

f <- function(z) 1/z  # the complex function
P <- c(-2-1i, 2-1i)   # straight line between two points

require(pracma)
cintegral(f, P)       # complex integral along path P
## [1] 0+2.214297i

and similar for functions integral.contour() or integral.segment() in package elliptic. To integrate function f around the origin, do

P <- c(-1i, 1, 1i, -1, -1i)  # rectangle around 0
cintegral(f, P)
## [1] 0+6.283185i

and the same integral value 2*pi*1i for a circle around the origin.

As @Carl Withhoft mentioned, complex integration is beyond what integrate can do. In the meantime, this is how you would obtain a complex number given the real and imaginary parts:

x <- 2
y <- 4

z <- complex(real=x, im=y)
# or
z <- x + y*1i

And if you want a function of a complex number:

fun <- function(z) 1/z
fun(z)  # 0.1 - 0.2i
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top