001: /*
002: *
003: * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy
007: * of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018: package org.apache.commons.math.analysis;
019:
020: // commons-math
021: import org.apache.commons.math.MathException;
022:
023: // junit
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests the PolynomialFunction implementation of a UnivariateRealFunction.
028: *
029: * @version $Revision: 155427 $
030: * @author Matt Cliff <matt@mattcliff.com>
031: */
032: public final class PolynomialFunctionTest extends TestCase {
033:
034: /** Error tolerance for tests */
035: protected double tolerance = 1.0e-12;
036:
037: /**
038: * tests the value of a constant polynomial.
039: *
040: * <p>value of this is 2.5 everywhere.</p>
041: */
042: public void testConstants() throws MathException {
043: double[] c = { 2.5 };
044: PolynomialFunction f = new PolynomialFunction(c);
045:
046: // verify that we are equal to c[0] at several (nonsymmetric) places
047: assertEquals(f.value(0.0), c[0], tolerance);
048: assertEquals(f.value(-1.0), c[0], tolerance);
049: assertEquals(f.value(-123.5), c[0], tolerance);
050: assertEquals(f.value(3.0), c[0], tolerance);
051: assertEquals(f.value(456.89), c[0], tolerance);
052:
053: assertEquals(f.degree(), 0);
054: assertEquals(f.derivative().value(0), 0, tolerance);
055:
056: assertEquals(f.polynomialDerivative().derivative().value(0), 0,
057: tolerance);
058: }
059:
060: /**
061: * tests the value of a linear polynomial.
062: *
063: * <p>This will test the function f(x) = 3*x - 1.5</p>
064: * <p>This will have the values
065: * <tt>f(0.0) = -1.5, f(-1.0) = -4.5, f(-2.5) = -9.0,
066: * f(0.5) = 0.0, f(1.5) = 3.0</tt> and <tt>f(3.0) = 7.5</tt>
067: * </p>
068: */
069: public void testLinear() throws MathException {
070: double[] c = { -1.5, 3.0 };
071: PolynomialFunction f = new PolynomialFunction(c);
072:
073: // verify that we are equal to c[0] when x=0
074: assertEquals(f.value(0.0), c[0], tolerance);
075:
076: // now check a few other places
077: assertEquals(-4.5, f.value(-1.0), tolerance);
078: assertEquals(-9.0, f.value(-2.5), tolerance);
079: assertEquals(0.0, f.value(0.5), tolerance);
080: assertEquals(3.0, f.value(1.5), tolerance);
081: assertEquals(7.5, f.value(3.0), tolerance);
082:
083: assertEquals(f.degree(), 1);
084:
085: assertEquals(f.polynomialDerivative().derivative().value(0), 0,
086: tolerance);
087:
088: }
089:
090: /**
091: * Tests a second order polynomial.
092: * <p> This will test the function f(x) = 2x^2 - 3x -2 = (2x+1)(x-2)</p>
093: *
094: */
095: public void testQuadratic() throws MathException {
096: double[] c = { -2.0, -3.0, 2.0 };
097: PolynomialFunction f = new PolynomialFunction(c);
098:
099: // verify that we are equal to c[0] when x=0
100: assertEquals(f.value(0.0), c[0], tolerance);
101:
102: // now check a few other places
103: assertEquals(0.0, f.value(-0.5), tolerance);
104: assertEquals(0.0, f.value(2.0), tolerance);
105: assertEquals(-2.0, f.value(1.5), tolerance);
106: assertEquals(7.0, f.value(-1.5), tolerance);
107: assertEquals(265.5312, f.value(12.34), tolerance);
108:
109: }
110:
111: /**
112: * This will test the quintic function
113: * f(x) = x^2(x-5)(x+3)(x-1) = x^5 - 3x^4 -13x^3 + 15x^2</p>
114: *
115: */
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: // verify that we are equal to c[0] when x=0
121: assertEquals(f.value(0.0), c[0], tolerance);
122:
123: // now check a few other places
124: 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: * tests the firstDerivative function by comparision
136: *
137: * <p>This will test the functions
138: * <tt>f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6</tt>
139: * and <tt>h(x) = 6x - 4</tt>
140: */
141: public void testfirstDerivativeComparision() throws MathException {
142: double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 };
143: double[] g_coeff = { 6.0, -4.0, 3.0 };
144: double[] h_coeff = { -4.0, 6.0 };
145:
146: PolynomialFunction f = new PolynomialFunction(f_coeff);
147: PolynomialFunction g = new PolynomialFunction(g_coeff);
148: PolynomialFunction h = new PolynomialFunction(h_coeff);
149:
150: // compare f' = g
151: assertEquals(f.derivative().value(0.0), g.value(0.0), tolerance);
152: assertEquals(f.derivative().value(1.0), g.value(1.0), tolerance);
153: assertEquals(f.derivative().value(100.0), g.value(100.0),
154: tolerance);
155: assertEquals(f.derivative().value(4.1), g.value(4.1), tolerance);
156: assertEquals(f.derivative().value(-3.25), g.value(-3.25),
157: tolerance);
158:
159: // compare g' = h
160:
161: // compare f'' = h
162: }
163:
164: }
|