commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1383437 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java
Date Tue, 11 Sep 2012 14:42:44 GMT
Author: luc
Date: Tue Sep 11 14:42:44 2012
New Revision: 1383437

URL: http://svn.apache.org/viewvc?rev=1383437&view=rev
Log:
Added arbitrary order derivative for polynomials in Newton form.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java?rev=1383437&r1=1383436&r2=1383437&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java
Tue Sep 11 14:42:44 2012
@@ -16,9 +16,10 @@
  */
 package org.apache.commons.math3.analysis.polynomials;
 
-import org.apache.commons.math3.exception.NoDataException;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
 import org.apache.commons.math3.exception.DimensionMismatchException;
-import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.exception.NoDataException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 
 /**
@@ -34,7 +35,7 @@ import org.apache.commons.math3.exceptio
  * @version $Id$
  * @since 1.2
  */
-public class PolynomialFunctionNewtonForm implements UnivariateFunction {
+public class PolynomialFunctionNewtonForm implements UnivariateDifferentiable {
 
     /**
      * The coefficients of the polynomial, ordered by degree -- i.e.
@@ -94,6 +95,20 @@ public class PolynomialFunctionNewtonFor
        return evaluate(a, c, z);
     }
 
+    /** {@inheritDoc} */
+    public DerivativeStructure value(final DerivativeStructure t) {
+        verifyInputArray(a, c);
+
+        final int n = c.length;
+        DerivativeStructure value = new DerivativeStructure(t.getFreeParameters(), t.getOrder(),
a[n]);
+        for (int i = n - 1; i >= 0; i--) {
+            value = t.subtract(c[i]).multiply(value).add(a[i]);
+        }
+
+        return value;
+
+    }
+
     /**
      * Returns the degree of the polynomial.
      *
@@ -221,4 +236,5 @@ public class PolynomialFunctionNewtonFor
                                                  a.length, c.length);
         }
     }
+
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java?rev=1383437&r1=1383436&r2=1383437&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.java
Tue Sep 11 14:42:44 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math3.analysis.polynomials;
 
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.junit.Assert;
 import org.junit.Test;
@@ -127,6 +128,30 @@ public final class PolynomialFunctionNew
     }
 
     /**
+     * Test for derivatives.
+     */
+    @Test
+    public void testDerivative() {
+
+        // x^3 = 0 * [1] + 1 * [x] + 3 * [x(x-1)] + 1 * [x(x-1)(x-2)]
+        PolynomialFunctionNewtonForm p =
+                new PolynomialFunctionNewtonForm(new double[] { 0, 1, 3, 1 },
+                                                 new double[] { 0, 1, 2 });
+
+        double eps = 2.0e-14;
+        for (double t = 0.0; t < 10.0; t += 0.1) {
+            DerivativeStructure x = new DerivativeStructure(1, 4, 0, t);
+            DerivativeStructure y = p.value(x);
+            Assert.assertEquals(t * t * t,   y.getValue(),              eps * t * t * t);
+            Assert.assertEquals(3.0 * t * t, y.getPartialDerivative(1), eps * 3.0 * t * t);
+            Assert.assertEquals(6.0 * t,     y.getPartialDerivative(2), eps * 6.0 * t);
+            Assert.assertEquals(6.0,         y.getPartialDerivative(3), eps * 6.0);
+            Assert.assertEquals(0.0,         y.getPartialDerivative(4), eps);
+        }
+
+    }
+
+    /**
      * Test of parameters for the polynomial.
      */
     @Test



Mime
View raw message