| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class PolynomialFunction implements OneVariableFunction |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private final double[] coefficients; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public PolynomialFunction( double[] coeffs) |
| 54 |
{ |
| 55 |
coefficients = coeffs; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 74 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public double coefficient( int n) |
| 90 |
{ |
| 91 |
return n < coefficients.length ? coefficients[n] : 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
public int degree() |
| 115 |
{ |
| 116 |
return coefficients.length - 1; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
public PolynomialFunction divide( double r) |
| 141 |
{ |
| 142 |
return multiply( 1 / r); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public PolynomialFunction divide( PolynomialFunction p) |
| 150 |
{ |
| 151 |
return divideWithRemainder(p)[0]; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
public PolynomialFunction integral( ) |
| 193 |
{ |
| 194 |
return integral( 0); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
|
| 213 |
|
| 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 |
|
| 226 |
|
| 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 |
|
| 243 |
|
| 244 |
public double[] roots() |
| 245 |
{ |
| 246 |
return roots( DrMath.defaultNumericalPrecision()); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 289 |
|
| 290 |
|
| 291 |
public PolynomialFunction subtract( double r) |
| 292 |
{ |
| 293 |
return add( -r); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 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 |
|
| 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 |
|
| 333 |
|
| 334 |
|
| 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 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 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 |
} |