You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2015/04/11 06:20:43 UTC
[1/2] [math] Added Laguerre complex solve methods taking maxEval
parameters. JIRA: MATH-1213.
Repository: commons-math
Updated Branches:
refs/heads/MATH_3_X beb4d1ce6 -> fcc40ccb4
Added Laguerre complex solve methods taking maxEval parameters. JIRA: MATH-1213.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/89a0c4b2
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/89a0c4b2
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/89a0c4b2
Branch: refs/heads/MATH_3_X
Commit: 89a0c4b22154326b7598d68b0bc45e3fbb9f3e2f
Parents: beb4d1c
Author: Phil Steitz <ph...@gmail.com>
Authored: Fri Apr 10 21:17:31 2015 -0700
Committer: Phil Steitz <ph...@gmail.com>
Committed: Fri Apr 10 21:17:31 2015 -0700
----------------------------------------------------------------------
.../math3/analysis/solvers/LaguerreSolver.java | 60 ++++++++++++++++++--
.../analysis/solvers/LaguerreSolverTest.java | 43 +++++++++++++-
2 files changed, 97 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/89a0c4b2/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java b/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java
index c127b42..9425112 100644
--- a/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java
+++ b/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java
@@ -181,9 +181,9 @@ public class LaguerreSolver extends AbstractPolynomialSolver {
*
* @param coefficients Polynomial coefficients.
* @param initial Start value.
- * @return the point at which the function value is zero.
+ * @return the full set of complex roots of the polynomial
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
- * if the maximum number of evaluations is exceeded.
+ * if the maximum number of evaluations is exceeded when solving for one of the roots
* @throws NullArgumentException if the {@code coefficients} is
* {@code null}.
* @throws NoDataException if the {@code coefficients} array is empty.
@@ -194,7 +194,32 @@ public class LaguerreSolver extends AbstractPolynomialSolver {
throws NullArgumentException,
NoDataException,
TooManyEvaluationsException {
- setup(Integer.MAX_VALUE,
+ return solveAllComplex(coefficients, initial, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Find all complex roots for the polynomial with the given
+ * coefficients, starting from the given initial value.
+ * <br/>
+ * Note: This method is not part of the API of {@link BaseUnivariateSolver}.
+ *
+ * @param coefficients polynomial coefficients
+ * @param initial start value
+ * @param maxEval maximum number of evaluations
+ * @return the full set of complex roots of the polynomial
+ * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
+ * if the maximum number of evaluations is exceeded when solving for one of the roots
+ * @throws NullArgumentException if the {@code coefficients} is
+ * {@code null}
+ * @throws NoDataException if the {@code coefficients} array is empty
+ * @since 3.5
+ */
+ public Complex[] solveAllComplex(double[] coefficients,
+ double initial, int maxEval)
+ throws NullArgumentException,
+ NoDataException,
+ TooManyEvaluationsException {
+ setup(maxEval,
new PolynomialFunction(coefficients),
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY,
@@ -211,7 +236,7 @@ public class LaguerreSolver extends AbstractPolynomialSolver {
*
* @param coefficients Polynomial coefficients.
* @param initial Start value.
- * @return the point at which the function value is zero.
+ * @return a complex root of the polynomial
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the maximum number of evaluations is exceeded.
* @throws NullArgumentException if the {@code coefficients} is
@@ -224,7 +249,32 @@ public class LaguerreSolver extends AbstractPolynomialSolver {
throws NullArgumentException,
NoDataException,
TooManyEvaluationsException {
- setup(Integer.MAX_VALUE,
+ return solveComplex(coefficients, initial, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Find a complex root for the polynomial with the given coefficients,
+ * starting from the given initial value.
+ * <br/>
+ * Note: This method is not part of the API of {@link BaseUnivariateSolver}.
+ *
+ * @param coefficients polynomial coefficients
+ * @param initial start value
+ * @param maxEval maximum number of evaluations
+ * @return a complex root of the polynomial
+ * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
+ * if the maximum number of evaluations is exceeded
+ * @throws NullArgumentException if the {@code coefficients} is
+ * {@code null}
+ * @throws NoDataException if the {@code coefficients} array is empty
+ * @since 3.1
+ */
+ public Complex solveComplex(double[] coefficients,
+ double initial, int maxEval)
+ throws NullArgumentException,
+ NoDataException,
+ TooManyEvaluationsException {
+ setup(maxEval,
new PolynomialFunction(coefficients),
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY,
http://git-wip-us.apache.org/repos/asf/commons-math/blob/89a0c4b2/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.java b/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.java
index d97a8e6..677132a 100644
--- a/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.java
+++ b/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.math3.analysis.solvers;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.NoBracketingException;
+import org.apache.commons.math3.exception.TooManyEvaluationsException;
import org.apache.commons.math3.complex.Complex;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.TestUtils;
@@ -26,7 +27,7 @@ import org.junit.Assert;
import org.junit.Test;
/**
- * Test case for Laguerre solver.
+ * Test cases for Laguerre solver.
* <p>
* Laguerre's method is very efficient in solving polynomials. Test runs
* show that for a default absolute accuracy of 1E-6, it generally takes
@@ -156,4 +157,44 @@ public final class LaguerreSolverTest {
// expected
}
}
+
+ @Test(expected=org.apache.commons.math3.exception.NoDataException.class)
+ public void testEmptyCoefficients() {
+ double coefficients[] = {};
+ LaguerreSolver solver = new LaguerreSolver();
+ solver.solveComplex(coefficients, 0);
+ }
+
+ @Test(expected=org.apache.commons.math3.exception.NullArgumentException.class)
+ public void testNullCoefficients() {
+ LaguerreSolver solver = new LaguerreSolver();
+ solver.solveComplex(null, 0);
+ }
+
+ @Test
+ public void testTooManyEvaluations() {
+ double coefficients[] = {1, 0, 0, 1}; // x^3 + 1 (cube roots of unity)
+ final double tol = 1e-12;
+ LaguerreSolver solver = new LaguerreSolver(tol);
+
+ // No evaluations limit -> solveAllComplex should get all roots
+ Complex [] expected = {new Complex(0.5, FastMath.sqrt(3) / 2),
+ new Complex(-1, 0), new Complex(0.5, -FastMath.sqrt(3) / 2)};
+ Complex [] roots = solver.solveAllComplex(coefficients, 0);
+
+ for (Complex expectedRoot : expected) {
+ final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
+ FastMath.abs(expectedRoot.abs() * solver.getRelativeAccuracy()));
+ TestUtils.assertContains(roots, expectedRoot, tolerance);
+ }
+
+ // Iterations limit too low -> throw TME
+ try {
+ solver.solveAllComplex(coefficients, 1000, 2);
+ Assert.fail("Expecting TooManyEvaluationsException");
+ } catch (TooManyEvaluationsException ex) {
+ // expected
+ }
+
+ }
}
[2/2] [math] Added Laguerre complex solve methods taking maxEval
parameters. JIRA: MATH-1213.
Posted by ps...@apache.org.
Added Laguerre complex solve methods taking maxEval parameters. JIRA: MATH-1213.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/fcc40ccb
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/fcc40ccb
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/fcc40ccb
Branch: refs/heads/MATH_3_X
Commit: fcc40ccb4248fabc60a58e7efef364bbb6a3864a
Parents: 89a0c4b
Author: Phil Steitz <ph...@gmail.com>
Authored: Fri Apr 10 21:20:31 2015 -0700
Committer: Phil Steitz <ph...@gmail.com>
Committed: Fri Apr 10 21:20:31 2015 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/fcc40ccb/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b7d3edc..561be34 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="3.5" date="TBD" description="TBD">
+ <action dev="psteitz" type="update" issue="MATH-1213">
+ Added Laguerre complex solve methods taking maxEval parameters.
+ </action>
<action dev="luc" type="fix" issue="MATH-1191">
Fixed ignored method parameters in QRDecomposition protected methods.
</action>