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]
using System;
using Imsl.Math;

public class HyperRectangleQuadratureEx1 : 
    HyperRectangleQuadrature.IFunction
{
    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;
    }

    public static void  Main(String[] args)
    {
        HyperRectangleQuadrature q = new HyperRectangleQuadrature(10);
        double result = q.Eval(new HyperRectangleQuadratureEx1());
        Console.Out.WriteLine("result = " + result);
    }
}

Output

result = 0.333125383208954

Link to C# source.