Home | History | Annotate | Download | only in integration
      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.math.analysis.integration;
     18 
     19 import org.apache.commons.math.ConvergingAlgorithmImpl;
     20 import org.apache.commons.math.MathRuntimeException;
     21 import org.apache.commons.math.analysis.UnivariateRealFunction;
     22 import org.apache.commons.math.exception.util.LocalizedFormats;
     23 import org.apache.commons.math.exception.NullArgumentException;
     24 
     25 /**
     26  * Provide a default implementation for several generic functions.
     27  *
     28  * @version $Revision: 1072409 $ $Date: 2011-02-19 19:50:36 +0100 (sam. 19 fvr. 2011) $
     29  * @since 1.2
     30  */
     31 public abstract class UnivariateRealIntegratorImpl
     32     extends ConvergingAlgorithmImpl implements UnivariateRealIntegrator {
     33 
     34     /** Serializable version identifier. */
     35     private static final long serialVersionUID = 6248808456637441533L;
     36 
     37     /** minimum number of iterations */
     38     protected int minimalIterationCount;
     39 
     40     /** default minimum number of iterations */
     41     protected int defaultMinimalIterationCount;
     42 
     43     /** indicates whether an integral has been computed */
     44     protected boolean resultComputed = false;
     45 
     46     /** the last computed integral */
     47     protected double result;
     48 
     49     /**
     50      * The integrand function.
     51      *
     52      * @deprecated as of 2.0 the integrand function is passed as an argument
     53      * to the {@link #integrate(UnivariateRealFunction, double, double)}method.
     54      */
     55     @Deprecated
     56     protected UnivariateRealFunction f;
     57 
     58     /**
     59      * Construct an integrator with given iteration count and accuracy.
     60      *
     61      * @param f the integrand function
     62      * @param defaultMaximalIterationCount maximum number of iterations
     63      * @throws IllegalArgumentException if f is null or the iteration
     64      * limits are not valid
     65      * @deprecated as of 2.0 the integrand function is passed as an argument
     66      * to the {@link #integrate(UnivariateRealFunction, double, double)}method.
     67      */
     68     @Deprecated
     69     protected UnivariateRealIntegratorImpl(final UnivariateRealFunction f,
     70                                            final int defaultMaximalIterationCount)
     71         throws IllegalArgumentException {
     72         super(defaultMaximalIterationCount, 1.0e-15);
     73         if (f == null) {
     74             throw new NullArgumentException(LocalizedFormats.FUNCTION);
     75         }
     76 
     77         this.f = f;
     78 
     79         // parameters that are problem specific
     80         setRelativeAccuracy(1.0e-6);
     81         this.defaultMinimalIterationCount = 3;
     82         this.minimalIterationCount = defaultMinimalIterationCount;
     83 
     84         verifyIterationCount();
     85     }
     86 
     87     /**
     88      * Construct an integrator with given iteration count and accuracy.
     89      *
     90      * @param defaultMaximalIterationCount maximum number of iterations
     91      * @throws IllegalArgumentException if f is null or the iteration
     92      * limits are not valid
     93      */
     94     protected UnivariateRealIntegratorImpl(final int defaultMaximalIterationCount)
     95         throws IllegalArgumentException {
     96         super(defaultMaximalIterationCount, 1.0e-15);
     97 
     98         // parameters that are problem specific
     99         setRelativeAccuracy(1.0e-6);
    100         this.defaultMinimalIterationCount = 3;
    101         this.minimalIterationCount = defaultMinimalIterationCount;
    102 
    103         verifyIterationCount();
    104     }
    105 
    106     /**
    107      * Access the last computed integral.
    108      *
    109      * @return the last computed integral
    110      * @throws IllegalStateException if no integral has been computed
    111      */
    112     public double getResult() throws IllegalStateException {
    113         if (resultComputed) {
    114             return result;
    115         } else {
    116             throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_RESULT_AVAILABLE);
    117         }
    118     }
    119 
    120     /**
    121      * Convenience function for implementations.
    122      *
    123      * @param newResult the result to set
    124      * @param iterationCount the iteration count to set
    125      */
    126     protected final void setResult(double newResult, int iterationCount) {
    127         this.result         = newResult;
    128         this.iterationCount = iterationCount;
    129         this.resultComputed = true;
    130     }
    131 
    132     /**
    133      * Convenience function for implementations.
    134      */
    135     protected final void clearResult() {
    136         this.iterationCount = 0;
    137         this.resultComputed = false;
    138     }
    139 
    140     /** {@inheritDoc} */
    141     public void setMinimalIterationCount(int count) {
    142         minimalIterationCount = count;
    143     }
    144 
    145     /** {@inheritDoc} */
    146     public int getMinimalIterationCount() {
    147         return minimalIterationCount;
    148     }
    149 
    150     /** {@inheritDoc} */
    151     public void resetMinimalIterationCount() {
    152         minimalIterationCount = defaultMinimalIterationCount;
    153     }
    154 
    155     /**
    156      * Verifies that the endpoints specify an interval.
    157      *
    158      * @param lower lower endpoint
    159      * @param upper upper endpoint
    160      * @throws IllegalArgumentException if not interval
    161      */
    162     protected void verifyInterval(double lower, double upper) throws
    163         IllegalArgumentException {
    164         if (lower >= upper) {
    165             throw MathRuntimeException.createIllegalArgumentException(
    166                     LocalizedFormats.ENDPOINTS_NOT_AN_INTERVAL,
    167                     lower, upper);
    168         }
    169     }
    170 
    171     /**
    172      * Verifies that the upper and lower limits of iterations are valid.
    173      *
    174      * @throws IllegalArgumentException if not valid
    175      */
    176     protected void verifyIterationCount() throws IllegalArgumentException {
    177         if ((minimalIterationCount <= 0) || (maximalIterationCount <= minimalIterationCount)) {
    178             throw MathRuntimeException.createIllegalArgumentException(
    179                     LocalizedFormats.INVALID_ITERATIONS_LIMITS,
    180                     minimalIterationCount, maximalIterationCount);
    181         }
    182     }
    183 }
    184