Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
UnivariateRealIntegratorImpl |
|
| 1.7058823529411764;1.706 |
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 java.io.Serializable; |
|
19 | ||
20 | /** |
|
21 | * Provide a default implementation for several generic functions. |
|
22 | * |
|
23 | * @version $Revision$ $Date: 2005-08-22 12:58:57 -0700 (Mon, 22 Aug 2005) $ |
|
24 | */ |
|
25 | public abstract class UnivariateRealIntegratorImpl implements |
|
26 | UnivariateRealIntegrator, Serializable { |
|
27 | ||
28 | /** serializable version identifier */ |
|
29 | static final long serialVersionUID = -3365294665201465048L; |
|
30 | ||
31 | /** maximum relative error */ |
|
32 | protected double relativeAccuracy; |
|
33 | ||
34 | /** maximum number of iterations */ |
|
35 | protected int maximalIterationCount; |
|
36 | ||
37 | /** minimum number of iterations */ |
|
38 | protected int minimalIterationCount; |
|
39 | ||
40 | /** default maximum relative error */ |
|
41 | protected double defaultRelativeAccuracy; |
|
42 | ||
43 | /** default maximum number of iterations */ |
|
44 | protected int defaultMaximalIterationCount; |
|
45 | ||
46 | /** default minimum number of iterations */ |
|
47 | protected int defaultMinimalIterationCount; |
|
48 | ||
49 | /** indicates whether an integral has been computed */ |
|
50 | 38 | protected boolean resultComputed = false; |
51 | ||
52 | /** the last computed integral */ |
|
53 | protected double result; |
|
54 | ||
55 | /** the last iteration count */ |
|
56 | protected int iterationCount; |
|
57 | ||
58 | /** the integrand function */ |
|
59 | protected UnivariateRealFunction f; |
|
60 | ||
61 | /** |
|
62 | * Construct an integrator with given iteration count and accuracy. |
|
63 | * |
|
64 | * @param f the integrand function |
|
65 | * @param defaultMaximalIterationCount maximum number of iterations |
|
66 | * @throws IllegalArgumentException if f is null or the iteration |
|
67 | * limits are not valid |
|
68 | */ |
|
69 | protected UnivariateRealIntegratorImpl( |
|
70 | UnivariateRealFunction f, |
|
71 | 38 | int defaultMaximalIterationCount) throws IllegalArgumentException { |
72 | ||
73 | 38 | if (f == null) { |
74 | 0 | throw new IllegalArgumentException("Function can not be null."); |
75 | } |
|
76 | ||
77 | 38 | this.f = f; |
78 | // parameters that may depend on algorithm |
|
79 | 38 | this.defaultMaximalIterationCount = defaultMaximalIterationCount; |
80 | 38 | this.maximalIterationCount = defaultMaximalIterationCount; |
81 | // parameters that are problem specific |
|
82 | 38 | this.defaultRelativeAccuracy = 1E-6; |
83 | 38 | this.relativeAccuracy = defaultRelativeAccuracy; |
84 | 38 | this.defaultMinimalIterationCount = 3; |
85 | 38 | this.minimalIterationCount = defaultMinimalIterationCount; |
86 | ||
87 | 38 | verifyIterationCount(); |
88 | 38 | } |
89 | ||
90 | /** |
|
91 | * Access the last computed integral. |
|
92 | * |
|
93 | * @return the last computed integral |
|
94 | * @throws IllegalStateException if no integral has been computed |
|
95 | */ |
|
96 | public double getResult() throws IllegalStateException { |
|
97 | 0 | if (resultComputed) { |
98 | 0 | return result; |
99 | } else { |
|
100 | 0 | throw new IllegalStateException("No result available."); |
101 | } |
|
102 | } |
|
103 | ||
104 | /** |
|
105 | * Access the last iteration count. |
|
106 | * |
|
107 | * @return the last iteration count |
|
108 | * @throws IllegalStateException if no integral has been computed |
|
109 | */ |
|
110 | public int getIterationCount() throws IllegalStateException { |
|
111 | 0 | if (resultComputed) { |
112 | 0 | return iterationCount; |
113 | } else { |
|
114 | 0 | throw new IllegalStateException("No result available."); |
115 | } |
|
116 | } |
|
117 | ||
118 | /** |
|
119 | * Convenience function for implementations. |
|
120 | * |
|
121 | * @param result the result to set |
|
122 | * @param iterationCount the iteration count to set |
|
123 | */ |
|
124 | protected final void setResult(double result, int iterationCount) { |
|
125 | 30 | this.result = result; |
126 | 30 | this.iterationCount = iterationCount; |
127 | 30 | this.resultComputed = true; |
128 | 30 | } |
129 | ||
130 | /** |
|
131 | * Convenience function for implementations. |
|
132 | */ |
|
133 | protected final void clearResult() { |
|
134 | 48 | this.resultComputed = false; |
135 | 48 | } |
136 | ||
137 | /** |
|
138 | * Set the upper limit for the number of iterations. |
|
139 | * |
|
140 | * @param count maximum number of iterations |
|
141 | */ |
|
142 | public void setMaximalIterationCount(int count) { |
|
143 | 12 | maximalIterationCount = count; |
144 | 12 | } |
145 | ||
146 | /** |
|
147 | * Get the upper limit for the number of iterations. |
|
148 | * |
|
149 | * @return the actual upper limit |
|
150 | */ |
|
151 | public int getMaximalIterationCount() { |
|
152 | 0 | return maximalIterationCount; |
153 | } |
|
154 | ||
155 | /** |
|
156 | * Reset the upper limit for the number of iterations to the default. |
|
157 | */ |
|
158 | public void resetMaximalIterationCount() { |
|
159 | 0 | maximalIterationCount = defaultMaximalIterationCount; |
160 | 0 | } |
161 | ||
162 | /** |
|
163 | * Set the lower limit for the number of iterations. |
|
164 | * |
|
165 | * @param count minimum number of iterations |
|
166 | */ |
|
167 | public void setMinimalIterationCount(int count) { |
|
168 | 12 | minimalIterationCount = count; |
169 | 12 | } |
170 | ||
171 | /** |
|
172 | * Get the lower limit for the number of iterations. |
|
173 | * |
|
174 | * @return the actual lower limit |
|
175 | */ |
|
176 | public int getMinimalIterationCount() { |
|
177 | 0 | return minimalIterationCount; |
178 | } |
|
179 | ||
180 | /** |
|
181 | * Reset the lower limit for the number of iterations to the default. |
|
182 | */ |
|
183 | public void resetMinimalIterationCount() { |
|
184 | 0 | minimalIterationCount = defaultMinimalIterationCount; |
185 | 0 | } |
186 | ||
187 | /** |
|
188 | * Set the relative accuracy. |
|
189 | * |
|
190 | * @param accuracy the relative accuracy |
|
191 | * @throws IllegalArgumentException if the accuracy can't be achieved by |
|
192 | * the integrator or is otherwise deemed unreasonable |
|
193 | */ |
|
194 | public void setRelativeAccuracy(double accuracy) { |
|
195 | 0 | relativeAccuracy = accuracy; |
196 | 0 | } |
197 | ||
198 | /** |
|
199 | * Get the actual relative accuracy. |
|
200 | * |
|
201 | * @return the accuracy |
|
202 | */ |
|
203 | public double getRelativeAccuracy() { |
|
204 | 30 | return relativeAccuracy; |
205 | } |
|
206 | ||
207 | /** |
|
208 | * Reset the relative accuracy to the default. |
|
209 | */ |
|
210 | public void resetRelativeAccuracy() { |
|
211 | 0 | relativeAccuracy = defaultRelativeAccuracy; |
212 | 0 | } |
213 | ||
214 | /** |
|
215 | * Returns true if the arguments form a (strictly) increasing sequence |
|
216 | * |
|
217 | * @param start first number |
|
218 | * @param mid second number |
|
219 | * @param end third number |
|
220 | * @return true if the arguments form an increasing sequence |
|
221 | */ |
|
222 | protected boolean isSequence(double start, double mid, double end) { |
|
223 | 80 | return (start < mid) && (mid < end); |
224 | } |
|
225 | ||
226 | /** |
|
227 | * Verifies that the endpoints specify an interval. |
|
228 | * |
|
229 | * @param lower lower endpoint |
|
230 | * @param upper upper endpoint |
|
231 | * @throws IllegalArgumentException if not interval |
|
232 | */ |
|
233 | protected void verifyInterval(double lower, double upper) throws |
|
234 | IllegalArgumentException { |
|
235 | 48 | if (lower >= upper) { |
236 | 6 | throw new IllegalArgumentException |
237 | ("Endpoints do not specify an interval: [" + lower + |
|
238 | ", " + upper + "]"); |
|
239 | } |
|
240 | 42 | } |
241 | ||
242 | /** |
|
243 | * Verifies that the upper and lower limits of iterations are valid. |
|
244 | * |
|
245 | * @throws IllegalArgumentException if not valid |
|
246 | */ |
|
247 | protected void verifyIterationCount() throws IllegalArgumentException { |
|
248 | 80 | if (!isSequence(0, minimalIterationCount, maximalIterationCount+1)) { |
249 | 6 | throw new IllegalArgumentException |
250 | ("Invalid iteration limits: min=" + minimalIterationCount + |
|
251 | " max=" + maximalIterationCount); |
|
252 | } |
|
253 | 74 | } |
254 | } |