New method "shift" to compute coefficients of a polynomial (due to R. di Costanzo).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1179671 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-10-06 15:44:41 +00:00
parent 61018c7997
commit 23da497319
3 changed files with 91 additions and 0 deletions

View File

@ -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

View File

@ -52,6 +52,11 @@ The <action> type attribute can be add,update,fix,remove.
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.

View File

@ -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());