Debian Codegraph @

src/dr/math/functionEval/PolynomialFunction.java

raw symbols SCIP
1 /*
2 * PolynomialFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.functionEval;
27
28 import dr.math.MathUtils;
29 import dr.math.interfaces.OneVariableFunction;
30 import dr.math.iterations.NewtonZeroFinder;
31
32 import java.util.Enumeration;
33 import java.util.Vector;
34
35 /**
36 * Mathematical polynomial:
37 * c[0] + c[1] * x + c[2] * x^2 + ....
38 *
39 * @author Didier H. Besset
40 */
41 public class PolynomialFunction implements OneVariableFunction
42 {
43 /**
44 * Polynomial coefficients.
45 */
46 private final double[] coefficients;
47
48
49 /**
50 * Constructor method.
51 * @param coeffs polynomial coefficients.
52 */
53 public PolynomialFunction( double[] coeffs)
54 {
55 coefficients = coeffs;
56 }
57 /**
58 *
59 * @param r double number added to the polynomial.
60 * @return DhbFunctionEvaluation.PolynomialFunction
61 */
62 public PolynomialFunction add( double r)
63 {
64 int n = coefficients.length;
65 double coef[] = new double[n];
66 coef[0] = coefficients[0] + r;
67 for ( int i = 1; i < n; i++)
68 coef[i] = coefficients[i];
69 return new PolynomialFunction( coef);
70 }
71 /**
72 *
73 * @param p DhbFunctionEvaluation.PolynomialFunction
74 * @return DhbFunctionEvaluation.PolynomialFunction
75 */
76 public PolynomialFunction add( PolynomialFunction p)
77 {
78 int n = Math.max( p.degree(), degree()) + 1;
79 double[] coef = new double[n];
80 for ( int i = 0; i < n; i++ )
81 coef[i] = coefficient(i) + p.coefficient(i);
82 return new PolynomialFunction( coef);
83 }
84 /**
85 * Returns the coefficient value at the desired position
86 * @param n int the position of the coefficient to be returned
87 * @return double the coefficient value
88 */
89 public double coefficient( int n)
90 {
91 return n < coefficients.length ? coefficients[n] : 0;
92 }
93 /**
94 *
95 * @param r double a root of the polynomial (no check made).
96 * @return PolynomialFunction the receiver divided by polynomial (x - r).
97 */
98 public PolynomialFunction deflate( double r)
99 {
100 int n = degree();
101 double remainder = coefficients[n];
102 double[] coef = new double[n];
103 for ( int k = n - 1; k >= 0; k--)
104 {
105 coef[k] = remainder;
106 remainder = remainder * r + coefficients[k];
107 }
108 return new PolynomialFunction( coef);
109 }
110 /**
111 * Returns degree of this polynomial function
112 * @return int degree of this polynomial function
113 */
114 public int degree()
115 {
116 return coefficients.length - 1;
117 }
118 /**
119 * Returns the derivative of the receiver.
120 * @return PolynomialFunction derivative of the receiver.
121 */
122 public PolynomialFunction derivative()
123 {
124 int n = degree();
125 if ( n == 0 )
126 {
127 double coef[] = {0};
128 return new PolynomialFunction( coef);
129 }
130 double coef[] = new double[n];
131 for ( int i = 1; i <= n; i++)
132 coef[i-1] = coefficients[i]*i;
133 return new PolynomialFunction( coef);
134 }
135 /**
136 *
137 * @param r double
138 * @return DhbFunctionEvaluation.PolynomialFunction
139 */
140 public PolynomialFunction divide( double r)
141 {
142 return multiply( 1 / r);
143 }
144 /**
145 *
146 * @param p PolynomialFunction
147 * @return PolynomialFunction
148 */
149 public PolynomialFunction divide( PolynomialFunction p)
150 {
151 return divideWithRemainder(p)[0];
152 }
153 /**
154 *
155 * @param p PolynomialFunction
156 * @return DhbFunctionEvaluation.PolynomialFunction
157 */
158 public PolynomialFunction[] divideWithRemainder( PolynomialFunction p)
159 {
160 PolynomialFunction[] answer = new PolynomialFunction[2];
161 int m = degree();
162 int n = p.degree();
163 if ( m < n )
164 {
165 double[] q = {0};
166 answer[0] = new PolynomialFunction( q);
167 answer[1] = p;
168 return answer;
169 }
170 double[] quotient = new double[ m - n + 1];
171 double[] coef = new double[ m + 1];
172 for ( int k = 0; k <= m; k++ )
173 coef[k] = coefficients[k];
174 double norm = 1 / p.coefficient( n);
175 for ( int k = m - n; k >= 0; k--)
176 {
177 quotient[k] = coef[ n + k] * norm;
178 for ( int j = n + k - 1; j >= k; j--)
179 coef[j] -= quotient[k] * p.coefficient(j-k);
180 }
181 double[] remainder = new double[n];
182 for ( int k = 0; k < n; k++)
183 remainder[k] = coef[k];
184 answer[0] = new PolynomialFunction( quotient);
185 answer[1] = new PolynomialFunction( remainder);
186 return answer;
187 }
188 /**
189 * Returns the integral of the receiver having the value 0 for X = 0.
190 * @return PolynomialFunction integral of the receiver.
191 */
192 public PolynomialFunction integral( )
193 {
194 return integral( 0);
195 }
196 /**
197 * Returns the integral of the receiver having the specified value for X = 0.
198 * @param value double value of the integral at x=0
199 * @return PolynomialFunction integral of the receiver.
200 */
201 public PolynomialFunction integral( double value)
202 {
203 int n = coefficients.length + 1;
204 double coef[] = new double[n];
205 coef[0] = value;
206 for ( int i = 1; i < n; i++)
207 coef[i] = coefficients[i-1]/i;
208 return new PolynomialFunction( coef);
209 }
210 /**
211 *
212 * @param r double
213 * @return DhbFunctionEvaluation.PolynomialFunction
214 */
215 public PolynomialFunction multiply( double r)
216 {
217 int n = coefficients.length;
218 double coef[] = new double[n];
219 for ( int i = 0; i < n; i++)
220 coef[i] = coefficients[i] * r;
221 return new PolynomialFunction( coef);
222 }
223 /**
224 *
225 * @param p DhbFunctionEvaluation.PolynomialFunction
226 * @return DhbFunctionEvaluation.PolynomialFunction
227 */
228 public PolynomialFunction multiply( PolynomialFunction p)
229 {
230 int n = p.degree() + degree();
231 double[] coef = new double[n + 1];
232 for ( int i = 0; i <= n; i++)
233 {
234 coef[i] = 0;
235 for ( int k = 0; k <= i; k++)
236 coef[i] += p.coefficient(k) * coefficient(i-k);
237 }
238 return new PolynomialFunction( coef);
239 }
240 /**
241 *
242 * @return double[]
243 */
244 public double[] roots()
245 {
246 return roots( DrMath.defaultNumericalPrecision());
247 }
248 /**
249 *
250 * @param desiredPrecision double
251 * @return double[]
252 */
253 public double[] roots( double desiredPrecision)
254 {
255 PolynomialFunction dp = derivative();
256 double start = 0;
257 while ( Math.abs( dp.value( start)) < desiredPrecision )
258 start = MathUtils.nextDouble();
259 PolynomialFunction p = this;
260 NewtonZeroFinder rootFinder = new NewtonZeroFinder( this, dp, start);
261 rootFinder.setDesiredPrecision( desiredPrecision);
262 Vector rootCollection = new Vector( degree());
263 while ( true)
264 {
265 rootFinder.evaluate();
266 if ( !rootFinder.hasConverged() )
267 break;
268 double r = rootFinder.getResult();
269 rootCollection.addElement(r);
270 p = p.deflate( r);
271 if ( p.degree() == 0 )
272 break;
273 rootFinder.setFunction( p);
274 try { rootFinder.setDerivative( p.derivative());}
275 catch ( IllegalArgumentException e) {}
276 }
277 double[] roots = new double[ rootCollection.size()];
278 Enumeration e = rootCollection.elements();
279 int n = 0;
280 while ( e.hasMoreElements() )
281 {
282 roots[n++] = (Double) e.nextElement();
283 }
284 return roots;
285 }
286 /**
287 *
288 * @param r double
289 * @return PolynomialFunction
290 */
291 public PolynomialFunction subtract( double r)
292 {
293 return add( -r);
294 }
295 /**
296 *
297 * @return PolynomialFunction
298 * @param p PolynomialFunction
299 */
300 public PolynomialFunction subtract( PolynomialFunction p)
301 {
302 int n = Math.max( p.degree(), degree()) + 1;
303 double[] coef = new double[n];
304 for ( int i = 0; i < n; i++ )
305 coef[i] = coefficient(i) - p.coefficient(i);
306 return new PolynomialFunction( coef);
307 }
308 /**
309 * Returns a string representing the receiver
310 */
311 public String toString()
312 {
313 StringBuffer sb = new StringBuffer();
314 boolean firstNonZeroCoefficientPrinted = false;
315 for ( int n = 0; n < coefficients.length; n++)
316 {
317 if ( coefficients[n] != 0 )
318 {
319 if ( firstNonZeroCoefficientPrinted)
320 sb.append( coefficients[n] > 0 ? " + " : " ");
321 else
322 firstNonZeroCoefficientPrinted = true;
323 if ( n == 0 || coefficients[n] != 1)
324 sb.append( Double.toString( coefficients[n]) );
325 if ( n > 0 )
326 sb.append( " X^"+n);
327 }
328 }
329 return sb.toString();
330 }
331 /**
332 * Returns the value of the polynomial for the specified variable value.
333 * @param x double value at which the polynomial is evaluated
334 * @return double polynomial value.
335 */
336 public double value( double x)
337 {
338 int n = coefficients.length;
339 double answer = coefficients[--n];
340 while ( n > 0 )
341 answer = answer * x + coefficients[--n];
342 return answer;
343 }
344 /**
345 * Returns the value and the derivative of the polynomial
346 * for the specified variable value in an array of two elements
347 * @param x double value at which the polynomial is evaluated
348 * @return double[0] the value of the polynomial
349 * @return double[1] the derivative of the polynomial
350 */
351 public double[] valueAndDerivative( double x)
352 {
353 int n = coefficients.length;
354 double[] answer = new double[2];
355 answer[0] = coefficients[--n];
356 answer[1] = 0;
357 while ( n > 0 )
358 {
359 answer[1] = answer[1] * x + answer[0];
360 answer[0] = answer[0] * x + coefficients[--n];
361 }
362 return answer;
363 }
364 }