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 Lagrange form of polynomial function.
024     * <p>
025     * We use n+1 points to interpolate a polynomial of degree n. This should
026     * give us the exact same polynomial as result. Thus we can use a very
027     * small tolerance to account only for round-off errors.
028     *
029     * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 
030     */
031    public final class PolynomialFunctionLagrangeFormTest extends TestCase {
032    
033        /**
034         * Test of polynomial for the linear function.
035         */
036        public void testLinearFunction() throws MathException {
037            PolynomialFunctionLagrangeForm p;
038            double c[], z, expected, result, tolerance = 1E-12;
039    
040            // p(x) = 1.5x - 4
041            double x[] = { 0.0, 3.0 };
042            double y[] = { -4.0, 0.5 };
043            p = new PolynomialFunctionLagrangeForm(x, y);
044    
045            z = 2.0; expected = -1.0; result = p.value(z);
046            assertEquals(expected, result, tolerance);
047    
048            z = 4.5; expected = 2.75; result = p.value(z);
049            assertEquals(expected, result, tolerance);
050    
051            z = 6.0; expected = 5.0; result = p.value(z);
052            assertEquals(expected, result, tolerance);
053    
054            assertEquals(1, p.degree());
055    
056            c = p.getCoefficients();
057            assertEquals(2, c.length);
058            assertEquals(-4.0, c[0], tolerance);
059            assertEquals(1.5, c[1], tolerance);
060        }
061    
062        /**
063         * Test of polynomial for the quadratic function.
064         */
065        public void testQuadraticFunction() throws MathException {
066            PolynomialFunctionLagrangeForm p;
067            double c[], z, expected, result, tolerance = 1E-12;
068    
069            // p(x) = 2x^2 + 5x - 3 = (2x - 1)(x + 3)
070            double x[] = { 0.0, -1.0, 0.5 };
071            double y[] = { -3.0, -6.0, 0.0 };
072            p = new PolynomialFunctionLagrangeForm(x, y);
073    
074            z = 1.0; expected = 4.0; result = p.value(z);
075            assertEquals(expected, result, tolerance);
076    
077            z = 2.5; expected = 22.0; result = p.value(z);
078            assertEquals(expected, result, tolerance);
079    
080            z = -2.0; expected = -5.0; result = p.value(z);
081            assertEquals(expected, result, tolerance);
082    
083            assertEquals(2, p.degree());
084    
085            c = p.getCoefficients();
086            assertEquals(3, c.length);
087            assertEquals(-3.0, c[0], tolerance);
088            assertEquals(5.0, c[1], tolerance);
089            assertEquals(2.0, c[2], tolerance);
090        }
091    
092        /**
093         * Test of polynomial for the quintic function.
094         */
095        public void testQuinticFunction() throws MathException {
096            PolynomialFunctionLagrangeForm p;
097            double c[], z, expected, result, tolerance = 1E-12;
098    
099            // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x = x(x^2 - 1)(x + 2)(x - 3)
100            double x[] = { 1.0, -1.0, 2.0, 3.0, -3.0, 0.5 };
101            double y[] = { 0.0, 0.0, -24.0, 0.0, -144.0, 2.34375 };
102            p = new PolynomialFunctionLagrangeForm(x, y);
103    
104            z = 0.0; expected = 0.0; result = p.value(z);
105            assertEquals(expected, result, tolerance);
106    
107            z = -2.0; expected = 0.0; result = p.value(z);
108            assertEquals(expected, result, tolerance);
109    
110            z = 4.0; expected = 360.0; result = p.value(z);
111            assertEquals(expected, result, tolerance);
112    
113            assertEquals(5, p.degree());
114    
115            c = p.getCoefficients();
116            assertEquals(6, c.length);
117            assertEquals(0.0, c[0], tolerance);
118            assertEquals(6.0, c[1], tolerance);
119            assertEquals(1.0, c[2], tolerance);
120            assertEquals(-7.0, c[3], tolerance);
121            assertEquals(-1.0, c[4], tolerance);
122            assertEquals(1.0, c[5], tolerance);
123        }
124    
125        /**
126         * Test of parameters for the polynomial.
127         */
128        public void testParameters() throws Exception {
129    
130            try {
131                // bad input array length
132                double x[] = { 1.0 };
133                double y[] = { 2.0 };
134                new PolynomialFunctionLagrangeForm(x, y);
135                fail("Expecting IllegalArgumentException - bad input array length");
136            } catch (IllegalArgumentException ex) {
137                // expected
138            }
139            try {
140                // mismatch input arrays
141                double x[] = { 1.0, 2.0, 3.0, 4.0 };
142                double y[] = { 0.0, -4.0, -24.0 };
143                new PolynomialFunctionLagrangeForm(x, y);
144                fail("Expecting IllegalArgumentException - mismatch input arrays");
145            } catch (IllegalArgumentException ex) {
146                // expected
147            }
148        }
149    }