Author: luc
Date: Sat Aug 18 18:09:45 2012
New Revision: 1374630

URL: http://svn.apache.org/viewvc?rev=1374630&view=rev
Log:
Polynomials now implement the new UnivariateDifferentiable interface.

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java?rev=1374630&r1=1374629&r2=1374630&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java
 Sat Aug 18 18:09:45 2012
@@ -25,6 +25,8 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
 import org.apache.commons.math3.analysis.UnivariateFunction;
 import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import 
org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
 import org.apache.commons.math3.util.FastMath;
 import org.apache.commons.math3.util.MathUtils;
 
@@ -36,7 +38,7 @@ import org.apache.commons.math3.util.Mat
  *
  * @version $Id$
  */
-public class PolynomialFunction implements DifferentiableUnivariateFunction, 
Serializable {
+public class PolynomialFunction implements UnivariateDifferentiable, 
DifferentiableUnivariateFunction, Serializable {
     /**
      * Serialization identifier
      */
@@ -137,6 +139,27 @@ public class PolynomialFunction implemen
         return result;
     }
 
+
+    /** {@inheritDoc}
+     * @since 3.1
+     * @throws NoDataException if {@code coefficients} is empty.
+     * @throws NullArgumentException if {@code coefficients} is {@code null}.
+     */
+    public DerivativeStructure value(final DerivativeStructure t)
+        throws NullArgumentException, NoDataException {
+        MathUtils.checkNotNull(coefficients);
+        int n = coefficients.length;
+        if (n == 0) {
+            throw new 
NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
+        }
+        DerivativeStructure result =
+                new DerivativeStructure(t.getFreeParameters(), t.getOrder(), 
coefficients[n - 1]);
+        for (int j = n - 2; j >= 0; j--) {
+            result = result.multiply(t).add(coefficients[j]);
+        }
+        return result;
+    }
+
     /**
      * Add a polynomial to the instance.
      *

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java?rev=1374630&r1=1374629&r2=1374630&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java
 Sat Aug 18 18:09:45 2012
@@ -21,6 +21,8 @@ import java.util.Arrays;
 import org.apache.commons.math3.util.MathArrays;
 import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
 import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
+import 
org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiable;
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.NumberIsTooSmallException;
 import org.apache.commons.math3.exception.DimensionMismatchException;
@@ -61,7 +63,7 @@ import org.apache.commons.math3.exceptio
  *
  * @version $Id$
  */
-public class PolynomialSplineFunction implements 
DifferentiableUnivariateFunction {
+public class PolynomialSplineFunction implements UnivariateDifferentiable, 
DifferentiableUnivariateFunction {
     /**
      * Spline segment interval delimiters (knots).
      * Size is n + 1 for n segments.
@@ -168,6 +170,28 @@ public class PolynomialSplineFunction im
         return new PolynomialSplineFunction(knots, derivativePolynomials);
     }
 
+
+    /** {@inheritDoc}
+     * @since 3.1
+     */
+    public DerivativeStructure value(final DerivativeStructure t) {
+        final double t0 = t.getValue();
+        if (t0 < knots[0] || t0 > knots[n]) {
+            throw new OutOfRangeException(t0, knots[0], knots[n]);
+        }
+        int i = Arrays.binarySearch(knots, t0);
+        if (i < 0) {
+            i = -i - 2;
+        }
+        // This will handle the case where t is the last knot value
+        // There are only n-1 polynomials, so if t is the last knot
+        // then we will use the last polynomial to calculate the value.
+        if ( i >= polynomials.length ) {
+            i--;
+        }
+        return polynomials[i].value(t.subtract(knots[i]));
+    }
+
     /**
      * Get the number of spline segments.
      * It is also the number of polynomials and the number of knot points - 1.


Reply via email to