KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > analysis > PolynomialFunctionTest


1 /*
2  *
3  * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18 package org.apache.commons.math.analysis;
19
20 // commons-math
21
import org.apache.commons.math.MathException;
22
23 // junit
24
import junit.framework.TestCase;
25
26 /**
27  * Tests the PolynomialFunction implementation of a UnivariateRealFunction.
28  *
29  * @version $Revision$
30  * @author Matt Cliff <matt@mattcliff.com>
31  */

32 public final class PolynomialFunctionTest extends TestCase {
33
34     /** Error tolerance for tests */
35     protected double tolerance = 1.0e-12;
36
37     /**
38      * tests the value of a constant polynomial.
39      *
40      * <p>value of this is 2.5 everywhere.</p>
41      */

42     public void testConstants() throws MathException {
43         double[] c = { 2.5 };
44         PolynomialFunction f = new PolynomialFunction( c );
45
46         // verify that we are equal to c[0] at several (nonsymmetric) places
47
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     /**
60      * tests the value of a linear polynomial.
61      *
62      * <p>This will test the function f(x) = 3*x - 1.5</p>
63      * <p>This will have the values
64      * <tt>f(0.0) = -1.5, f(-1.0) = -4.5, f(-2.5) = -9.0,
65      * f(0.5) = 0.0, f(1.5) = 3.0</tt> and <tt>f(3.0) = 7.5</tt>
66      * </p>
67      */

68     public void testLinear() throws MathException {
69         double[] c = { -1.5, 3.0 };
70         PolynomialFunction f = new PolynomialFunction( c );
71
72         // verify that we are equal to c[0] when x=0
73
assertEquals( f.value( 0.0), c[0], tolerance );
74
75         // now check a few other places
76
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     /**
90      * Tests a second order polynomial.
91      * <p> This will test the function f(x) = 2x^2 - 3x -2 = (2x+1)(x-2)</p>
92      *
93      */

94     public void testQuadratic() throws MathException {
95         double[] c = { -2.0, -3.0, 2.0 };
96         PolynomialFunction f = new PolynomialFunction( c );
97
98         // verify that we are equal to c[0] when x=0
99
assertEquals( f.value( 0.0), c[0], tolerance );
100
101         // now check a few other places
102
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     /**
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     /**
136      * tests the firstDerivative function by comparision
137      *
138      * <p>This will test the functions
139      * <tt>f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6</tt>
140      * and <tt>h(x) = 6x - 4</tt>
141      */

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         // compare f' = g
152
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         // compare g' = h
159

160
161         // compare f'' = h
162
}
163
164 }
165
Popular Tags