Example: HyperRectangle Quadrature

This example evaluates the following multidimensional integral, with n =10.
\int_{a_{n-1}}^{b_{n-1}} \cdots \int_{a_0}^{b_0} \left[ \sum_{i=0}^n (-1)^i \prod_{j=0}^i x_j \right] \, dx_0 \ldots dx_{n-1} = \frac{1}{3}\left[ 1-\left(-\frac{1}{2}\right)^n\right]
import com.imsl.math.*;

public class HyperRectangleQuadratureEx1 {
    public static void main(String args[]) {
        
        HyperRectangleQuadrature.Function fcn = 
        new HyperRectangleQuadrature.Function() {
            public double f(double x[]) {
                int sign = 1;
                double sum = 0.0;
                for (int i = 0;  i < x.length;  i++) {
                    double prod = 1.0;
                    for (int j = 0;  j <= i;  j++) {
                        prod *= x[j];
                    }
                    sum += sign * prod;
                    sign = -sign;
                }
                return sum;
            }
        };
        
        HyperRectangleQuadrature q = new HyperRectangleQuadrature(10);
        double result = q.eval(fcn);
        System.out.println("result = "+result);
    }
}

Output

result = 0.3331253832089543
Link to Java source.