1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.analysis.polynomials;
18
19 import java.util.Arrays;
20
21 import org.apache.commons.math4.legacy.analysis.ParametricUnivariateFunction;
22 import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
23 import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
24 import org.apache.commons.math4.legacy.exception.NoDataException;
25 import org.apache.commons.math4.legacy.exception.NullArgumentException;
26 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
27 import org.apache.commons.math4.core.jdkmath.JdkMath;
28
29
30
31
32
33
34
35
36 public class PolynomialFunction implements UnivariateDifferentiableFunction {
37
38
39
40
41
42 private final double[] coefficients;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public PolynomialFunction(double[] c)
59 throws NullArgumentException, NoDataException {
60 super();
61 NullArgumentException.check(c);
62 int n = c.length;
63 if (n == 0) {
64 throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
65 }
66 while (n > 1 && c[n - 1] == 0) {
67 --n;
68 }
69 this.coefficients = new double[n];
70 System.arraycopy(c, 0, this.coefficients, 0, n);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85 @Override
86 public double value(double x) {
87 return evaluate(coefficients, x);
88 }
89
90
91
92
93
94
95 public int degree() {
96 return coefficients.length - 1;
97 }
98
99
100
101
102
103
104
105
106
107 public double[] getCoefficients() {
108 return coefficients.clone();
109 }
110
111
112
113
114
115
116
117
118
119
120
121 protected static double evaluate(double[] coefficients, double argument)
122 throws NullArgumentException, NoDataException {
123 NullArgumentException.check(coefficients);
124 int n = coefficients.length;
125 if (n == 0) {
126 throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
127 }
128 double result = coefficients[n - 1];
129 for (int j = n - 2; j >= 0; j--) {
130 result = argument * result + coefficients[j];
131 }
132 return result;
133 }
134
135
136
137
138
139
140
141 @Override
142 public DerivativeStructure value(final DerivativeStructure t)
143 throws NullArgumentException, NoDataException {
144 NullArgumentException.check(coefficients);
145 int n = coefficients.length;
146 if (n == 0) {
147 throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
148 }
149 DerivativeStructure result =
150 new DerivativeStructure(t.getFreeParameters(), t.getOrder(), coefficients[n - 1]);
151 for (int j = n - 2; j >= 0; j--) {
152 result = result.multiply(t).add(coefficients[j]);
153 }
154 return result;
155 }
156
157
158
159
160
161
162
163 public PolynomialFunction add(final PolynomialFunction p) {
164
165 final int lowLength = JdkMath.min(coefficients.length, p.coefficients.length);
166 final int highLength = JdkMath.max(coefficients.length, p.coefficients.length);
167
168
169 double[] newCoefficients = new double[highLength];
170 for (int i = 0; i < lowLength; ++i) {
171 newCoefficients[i] = coefficients[i] + p.coefficients[i];
172 }
173 System.arraycopy((coefficients.length < p.coefficients.length) ?
174 p.coefficients : coefficients,
175 lowLength,
176 newCoefficients, lowLength,
177 highLength - lowLength);
178
179 return new PolynomialFunction(newCoefficients);
180 }
181
182
183
184
185
186
187
188 public PolynomialFunction subtract(final PolynomialFunction p) {
189
190 int lowLength = JdkMath.min(coefficients.length, p.coefficients.length);
191 int highLength = JdkMath.max(coefficients.length, p.coefficients.length);
192
193
194 double[] newCoefficients = new double[highLength];
195 for (int i = 0; i < lowLength; ++i) {
196 newCoefficients[i] = coefficients[i] - p.coefficients[i];
197 }
198 if (coefficients.length < p.coefficients.length) {
199 for (int i = lowLength; i < highLength; ++i) {
200 newCoefficients[i] = -p.coefficients[i];
201 }
202 } else {
203 System.arraycopy(coefficients, lowLength, newCoefficients, lowLength,
204 highLength - lowLength);
205 }
206
207 return new PolynomialFunction(newCoefficients);
208 }
209
210
211
212
213
214
215 public PolynomialFunction negate() {
216 double[] newCoefficients = new double[coefficients.length];
217 for (int i = 0; i < coefficients.length; ++i) {
218 newCoefficients[i] = -coefficients[i];
219 }
220 return new PolynomialFunction(newCoefficients);
221 }
222
223
224
225
226
227
228
229 public PolynomialFunction multiply(final PolynomialFunction p) {
230 double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1];
231
232 for (int i = 0; i < newCoefficients.length; ++i) {
233 newCoefficients[i] = 0.0;
234 for (int j = JdkMath.max(0, i + 1 - p.coefficients.length);
235 j < JdkMath.min(coefficients.length, i + 1);
236 ++j) {
237 newCoefficients[i] += coefficients[j] * p.coefficients[i-j];
238 }
239 }
240
241 return new PolynomialFunction(newCoefficients);
242 }
243
244
245
246
247
248
249
250
251
252 protected static double[] differentiate(double[] coefficients)
253 throws NullArgumentException, NoDataException {
254 NullArgumentException.check(coefficients);
255 int n = coefficients.length;
256 if (n == 0) {
257 throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
258 }
259 if (n == 1) {
260 return new double[]{0};
261 }
262 double[] result = new double[n - 1];
263 for (int i = n - 1; i > 0; i--) {
264 result[i - 1] = i * coefficients[i];
265 }
266 return result;
267 }
268
269
270
271
272
273
274 public PolynomialFunction polynomialDerivative() {
275 return new PolynomialFunction(differentiate(coefficients));
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 @Override
294 public String toString() {
295 StringBuilder s = new StringBuilder();
296 if (coefficients[0] == 0.0) {
297 if (coefficients.length == 1) {
298 return "0";
299 }
300 } else {
301 s.append(toString(coefficients[0]));
302 }
303
304 for (int i = 1; i < coefficients.length; ++i) {
305 if (coefficients[i] != 0) {
306 if (s.length() > 0) {
307 if (coefficients[i] < 0) {
308 s.append(" - ");
309 } else {
310 s.append(" + ");
311 }
312 } else {
313 if (coefficients[i] < 0) {
314 s.append("-");
315 }
316 }
317
318 double absAi = JdkMath.abs(coefficients[i]);
319 if ((absAi - 1) != 0) {
320 s.append(toString(absAi));
321 s.append(' ');
322 }
323
324 s.append("x");
325 if (i > 1) {
326 s.append('^');
327 s.append(Integer.toString(i));
328 }
329 }
330 }
331
332 return s.toString();
333 }
334
335
336
337
338
339
340
341 private static String toString(double coeff) {
342 final String c = Double.toString(coeff);
343 if (c.endsWith(".0")) {
344 return c.substring(0, c.length() - 2);
345 } else {
346 return c;
347 }
348 }
349
350
351 @Override
352 public int hashCode() {
353 final int prime = 31;
354 int result = 1;
355 result = prime * result + Arrays.hashCode(coefficients);
356 return result;
357 }
358
359
360 @Override
361 public boolean equals(Object obj) {
362 if (this == obj) {
363 return true;
364 }
365 if (!(obj instanceof PolynomialFunction)) {
366 return false;
367 }
368 PolynomialFunction other = (PolynomialFunction) obj;
369 return Arrays.equals(coefficients, other.coefficients);
370 }
371
372
373
374
375
376
377 public static class Parametric implements ParametricUnivariateFunction {
378
379 @Override
380 public double[] gradient(double x, double ... parameters) {
381 final double[] gradient = new double[parameters.length];
382 double xn = 1.0;
383 for (int i = 0; i < parameters.length; ++i) {
384 gradient[i] = xn;
385 xn *= x;
386 }
387 return gradient;
388 }
389
390
391 @Override
392 public double value(final double x, final double ... parameters)
393 throws NoDataException {
394 return PolynomialFunction.evaluate(parameters, x);
395 }
396 }
397 }