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/TrapezoidalRule.html">
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Trapezoidal 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 * The function should be integrable.</p>
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class TrapezoidIntegrator extends UnivariateRealIntegratorImpl {
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Intermediate result. */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private double s;
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct an integrator for the given function.
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param f function to integrate
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.0 the integrand function is passed as an argument
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@link #integrate(UnivariateRealFunction, double, double)}method.
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public TrapezoidIntegrator(UnivariateRealFunction f) {
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(f, 64);
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct an integrator.
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public TrapezoidIntegrator() {
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super(64);
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Compute the n-th stage integral of trapezoid rule. This function
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * should only be called by API <code>integrate()</code> in the package.
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * To save time it does not verify arguments - caller does.
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The interval is divided equally into 2^n sections rather than an
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * arbitrary m sections because this configuration can best utilize the
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * alrealy computed values.</p>
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param f the integrand function
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param min the lower bound for the interval
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param max the upper bound for the interval
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n the stage of 1/2 refinement, n = 0 is no refinement
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the value of n-th stage integral
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws FunctionEvaluationException if an error occurs evaluating the function
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double stage(final UnivariateRealFunction f,
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 final double min, final double max, final int n)
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws FunctionEvaluationException {
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n == 0) {
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            s = 0.5 * (max - min) * (f.value(min) + f.value(max));
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return s;
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final long np = 1L << (n-1);           // number of new points in this stage
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double sum = 0;
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double spacing = (max - min) / np; // spacing between adjacent new points
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double x = min + 0.5 * spacing;    // the first new point
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (long i = 0; i < np; i++) {
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                sum += f.value(x);
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                x += spacing;
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // add the new sum to previously calculated result
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            s = 0.5 * (s + sum * spacing);
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return s;
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final double min, final double max)
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws MaxIterationsExceededException, FunctionEvaluationException, IllegalArgumentException {
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return integrate(f, min, max);
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double integrate(final UnivariateRealFunction f, final double min, final double max)
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws MaxIterationsExceededException, FunctionEvaluationException, IllegalArgumentException {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        clearResult();
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyInterval(min, max);
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        verifyIterationCount();
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double oldt = stage(f, min, max, 0);
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i <= maximalIterationCount; ++i) {
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double t = stage(f, min, max, i);
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (i >= minimalIterationCount) {
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final double delta = FastMath.abs(t - oldt);
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final double rLimit =
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    relativeAccuracy * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5;
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if ((delta <= rLimit) || (delta <= absoluteAccuracy)) {
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    setResult(t, i);
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    return result;
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            oldt = t;
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throw new MaxIterationsExceededException(maximalIterationCount);
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    protected void verifyIterationCount() throws IllegalArgumentException {
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super.verifyIterationCount();
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // at most 64 bisection refinements
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (maximalIterationCount > 64) {
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.INVALID_ITERATIONS_LIMITS,
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    0, 64);
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
143