# two-dimensional function to integrate (here: isotropic Gaussian density with zero mean)
f <- function (s, sigma = 5) exp(-rowSums(s^2)/2/sigma^2) / (2*pi*sigma^2)
# simple polygonal integration domain
disc.owin <- discpoly(c(3,2), 5, npoly=8, class="owin")
# plot image of the function and integration domain
gr <- seq(-8,8,by=0.2)
vals <- matrix(f(expand.grid(gr,gr)), nrow=length(gr))
image(x=gr, y=gr, z=vals)
library("spatstat")
plot.owin(disc.owin, hatch=TRUE, add=TRUE)
# Two-dimensional midpoint rule
testmidpoint <- function (eps, main=paste("Two-dimensional midpoint rule with eps =",eps))
{
image(x=gr, y=gr, z=vals, main=main)
plot.owin(disc.owin, hatch=TRUE, add=TRUE)
# add binary mask to plot
#plot(as.mask(disc.owin, eps=eps), add=TRUE, box=FALSE)
# add evaluation points to plot
with(as.mask(disc.owin, eps=eps), points(expand.grid(xcol, yrow), col=m, pch=20))
polyCub.midpoint(disc.owin, f, eps=eps)
}
testmidpoint(5)
testmidpoint(3)
testmidpoint(0.5)
testmidpoint(0.2)
# Product Gauss cubature by Sommariva & Vianello (2007)
if (require("statmod")) {
for (nGQ in c(1:5,10,20,60)) {
cat("nGQ =", sprintf("%2i",nGQ), ": ",
format(polyCub.SV(disc.owin, f, nGQ=nGQ), digits=16),
"")
}
}
# quasi-exact cubature by triangulation (gpclib) and using mvtnorm::pmvnorm()
if (require("mvtnorm") && require("gpclib")) {
oopt <- surveillance.options(gpclib=TRUE)
print(polyCub.exact.Gauss(disc.owin, mean=c(0,0), Sigma=5^2*diag(2),
plot=TRUE), digits=16)
surveillance.options(oopt)
}
Run the code above in your browser using DataLab