1 18 package org.apache.commons.math.analysis; 19 20 import org.apache.commons.math.MathException; 22 23 import junit.framework.TestCase; 25 26 32 public final class PolynomialFunctionTest extends TestCase { 33 34 35 protected double tolerance = 1.0e-12; 36 37 42 public void testConstants() throws MathException { 43 double[] c = { 2.5 }; 44 PolynomialFunction f = new PolynomialFunction( c ); 45 46 assertEquals( f.value( 0.0), c[0], tolerance ); 48 assertEquals( f.value( -1.0), c[0], tolerance ); 49 assertEquals( f.value( -123.5), c[0], tolerance ); 50 assertEquals( f.value( 3.0), c[0], tolerance ); 51 assertEquals( f.value( 456.89), c[0], tolerance ); 52 53 assertEquals(f.degree(), 0); 54 assertEquals(f.derivative().value(0), 0, tolerance); 55 56 assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); 57 } 58 59 68 public void testLinear() throws MathException { 69 double[] c = { -1.5, 3.0 }; 70 PolynomialFunction f = new PolynomialFunction( c ); 71 72 assertEquals( f.value( 0.0), c[0], tolerance ); 74 75 assertEquals( -4.5, f.value( -1.0), tolerance ); 77 assertEquals( -9.0, f.value( -2.5), tolerance ); 78 assertEquals( 0.0, f.value( 0.5), tolerance ); 79 assertEquals( 3.0, f.value( 1.5), tolerance ); 80 assertEquals( 7.5, f.value( 3.0), tolerance ); 81 82 assertEquals(f.degree(), 1); 83 84 assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); 85 86 } 87 88 89 94 public void testQuadratic() throws MathException { 95 double[] c = { -2.0, -3.0, 2.0 }; 96 PolynomialFunction f = new PolynomialFunction( c ); 97 98 assertEquals( f.value( 0.0), c[0], tolerance ); 100 101 assertEquals( 0.0, f.value( -0.5), tolerance ); 103 assertEquals( 0.0, f.value( 2.0), tolerance ); 104 assertEquals( -2.0, f.value( 1.5), tolerance ); 105 assertEquals( 7.0, f.value( -1.5), tolerance ); 106 assertEquals( 265.5312, f.value( 12.34), tolerance ); 107 108 } 109 110 111 116 public void testQuintic() throws MathException { 117 double[] c = { 0.0, 0.0, 15.0, -13.0, -3.0, 1.0 }; 118 PolynomialFunction f = new PolynomialFunction( c ); 119 120 assertEquals( f.value( 0.0), c[0], tolerance ); 122 123 assertEquals( 0.0, f.value( 5.0), tolerance ); 125 assertEquals( 0.0, f.value( 1.0), tolerance ); 126 assertEquals( 0.0, f.value( -3.0), tolerance ); 127 assertEquals( 54.84375, f.value( -1.5), tolerance ); 128 assertEquals( -8.06637, f.value( 1.3), tolerance ); 129 130 assertEquals(f.degree(), 5); 131 132 } 133 134 135 142 public void testfirstDerivativeComparision() throws MathException { 143 double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 }; 144 double[] g_coeff = { 6.0, -4.0, 3.0 }; 145 double[] h_coeff = { -4.0, 6.0 }; 146 147 PolynomialFunction f = new PolynomialFunction( f_coeff ); 148 PolynomialFunction g = new PolynomialFunction( g_coeff ); 149 PolynomialFunction h = new PolynomialFunction( h_coeff ); 150 151 assertEquals( f.derivative().value(0.0), g.value(0.0), tolerance ); 153 assertEquals( f.derivative().value(1.0), g.value(1.0), tolerance ); 154 assertEquals( f.derivative().value(100.0), g.value(100.0), tolerance ); 155 assertEquals( f.derivative().value(4.1), g.value(4.1), tolerance ); 156 assertEquals( f.derivative().value(-3.25), g.value(-3.25), tolerance ); 157 158 160 161 } 163 164 } 165 | Popular Tags |