You can split the domain into three triangles (make a picture). Define these three triangles by giving their vertices in columns:
T1 <- cbind(c(0,0), c(1,0), c(1,1))
T2 <- cbind(c(1,0), c(2,0), c(1,1))
T3 <- cbind(c(1,1), c(2,1), c(2,0))
Then you can use the SimplicialCubature
package. First define the union of the three triangles in an array:
Domain <- abind::abind(T1, T2, T3, along=3)
Define the integrand:
f <- function(xy) 4*(xy[1]-xy[2])*(1-xy[2])
Then:
library(SimplicialCubature)
> adaptIntegrateSimplex(f, Domain)
$integral
[1] 2.833333
$estAbsError
[1] 2.833333e-12
$functionEvaluations
[1] 96
The exact value of the integral is 17/6 = 2.833333....
. Thus the approximation we get is better than the one given by pracma
in the other answer (not surprising: the integrand with the term (y<x)
is not smooth).
For this example, we can even get something better with SimplicialCubature
. The integrand is a polynomial: f(x,y) = 4*x - 4*xy - 4*y + 4*y²
. The SimplicialCubature
package has an exact method for polynomials.
> p <- definePoly(c(4,-4,-4,4), rbind(c(1,0),c(1,1),c(0,1),c(0,2)))
> printPoly(p)
Polynomial has 4 terms and 2 variables
4 * x[1]^1 (degree= 1 )
-4 * x[1]^1 * x[2]^1 (degree= 2 )
-4 * x[2]^1 (degree= 1 )
+ 4 * x[2]^2 (degree= 2 )
> integrateSimplexPolynomial(p, Domain)
$integral
[1] 2.833333
$functionEvaluations
[1] 9