Coverage Report - org.apache.commons.math.analysis.TrapezoidIntegrator

Classes in this File Line Coverage Branch Coverage Complexity
TrapezoidIntegrator
97% 
100% 
3.5

 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/TrapezoidalRule.html">
 23  
  * Trapezoidal 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  
  * The function should be integrable.
 28  
  *  
 29  
  * @version $Revision$ $Date: 2005-08-24 15:27:44 -0700 (Wed, 24 Aug 2005) $
 30  
  */
 31  
 public class TrapezoidIntegrator extends UnivariateRealIntegratorImpl {
 32  
 
 33  
     /** serializable version identifier */
 34  
     static final long serialVersionUID = 4978222553983172543L;
 35  
 
 36  
     /** intermediate result */
 37  
     private double s;
 38  
 
 39  
     /**
 40  
      * Construct an integrator for the given function.
 41  
      * 
 42  
      * @param f function to integrate
 43  
      */
 44  
     public TrapezoidIntegrator(UnivariateRealFunction f) {
 45  26
         super(f, 64);
 46  26
     }
 47  
 
 48  
     /**
 49  
      * Compute the n-th stage integral of trapezoid rule. This function
 50  
      * should only be called by API <code>integrate()</code> in the package.
 51  
      * To save time it does not verify arguments - caller does.
 52  
      * <p>
 53  
      * The interval is divided equally into 2^n sections rather than an
 54  
      * arbitrary m sections because this configuration can best utilize the
 55  
      * alrealy computed values.
 56  
      *
 57  
      * @param min the lower bound for the interval
 58  
      * @param max the upper bound for the interval
 59  
      * @param n the stage of 1/2 refinement, n = 0 is no refinement
 60  
      * @return the value of n-th stage integral
 61  
      * @throws FunctionEvaluationException if an error occurs evaluating the
 62  
      * function
 63  
      */
 64  
     double stage(double min, double max, int n) throws
 65  
         FunctionEvaluationException {
 66  
         
 67  
         long i, np;
 68  238
         double x, spacing, sum = 0;
 69  
         
 70  238
         if (n == 0) {
 71  30
             s = 0.5 * (max - min) * (f.value(min) + f.value(max));
 72  30
             return s;
 73  
         } else {
 74  208
             np = 1L << (n-1);           // number of new points in this stage
 75  208
             spacing = (max - min) / np; // spacing between adjacent new points
 76  208
             x = min + 0.5 * spacing;    // the first new point
 77  27762
             for (i = 0; i < np; i++) {
 78  27554
                 sum += f.value(x);
 79  27554
                 x += spacing;
 80  
             }
 81  
             // add the new sum to previously calculated result
 82  208
             s = 0.5 * (s + sum * spacing);
 83  208
             return s;
 84  
         }
 85  
     }
 86  
 
 87  
     /**
 88  
      * Integrate the function in the given interval.
 89  
      * 
 90  
      * @param min the lower bound for the interval
 91  
      * @param max the upper bound for the interval
 92  
      * @return the value of integral
 93  
      * @throws ConvergenceException if the maximum iteration count is exceeded
 94  
      * or the integrator detects convergence problems otherwise
 95  
      * @throws FunctionEvaluationException if an error occurs evaluating the
 96  
      * function
 97  
      * @throws IllegalArgumentException if any parameters are invalid
 98  
      */
 99  
     public double integrate(double min, double max) throws ConvergenceException,
 100  
         FunctionEvaluationException, IllegalArgumentException {
 101  
         
 102  16
         int i = 1;
 103  
         double t, oldt;
 104  
         
 105  16
         clearResult();
 106  16
         verifyInterval(min, max);
 107  14
         verifyIterationCount();
 108  
 
 109  10
         oldt = stage(min, max, 0);
 110  112
         while (i <= maximalIterationCount) {
 111  112
             t = stage(min, max, i);
 112  112
             if (i >= minimalIterationCount) {
 113  92
                 if (Math.abs(t - oldt) <= Math.abs(relativeAccuracy * oldt)) {
 114  10
                     setResult(t, i);
 115  10
                     return result;
 116  
                 }
 117  
             }
 118  102
             oldt = t;
 119  102
             i++;
 120  
         }
 121  0
         throw new ConvergenceException("Maximum number of iterations exceeded.");
 122  
     }
 123  
 
 124  
     /**
 125  
      * Verifies that the iteration limits are valid and within the range.
 126  
      * 
 127  
      * @throws IllegalArgumentException if not
 128  
      */
 129  
     protected void verifyIterationCount() throws IllegalArgumentException {
 130  40
         super.verifyIterationCount();
 131  
         // at most 64 bisection refinements
 132  38
         if (maximalIterationCount > 64) {
 133  2
             throw new IllegalArgumentException
 134  
                 ("Iteration upper limit out of [0, 64] range: " +
 135  
                 maximalIterationCount);
 136  
         }
 137  36
     }
 138  
 }