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.FunctionEvaluationException;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MaxIterationsExceededException;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.UnivariateRealFunction;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implements the <a href="http://mathworld.wolfram.com/SimpsonsRule.html">
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Simpson's Rule</a> for integration of real univariate functions. For
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * reference, see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X,
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * chapter 3.
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This implementation employs basic trapezoid rule as building blocks to
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * calculate the Simpson's rule of alternating 2/3 and 4/3.</p>
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class SimpsonIntegrator extends UnivariateRealIntegratorImpl {
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct an integrator for the given function.
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param f function to integrate
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.0 the integrand function is passed as an argument
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@link #integrate(UnivariateRealFunction, double, double)}method.
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SimpsonIntegrator(UnivariateRealFunction f) {
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(f, 64);
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct an integrator.
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SimpsonIntegrator() {
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(64);
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final double min, final double max)
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws MaxIterationsExceededException, FunctionEvaluationException, IllegalArgumentException {
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return integrate(f, min, max);
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final UnivariateRealFunction f, final double min, final double max)
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws MaxIterationsExceededException, FunctionEvaluationException, IllegalArgumentException {
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        clearResult();
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyInterval(min, max);
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyIterationCount();
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (minimalIterationCount == 1) {
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double s = (4 * qtrap.stage(f, min, max, 1) - qtrap.stage(f, min, max, 0)) / 3.0;
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            setResult(s, 1);
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return result;
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Simpson's rule requires at least two trapezoid stages.
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double olds = 0;
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double oldt = qtrap.stage(f, min, max, 0);
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i <= maximalIterationCount; ++i) {
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double t = qtrap.stage(f, min, max, i);
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double s = (4 * t - oldt) / 3.0;
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (i >= minimalIterationCount) {
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final double delta = FastMath.abs(s - olds);
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final double rLimit =
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    relativeAccuracy * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5;
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if ((delta <= rLimit) || (delta <= absoluteAccuracy)) {
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    setResult(s, i);
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    return result;
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            olds = s;
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            oldt = t;
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throw new MaxIterationsExceededException(maximalIterationCount);
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    protected void verifyIterationCount() throws IllegalArgumentException {
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super.verifyIterationCount();
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // at most 64 bisection refinements
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (maximalIterationCount > 64) {
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.INVALID_ITERATIONS_LIMITS,
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    0, 64);
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
113