# To integrate round the unit circle, we need the contour and its
# derivative:
u <- function(x){exp(pi*2i*x)}
udash <- function(x){pi*2i*exp(pi*2i*x)}
# First, some elementary functions, for practice:
integrate.contour(function(z){1/z},u,udash) # should be 2*pi*i
integrate.contour(function(z){log(z)},u,udash) # should be -2*pi*i
integrate.contour(function(z){sin(z)+1/z^2},u,udash) # should be zero
# Now, some elliptic functions:
g <- c(3,2+4i)
Zeta <- function(z){zeta(z,g)}
Sigma <- function(z){sigma(z,g)}
WeierstrassP <- function(z){P(z,g)}
jj <- integrate.contour(Zeta,u,udash)
abs(jj-2*pi*1i) # should be zero
abs(integrate.contour(Sigma,u,udash)) # should be zero
abs(integrate.contour(WeierstrassP,u,udash)) # should be zero
# Now integrate over a semicircle. Observe how it's better to split the
# path into two separate pieces.
R <- 400
u1 <- function(x){R*exp(pi*1i*x)}
u1dash <- function(x){R*pi*1i*exp(pi*1i*x)}
u2 <- function(x){R*(2*x-1)}
u2dash <- function(x){R*2}
f <- function(z){exp(1i*z)/(1+z^2)}
answer.approximate <-
integrate.contour(f,u1,u1dash) +
integrate.contour(f,u2,u2dash)
answer.exact <- pi/exp(1)
abs(answer.approximate - answer.exact)
# Now try integrating over a triangle, with base on the real axis:
abs(integrate.segments(f,c(-R,R,1i*R))- answer.exact)Run the code above in your browser using DataLab