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
23 /**
24 * Provide a default implementation for several generic functions.
25 *
26 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
27 * @since 1.2
28 */
29 public abstract class UnivariateRealIntegratorImpl
30 extends ConvergingAlgorithmImpl implements UnivariateRealIntegrator {
31
32 /** Serializable version identifier. */
33 private static final long serialVersionUID = 6248808456637441533L;
34
35 /** minimum number of iterations */
36 protected int minimalIterationCount;
37
38 /** default minimum number of iterations */
39 protected int defaultMinimalIterationCount;
40
41 /** indicates whether an integral has been computed */
42 protected boolean resultComputed = false;
43
44 /** the last computed integral */
45 protected double result;
46
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. */
50 @Deprecated
51 protected UnivariateRealFunction f;
52
53 /**
54 * Construct an integrator with given iteration count and accuracy.
55 *
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.
62 */
63 @Deprecated
64 protected UnivariateRealIntegratorImpl(final UnivariateRealFunction f,
65 final int defaultMaximalIterationCount)
66 throws IllegalArgumentException {
67 super(defaultMaximalIterationCount, 1.0e-15);
68 if (f == null) {
69 throw MathRuntimeException.createIllegalArgumentException("function is null");
70 }
71
72 this.f = f;
73
74 // parameters that are problem specific
75 setRelativeAccuracy(1.0e-6);
76 this.defaultMinimalIterationCount = 3;
77 this.minimalIterationCount = defaultMinimalIterationCount;
78
79 verifyIterationCount();
80 }
81
82 /**
83 * Construct an integrator with given iteration count and accuracy.
84 *
85 * @param defaultMaximalIterationCount maximum number of iterations
86 * @throws IllegalArgumentException if f is null or the iteration
87 * limits are not valid
88 */
89 protected UnivariateRealIntegratorImpl(final int defaultMaximalIterationCount)
90 throws IllegalArgumentException {
91 super(defaultMaximalIterationCount, 1.0e-15);
92
93 // parameters that are problem specific
94 setRelativeAccuracy(1.0e-6);
95 this.defaultMinimalIterationCount = 3;
96 this.minimalIterationCount = defaultMinimalIterationCount;
97
98 verifyIterationCount();
99 }
100
101 /**
102 * Access the last computed integral.
103 *
104 * @return the last computed integral
105 * @throws IllegalStateException if no integral has been computed
106 */
107 public double getResult() throws IllegalStateException {
108 if (resultComputed) {
109 return result;
110 } else {
111 throw MathRuntimeException.createIllegalStateException("no result available");
112 }
113 }
114
115 /**
116 * Convenience function for implementations.
117 *
118 * @param result the result to set
119 * @param iterationCount the iteration count to set
120 */
121 protected final void setResult(double result, int iterationCount) {
122 this.result = result;
123 this.iterationCount = iterationCount;
124 this.resultComputed = true;
125 }
126
127 /**
128 * Convenience function for implementations.
129 */
130 protected final void clearResult() {
131 this.iterationCount = 0;
132 this.resultComputed = false;
133 }
134
135 /** {@inheritDoc} */
136 public void setMinimalIterationCount(int count) {
137 minimalIterationCount = count;
138 }
139
140 /** {@inheritDoc} */
141 public int getMinimalIterationCount() {
142 return minimalIterationCount;
143 }
144
145 /** {@inheritDoc} */
146 public void resetMinimalIterationCount() {
147 minimalIterationCount = defaultMinimalIterationCount;
148 }
149
150 /**
151 * Verifies that the endpoints specify an interval.
152 *
153 * @param lower lower endpoint
154 * @param upper upper endpoint
155 * @throws IllegalArgumentException if not interval
156 */
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}]",
162 lower, upper);
163 }
164 }
165
166 /**
167 * Verifies that the upper and lower limits of iterations are valid.
168 *
169 * @throws IllegalArgumentException if not valid
170 */
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);
176 }
177 }
178 }