1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis.polynomials;
18
19 import org.apache.commons.math.MathException;
20 import junit.framework.TestCase;
21
22
23
24
25
26
27
28
29 public final class PolynomialFunctionNewtonFormTest extends TestCase {
30
31
32
33
34 public void testLinearFunction() throws MathException {
35 PolynomialFunctionNewtonForm p;
36 double coefficients[], z, expected, result, tolerance = 1E-12;
37
38
39 double a[] = { 2.0, 1.5 };
40 double c[] = { 4.0 };
41 p = new PolynomialFunctionNewtonForm(a, c);
42
43 z = 2.0; expected = -1.0; result = p.value(z);
44 assertEquals(expected, result, tolerance);
45
46 z = 4.5; expected = 2.75; result = p.value(z);
47 assertEquals(expected, result, tolerance);
48
49 z = 6.0; expected = 5.0; result = p.value(z);
50 assertEquals(expected, result, tolerance);
51
52 assertEquals(1, p.degree());
53
54 coefficients = p.getCoefficients();
55 assertEquals(2, coefficients.length);
56 assertEquals(-4.0, coefficients[0], tolerance);
57 assertEquals(1.5, coefficients[1], tolerance);
58 }
59
60
61
62
63 public void testQuadraticFunction() throws MathException {
64 PolynomialFunctionNewtonForm p;
65 double coefficients[], z, expected, result, tolerance = 1E-12;
66
67
68 double a[] = { 4.0, 3.0, 2.0 };
69 double c[] = { 1.0, -2.0 };
70 p = new PolynomialFunctionNewtonForm(a, c);
71
72 z = 1.0; expected = 4.0; result = p.value(z);
73 assertEquals(expected, result, tolerance);
74
75 z = 2.5; expected = 22.0; result = p.value(z);
76 assertEquals(expected, result, tolerance);
77
78 z = -2.0; expected = -5.0; result = p.value(z);
79 assertEquals(expected, result, tolerance);
80
81 assertEquals(2, p.degree());
82
83 coefficients = p.getCoefficients();
84 assertEquals(3, coefficients.length);
85 assertEquals(-3.0, coefficients[0], tolerance);
86 assertEquals(5.0, coefficients[1], tolerance);
87 assertEquals(2.0, coefficients[2], tolerance);
88 }
89
90
91
92
93 public void testQuinticFunction() throws MathException {
94 PolynomialFunctionNewtonForm p;
95 double coefficients[], z, expected, result, tolerance = 1E-12;
96
97
98
99 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
126
127 public void testParameters() throws Exception {
128
129 try {
130
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
137 }
138 try {
139
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
146 }
147 }
148 }