| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| SimpsonIntegrator |
|
| 4.0;4 |
| 1 | /* |
|
| 2 | * Copyright 2005 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package org.apache.commons.math.analysis; |
|
| 17 | ||
| 18 | import org.apache.commons.math.ConvergenceException; |
|
| 19 | import org.apache.commons.math.FunctionEvaluationException; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * Implements the <a href="http://mathworld.wolfram.com/SimpsonsRule.html"> |
|
| 23 | * Simpson's Rule</a> for integration of real univariate functions. For |
|
| 24 | * reference, see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X, |
|
| 25 | * chapter 3. |
|
| 26 | * <p> |
|
| 27 | * This implementation employs basic trapezoid rule as building blocks to |
|
| 28 | * calculate the Simpson's rule of alternating 2/3 and 4/3. |
|
| 29 | * |
|
| 30 | * @version $Revision$ $Date: 2005-08-24 15:27:44 -0700 (Wed, 24 Aug 2005) $ |
|
| 31 | */ |
|
| 32 | public class SimpsonIntegrator extends UnivariateRealIntegratorImpl { |
|
| 33 | ||
| 34 | /** serializable version identifier */ |
|
| 35 | static final long serialVersionUID = 3405465123320678216L; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Construct an integrator for the given function. |
|
| 39 | * |
|
| 40 | * @param f function to integrate |
|
| 41 | */ |
|
| 42 | public SimpsonIntegrator(UnivariateRealFunction f) { |
|
| 43 | 6 | super(f, 64); |
| 44 | 6 | } |
| 45 | ||
| 46 | /** |
|
| 47 | * Integrate the function in the given interval. |
|
| 48 | * |
|
| 49 | * @param min the lower bound for the interval |
|
| 50 | * @param max the upper bound for the interval |
|
| 51 | * @return the value of integral |
|
| 52 | * @throws ConvergenceException if the maximum iteration count is exceeded |
|
| 53 | * or the integrator detects convergence problems otherwise |
|
| 54 | * @throws FunctionEvaluationException if an error occurs evaluating the |
|
| 55 | * function |
|
| 56 | * @throws IllegalArgumentException if any parameters are invalid |
|
| 57 | */ |
|
| 58 | public double integrate(double min, double max) throws ConvergenceException, |
|
| 59 | FunctionEvaluationException, IllegalArgumentException { |
|
| 60 | ||
| 61 | 16 | int i = 1; |
| 62 | double s, olds, t, oldt; |
|
| 63 | ||
| 64 | 16 | clearResult(); |
| 65 | 16 | verifyInterval(min, max); |
| 66 | 14 | verifyIterationCount(); |
| 67 | ||
| 68 | 10 | TrapezoidIntegrator qtrap = new TrapezoidIntegrator(this.f); |
| 69 | 10 | if (minimalIterationCount == 1) { |
| 70 | 0 | s = (4 * qtrap.stage(min, max, 1) - qtrap.stage(min, max, 0)) / 3.0; |
| 71 | 0 | setResult(s, 1); |
| 72 | 0 | return result; |
| 73 | } |
|
| 74 | // Simpson's rule requires at least two trapezoid stages. |
|
| 75 | 10 | olds = 0; |
| 76 | 10 | oldt = qtrap.stage(min, max, 0); |
| 77 | 62 | while (i <= maximalIterationCount) { |
| 78 | 62 | t = qtrap.stage(min, max, i); |
| 79 | 62 | s = (4 * t - oldt) / 3.0; |
| 80 | 62 | if (i >= minimalIterationCount) { |
| 81 | 42 | if (Math.abs(s - olds) <= Math.abs(relativeAccuracy * olds)) { |
| 82 | 10 | setResult(s, i); |
| 83 | 10 | return result; |
| 84 | } |
|
| 85 | } |
|
| 86 | 52 | olds = s; |
| 87 | 52 | oldt = t; |
| 88 | 52 | i++; |
| 89 | } |
|
| 90 | 0 | throw new ConvergenceException("Maximum number of iterations exceeded."); |
| 91 | } |
|
| 92 | ||
| 93 | /** |
|
| 94 | * Verifies that the iteration limits are valid and within the range. |
|
| 95 | * |
|
| 96 | * @throws IllegalArgumentException if not |
|
| 97 | */ |
|
| 98 | protected void verifyIterationCount() throws IllegalArgumentException { |
|
| 99 | 20 | super.verifyIterationCount(); |
| 100 | // at most 64 bisection refinements |
|
| 101 | 18 | if (maximalIterationCount > 64) { |
| 102 | 2 | throw new IllegalArgumentException |
| 103 | ("Iteration upper limit out of [0, 64] range: " + |
|
| 104 | maximalIterationCount); |
|
| 105 | } |
|
| 106 | 16 | } |
| 107 | } |