1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.math4.legacy.analysis.integration; 18 19 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException; 20 import org.apache.commons.math4.core.jdkmath.JdkMath; 21 22 /** 23 * Implements <a href="http://mathworld.wolfram.com/SimpsonsRule.html"> 24 * Simpson's Rule</a> for integration of real univariate functions. 25 * 26 * See <b>Introduction to Numerical Analysis</b>, ISBN 038795452X, chapter 3. 27 * 28 * <p> 29 * This implementation employs the basic trapezoid rule to calculate Simpson's 30 * rule. 31 * 32 * <p> 33 * <em>Caveat:</em> At each iteration, the algorithm refines the estimation by 34 * evaluating the function twice as many times as in the previous iteration; 35 * When specifying a {@link #integrate(int,UnivariateFunction,double,double) 36 * maximum number of function evaluations}, the caller must ensure that it 37 * is compatible with the {@link #SimpsonIntegrator(int,int) requested minimal 38 * number of iterations}. 39 * 40 * @since 1.2 41 */ 42 public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { 43 /** Maximal number of iterations for Simpson. */ 44 private static final int SIMPSON_MAX_ITERATIONS_COUNT = 30; 45 46 /** 47 * Build a Simpson integrator with given accuracies and iterations counts. 48 * @param relativeAccuracy relative accuracy of the result 49 * @param absoluteAccuracy absolute accuracy of the result 50 * @param minimalIterationCount Minimum number of iterations. 51 * @param maximalIterationCount Maximum number of iterations. 52 * It must be less than or equal to 30. 53 * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException 54 * if {@code minimalIterationCount <= 0}. 55 * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException 56 * if {@code maximalIterationCount < minimalIterationCount}. 57 * is lesser than or equal to the minimal number of iterations 58 * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. 59 */ 60 public SimpsonIntegrator(final double relativeAccuracy, 61 final double absoluteAccuracy, 62 final int minimalIterationCount, 63 final int maximalIterationCount) { 64 super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); 65 if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { 66 throw new NumberIsTooLargeException(maximalIterationCount, 67 SIMPSON_MAX_ITERATIONS_COUNT, false); 68 } 69 } 70 71 /** 72 * Build a Simpson integrator with given iteration counts. 73 * @param minimalIterationCount Minimum number of iterations. 74 * @param maximalIterationCount Maximum number of iterations. 75 * It must be less than or equal to 30. 76 * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException 77 * if {@code minimalIterationCount <= 0}. 78 * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException 79 * if {@code maximalIterationCount < minimalIterationCount}. 80 * is lesser than or equal to the minimal number of iterations 81 * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. 82 */ 83 public SimpsonIntegrator(final int minimalIterationCount, 84 final int maximalIterationCount) { 85 super(minimalIterationCount, maximalIterationCount); 86 if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { 87 throw new NumberIsTooLargeException(maximalIterationCount, 88 SIMPSON_MAX_ITERATIONS_COUNT, false); 89 } 90 } 91 92 /** 93 * Construct an integrator with default settings. 94 */ 95 public SimpsonIntegrator() { 96 super(DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT); 97 } 98 99 /** {@inheritDoc} */ 100 @Override 101 protected double doIntegrate() { 102 // Simpson's rule requires at least two trapezoid stages. 103 // So we set the first sum using two trapezoid stages. 104 final TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); 105 106 final double s0 = qtrap.stage(this, 0); 107 double oldt = qtrap.stage(this, 1); 108 double olds = (4 * oldt - s0) / 3.0; 109 while (true) { 110 // The first iteration is the first refinement of the sum. 111 iterations.increment(); 112 final int i = getIterations(); 113 final double t = qtrap.stage(this, i + 1); // 1-stage ahead of the iteration 114 final double s = (4 * t - oldt) / 3.0; 115 if (i >= getMinimalIterationCount()) { 116 final double delta = JdkMath.abs(s - olds); 117 final double rLimit = getRelativeAccuracy() * (JdkMath.abs(olds) + JdkMath.abs(s)) * 0.5; 118 if (delta <= rLimit || 119 delta <= getAbsoluteAccuracy()) { 120 return s; 121 } 122 } 123 olds = s; 124 oldt = t; 125 } 126 } 127 }