- UnivariateSolver,
- DifferentiableUnivariateSolver and
+ UnivariateSolver,
+ UnivariateDifferentiableSolver and
PolynomialSolver provide means to find roots of
univariate real-valued functions,
- differentiable univariate real-valued functions,
+ differentiable univariate real-valued functions,
and polynomial functions respectively.
A root is the value where the function takes the value 0. Commons-Math
includes implementations of the several root-finding algorithms:
@@ -155,8 +155,8 @@
no |
- Newton's Method |
- differentiable univariate real-valued functions |
+ Newton-Raphson's Method |
+ differentiable univariate real-valued functions |
quadratic, non-guaranteed |
no |
no |
diff --git a/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolverTest.java b/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolverTest.java
index 18eeb04af..866daacff 100644
--- a/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolverTest.java
+++ b/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolverTest.java
@@ -17,7 +17,6 @@
package org.apache.commons.math3.analysis.solvers;
-import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
import org.apache.commons.math3.analysis.QuinticFunction;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
@@ -150,37 +149,32 @@ public final class BracketingNthOrderBrentSolverTest extends BaseSecantSolverAbs
private void compare(final UnivariateDifferentiable f,
double root, double min, double max) {
- DifferentiableUnivariateFunction df = new DifferentiableUnivariateFunction() {
- public double value(double x) {
- return f.value(x);
- }
-
- public UnivariateFunction derivative() {
- return new UnivariateFunction() {
- public double value(double x) {
- return f.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1);
- }
- };
- }
- };
- NewtonSolver newton = new NewtonSolver(1.0e-12);
+ NewtonRaphsonSolver newton = new NewtonRaphsonSolver(1.0e-12);
BracketingNthOrderBrentSolver bracketing =
new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-12, 1.0e-18, 5);
double resultN;
try {
- resultN = newton.solve(100, df, min, max);
+ resultN = newton.solve(100, f, min, max);
} catch (TooManyEvaluationsException tmee) {
resultN = Double.NaN;
}
double resultB;
try {
- resultB = bracketing.solve(100, df, min, max);
+ resultB = bracketing.solve(100, f, min, max);
} catch (TooManyEvaluationsException tmee) {
resultB = Double.NaN;
}
Assert.assertEquals(root, resultN, newton.getAbsoluteAccuracy());
Assert.assertEquals(root, resultB, bracketing.getAbsoluteAccuracy());
- Assert.assertTrue(bracketing.getEvaluations() < newton.getEvaluations());
+
+ // bracketing solver evaluates only function value, we set the weight to 1
+ final int weightedBracketingEvaluations = bracketing.getEvaluations();
+
+ // Newton-Raphson solver evaluates both function value and derivative, we set the weight to 2
+ final int weightedNewtonEvaluations = 2 * newton.getEvaluations();
+
+ Assert.assertTrue(weightedBracketingEvaluations < weightedNewtonEvaluations);
+
}
private static abstract class TestFunction implements UnivariateDifferentiable {
diff --git a/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolverTest.java b/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolverTest.java
new file mode 100644
index 000000000..35fad5872
--- /dev/null
+++ b/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolverTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.analysis.solvers;
+
+import org.apache.commons.math3.analysis.QuinticFunction;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
+import org.apache.commons.math3.analysis.function.Sin;
+import org.apache.commons.math3.util.FastMath;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ * @version $Id$
+ */
+public final class NewtonRaphsonSolverTest {
+ /**
+ *
+ */
+ @Test
+ public void testSinZero() {
+ UnivariateDifferentiable f = new Sin();
+ double result;
+
+ NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
+ result = solver.solve(100, f, 3, 4);
+ Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 1, 4);
+ Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());
+
+ Assert.assertTrue(solver.getEvaluations() > 0);
+ }
+
+ /**
+ *
+ */
+ @Test
+ public void testQuinticZero() {
+ final UnivariateDifferentiable f = new QuinticFunction();
+ double result;
+
+ NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
+ result = solver.solve(100, f, -0.2, 0.2);
+ Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, -0.1, 0.3);
+ Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, -0.3, 0.45);
+ Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.3, 0.7);
+ Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.2, 0.6);
+ Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.05, 0.95);
+ Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.85, 1.25);
+ Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.8, 1.2);
+ Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.85, 1.75);
+ Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.55, 1.45);
+ Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+
+ result = solver.solve(100, f, 0.85, 5);
+ Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+ }
+}
diff --git a/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonSolverTest.java b/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonSolverTest.java
index 6b82e9000..6c66f49f5 100644
--- a/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonSolverTest.java
+++ b/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonSolverTest.java
@@ -29,7 +29,9 @@ import org.junit.Test;
/**
* @version $Id$
+ * @deprecated
*/
+@Deprecated
public final class NewtonSolverTest {
/**
*