1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis;
18
19 import java.io.Serializable;
20
21
22
23
24
25
26
27 public abstract class UnivariateRealIntegratorImpl implements
28 UnivariateRealIntegrator, Serializable {
29
30
31 static final long serialVersionUID = -3365294665201465048L;
32
33
34 protected double relativeAccuracy;
35
36
37 protected int maximalIterationCount;
38
39
40 protected int minimalIterationCount;
41
42
43 protected double defaultRelativeAccuracy;
44
45
46 protected int defaultMaximalIterationCount;
47
48
49 protected int defaultMinimalIterationCount;
50
51
52 protected boolean resultComputed = false;
53
54
55 protected double result;
56
57
58 protected int iterationCount;
59
60
61 protected UnivariateRealFunction f;
62
63
64
65
66
67
68
69
70
71 protected UnivariateRealIntegratorImpl(
72 UnivariateRealFunction f,
73 int defaultMaximalIterationCount) throws IllegalArgumentException {
74
75 if (f == null) {
76 throw new IllegalArgumentException("Function can not be null.");
77 }
78
79 this.f = f;
80
81 this.defaultMaximalIterationCount = defaultMaximalIterationCount;
82 this.maximalIterationCount = defaultMaximalIterationCount;
83
84 this.defaultRelativeAccuracy = 1E-6;
85 this.relativeAccuracy = defaultRelativeAccuracy;
86 this.defaultMinimalIterationCount = 3;
87 this.minimalIterationCount = defaultMinimalIterationCount;
88
89 verifyIterationCount();
90 }
91
92
93
94
95
96
97
98 public double getResult() throws IllegalStateException {
99 if (resultComputed) {
100 return result;
101 } else {
102 throw new IllegalStateException("No result available.");
103 }
104 }
105
106
107
108
109
110
111
112 public int getIterationCount() throws IllegalStateException {
113 if (resultComputed) {
114 return iterationCount;
115 } else {
116 throw new IllegalStateException("No result available.");
117 }
118 }
119
120
121
122
123
124
125
126 protected final void setResult(double result, int iterationCount) {
127 this.result = result;
128 this.iterationCount = iterationCount;
129 this.resultComputed = true;
130 }
131
132
133
134
135 protected final void clearResult() {
136 this.resultComputed = false;
137 }
138
139
140
141
142
143
144 public void setMaximalIterationCount(int count) {
145 maximalIterationCount = count;
146 }
147
148
149
150
151
152
153 public int getMaximalIterationCount() {
154 return maximalIterationCount;
155 }
156
157
158
159
160 public void resetMaximalIterationCount() {
161 maximalIterationCount = defaultMaximalIterationCount;
162 }
163
164
165
166
167
168
169 public void setMinimalIterationCount(int count) {
170 minimalIterationCount = count;
171 }
172
173
174
175
176
177
178 public int getMinimalIterationCount() {
179 return minimalIterationCount;
180 }
181
182
183
184
185 public void resetMinimalIterationCount() {
186 minimalIterationCount = defaultMinimalIterationCount;
187 }
188
189
190
191
192
193
194
195
196 public void setRelativeAccuracy(double accuracy) {
197 relativeAccuracy = accuracy;
198 }
199
200
201
202
203
204
205 public double getRelativeAccuracy() {
206 return relativeAccuracy;
207 }
208
209
210
211
212 public void resetRelativeAccuracy() {
213 relativeAccuracy = defaultRelativeAccuracy;
214 }
215
216
217
218
219
220
221
222
223
224 protected boolean isSequence(double start, double mid, double end) {
225 return (start < mid) && (mid < end);
226 }
227
228
229
230
231
232
233
234
235 protected void verifyInterval(double lower, double upper) throws
236 IllegalArgumentException {
237 if (lower >= upper) {
238 throw new IllegalArgumentException
239 ("Endpoints do not specify an interval: [" + lower +
240 ", " + upper + "]");
241 }
242 }
243
244
245
246
247
248
249 protected void verifyIterationCount() throws IllegalArgumentException {
250 if (!isSequence(0, minimalIterationCount, maximalIterationCount+1)) {
251 throw new IllegalArgumentException
252 ("Invalid iteration limits: min=" + minimalIterationCount +
253 " max=" + maximalIterationCount);
254 }
255 }
256 }