commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1179671 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java site/xdoc/changes.xml test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
Date Thu, 06 Oct 2011 15:44:42 GMT
Author: erans
Date: Thu Oct  6 15:44:41 2011
New Revision: 1179671

URL: http://svn.apache.org/viewvc?rev=1179671&view=rev
Log:
MATH-683     
New method "shift" to compute coefficients of a polynomial (due to R. di Costanzo).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java?rev=1179671&r1=1179670&r2=1179671&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtils.java
Thu Oct  6 15:44:41 2011
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 
 import org.apache.commons.math.fraction.BigFraction;
 import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.util.MathUtils;
 
 /**
  * A collection of static methods that operate on or return polynomials.
@@ -184,6 +185,61 @@ public class PolynomialsUtils {
         });
     }
 
+    /**
+     * Compute the coefficients of the polynomial <code>P<sub>s</sub>(x)</code>
+     * whose values at point {@code x} will be the same as the those from the
+     * original polynomial <code>P(x)</code> when computed at {@code x + shift}.
+     * Thus, if <code>P(x) = &Sigma;<sub>i</sub> a<sub>i</sub>
x<sup>i</sup></code>,
+     * then
+     * <pre>
+     *  <table>
+     *   <tr>
+     *    <td><code>P<sub>s</sub>(x)</td>
+     *    <td>= &Sigma;<sub>i</sub> b<sub>i</sub> x<sup>i</sup></code></td>
+     *   </tr>
+     *   <tr>
+     *    <td></td>
+     *    <td>= &Sigma;<sub>i</sub> a<sub>i</sub> (x +
shift)<sup>i</sup></code></td>
+     *   </tr>
+     *  </table>
+     * </pre>
+     *
+     * @param coefficients Coefficients of the original polynomial.
+     * @param shift Shift value.
+     * @return the coefficients <code>b<sub>i</sub></code> of the
shifted
+     * polynomial.
+     */
+    public static double[] shift(final double[] coefficients,
+                                 final double shift) {
+        final int dp1 = coefficients.length;
+        final double[] newCoefficients = new double[dp1];
+
+        // Pascal triangle.
+        final int[][] coeff = new int[dp1][dp1];
+        for (int i = 0; i < dp1; i++){
+            for(int j = 0; j <= i; j++){
+                coeff[i][j] = (int) MathUtils.binomialCoefficient(i, j);
+            }
+        }
+
+        // First polynomial coefficient.
+        for (int i = 0; i < dp1; i++){
+            newCoefficients[0] += coefficients[i] * FastMath.pow(shift, i);
+        }
+
+        // Superior order.
+        final int d = dp1 - 1;
+        for (int i = 0; i < d; i++) {
+            for (int j = i; j < d; j++){
+                newCoefficients[i + 1] += coeff[j + 1][j - i] *
+                    coefficients[j + 1] * FastMath.pow(shift, j - i);
+            }
+        }
+
+        return newCoefficients;
+    }
+
+
     /** Get the coefficients array for a given degree.
      * @param degree degree of the polynomial
      * @param coefficients list where the computed coefficients are stored

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=1179671&r1=1179670&r2=1179671&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Oct  6 15:44:41 2011
@@ -52,6 +52,11 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="erans" type="add" issue="MATH-683" due-to="Romain di Costanzo">
+         Added "shift" method to compute the coefficients of a new polynomial
+         whose values are the same as those of another polynomial but computed
+         at a shifted point.
+      </action>
       <action dev="erans" type="fix" issue="MATH-676">
          Faster "multiply" method in "Array2DRowRealMatrix". Code inspired
          from the Jama project.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java?rev=1179671&r1=1179670&r2=1179671&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
Thu Oct  6 15:44:41 2011
@@ -207,6 +207,36 @@ public class PolynomialsUtilsTest {
         }
     }
 
+    @Test
+    public void testShift(){
+        // f1(x) = 1 + x + 2 x^2
+        PolynomialFunction f1x = new PolynomialFunction(new double[] { 1, 1, 2 });
+
+        PolynomialFunction f1x1
+            = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 1));
+        checkPolynomial(f1x1, "4 + 5 x + 2 x^2");
+
+        PolynomialFunction f1xM1
+            = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), -1));
+        checkPolynomial(f1xM1, "2 - 3 x + 2 x^2");
+        
+        PolynomialFunction f1x3
+            = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 3));
+        checkPolynomial(f1x3, "22 + 13 x + 2 x^2");
+
+        // f2(x) = 2 + 3 x^2 + 8 x^3 + 121 x^5
+        PolynomialFunction f2x = new PolynomialFunction(new double[]{2, 0, 3, 8, 0, 121});
+
+        PolynomialFunction f2x1
+            = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 1));
+        checkPolynomial(f2x1, "134 + 635 x + 1237 x^2 + 1218 x^3 + 605 x^4 + 121 x^5");
+
+        PolynomialFunction f2x3
+            = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 3));
+        checkPolynomial(f2x3, "29648 + 49239 x + 32745 x^2 + 10898 x^3 + 1815 x^4 + 121 x^5");
+    }
+
+
     private void checkPolynomial(PolynomialFunction p, long denominator, String reference)
{
         PolynomialFunction q = new PolynomialFunction(new double[] { denominator});
         Assert.assertEquals(reference, p.multiply(q).toString());



Mime
View raw message