001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy 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,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.analysis.polynomials;
018
019 import org.apache.commons.math.MathException;
020 import junit.framework.TestCase;
021
022 /**
023 * Testcase for Newton form of polynomial function.
024 * <p>
025 * The small tolerance number is used only to account for round-off errors.
026 *
027 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
028 */
029 public final class PolynomialFunctionNewtonFormTest extends TestCase {
030
031 /**
032 * Test of polynomial for the linear function.
033 */
034 public void testLinearFunction() throws MathException {
035 PolynomialFunctionNewtonForm p;
036 double coefficients[], z, expected, result, tolerance = 1E-12;
037
038 // p(x) = 1.5x - 4 = 2 + 1.5(x-4)
039 double a[] = { 2.0, 1.5 };
040 double c[] = { 4.0 };
041 p = new PolynomialFunctionNewtonForm(a, c);
042
043 z = 2.0; expected = -1.0; result = p.value(z);
044 assertEquals(expected, result, tolerance);
045
046 z = 4.5; expected = 2.75; result = p.value(z);
047 assertEquals(expected, result, tolerance);
048
049 z = 6.0; expected = 5.0; result = p.value(z);
050 assertEquals(expected, result, tolerance);
051
052 assertEquals(1, p.degree());
053
054 coefficients = p.getCoefficients();
055 assertEquals(2, coefficients.length);
056 assertEquals(-4.0, coefficients[0], tolerance);
057 assertEquals(1.5, coefficients[1], tolerance);
058 }
059
060 /**
061 * Test of polynomial for the quadratic function.
062 */
063 public void testQuadraticFunction() throws MathException {
064 PolynomialFunctionNewtonForm p;
065 double coefficients[], z, expected, result, tolerance = 1E-12;
066
067 // p(x) = 2x^2 + 5x - 3 = 4 + 3(x-1) + 2(x-1)(x+2)
068 double a[] = { 4.0, 3.0, 2.0 };
069 double c[] = { 1.0, -2.0 };
070 p = new PolynomialFunctionNewtonForm(a, c);
071
072 z = 1.0; expected = 4.0; result = p.value(z);
073 assertEquals(expected, result, tolerance);
074
075 z = 2.5; expected = 22.0; result = p.value(z);
076 assertEquals(expected, result, tolerance);
077
078 z = -2.0; expected = -5.0; result = p.value(z);
079 assertEquals(expected, result, tolerance);
080
081 assertEquals(2, p.degree());
082
083 coefficients = p.getCoefficients();
084 assertEquals(3, coefficients.length);
085 assertEquals(-3.0, coefficients[0], tolerance);
086 assertEquals(5.0, coefficients[1], tolerance);
087 assertEquals(2.0, coefficients[2], tolerance);
088 }
089
090 /**
091 * Test of polynomial for the quintic function.
092 */
093 public void testQuinticFunction() throws MathException {
094 PolynomialFunctionNewtonForm p;
095 double coefficients[], z, expected, result, tolerance = 1E-12;
096
097 // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x
098 // = 6x - 6x^2 -6x^2(x-1) + x^2(x-1)(x+1) + x^2(x-1)(x+1)(x-2)
099 double a[] = { 0.0, 6.0, -6.0, -6.0, 1.0, 1.0 };
100 double c[] = { 0.0, 0.0, 1.0, -1.0, 2.0 };
101 p = new PolynomialFunctionNewtonForm(a, c);
102
103 z = 0.0; expected = 0.0; result = p.value(z);
104 assertEquals(expected, result, tolerance);
105
106 z = -2.0; expected = 0.0; result = p.value(z);
107 assertEquals(expected, result, tolerance);
108
109 z = 4.0; expected = 360.0; result = p.value(z);
110 assertEquals(expected, result, tolerance);
111
112 assertEquals(5, p.degree());
113
114 coefficients = p.getCoefficients();
115 assertEquals(6, coefficients.length);
116 assertEquals(0.0, coefficients[0], tolerance);
117 assertEquals(6.0, coefficients[1], tolerance);
118 assertEquals(1.0, coefficients[2], tolerance);
119 assertEquals(-7.0, coefficients[3], tolerance);
120 assertEquals(-1.0, coefficients[4], tolerance);
121 assertEquals(1.0, coefficients[5], tolerance);
122 }
123
124 /**
125 * Test of parameters for the polynomial.
126 */
127 public void testParameters() throws Exception {
128
129 try {
130 // bad input array length
131 double a[] = { 1.0 };
132 double c[] = { 2.0 };
133 new PolynomialFunctionNewtonForm(a, c);
134 fail("Expecting IllegalArgumentException - bad input array length");
135 } catch (IllegalArgumentException ex) {
136 // expected
137 }
138 try {
139 // mismatch input arrays
140 double a[] = { 1.0, 2.0, 3.0, 4.0 };
141 double c[] = { 4.0, 3.0, 2.0, 1.0 };
142 new PolynomialFunctionNewtonForm(a, c);
143 fail("Expecting IllegalArgumentException - mismatch input arrays");
144 } catch (IllegalArgumentException ex) {
145 // expected
146 }
147 }
148 }