Return-Path:
X-Original-To: apmail-commons-commits-archive@minotaur.apache.org
Delivered-To: apmail-commons-commits-archive@minotaur.apache.org
Received: from mail.apache.org (hermes.apache.org [140.211.11.3])
by minotaur.apache.org (Postfix) with SMTP id 5BAB065BC
for ;
Sat, 2 Jul 2011 16:27:30 +0000 (UTC)
Received: (qmail 29765 invoked by uid 500); 2 Jul 2011 16:27:29 -0000
Delivered-To: apmail-commons-commits-archive@commons.apache.org
Received: (qmail 29575 invoked by uid 500); 2 Jul 2011 16:27:28 -0000
Mailing-List: contact commits-help@commons.apache.org; run by ezmlm
Precedence: bulk
List-Help:
List-Unsubscribe:
List-Post:
List-Id:
Reply-To: dev@commons.apache.org
Delivered-To: mailing list commits@commons.apache.org
Received: (qmail 29568 invoked by uid 99); 2 Jul 2011 16:27:28 -0000
Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230)
by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Jul 2011 16:27:28 +0000
X-ASF-Spam-Status: No, hits=-2000.0 required=5.0
tests=ALL_TRUSTED
X-Spam-Check-By: apache.org
Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4)
by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Jul 2011 16:27:25 +0000
Received: from eris.apache.org (localhost [127.0.0.1])
by eris.apache.org (Postfix) with ESMTP id B41CC23889DE
for ; Sat, 2 Jul 2011 16:27:03 +0000 (UTC)
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: svn commit: r1142244 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/analysis/solvers/ site/xdoc/
test/java/org/apache/commons/math/analysis/solvers/
Date: Sat, 02 Jul 2011 16:27:03 -0000
To: commits@commons.apache.org
From: luc@apache.org
X-Mailer: svnmailer-1.0.8
Message-Id: <20110702162703.B41CC23889DE@eris.apache.org>
X-Virus-Checked: Checked by ClamAV on apache.org
Author: luc
Date: Sat Jul 2 16:27:03 2011
New Revision: 1142244
URL: http://svn.apache.org/viewvc?rev=1142244&view=rev
Log:
Added BELOW_SIDE and ABOVE_SIDE in the possible allowed solutions for bracketing 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/BaseSecantSolver.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
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=1142244&r1=1142243&r2=1142244&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 Sat Jul 2 16:27:03 2011
@@ -18,9 +18,10 @@
package org.apache.commons.math.analysis.solvers;
-/** The kinds of solutions that a {@link UnivariateRealSolver (univariate real)
- * root-finding algorithm} may accept as solutions. This basically controls
- * whether or not under-approximations and over-approximations are allowed.
+/** The kinds of solutions that a {@link BracketedUnivariateRealSolver
+ * (bracketed univariate real) root-finding algorithm} may accept as solutions.
+ * This basically controls whether or not under-approximations and
+ * over-approximations are allowed.
*
* If all solutions are accepted ({@link #EITHER_SIDE}), then the solution
* that the root-finding algorithm returns for a given root may be equal to the
@@ -30,8 +31,8 @@ package org.apache.commons.math.analysis
* tolerances. In certain cases however, in particular for
* {@link org.apache.commons.math.ode.events.EventHandler state events} of
* {@link org.apache.commons.math.ode.ODEIntegrator ODE solvers}, it
- * may be necessary to guarantee that a solution is returned that does not
- * under-approximate the solution.
+ * may be necessary to guarantee that a solution is returned that lies on a
+ * specific side the solution.
*
* @see BracketedUnivariateRealSolver
* @since 3.0
@@ -40,23 +41,36 @@ package org.apache.commons.math.analysis
public enum AllowedSolutions {
/** There are no additional side restriction on the solutions for
* root-finding. That is, both under-approximations and over-approximations
- * are allowed. So, if a function f(x) has a root at x = y, then the
- * root-finding result s may be smaller than y, equal to y, or greater
- * than y.
+ * are allowed. So, if a function f(x) has a root at x = x0, then the
+ * root-finding result s may be smaller than x0, equal to x0, or greater
+ * than x0.
*/
EITHER_SIDE,
/** Only solutions that are less than or equal to the actual root are
* acceptable as solutions for root-finding. In other words,
* over-approximations are not allowed. So, if a function f(x) has a root
- * at x = y, then the root-finding result s must satisfy s <= y.
+ * at x = x0, then the root-finding result s must satisfy s <= x0.
*/
LEFT_SIDE,
/** Only solutions that are greater than or equal to the actual root are
* acceptable as solutions for root-finding. In other words,
* under-approximations are not allowed. So, if a function f(x) has a root
- * at x = y, then the root-finding result s must satisfy s >= y.
+ * at x = x0, then the root-finding result s must satisfy s >= x0.
*/
- RIGHT_SIDE;
+ RIGHT_SIDE,
+
+ /** Only solutions for which values are less than or equal to zero are
+ * acceptable as solutions for root-finding. So, if a function f(x) has
+ * a root at x = x0, then the root-finding result s must satisfy f(s) <= 0.
+ */
+ BELOW_SIDE,
+
+ /** Only solutions for which values are greater than or equal to zero are
+ * acceptable as solutions for root-finding. So, if a function f(x) has
+ * a root at x = x0, then the root-finding result s must satisfy f(s) >= 0.
+ */
+ ABOVE_SIDE;
+
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BaseSecantSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BaseSecantSolver.java?rev=1142244&r1=1142243&r2=1142244&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BaseSecantSolver.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BaseSecantSolver.java Sat Jul 2 16:27:03 2011
@@ -101,10 +101,6 @@ public abstract class BaseSecantSolver e
final double atol = getAbsoluteAccuracy();
final double rtol = getRelativeAccuracy();
- // Variables to hold new bounds.
- double x;
- double fx;
-
// Keep track of inverted intervals, meaning that the left bound is
// larger than the right bound. Not used for the original Secant
// method.
@@ -113,8 +109,8 @@ public abstract class BaseSecantSolver e
// Keep finding better approximations.
while (true) {
// Calculate the next approximation.
- x = x1 - ((f1 * (x1 - x0)) / (f1 - f0));
- fx = computeObjectiveValue(x);
+ final double x = x1 - ((f1 * (x1 - x0)) / (f1 - f0));
+ final double fx = computeObjectiveValue(x);
// If the new approximation is the exact root, return it. Since
// this is not an under-approximation or an over-approximation,
@@ -151,7 +147,7 @@ public abstract class BaseSecantSolver e
}
// If the function value of the last approximation is too small,
- // given the function value accuracy, then we can't get close to
+ // given the function value accuracy, then we can't get closer to
// the root than we already are.
if (FastMath.abs(f1) <= ftol) {
switch (allowedSolutions) {
@@ -167,6 +163,16 @@ public abstract class BaseSecantSolver e
return x1;
}
break;
+ case BELOW_SIDE:
+ if (f1 <= 0) {
+ return x1;
+ }
+ break;
+ case ABOVE_SIDE:
+ if (f1 >= 0) {
+ return x1;
+ }
+ break;
default:
throw new MathInternalError();
}
@@ -183,6 +189,10 @@ public abstract class BaseSecantSolver e
return inverted ? x1 : x0;
case RIGHT_SIDE:
return inverted ? x0 : x1;
+ case BELOW_SIDE:
+ return (f1 <= 0) ? x1 : x0;
+ case ABOVE_SIDE:
+ return (f1 >= 0) ? x1 : x0;
default:
throw new MathInternalError();
}
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1142244&r1=1142243&r2=1142244&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sat Jul 2 16:27:03 2011
@@ -59,7 +59,8 @@ The type attribute can be add,u
Modified "SecantSolver" to comply with the original algorithm. Added several
- secant-based solvers.
+ secant-based solvers. Added a way to select the side of the root with bracketing
+ solvers.
Fixed javadoc for ODEIntegrator interface
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=1142244&r1=1142243&r2=1142244&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 Sat Jul 2 16:27:03 2011
@@ -195,4 +195,42 @@ public abstract class BaseSecantSolverAb
right += 0.3;
}
}
+ @Test
+ 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);
+
+ // Prepare for next test.
+ left -= 0.1;
+ right += 0.3;
+ }
+ }
+
+ @Test
+ 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);
+
+ // Prepare for next test.
+ left -= 0.1;
+ right += 0.3;
+ }
+ }
+
}