Author: psteitz
Date: Fri Jun 3 22:36:42 2005
New Revision: 179958
URL: http://svn.apache.org/viewcvs?rev=179958&view=rev
Log:
Eliminated redundant endpoint function evaluations in BrentSolver,
SecantSolver. BZ #35042.
Modified:
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/SecantSolver.java
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
Modified:
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java?rev=179958&r1=179957&r2=179958&view=diff
==============================================================================
---
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
(original)
+++
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/BrentSolver.java
Fri Jun 3 22:36:42 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2004 The Apache Software Foundation.
+ * Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -82,7 +82,7 @@
FunctionEvaluationException {
clearResult();
- verifyBracketing(min, max, f);
+ verifyInterval(min, max);
// Index 0 is the old approximation for the root.
// Index 1 is the last calculated approximation for the root.
@@ -93,6 +93,14 @@
double y1;
y0 = f.value(x0);
y1 = f.value(x1);
+
+ // Verify bracketing
+ if (y0 * y1 >= 0) {
+ throw new IllegalArgumentException
+ ("Function values at endpoints do not have different signs." +
+ " Endpoints: [" + min + "," + max + "]" +
+ " Values: [" + y0 + "," + y1 + "]");
+ }
double x2 = x0;
double y2 = y0;
Modified:
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/SecantSolver.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/SecantSolver.java?rev=179958&r1=179957&r2=179958&view=diff
==============================================================================
---
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/SecantSolver.java
(original)
+++
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/SecantSolver.java
Fri Jun 3 22:36:42 2005
@@ -84,7 +84,7 @@
FunctionEvaluationException {
clearResult();
- verifyBracketing(min, max, f);
+ verifyInterval(min, max);
// Index 0 is the old approximation for the root.
// Index 1 is the last calculated approximation for the root.
@@ -95,6 +95,15 @@
double x1 = max;
double y0 = f.value(x0);
double y1 = f.value(x1);
+
+ // Verify bracketing
+ if (y0 * y1 >= 0) {
+ throw new IllegalArgumentException
+ ("Function values at endpoints do not have different signs." +
+ " Endpoints: [" + min + "," + max + "]" +
+ " Values: [" + y0 + "," + y1 + "]");
+ }
+
double x2 = x0;
double y2 = y0;
double oldDelta = x2 - x1;
Modified:
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java?rev=179958&r1=179957&r2=179958&view=diff
==============================================================================
---
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
(original)
+++
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/BrentSolverTest.java
Fri Jun 3 22:36:42 2005
@@ -266,4 +266,21 @@
result = UnivariateRealSolverUtils.solve(f, 0.85, 5);
assertEquals(result, 1.0, 1E-6);
}
+
+ public void testBadEndpoints() throws Exception {
+ UnivariateRealFunction f = new SinFunction();
+ UnivariateRealSolver solver = new BrentSolver(f);
+ try { // bad interval
+ solver.solve(1, -1);
+ fail("Expecting IllegalArgumentException - bad interval");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try { // no bracket
+ solver.solve(1, 1.5);
+ fail("Expecting IllegalArgumentException - non-bracketing");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]