1 16 17 package org.apache.commons.math.analysis; 18 19 import java.io.Serializable ; 20 21 import org.apache.commons.math.FunctionEvaluationException; 22 23 29 public abstract class UnivariateRealSolverImpl implements UnivariateRealSolver, 30 Serializable { 31 32 33 static final long serialVersionUID = 1112491292565386596L; 34 35 36 protected double absoluteAccuracy; 37 38 39 protected double relativeAccuracy; 40 41 42 protected double functionValueAccuracy; 43 44 45 protected int maximalIterationCount; 46 47 48 protected double defaultAbsoluteAccuracy; 49 50 51 protected double defaultRelativeAccuracy; 52 53 54 protected double defaultFunctionValueAccuracy; 55 56 57 protected int defaultMaximalIterationCount; 58 59 60 protected boolean resultComputed = false; 61 62 63 protected double result; 64 65 67 protected int iterationCount; 68 69 70 protected UnivariateRealFunction f; 71 72 81 protected UnivariateRealSolverImpl( 82 UnivariateRealFunction f, 83 int defaultMaximalIterationCount, 84 double defaultAbsoluteAccuracy) { 85 86 super(); 87 88 if (f == null) { 89 throw new IllegalArgumentException ("function can not be null."); 90 } 91 92 this.f = f; 93 this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy; 94 this.defaultRelativeAccuracy = 1E-14; 95 this.defaultFunctionValueAccuracy = 1E-15; 96 this.absoluteAccuracy = defaultAbsoluteAccuracy; 97 this.relativeAccuracy = defaultRelativeAccuracy; 98 this.functionValueAccuracy = defaultFunctionValueAccuracy; 99 this.defaultMaximalIterationCount = defaultMaximalIterationCount; 100 this.maximalIterationCount = defaultMaximalIterationCount; 101 } 102 103 109 public double getResult() { 110 if (resultComputed) { 111 return result; 112 } else { 113 throw new IllegalStateException ("No result available"); 114 } 115 } 116 117 124 public int getIterationCount() { 125 if (resultComputed) { 126 return iterationCount; 127 } else { 128 throw new IllegalStateException ("No result available"); 129 } 130 } 131 132 138 protected final void setResult(double result, int iterationCount) { 139 this.result = result; 140 this.iterationCount = iterationCount; 141 this.resultComputed = true; 142 } 143 144 147 protected final void clearResult() { 148 this.resultComputed = false; 149 } 150 151 158 public void setAbsoluteAccuracy(double accuracy) { 159 absoluteAccuracy = accuracy; 160 } 161 162 167 public double getAbsoluteAccuracy() { 168 return absoluteAccuracy; 169 } 170 171 174 public void resetAbsoluteAccuracy() { 175 absoluteAccuracy = defaultAbsoluteAccuracy; 176 } 177 178 183 public void setMaximalIterationCount(int count) { 184 maximalIterationCount = count; 185 } 186 187 192 public int getMaximalIterationCount() { 193 return maximalIterationCount; 194 } 195 196 199 public void resetMaximalIterationCount() { 200 maximalIterationCount = defaultMaximalIterationCount; 201 } 202 203 210 public void setRelativeAccuracy(double accuracy) { 211 relativeAccuracy = accuracy; 212 } 213 214 218 public double getRelativeAccuracy() { 219 return relativeAccuracy; 220 } 221 222 225 public void resetRelativeAccuracy() { 226 relativeAccuracy = defaultRelativeAccuracy; 227 } 228 229 236 public void setFunctionValueAccuracy(double accuracy) { 237 functionValueAccuracy = accuracy; 238 } 239 240 244 public double getFunctionValueAccuracy() { 245 return functionValueAccuracy; 246 } 247 248 251 public void resetFunctionValueAccuracy() { 252 functionValueAccuracy = defaultFunctionValueAccuracy; 253 } 254 255 256 266 protected boolean isBracketing(double lower, double upper, 267 UnivariateRealFunction f) throws FunctionEvaluationException { 268 return (f.value(lower) * f.value(upper) < 0); 269 } 270 271 279 protected boolean isSequence(double start, double mid, double end) { 280 return (start < mid) && (mid < end); 281 } 282 283 291 protected void verifyInterval(double lower, double upper) { 292 if (lower >= upper) { 293 throw new IllegalArgumentException 294 ("Endpoints do not specify an interval: [" + lower + 295 "," + upper + "]"); 296 } 297 } 298 299 308 protected void verifySequence(double lower, double initial, double upper) { 309 if (!isSequence(lower, initial, upper)) { 310 throw new IllegalArgumentException 311 ("Invalid interval, initial value parameters: lower=" + 312 lower + " initial=" + initial + " upper=" + upper); 313 } 314 } 315 316 327 protected void verifyBracketing(double lower, double upper, 328 UnivariateRealFunction f) throws FunctionEvaluationException { 329 330 verifyInterval(lower, upper); 331 if (!isBracketing(lower, upper, f)) { 332 throw new IllegalArgumentException 333 ("Function values at endpoints do not have different signs." + 334 " Endpoints: [" + lower + "," + upper + "]" + 335 " Values: [" + f.value(lower) + "," + f.value(upper) + "]"); 336 } 337 } 338 } 339 | Popular Tags |