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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.analysis.integration;
19
import org.apache.commons.math.ConvergingAlgorithmImpl;
20
import org.apache.commons.math.MathRuntimeException;
21
import org.apache.commons.math.analysis.UnivariateRealFunction;
24
* Provide a default implementation for several generic functions.
26
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
29
public abstract class UnivariateRealIntegratorImpl
30
extends ConvergingAlgorithmImpl implements UnivariateRealIntegrator {
32
/** Serializable version identifier. */
33
private static final long serialVersionUID = 6248808456637441533L;
35
/** minimum number of iterations */
36
protected int minimalIterationCount;
38
/** default minimum number of iterations */
39
protected int defaultMinimalIterationCount;
41
/** indicates whether an integral has been computed */
42
protected boolean resultComputed = false;
44
/** the last computed integral */
45
protected double result;
47
/** The integrand functione.
48
* @deprecated as of 2.0 the integrand function is passed as an argument
49
* to the {@link #integrate(UnivariateRealFunction, double, double)}method. */
51
protected UnivariateRealFunction f;
54
* Construct an integrator with given iteration count and accuracy.
56
* @param f the integrand function
57
* @param defaultMaximalIterationCount maximum number of iterations
58
* @throws IllegalArgumentException if f is null or the iteration
59
* limits are not valid
60
* @deprecated as of 2.0 the integrand function is passed as an argument
61
* to the {@link #integrate(UnivariateRealFunction, double, double)}method.
64
protected UnivariateRealIntegratorImpl(final UnivariateRealFunction f,
65
final int defaultMaximalIterationCount)
66
throws IllegalArgumentException {
67
super(defaultMaximalIterationCount, 1.0e-15);
69
throw MathRuntimeException.createIllegalArgumentException("function is null");
74
// parameters that are problem specific
75
setRelativeAccuracy(1.0e-6);
76
this.defaultMinimalIterationCount = 3;
77
this.minimalIterationCount = defaultMinimalIterationCount;
79
verifyIterationCount();
83
* Construct an integrator with given iteration count and accuracy.
85
* @param defaultMaximalIterationCount maximum number of iterations
86
* @throws IllegalArgumentException if f is null or the iteration
87
* limits are not valid
89
protected UnivariateRealIntegratorImpl(final int defaultMaximalIterationCount)
90
throws IllegalArgumentException {
91
super(defaultMaximalIterationCount, 1.0e-15);
93
// parameters that are problem specific
94
setRelativeAccuracy(1.0e-6);
95
this.defaultMinimalIterationCount = 3;
96
this.minimalIterationCount = defaultMinimalIterationCount;
98
verifyIterationCount();
102
* Access the last computed integral.
104
* @return the last computed integral
105
* @throws IllegalStateException if no integral has been computed
107
public double getResult() throws IllegalStateException {
108
if (resultComputed) {
111
throw MathRuntimeException.createIllegalStateException("no result available");
116
* Convenience function for implementations.
118
* @param result the result to set
119
* @param iterationCount the iteration count to set
121
protected final void setResult(double result, int iterationCount) {
122
this.result = result;
123
this.iterationCount = iterationCount;
124
this.resultComputed = true;
128
* Convenience function for implementations.
130
protected final void clearResult() {
131
this.iterationCount = 0;
132
this.resultComputed = false;
136
public void setMinimalIterationCount(int count) {
137
minimalIterationCount = count;
141
public int getMinimalIterationCount() {
142
return minimalIterationCount;
146
public void resetMinimalIterationCount() {
147
minimalIterationCount = defaultMinimalIterationCount;
151
* Verifies that the endpoints specify an interval.
153
* @param lower lower endpoint
154
* @param upper upper endpoint
155
* @throws IllegalArgumentException if not interval
157
protected void verifyInterval(double lower, double upper) throws
158
IllegalArgumentException {
159
if (lower >= upper) {
160
throw MathRuntimeException.createIllegalArgumentException(
161
"endpoints do not specify an interval: [{0}, {1}]",
167
* Verifies that the upper and lower limits of iterations are valid.
169
* @throws IllegalArgumentException if not valid
171
protected void verifyIterationCount() throws IllegalArgumentException {
172
if ((minimalIterationCount <= 0) || (maximalIterationCount <= minimalIterationCount)) {
173
throw MathRuntimeException.createIllegalArgumentException(
174
"invalid iteration limits: min={0}, max={1}",
175
minimalIterationCount, maximalIterationCount);