1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.analysis.integration;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ConvergenceException;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.FunctionEvaluationException;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MaxIterationsExceededException;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.UnivariateRealFunction;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implements the <a href="http://mathworld.wolfram.com/Legendre-GaussQuadrature.html">
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Legendre-Gauss</a> quadrature formula.
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Legendre-Gauss integrators are efficient integrators that can
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * accurately integrate functions with few functions evaluations. A
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Legendre-Gauss integrator using an n-points quadrature formula can
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integrate exactly 2n-1 degree polynomials.
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * These integrators evaluate the function on n carefully chosen
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * abscissas in each step interval (mapped to the canonical [-1  1] interval).
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The evaluation abscissas are not evenly spaced and none of them are
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * at the interval endpoints. This implies the function integrated can be
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * undefined at integration interval endpoints.
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The evaluation abscissas x<sub>i</sub> are the roots of the degree n
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Legendre polynomial. The weights a<sub>i</sub> of the quadrature formula
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integrals from -1 to +1 &int; Li<sup>2</sup> where Li (x) =
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * &prod; (x-x<sub>k</sub>)/(x<sub>i</sub>-x<sub>k</sub>) for k != i.
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class LegendreGaussIntegrator extends UnivariateRealIntegratorImpl {
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Abscissas for the 2 points method. */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] ABSCISSAS_2 = {
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -1.0 / FastMath.sqrt(3.0),
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         1.0 / FastMath.sqrt(3.0)
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Weights for the 2 points method. */
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] WEIGHTS_2 = {
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        1.0,
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        1.0
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Abscissas for the 3 points method. */
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] ABSCISSAS_3 = {
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -FastMath.sqrt(0.6),
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         0.0,
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         FastMath.sqrt(0.6)
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Weights for the 3 points method. */
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] WEIGHTS_3 = {
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        5.0 / 9.0,
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        8.0 / 9.0,
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        5.0 / 9.0
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Abscissas for the 4 points method. */
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] ABSCISSAS_4 = {
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -FastMath.sqrt((15.0 + 2.0 * FastMath.sqrt(30.0)) / 35.0),
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -FastMath.sqrt((15.0 - 2.0 * FastMath.sqrt(30.0)) / 35.0),
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         FastMath.sqrt((15.0 - 2.0 * FastMath.sqrt(30.0)) / 35.0),
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         FastMath.sqrt((15.0 + 2.0 * FastMath.sqrt(30.0)) / 35.0)
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Weights for the 4 points method. */
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] WEIGHTS_4 = {
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (90.0 - 5.0 * FastMath.sqrt(30.0)) / 180.0,
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (90.0 + 5.0 * FastMath.sqrt(30.0)) / 180.0,
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (90.0 + 5.0 * FastMath.sqrt(30.0)) / 180.0,
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (90.0 - 5.0 * FastMath.sqrt(30.0)) / 180.0
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Abscissas for the 5 points method. */
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] ABSCISSAS_5 = {
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -FastMath.sqrt((35.0 + 2.0 * FastMath.sqrt(70.0)) / 63.0),
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        -FastMath.sqrt((35.0 - 2.0 * FastMath.sqrt(70.0)) / 63.0),
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         0.0,
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         FastMath.sqrt((35.0 - 2.0 * FastMath.sqrt(70.0)) / 63.0),
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         FastMath.sqrt((35.0 + 2.0 * FastMath.sqrt(70.0)) / 63.0)
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Weights for the 5 points method. */
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double[] WEIGHTS_5 = {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (322.0 - 13.0 * FastMath.sqrt(70.0)) / 900.0,
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (322.0 + 13.0 * FastMath.sqrt(70.0)) / 900.0,
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        128.0 / 225.0,
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (322.0 + 13.0 * FastMath.sqrt(70.0)) / 900.0,
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        (322.0 - 13.0 * FastMath.sqrt(70.0)) / 900.0
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    };
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Abscissas for the current method. */
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double[] abscissas;
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Weights for the current method. */
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double[] weights;
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Build a Legendre-Gauss integrator.
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n number of points desired (must be between 2 and 5 inclusive)
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param defaultMaximalIterationCount maximum number of iterations
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if the number of points is not
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * in the supported range
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public LegendreGaussIntegrator(final int n, final int defaultMaximalIterationCount)
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(defaultMaximalIterationCount);
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        switch(n) {
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case 2 :
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            abscissas = ABSCISSAS_2;
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            weights   = WEIGHTS_2;
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case 3 :
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            abscissas = ABSCISSAS_3;
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            weights   = WEIGHTS_3;
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case 4 :
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            abscissas = ABSCISSAS_4;
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            weights   = WEIGHTS_4;
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case 5 :
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            abscissas = ABSCISSAS_5;
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            weights   = WEIGHTS_5;
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        default :
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED,
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    n, 2, 5);
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final double min, final double max)
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws ConvergenceException,  FunctionEvaluationException, IllegalArgumentException {
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return integrate(f, min, max);
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final UnivariateRealFunction f, final double min, final double max)
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws ConvergenceException,  FunctionEvaluationException, IllegalArgumentException {
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        clearResult();
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyInterval(min, max);
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyIterationCount();
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // compute first estimate with a single step
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double oldt = stage(f, min, max, 1);
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int n = 2;
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < maximalIterationCount; ++i) {
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // improve integral with a larger number of steps
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double t = stage(f, min, max, n);
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // estimate error
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double delta = FastMath.abs(t - oldt);
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double limit =
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                FastMath.max(absoluteAccuracy,
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                         relativeAccuracy * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5);
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // check convergence
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((i + 1 >= minimalIterationCount) && (delta <= limit)) {
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                setResult(t, i);
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return result;
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // prepare next iteration
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / abscissas.length));
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            n = FastMath.max((int) (ratio * n), n + 1);
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            oldt = t;
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throw new MaxIterationsExceededException(maximalIterationCount);
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Compute the n-th stage integral.
206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param f the integrand function
207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param min the lower bound for the interval
208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param max the upper bound for the interval
209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n number of steps
210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the value of n-th stage integral
211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws FunctionEvaluationException if an error occurs evaluating the
212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * function
213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private double stage(final UnivariateRealFunction f,
215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                         final double min, final double max, final int n)
216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws FunctionEvaluationException {
217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // set up the step for the current stage
219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double step     = (max - min) / n;
220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double halfStep = step / 2.0;
221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // integrate over all elementary steps
223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double midPoint = min + halfStep;
224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum = 0.0;
225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < n; ++i) {
226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int j = 0; j < abscissas.length; ++j) {
227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                sum += weights[j] * f.value(midPoint + halfStep * abscissas[j]);
228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            midPoint += step;
230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return halfStep * sum;
233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
237