From 4117b2a53aa93f26d66b5bd8594b211ff61bd343 Mon Sep 17 00:00:00 2001
From: Gilles Sadowski
* Horner's Method
- * is used to evaluate the function.
- * The value returned is
- * coefficients[n] * x^n + ... + coefficients[1] * x + coefficients[0]
+ * The value returned is
+ * coefficients[n] * x^n + ... + coefficients[1] * x + coefficients[0]
*
The representation is user oriented. Terms are displayed lowest
* degrees first. The multiplications signs, coefficients equals to
* one and null terms are not displayed (except if the polynomial is 0,
@@ -274,56 +270,67 @@ public class PolynomialFunction implements DifferentiableUnivariateRealFunction,
* (i.e. we display -3
for a constant negative polynomial,
* but 1 - 3 x + x^2
if the negative coefficient is not
* the first one displayed).
value of this is 2.5 everywhere.
*/ + @Test public void testConstants() { double[] c = { 2.5 }; - PolynomialFunction f = new PolynomialFunction( c ); + PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] at several (nonsymmetric) places - assertEquals( f.value( 0.0), c[0], tolerance ); - assertEquals( f.value( -1.0), c[0], tolerance ); - assertEquals( f.value( -123.5), c[0], tolerance ); - assertEquals( f.value( 3.0), c[0], tolerance ); - assertEquals( f.value( 456.89), c[0], tolerance ); + Assert.assertEquals(f.value(0), c[0], tolerance); + Assert.assertEquals(f.value(-1), c[0], tolerance); + Assert.assertEquals(f.value(-123.5), c[0], tolerance); + Assert.assertEquals(f.value(3), c[0], tolerance); + Assert.assertEquals(f.value(456.89), c[0], tolerance); - assertEquals(f.degree(), 0); - assertEquals(f.derivative().value(0), 0, tolerance); + Assert.assertEquals(f.degree(), 0); + Assert.assertEquals(f.derivative().value(0), 0, tolerance); - assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); + Assert.assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); } /** @@ -59,77 +59,72 @@ public final class PolynomialFunctionTest extends TestCase { * *This will test the function f(x) = 3*x - 1.5
*This will have the values - * f(0.0) = -1.5, f(-1.0) = -4.5, f(-2.5) = -9.0, - * f(0.5) = 0.0, f(1.5) = 3.0 and f(3.0) = 7.5 + * f(0) = -1.5, f(-1) = -4.5, f(-2.5) = -9, + * f(0.5) = 0, f(1.5) = 3 and f(3) = 7.5 *
*/ + @Test public void testLinear() { - double[] c = { -1.5, 3.0 }; - PolynomialFunction f = new PolynomialFunction( c ); + double[] c = { -1.5, 3 }; + PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 - assertEquals( f.value( 0.0), c[0], tolerance ); + Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places - assertEquals( -4.5, f.value( -1.0), tolerance ); - assertEquals( -9.0, f.value( -2.5), tolerance ); - assertEquals( 0.0, f.value( 0.5), tolerance ); - assertEquals( 3.0, f.value( 1.5), tolerance ); - assertEquals( 7.5, f.value( 3.0), tolerance ); + Assert.assertEquals(-4.5, f.value(-1), tolerance); + Assert.assertEquals(-9, f.value(-2.5), tolerance); + Assert.assertEquals(0, f.value(0.5), tolerance); + Assert.assertEquals(3, f.value(1.5), tolerance); + Assert.assertEquals(7.5, f.value(3), tolerance); - assertEquals(f.degree(), 1); - - assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); + Assert.assertEquals(f.degree(), 1); + Assert.assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); } - /** * Tests a second order polynomial. *This will test the function f(x) = 2x^2 - 3x -2 = (2x+1)(x-2)
- * */ + @Test public void testQuadratic() { - double[] c = { -2.0, -3.0, 2.0 }; - PolynomialFunction f = new PolynomialFunction( c ); + double[] c = { -2, -3, 2 }; + PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 - assertEquals( f.value( 0.0), c[0], tolerance ); + Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places - assertEquals( 0.0, f.value( -0.5), tolerance ); - assertEquals( 0.0, f.value( 2.0), tolerance ); - assertEquals( -2.0, f.value( 1.5), tolerance ); - assertEquals( 7.0, f.value( -1.5), tolerance ); - assertEquals( 265.5312, f.value( 12.34), tolerance ); - + Assert.assertEquals(0, f.value(-0.5), tolerance); + Assert.assertEquals(0, f.value(2), tolerance); + Assert.assertEquals(-2, f.value(1.5), tolerance); + Assert.assertEquals(7, f.value(-1.5), tolerance); + Assert.assertEquals(265.5312, f.value(12.34), tolerance); } - /** * This will test the quintic function * f(x) = x^2(x-5)(x+3)(x-1) = x^5 - 3x^4 -13x^3 + 15x^2 - * */ + @Test public void testQuintic() { - double[] c = { 0.0, 0.0, 15.0, -13.0, -3.0, 1.0 }; - PolynomialFunction f = new PolynomialFunction( c ); + double[] c = { 0, 0, 15, -13, -3, 1 }; + PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 - assertEquals( f.value( 0.0), c[0], tolerance ); + Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places - assertEquals( 0.0, f.value( 5.0), tolerance ); - assertEquals( 0.0, f.value( 1.0), tolerance ); - assertEquals( 0.0, f.value( -3.0), tolerance ); - assertEquals( 54.84375, f.value( -1.5), tolerance ); - assertEquals( -8.06637, f.value( 1.3), tolerance ); - - assertEquals(f.degree(), 5); + Assert.assertEquals(0, f.value(5), tolerance); + Assert.assertEquals(0, f.value(1), tolerance); + Assert.assertEquals(0, f.value(-3), tolerance); + Assert.assertEquals(54.84375, f.value(-1.5), tolerance); + Assert.assertEquals(-8.06637, f.value(1.3), tolerance); + Assert.assertEquals(f.degree(), 5); } - /** * tests the firstDerivative function by comparison * @@ -137,97 +132,96 @@ public final class PolynomialFunctionTest extends TestCase { * f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6 * and h(x) = 6x - 4 */ + @Test public void testfirstDerivativeComparison() { - double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 }; - double[] g_coeff = { 6.0, -4.0, 3.0 }; - double[] h_coeff = { -4.0, 6.0 }; + double[] f_coeff = { 3, 6, -2, 1 }; + double[] g_coeff = { 6, -4, 3 }; + double[] h_coeff = { -4, 6 }; - PolynomialFunction f = new PolynomialFunction( f_coeff ); - PolynomialFunction g = new PolynomialFunction( g_coeff ); - PolynomialFunction h = new PolynomialFunction( h_coeff ); + PolynomialFunction f = new PolynomialFunction(f_coeff); + PolynomialFunction g = new PolynomialFunction(g_coeff); + PolynomialFunction h = new PolynomialFunction(h_coeff); // compare f' = g - assertEquals( f.derivative().value(0.0), g.value(0.0), tolerance ); - assertEquals( f.derivative().value(1.0), g.value(1.0), tolerance ); - assertEquals( f.derivative().value(100.0), g.value(100.0), tolerance ); - assertEquals( f.derivative().value(4.1), g.value(4.1), tolerance ); - assertEquals( f.derivative().value(-3.25), g.value(-3.25), tolerance ); + Assert.assertEquals(f.derivative().value(0), g.value(0), tolerance); + Assert.assertEquals(f.derivative().value(1), g.value(1), tolerance); + Assert.assertEquals(f.derivative().value(100), g.value(100), tolerance); + Assert.assertEquals(f.derivative().value(4.1), g.value(4.1), tolerance); + Assert.assertEquals(f.derivative().value(-3.25), g.value(-3.25), tolerance); // compare g' = h - assertEquals( g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance ); - assertEquals( g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance ); - + Assert.assertEquals(g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance); + Assert.assertEquals(g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance); } + @Test public void testString() { - PolynomialFunction p = new PolynomialFunction(new double[] { -5.0, 3.0, 1.0 }); - checkPolynomial(p, "-5.0 + 3.0 x + x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 0.0, -2.0, 3.0 }), - "-2.0 x + 3.0 x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 1.0, -2.0, 3.0 }), - "1.0 - 2.0 x + 3.0 x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 0.0, 2.0, 3.0 }), - "2.0 x + 3.0 x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 1.0, 2.0, 3.0 }), - "1.0 + 2.0 x + 3.0 x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 1.0, 0.0, 3.0 }), - "1.0 + 3.0 x^2"); - checkPolynomial(new PolynomialFunction(new double[] { 0.0 }), + PolynomialFunction p = new PolynomialFunction(new double[] { -5, 3, 1 }); + checkPolynomial(p, "-5 + 3 x + x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 0, -2, 3 }), + "-2 x + 3 x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 1, -2, 3 }), + "1 - 2 x + 3 x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 0, 2, 3 }), + "2 x + 3 x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 1, 2, 3 }), + "1 + 2 x + 3 x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 1, 0, 3 }), + "1 + 3 x^2"); + checkPolynomial(new PolynomialFunction(new double[] { 0 }), "0"); } + @Test public void testAddition() { - - PolynomialFunction p1 = new PolynomialFunction(new double[] { -2.0, 1.0 }); - PolynomialFunction p2 = new PolynomialFunction(new double[] { 2.0, -1.0, 0.0 }); + PolynomialFunction p1 = new PolynomialFunction(new double[] { -2, 1 }); + PolynomialFunction p2 = new PolynomialFunction(new double[] { 2, -1, 0 }); checkNullPolynomial(p1.add(p2)); p2 = p1.add(p1); - checkPolynomial(p2, "-4.0 + 2.0 x"); + checkPolynomial(p2, "-4 + 2 x"); - p1 = new PolynomialFunction(new double[] { 1.0, -4.0, 2.0 }); - p2 = new PolynomialFunction(new double[] { -1.0, 3.0, -2.0 }); + p1 = new PolynomialFunction(new double[] { 1, -4, 2 }); + p2 = new PolynomialFunction(new double[] { -1, 3, -2 }); p1 = p1.add(p2); - assertEquals(1, p1.degree()); + Assert.assertEquals(1, p1.degree()); checkPolynomial(p1, "-x"); - } + @Test public void testSubtraction() { - - PolynomialFunction p1 = new PolynomialFunction(new double[] { -2.0, 1.0 }); + PolynomialFunction p1 = new PolynomialFunction(new double[] { -2, 1 }); checkNullPolynomial(p1.subtract(p1)); - PolynomialFunction p2 = new PolynomialFunction(new double[] { -2.0, 6.0 }); + PolynomialFunction p2 = new PolynomialFunction(new double[] { -2, 6 }); p2 = p2.subtract(p1); - checkPolynomial(p2, "5.0 x"); + checkPolynomial(p2, "5 x"); - p1 = new PolynomialFunction(new double[] { 1.0, -4.0, 2.0 }); - p2 = new PolynomialFunction(new double[] { -1.0, 3.0, 2.0 }); + p1 = new PolynomialFunction(new double[] { 1, -4, 2 }); + p2 = new PolynomialFunction(new double[] { -1, 3, 2 }); p1 = p1.subtract(p2); - assertEquals(1, p1.degree()); - checkPolynomial(p1, "2.0 - 7.0 x"); - + Assert.assertEquals(1, p1.degree()); + checkPolynomial(p1, "2 - 7 x"); } + @Test public void testMultiplication() { + PolynomialFunction p1 = new PolynomialFunction(new double[] { -3, 2 }); + PolynomialFunction p2 = new PolynomialFunction(new double[] { 3, 2, 1 }); + checkPolynomial(p1.multiply(p2), "-9 + x^2 + 2 x^3"); - PolynomialFunction p1 = new PolynomialFunction(new double[] { -3.0, 2.0 }); - PolynomialFunction p2 = new PolynomialFunction(new double[] { 3.0, 2.0, 1.0 }); - checkPolynomial(p1.multiply(p2), "-9.0 + x^2 + 2.0 x^3"); - - p1 = new PolynomialFunction(new double[] { 0.0, 1.0 }); + p1 = new PolynomialFunction(new double[] { 0, 1 }); p2 = p1; for (int i = 2; i < 10; ++i) { p2 = p2.multiply(p1); checkPolynomial(p2, "x^" + i); } - } + @Test public void testSerial() { - PolynomialFunction p2 = new PolynomialFunction(new double[] { 3.0, 2.0, 1.0 }); - assertEquals(p2, TestUtils.serializeAndRecover(p2)); + PolynomialFunction p2 = new PolynomialFunction(new double[] { 3, 2, 1 }); + Assert.assertEquals(p2, TestUtils.serializeAndRecover(p2)); } /** @@ -237,35 +231,35 @@ public final class PolynomialFunctionTest extends TestCase { * f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6 * and h(x) = 6x - 4 */ + @Test public void testMath341() { - double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 }; - double[] g_coeff = { 6.0, -4.0, 3.0 }; - double[] h_coeff = { -4.0, 6.0 }; + double[] f_coeff = { 3, 6, -2, 1 }; + double[] g_coeff = { 6, -4, 3 }; + double[] h_coeff = { -4, 6 }; - PolynomialFunction f = new PolynomialFunction( f_coeff ); - PolynomialFunction g = new PolynomialFunction( g_coeff ); - PolynomialFunction h = new PolynomialFunction( h_coeff ); + PolynomialFunction f = new PolynomialFunction(f_coeff); + PolynomialFunction g = new PolynomialFunction(g_coeff); + PolynomialFunction h = new PolynomialFunction(h_coeff); // compare f' = g - assertEquals( f.derivative().value(0.0), g.value(0.0), tolerance ); - assertEquals( f.derivative().value(1.0), g.value(1.0), tolerance ); - assertEquals( f.derivative().value(100.0), g.value(100.0), tolerance ); - assertEquals( f.derivative().value(4.1), g.value(4.1), tolerance ); - assertEquals( f.derivative().value(-3.25), g.value(-3.25), tolerance ); + Assert.assertEquals(f.derivative().value(0), g.value(0), tolerance); + Assert.assertEquals(f.derivative().value(1), g.value(1), tolerance); + Assert.assertEquals(f.derivative().value(100), g.value(100), tolerance); + Assert.assertEquals(f.derivative().value(4.1), g.value(4.1), tolerance); + Assert.assertEquals(f.derivative().value(-3.25), g.value(-3.25), tolerance); // compare g' = h - assertEquals( g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance ); - assertEquals( g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance ); + Assert.assertEquals(g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance); + Assert.assertEquals(g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance); } public void checkPolynomial(PolynomialFunction p, String reference) { - assertEquals(reference, p.toString()); + Assert.assertEquals(reference, p.toString()); } private void checkNullPolynomial(PolynomialFunction p) { for (double coefficient : p.getCoefficients()) { - assertEquals(0.0, coefficient, 1.0e-15); + Assert.assertEquals(0, coefficient, 1e-15); } } - } diff --git a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java index cb6f27464..8b35abc71 100644 --- a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java +++ b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java @@ -18,38 +18,41 @@ package org.apache.commons.math.analysis.polynomials; import org.apache.commons.math.util.FastMath; -import junit.framework.TestCase; +import org.junit.Test; +import org.junit.Assert; /** * Tests the PolynomialsUtils class. * * @version $Revision$ $Date$ */ -public class PolynomialsUtilsTest extends TestCase { +public class PolynomialsUtilsTest { + @Test public void testFirstChebyshevPolynomials() { - - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(3), "-3.0 x + 4.0 x^3"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(2), "-1.0 + 2.0 x^2"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(3), "-3 x + 4 x^3"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(2), "-1 + 2 x^2"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(1), "x"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(0), "1.0"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(0), "1"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(7), "-7.0 x + 56.0 x^3 - 112.0 x^5 + 64.0 x^7"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(6), "-1.0 + 18.0 x^2 - 48.0 x^4 + 32.0 x^6"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(5), "5.0 x - 20.0 x^3 + 16.0 x^5"); - checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(4), "1.0 - 8.0 x^2 + 8.0 x^4"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(7), "-7 x + 56 x^3 - 112 x^5 + 64 x^7"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(6), "-1 + 18 x^2 - 48 x^4 + 32 x^6"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(5), "5 x - 20 x^3 + 16 x^5"); + checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(4), "1 - 8 x^2 + 8 x^4"); } + @Test public void testChebyshevBounds() { for (int k = 0; k < 12; ++k) { PolynomialFunction Tk = PolynomialsUtils.createChebyshevPolynomial(k); - for (double x = -1.0; x <= 1.0; x += 0.02) { - assertTrue(k + " " + Tk.value(x), FastMath.abs(Tk.value(x)) < (1.0 + 1.0e-12)); + for (double x = -1; x <= 1; x += 0.02) { + Assert.assertTrue(k + " " + Tk.value(x), FastMath.abs(Tk.value(x)) < (1 + 1e-12)); } } } + @Test public void testChebyshevDifferentials() { for (int k = 0; k < 12; ++k) { @@ -70,17 +73,17 @@ public class PolynomialsUtilsTest extends TestCase { } } + @Test public void testFirstHermitePolynomials() { + checkPolynomial(PolynomialsUtils.createHermitePolynomial(3), "-12 x + 8 x^3"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(2), "-2 + 4 x^2"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(1), "2 x"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(0), "1"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(3), "-12.0 x + 8.0 x^3"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(2), "-2.0 + 4.0 x^2"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(1), "2.0 x"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(0), "1.0"); - - checkPolynomial(PolynomialsUtils.createHermitePolynomial(7), "-1680.0 x + 3360.0 x^3 - 1344.0 x^5 + 128.0 x^7"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(6), "-120.0 + 720.0 x^2 - 480.0 x^4 + 64.0 x^6"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(5), "120.0 x - 160.0 x^3 + 32.0 x^5"); - checkPolynomial(PolynomialsUtils.createHermitePolynomial(4), "12.0 - 48.0 x^2 + 16.0 x^4"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(7), "-1680 x + 3360 x^3 - 1344 x^5 + 128 x^7"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(6), "-120 + 720 x^2 - 480 x^4 + 64 x^6"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(5), "120 x - 160 x^3 + 32 x^5"); + checkPolynomial(PolynomialsUtils.createHermitePolynomial(4), "12 - 48 x^2 + 16 x^4"); } @@ -104,23 +107,23 @@ public class PolynomialsUtilsTest extends TestCase { } } + @Test public void testFirstLaguerrePolynomials() { - - checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(3), 6l, "6.0 - 18.0 x + 9.0 x^2 - x^3"); - checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(2), 2l, "2.0 - 4.0 x + x^2"); - checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(1), 1l, "1.0 - x"); - checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(0), 1l, "1.0"); + checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(3), 6l, "6 - 18 x + 9 x^2 - x^3"); + checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(2), 2l, "2 - 4 x + x^2"); + checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(1), 1l, "1 - x"); + checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(0), 1l, "1"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(7), 5040l, - "5040.0 - 35280.0 x + 52920.0 x^2 - 29400.0 x^3" - + " + 7350.0 x^4 - 882.0 x^5 + 49.0 x^6 - x^7"); + "5040 - 35280 x + 52920 x^2 - 29400 x^3" + + " + 7350 x^4 - 882 x^5 + 49 x^6 - x^7"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(6), 720l, - "720.0 - 4320.0 x + 5400.0 x^2 - 2400.0 x^3 + 450.0 x^4" - + " - 36.0 x^5 + x^6"); + "720 - 4320 x + 5400 x^2 - 2400 x^3 + 450 x^4" + + " - 36 x^5 + x^6"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(5), 120l, - "120.0 - 600.0 x + 600.0 x^2 - 200.0 x^3 + 25.0 x^4 - x^5"); + "120 - 600 x + 600 x^2 - 200 x^3 + 25 x^4 - x^5"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(4), 24l, - "24.0 - 96.0 x + 72.0 x^2 - 16.0 x^3 + x^4"); + "24 - 96 x + 72 x^2 - 16 x^3 + x^4"); } @@ -144,20 +147,21 @@ public class PolynomialsUtilsTest extends TestCase { } } + @Test public void testFirstLegendrePolynomials() { - - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3), 2l, "-3.0 x + 5.0 x^3"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(2), 2l, "-1.0 + 3.0 x^2"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3), 2l, "-3 x + 5 x^3"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(2), 2l, "-1 + 3 x^2"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(1), 1l, "x"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(0), 1l, "1.0"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(0), 1l, "1"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(7), 16l, "-35.0 x + 315.0 x^3 - 693.0 x^5 + 429.0 x^7"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(6), 16l, "-5.0 + 105.0 x^2 - 315.0 x^4 + 231.0 x^6"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(5), 8l, "15.0 x - 70.0 x^3 + 63.0 x^5"); - checkPolynomial(PolynomialsUtils.createLegendrePolynomial(4), 8l, "3.0 - 30.0 x^2 + 35.0 x^4"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(7), 16l, "-35 x + 315 x^3 - 693 x^5 + 429 x^7"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(6), 16l, "-5 + 105 x^2 - 315 x^4 + 231 x^6"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(5), 8l, "15 x - 70 x^3 + 63 x^5"); + checkPolynomial(PolynomialsUtils.createLegendrePolynomial(4), 8l, "3 - 30 x^2 + 35 x^4"); } + @Test public void testLegendreDifferentials() { for (int k = 0; k < 12; ++k) { @@ -178,41 +182,41 @@ public class PolynomialsUtilsTest extends TestCase { } } + @Test public void testHighDegreeLegendre() { PolynomialsUtils.createLegendrePolynomial(40); double[] l40 = PolynomialsUtils.createLegendrePolynomial(40).getCoefficients(); - double denominator = 274877906944.0; + double denominator = 274877906944d; double[] numerators = new double[] { - +34461632205.0, -28258538408100.0, +3847870979902950.0, -207785032914759300.0, - +5929294332103310025.0, -103301483474866556880.0, +1197358103913226000200.0, -9763073770369381232400.0, - +58171647881784229843050.0, -260061484647976556945400.0, +888315281771246239250340.0, -2345767627188139419665400.0, - +4819022625419112503443050.0, -7710436200670580005508880.0, +9566652323054238154983240.0, -9104813935044723209570256.0, - +6516550296251767619752905.0, -3391858621221953912598660.0, +1211378079007840683070950.0, -265365894974690562152100.0, - +26876802183334044115405.0 + +34461632205d, -28258538408100d, +3847870979902950d, -207785032914759300d, + +5929294332103310025d, -103301483474866556880d, +1197358103913226000200d, -9763073770369381232400d, + +58171647881784229843050d, -260061484647976556945400d, +888315281771246239250340d, -2345767627188139419665400d, + +4819022625419112503443050d, -7710436200670580005508880d, +9566652323054238154983240d, -9104813935044723209570256d, + +6516550296251767619752905d, -3391858621221953912598660d, +1211378079007840683070950d, -265365894974690562152100d, + +26876802183334044115405d }; for (int i = 0; i < l40.length; ++i) { if (i % 2 == 0) { double ci = numerators[i / 2] / denominator; - assertEquals(ci, l40[i], FastMath.abs(ci) * 1.0e-15); + Assert.assertEquals(ci, l40[i], FastMath.abs(ci) * 1e-15); } else { - assertEquals(0.0, l40[i], 0.0); + Assert.assertEquals(0, l40[i], 0); } } } private void checkPolynomial(PolynomialFunction p, long denominator, String reference) { PolynomialFunction q = new PolynomialFunction(new double[] { denominator}); - assertEquals(reference, p.multiply(q).toString()); + Assert.assertEquals(reference, p.multiply(q).toString()); } private void checkPolynomial(PolynomialFunction p, String reference) { - assertEquals(reference, p.toString()); + Assert.assertEquals(reference, p.toString()); } private void checkNullPolynomial(PolynomialFunction p) { for (double coefficient : p.getCoefficients()) { - assertEquals(0.0, coefficient, 1.0e-13); + Assert.assertEquals(0, coefficient, 1e-13); } } - }