Added orthogonality tests for special polynomials.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1180055 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-10-07 14:34:45 +00:00
parent 732f83c0b5
commit ca133d8940

View File

@ -16,10 +16,11 @@
*/
package org.apache.commons.math.analysis.polynomials;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.integration.LegendreGaussIntegrator;
import org.apache.commons.math.util.FastMath;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests the PolynomialsUtils class.
@ -73,6 +74,22 @@ public class PolynomialsUtilsTest {
}
}
@Test
public void testChebyshevOrthogonality() {
UnivariateRealFunction weight = new UnivariateRealFunction() {
public double value(double x) {
return 1 / FastMath.sqrt(1 - x * x);
}
};
for (int i = 0; i < 10; ++i) {
PolynomialFunction pi = PolynomialsUtils.createChebyshevPolynomial(i);
for (int j = 0; j <= i; ++j) {
PolynomialFunction pj = PolynomialsUtils.createChebyshevPolynomial(j);
checkOrthogonality(pi, pj, weight, -0.9999, 0.9999, 1.5, 0.03);
}
}
}
@Test
public void testFirstHermitePolynomials() {
checkPolynomial(PolynomialsUtils.createHermitePolynomial(3), "-12 x + 8 x^3");
@ -108,6 +125,22 @@ public class PolynomialsUtilsTest {
}
}
@Test
public void testHermiteOrthogonality() {
UnivariateRealFunction weight = new UnivariateRealFunction() {
public double value(double x) {
return FastMath.exp(-x * x);
}
};
for (int i = 0; i < 10; ++i) {
PolynomialFunction pi = PolynomialsUtils.createHermitePolynomial(i);
for (int j = 0; j <= i; ++j) {
PolynomialFunction pj = PolynomialsUtils.createHermitePolynomial(j);
checkOrthogonality(pi, pj, weight, -50, 50, 1.5, 1.0e-8);
}
}
}
@Test
public void testFirstLaguerrePolynomials() {
checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(3), 6l, "6 - 18 x + 9 x^2 - x^3");
@ -149,6 +182,22 @@ public class PolynomialsUtilsTest {
}
}
@Test
public void testLaguerreOrthogonality() {
UnivariateRealFunction weight = new UnivariateRealFunction() {
public double value(double x) {
return FastMath.exp(-x);
}
};
for (int i = 0; i < 10; ++i) {
PolynomialFunction pi = PolynomialsUtils.createLaguerrePolynomial(i);
for (int j = 0; j <= i; ++j) {
PolynomialFunction pj = PolynomialsUtils.createLaguerrePolynomial(j);
checkOrthogonality(pi, pj, weight, 0.0, 100.0, 0.99999, 1.0e-13);
}
}
}
@Test
public void testFirstLegendrePolynomials() {
checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3), 2l, "-3 x + 5 x^3");
@ -184,6 +233,22 @@ public class PolynomialsUtilsTest {
}
}
@Test
public void testLegendreOrthogonality() {
UnivariateRealFunction weight = new UnivariateRealFunction() {
public double value(double x) {
return 1;
}
};
for (int i = 0; i < 10; ++i) {
PolynomialFunction pi = PolynomialsUtils.createLegendrePolynomial(i);
for (int j = 0; j <= i; ++j) {
PolynomialFunction pj = PolynomialsUtils.createLegendrePolynomial(j);
checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-13);
}
}
}
@Test
public void testHighDegreeLegendre() {
PolynomialsUtils.createLegendrePolynomial(40);
@ -208,7 +273,7 @@ public class PolynomialsUtilsTest {
}
@Test
public void testShift(){
public void testShift() {
// f1(x) = 1 + x + 2 x^2
PolynomialFunction f1x = new PolynomialFunction(new double[] { 1, 1, 2 });
@ -251,4 +316,28 @@ public class PolynomialsUtilsTest {
Assert.assertEquals(0, coefficient, 1e-13);
}
}
private void checkOrthogonality(final PolynomialFunction p1,
final PolynomialFunction p2,
final UnivariateRealFunction weight,
final double a, final double b,
final double nonZeroThreshold,
final double zeroThreshold) {
UnivariateRealFunction f = new UnivariateRealFunction() {
public double value(double x) {
return weight.value(x) * p1.value(x) * p2.value(x);
}
};
double dotProduct =
new LegendreGaussIntegrator(5, 1.0e-9, 1.0e-8, 2, 15).integrate(1000000, f, a, b);
if (p1.degree() == p2.degree()) {
// integral should be non-zero
Assert.assertTrue("I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct,
FastMath.abs(dotProduct) > nonZeroThreshold);
} else {
// integral should be zero
Assert.assertEquals("I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct,
0.0, FastMath.abs(dotProduct), zeroThreshold);
}
}
}