Author: luc
Date: Sun Jul 10 11:10:55 2011
New Revision: 1144830
URL: http://svn.apache.org/viewvc?rev=1144830&view=rev
Log:
Select bracketing side from the solve method instead of through a setter for
consistency with the other solvers.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/AllowedSolutions.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketedUnivariateRealSolver.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/IllinoisSolver.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/PegasusSolver.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/RegulaFalsiSolver.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BaseSecantSolverAbstractTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/AllowedSolutions.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/AllowedSolutions.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/AllowedSolutions.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/AllowedSolutions.java
Sun Jul 10 11:10:55 2011
@@ -23,7 +23,7 @@ package org.apache.commons.math.analysis
* This basically controls whether or not under-approximations and
* over-approximations are allowed.
*
- * <p>If all solutions are accepted ({@link #EITHER_SIDE}), then the solution
+ * <p>If all solutions are accepted ({@link #ANY_SIDE}), then the solution
* that the root-finding algorithm returns for a given root may be equal to the
* actual root, but it may also be an approximation that is slightly smaller
* or slightly larger than the actual root. Root-finding algorithms generally
@@ -45,7 +45,7 @@ public enum AllowedSolutions {
* root-finding result s may be smaller than x0, equal to x0, or greater
* than x0.
*/
- EITHER_SIDE,
+ ANY_SIDE,
/** Only solutions that are less than or equal to the actual root are
* acceptable as solutions for root-finding. In other words,
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketedUnivariateRealSolver.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketedUnivariateRealSolver.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketedUnivariateRealSolver.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketedUnivariateRealSolver.java
Sun Jul 10 11:10:55 2011
@@ -17,6 +17,8 @@
package org.apache.commons.math.analysis.solvers;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+
/** Interface for {@link UnivariateRealSolver (univariate real) root-finding
* algorithms} that maintain a bracketed solution. There are several advantages
* to having such root-finding algorithms:
@@ -34,27 +36,57 @@ package org.apache.commons.math.analysis
* </ul>
*
* <p>For backwards compatibility, all root-finding algorithms must have
- * {@link AllowedSolutions#EITHER_SIDE EITHER_SIDE} as default for the allowed
+ * {@link AllowedSolutions#ANY_SIDE ANY_SIDE} as default for the allowed
* solutions.</p>
*
* @see AllowedSolutions
* @since 3.0
* @version $Id$
*/
-public interface BracketedUnivariateRealSolver extends UnivariateRealSolver {
- /** Returns the kind of solutions that the root-finding algorithm may
- * accept as solutions.
+public interface BracketedUnivariateRealSolver<FUNC extends
UnivariateRealFunction>
+ extends BaseUnivariateRealSolver<FUNC> {
+
+ /**
+ * Solve for a zero in the given interval.
+ * A solver may require that the interval brackets a single zero root.
+ * Solvers that do require bracketing should be able to handle the case
+ * where one of the endpoints is itself a root.
*
- * @return the kind of solutions that the root-finding algorithm may
- * accept as solutions
+ * @param maxEval Maximum number of evaluations.
+ * @param f Function to solve.
+ * @param min Lower bound for the interval.
+ * @param max Upper bound for the interval.
+ * @param allowedSolutions the kind of solutions that the root-finding
algorithm may
+ * accept as solutions.
+ * @return a value where the function is zero.
+ * @throws org.apache.commons.math.exception.MathIllegalArgumentException
+ * if the arguments do not satisfy the requirements specified by the
solver.
+ * @throws org.apache.commons.math.exception.TooManyEvaluationsException if
+ * the allowed number of evaluations is exceeded.
*/
- AllowedSolutions getAllowedSolutions();
+ double solve(int maxEval, FUNC f, double min, double max,
+ AllowedSolutions allowedSolutions);
- /** Sets the kind of solutions that the root-finding algorithm may accept
- * as solutions.
+ /**
+ * Solve for a zero in the given interval, start at {@code startValue}.
+ * A solver may require that the interval brackets a single zero root.
+ * Solvers that do require bracketing should be able to handle the case
+ * where one of the endpoints is itself a root.
*
- * @param allowedSolutions the kind of solutions that the root-finding
- * algorithm may accept as solutions
+ * @param maxEval Maximum number of evaluations.
+ * @param f Function to solve.
+ * @param min Lower bound for the interval.
+ * @param max Upper bound for the interval.
+ * @param startValue Start value to use.
+ * @param allowedSolutions the kind of solutions that the root-finding
algorithm may
+ * accept as solutions.
+ * @return a value where the function is zero.
+ * @throws org.apache.commons.math.exception.MathIllegalArgumentException
+ * if the arguments do not satisfy the requirements specified by the
solver.
+ * @throws org.apache.commons.math.exception.TooManyEvaluationsException if
+ * the allowed number of evaluations is exceeded.
*/
- void setAllowedSolutions(AllowedSolutions allowedSolutions);
+ double solve(int maxEval, FUNC f, double min, double max, double
startValue,
+ AllowedSolutions allowedSolutions);
+
}
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/IllinoisSolver.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/IllinoisSolver.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/IllinoisSolver.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/IllinoisSolver.java
Sun Jul 10 11:10:55 2011
@@ -38,7 +38,8 @@ package org.apache.commons.math.analysis
* @since 3.0
* @version $Id$
*/
-public class IllinoisSolver extends BaseBracketedSecantSolver {
+public class IllinoisSolver extends BaseSecantSolver {
+
/** Construct a solver with default accuracy (1e-6). */
public IllinoisSolver() {
super(DEFAULT_ABSOLUTE_ACCURACY, Method.ILLINOIS);
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/PegasusSolver.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/PegasusSolver.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/PegasusSolver.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/PegasusSolver.java
Sun Jul 10 11:10:55 2011
@@ -40,7 +40,8 @@ package org.apache.commons.math.analysis
* @since 3.0
* @version $Id$
*/
-public class PegasusSolver extends BaseBracketedSecantSolver {
+public class PegasusSolver extends BaseSecantSolver {
+
/** Construct a solver with default accuracy (1e-6). */
public PegasusSolver() {
super(DEFAULT_ABSOLUTE_ACCURACY, Method.PEGASUS);
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/RegulaFalsiSolver.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/RegulaFalsiSolver.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/RegulaFalsiSolver.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/RegulaFalsiSolver.java
Sun Jul 10 11:10:55 2011
@@ -35,7 +35,8 @@ package org.apache.commons.math.analysis
* @since 3.0
* @version $Id$
*/
-public class RegulaFalsiSolver extends BaseBracketedSecantSolver {
+public class RegulaFalsiSolver extends BaseSecantSolver {
+
/** Construct a solver with default accuracy (1e-6). */
public RegulaFalsiSolver() {
super(DEFAULT_ABSOLUTE_ACCURACY, Method.REGULA_FALSI);
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BaseSecantSolverAbstractTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BaseSecantSolverAbstractTest.java?rev=1144830&r1=1144829&r2=1144830&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BaseSecantSolverAbstractTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/BaseSecantSolverAbstractTest.java
Sun Jul 10 11:10:55 2011
@@ -162,14 +162,14 @@ public abstract class BaseSecantSolverAb
public void testSolutionLeftSide() {
UnivariateRealFunction f = new SinFunction();
UnivariateRealSolver solver = getSolver();
- if (!(solver instanceof BracketedUnivariateRealSolver)) return;
-
((BracketedUnivariateRealSolver)solver).setAllowedSolutions(AllowedSolutions.LEFT_SIDE);
double left = -1.5;
double right = 0.05;
for(int i = 0; i < 10; i++) {
// Test whether the allowed solutions are taken into account.
- double solution = solver.solve(100, f, left, right);
- Assert.assertTrue(solution <= 0.0);
+ double solution = getSolution(solver, 100, f, left, right,
AllowedSolutions.LEFT_SIDE);
+ if (!Double.isNaN(solution)) {
+ Assert.assertTrue(solution <= 0.0);
+ }
// Prepare for next test.
left -= 0.1;
@@ -181,14 +181,14 @@ public abstract class BaseSecantSolverAb
public void testSolutionRightSide() {
UnivariateRealFunction f = new SinFunction();
UnivariateRealSolver solver = getSolver();
- if (!(solver instanceof BracketedUnivariateRealSolver)) return;
-
((BracketedUnivariateRealSolver)solver).setAllowedSolutions(AllowedSolutions.RIGHT_SIDE);
double left = -1.5;
double right = 0.05;
for(int i = 0; i < 10; i++) {
// Test whether the allowed solutions are taken into account.
- double solution = solver.solve(100, f, left, right);
- Assert.assertTrue(solution >= 0.0);
+ double solution = getSolution(solver, 100, f, left, right,
AllowedSolutions.RIGHT_SIDE);
+ if (!Double.isNaN(solution)) {
+ Assert.assertTrue(solution >= 0.0);
+ }
// Prepare for next test.
left -= 0.1;
@@ -199,14 +199,14 @@ public abstract class BaseSecantSolverAb
public void testSolutionBelowSide() {
UnivariateRealFunction f = new SinFunction();
UnivariateRealSolver solver = getSolver();
- if (!(solver instanceof BracketedUnivariateRealSolver)) return;
-
((BracketedUnivariateRealSolver)solver).setAllowedSolutions(AllowedSolutions.BELOW_SIDE);
double left = -1.5;
double right = 0.05;
for(int i = 0; i < 10; i++) {
// Test whether the allowed solutions are taken into account.
- double solution = solver.solve(100, f, left, right);
- Assert.assertTrue(f.value(solution) <= 0.0);
+ double solution = getSolution(solver, 100, f, left, right,
AllowedSolutions.BELOW_SIDE);
+ if (!Double.isNaN(solution)) {
+ Assert.assertTrue(f.value(solution) <= 0.0);
+ }
// Prepare for next test.
left -= 0.1;
@@ -218,14 +218,14 @@ public abstract class BaseSecantSolverAb
public void testSolutionAboveSide() {
UnivariateRealFunction f = new SinFunction();
UnivariateRealSolver solver = getSolver();
- if (!(solver instanceof BracketedUnivariateRealSolver)) return;
-
((BracketedUnivariateRealSolver)solver).setAllowedSolutions(AllowedSolutions.ABOVE_SIDE);
double left = -1.5;
double right = 0.05;
for(int i = 0; i < 10; i++) {
// Test whether the allowed solutions are taken into account.
- double solution = solver.solve(100, f, left, right);
- Assert.assertTrue(f.value(solution) >= 0.0);
+ double solution = getSolution(solver, 100, f, left, right,
AllowedSolutions.ABOVE_SIDE);
+ if (!Double.isNaN(solution)) {
+ Assert.assertTrue(f.value(solution) >= 0.0);
+ }
// Prepare for next test.
left -= 0.1;
@@ -233,4 +233,25 @@ public abstract class BaseSecantSolverAb
}
}
+ private double getSolution(UnivariateRealSolver solver, int maxEval,
UnivariateRealFunction f,
+ double left, double right, AllowedSolutions
allowedSolutions) {
+ try {
+ @SuppressWarnings("unchecked")
+ BracketedUnivariateRealSolver<UnivariateRealFunction> bracketing =
+ (BracketedUnivariateRealSolver<UnivariateRealFunction>) solver;
+ return bracketing.solve(100, f, left, right, allowedSolutions);
+ } catch (ClassCastException cce) {
+ double baseRoot = solver.solve(maxEval, f, left, right);
+ if ((baseRoot <= left) || (baseRoot >= right)) {
+ // the solution slipped out of interval
+ return Double.NaN;
+ }
+ PegasusSolver bracketing =
+ new PegasusSolver(solver.getRelativeAccuracy(),
solver.getAbsoluteAccuracy());
+ return UnivariateRealSolverUtils.forceSide(maxEval -
solver.getEvaluations(),
+ f, bracketing,
baseRoot, left, right,
+ allowedSolutions);
+ }
+ }
+
}