| 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; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import java.math.BigDecimal; |
| 32 |
import java.math.MathContext; |
| 33 |
import java.util.Arrays; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public interface Polynomial extends Cloneable { |
| 40 |
|
| 41 |
public int getDegree(); |
| 42 |
|
| 43 |
public Polynomial multiply(Polynomial b); |
| 44 |
|
| 45 |
public Polynomial integrate(); |
| 46 |
|
| 47 |
public double evaluate(double x); |
| 48 |
|
| 49 |
public double logEvaluate(double x); |
| 50 |
|
| 51 |
public double logEvaluateHorner(double x); |
| 52 |
|
| 53 |
public void expand(double x); |
| 54 |
|
| 55 |
public Polynomial integrateWithLowerBound(double bound); |
| 56 |
|
| 57 |
public double getCoefficient(int n); |
| 58 |
|
| 59 |
public String getCoefficientString(int n); |
| 60 |
|
| 61 |
public void setCoefficient(int n, double x); |
| 62 |
|
| 63 |
public Polynomial getPolynomial(); |
| 64 |
|
| 65 |
public Polynomial copy(); |
| 66 |
|
| 67 |
public abstract class Abstract implements Polynomial { |
| 68 |
|
| 69 |
public abstract int getDegree(); |
| 70 |
|
| 71 |
public abstract Polynomial multiply(Polynomial b); |
| 72 |
|
| 73 |
public abstract Polynomial integrate(); |
| 74 |
|
| 75 |
public abstract double evaluate(double x); |
| 76 |
|
| 77 |
public abstract double getCoefficient(int n); |
| 78 |
|
| 79 |
public abstract void setCoefficient(int n, double x); |
| 80 |
|
| 81 |
public abstract Polynomial integrateWithLowerBound(double bound); |
| 82 |
|
| 83 |
public Polynomial getPolynomial() { return this; } |
| 84 |
|
| 85 |
public abstract double logEvaluate(double x); |
| 86 |
|
| 87 |
public abstract double logEvaluateHorner(double x); |
| 88 |
|
| 89 |
public abstract void expand(double x); |
| 90 |
|
| 91 |
public String toString() { |
| 92 |
StringBuffer bf = new StringBuffer(); |
| 93 |
for (int n = getDegree(); n >= 0; n--) { |
| 94 |
bf.append(getCoefficientString(n)); |
| 95 |
bf.append(X); |
| 96 |
bf.append(n); |
| 97 |
if (n > 0) |
| 98 |
bf.append(" + "); |
| 99 |
} |
| 100 |
return bf.toString(); |
| 101 |
} |
| 102 |
|
| 103 |
public abstract String getCoefficientString(int n); |
| 104 |
|
| 105 |
|
| 106 |
protected static final String FORMAT = "%3.2e"; |
| 107 |
private static final String X = " x^"; |
| 108 |
} |
| 109 |
|
| 110 |
public class LogDouble extends Abstract { |
| 111 |
|
| 112 |
public LogDouble(double[] coefficient) { |
| 113 |
this.logCoefficient = new double[coefficient.length]; |
| 114 |
this.positiveCoefficient = new boolean[coefficient.length]; |
| 115 |
for(int i=0; i<coefficient.length; i++) { |
| 116 |
if(coefficient[i] < 0) { |
| 117 |
this.logCoefficient[i] = Math.log(-coefficient[i]); |
| 118 |
this.positiveCoefficient[i] = false; |
| 119 |
} else { |
| 120 |
this.logCoefficient[i] = Math.log(coefficient[i]); |
| 121 |
this.positiveCoefficient[i] = true; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public double getLogCoefficient(int n) { |
| 127 |
return logCoefficient[n]; |
| 128 |
} |
| 129 |
|
| 130 |
public void expand(double x) { |
| 131 |
final int degree = getDegree(); |
| 132 |
for(int i=0; i<=degree; i++) |
| 133 |
logCoefficient[i] = x + logCoefficient[i]; |
| 134 |
} |
| 135 |
|
| 136 |
public String getCoefficientString(int n) { |
| 137 |
return String.format(FORMAT, getCoefficient(n)); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public LogDouble(double[] logCoefficient, boolean[] positiveCoefficient) { |
| 142 |
this.logCoefficient = logCoefficient; |
| 143 |
if (positiveCoefficient != null) |
| 144 |
this.positiveCoefficient = positiveCoefficient; |
| 145 |
else { |
| 146 |
this.positiveCoefficient = new boolean[logCoefficient.length]; |
| 147 |
Arrays.fill(this.positiveCoefficient,true); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public LogDouble copy() { |
| 152 |
return new LogDouble(logCoefficient.clone(), positiveCoefficient.clone()); |
| 153 |
} |
| 154 |
|
| 155 |
public int getDegree() { |
| 156 |
return logCoefficient.length - 1; |
| 157 |
} |
| 158 |
|
| 159 |
public LogDouble multiply(Polynomial inB) { |
| 160 |
if (!(inB.getPolynomial() instanceof LogDouble)) |
| 161 |
throw new RuntimeException("yuck!"); |
| 162 |
|
| 163 |
LogDouble b = (LogDouble) inB.getPolynomial(); |
| 164 |
final int degreeA = getDegree(); |
| 165 |
final int degreeB = b.getDegree(); |
| 166 |
|
| 167 |
double[] newLogCoefficient = new double[degreeA + degreeB + 1]; |
| 168 |
boolean[] newPositiveCoefficient = new boolean[degreeA + degreeB + 1]; |
| 169 |
Arrays.fill(newLogCoefficient, java.lang.Double.NEGATIVE_INFINITY); |
| 170 |
Arrays.fill(newPositiveCoefficient, true); |
| 171 |
|
| 172 |
for (int n = 0; n <= degreeA; n++) { |
| 173 |
for (int m = 0; m <= degreeB; m++) { |
| 174 |
final double change = logCoefficient[n] + b.logCoefficient[m]; |
| 175 |
final int nm = n + m; |
| 176 |
final boolean positiveChange = !(positiveCoefficient[n] ^ b.positiveCoefficient[m]); |
| 177 |
if (newLogCoefficient[nm] == java.lang.Double.NEGATIVE_INFINITY) { |
| 178 |
newLogCoefficient[nm] = change; |
| 179 |
newPositiveCoefficient[nm] = positiveChange; |
| 180 |
} else { |
| 181 |
if (change != 0.0) { |
| 182 |
if (newPositiveCoefficient[nm] ^ positiveChange) { |
| 183 |
if (newLogCoefficient[nm] > change) |
| 184 |
newLogCoefficient[nm] = LogTricks.logDiff(newLogCoefficient[nm], change); |
| 185 |
else { |
| 186 |
newLogCoefficient[nm] = LogTricks.logDiff(change, newLogCoefficient[nm]); |
| 187 |
newPositiveCoefficient[nm] = !newPositiveCoefficient[nm]; |
| 188 |
} |
| 189 |
} else { |
| 190 |
newLogCoefficient[nm] = LogTricks.logSum(newLogCoefficient[nm], change); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
return new LogDouble(newLogCoefficient,newPositiveCoefficient); |
| 197 |
} |
| 198 |
|
| 199 |
public LogDouble integrate() { |
| 200 |
final int degree = getDegree(); |
| 201 |
double[] newLogCoefficient = new double[degree + 2]; |
| 202 |
boolean[] newPositiveCoefficient = new boolean[degree + 2]; |
| 203 |
for (int n=0; n<=degree; n++) { |
| 204 |
newLogCoefficient[n+1] = logCoefficient[n] - Math.log(n+1); |
| 205 |
newPositiveCoefficient[n+1] = positiveCoefficient[n]; |
| 206 |
} |
| 207 |
newLogCoefficient[0] = java.lang.Double.NEGATIVE_INFINITY; |
| 208 |
newPositiveCoefficient[0] = true; |
| 209 |
return new LogDouble(newLogCoefficient,newPositiveCoefficient); |
| 210 |
} |
| 211 |
|
| 212 |
public double evaluate(double x) { |
| 213 |
SignedLogDouble result = signedLogEvaluate(x); |
| 214 |
double value = Math.exp(result.value); |
| 215 |
if (!result.positive) |
| 216 |
value = -value; |
| 217 |
return value; |
| 218 |
} |
| 219 |
|
| 220 |
public double evaluateAsReal(double x) { |
| 221 |
double result = 0; |
| 222 |
double xn = 1; |
| 223 |
for (int n = 0; n <= getDegree(); n++) { |
| 224 |
result += xn * getCoefficient(n); |
| 225 |
xn *= x; |
| 226 |
} |
| 227 |
return result; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
public double logEvaluate(double x) { |
| 232 |
if (x < 0) |
| 233 |
throw new RuntimeException("Negative arguments not yet implemented in Polynomial.LogDouble"); |
| 234 |
SignedLogDouble result = signedLogEvaluate(x); |
| 235 |
if (result.positive) |
| 236 |
return result.value; |
| 237 |
return java.lang.Double.NaN; |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
public double logEvaluateHorner(double x) { |
| 242 |
if (x < 0) |
| 243 |
throw new RuntimeException("Negative arguments not yet implemented in Polynomial.LogDouble"); |
| 244 |
SignedLogDouble result = signedLogEvaluateHorners(x); |
| 245 |
if (result.positive) |
| 246 |
return result.value; |
| 247 |
return java.lang.Double.NaN; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
public SignedLogDouble signedLogEvaluateHorners(double x) { |
| 253 |
|
| 254 |
final double logX = Math.log(x); |
| 255 |
final int degree = getDegree(); |
| 256 |
double logResult = logCoefficient[degree]; |
| 257 |
boolean positive = positiveCoefficient[degree]; |
| 258 |
for(int n=degree-1; n>=0; n--) { |
| 259 |
logResult += logX; |
| 260 |
if (!(positiveCoefficient[n] ^ positive)) |
| 261 |
logResult = LogTricks.logSum(logResult,logCoefficient[n]); |
| 262 |
else { |
| 263 |
if (logResult > logCoefficient[n]) |
| 264 |
logResult = LogTricks.logDiff(logResult,logCoefficient[n]); |
| 265 |
else { |
| 266 |
logResult = LogTricks.logDiff(logCoefficient[n],logResult); |
| 267 |
positive = !positive; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
return new SignedLogDouble(logResult,positive); |
| 272 |
} |
| 273 |
|
| 274 |
private SignedLogDouble signedLogEvaluate(double x) { |
| 275 |
final double logX = Math.log(x); |
| 276 |
final int degree = getDegree(); |
| 277 |
double logResult = logCoefficient[0]; |
| 278 |
boolean positive = positiveCoefficient[0]; |
| 279 |
for(int n=1; n<=degree; n++) { |
| 280 |
|
| 281 |
final double value = n*logX + logCoefficient[n]; |
| 282 |
if (!(positiveCoefficient[n] ^ positive)) |
| 283 |
logResult = LogTricks.logSum(logResult,value); |
| 284 |
else { |
| 285 |
if (logResult > value) |
| 286 |
logResult = LogTricks.logDiff(logResult,value); |
| 287 |
else { |
| 288 |
logResult = LogTricks.logDiff(value,logResult); |
| 289 |
positive = !positive; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
return new SignedLogDouble(logResult,positive); |
| 294 |
} |
| 295 |
|
| 296 |
public double getCoefficient(int n) { |
| 297 |
double coef = Math.exp(logCoefficient[n]); |
| 298 |
if (!positiveCoefficient[n]) |
| 299 |
coef *= -1; |
| 300 |
return coef; |
| 301 |
} |
| 302 |
|
| 303 |
public void setCoefficient(int n, double x) { |
| 304 |
if (x < 0) { |
| 305 |
positiveCoefficient[n] = false; |
| 306 |
x = -x; |
| 307 |
} else |
| 308 |
positiveCoefficient[n] = true; |
| 309 |
logCoefficient[n] = Math.log(x); |
| 310 |
} |
| 311 |
|
| 312 |
public Polynomial integrateWithLowerBound(double bound) { |
| 313 |
LogDouble integrand = integrate(); |
| 314 |
SignedLogDouble signedLogDouble = integrand.signedLogEvaluate(bound); |
| 315 |
integrand.logCoefficient[0] = signedLogDouble.value; |
| 316 |
integrand.positiveCoefficient[0] = !signedLogDouble.positive; |
| 317 |
return integrand; |
| 318 |
} |
| 319 |
|
| 320 |
double[] logCoefficient; |
| 321 |
boolean[] positiveCoefficient; |
| 322 |
|
| 323 |
class SignedLogDouble { |
| 324 |
|
| 325 |
double value; |
| 326 |
boolean positive; |
| 327 |
|
| 328 |
SignedLogDouble(double value, boolean positive) { |
| 329 |
this.value = value; |
| 330 |
this.positive = positive; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public class BigDouble extends Abstract { |
| 336 |
|
| 337 |
private static MathContext precision = new MathContext(1000); |
| 338 |
|
| 339 |
public BigDouble(double[] doubleCoefficient) { |
| 340 |
this.coefficient = new BigDecimal[doubleCoefficient.length]; |
| 341 |
for(int i=0; i<doubleCoefficient.length; i++) |
| 342 |
coefficient[i] = new BigDecimal(doubleCoefficient[i]); |
| 343 |
} |
| 344 |
|
| 345 |
public BigDouble copy() { |
| 346 |
return new BigDouble(coefficient.clone()); |
| 347 |
} |
| 348 |
|
| 349 |
public String getCoefficientString(int n) { |
| 350 |
return coefficient[n].toString(); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
public void expand(double x) { |
| 355 |
throw new RuntimeException("Not yet implement: Polynomial.BigDouble.expand()"); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public BigDouble(BigDecimal[] coefficient) { |
| 361 |
this.coefficient = coefficient; |
| 362 |
} |
| 363 |
|
| 364 |
public int getDegree() { |
| 365 |
return coefficient.length - 1; |
| 366 |
} |
| 367 |
|
| 368 |
public BigDouble multiply(Polynomial b) { |
| 369 |
if (!(b.getPolynomial() instanceof BigDouble)) |
| 370 |
throw new RuntimeException("Incompatiable polynomial types"); |
| 371 |
BigDouble bd = (BigDouble) b.getPolynomial(); |
| 372 |
BigDecimal[] newCoefficient = new BigDecimal[getDegree() + bd.getDegree()+1]; |
| 373 |
for(int i=0; i<newCoefficient.length; i++) |
| 374 |
newCoefficient[i] = new BigDecimal(0.0); |
| 375 |
for(int n=0; n<=getDegree(); n++) { |
| 376 |
for(int m=0; m<=bd.getDegree(); m++) |
| 377 |
newCoefficient[n+m] = newCoefficient[n+m].add(coefficient[n].multiply(bd.coefficient[m])); |
| 378 |
} |
| 379 |
return new BigDouble(newCoefficient); |
| 380 |
} |
| 381 |
|
| 382 |
public BigDouble integrate() { |
| 383 |
BigDecimal[] newCoefficient = new BigDecimal[getDegree()+2]; |
| 384 |
for(int n=0; n<=getDegree(); n++) { |
| 385 |
newCoefficient[n+1] = coefficient[n].divide(new BigDecimal(n+1),precision); |
| 386 |
} |
| 387 |
newCoefficient[0] = new BigDecimal(0.0); |
| 388 |
return new BigDouble(newCoefficient); |
| 389 |
} |
| 390 |
|
| 391 |
public double evaluate(double x) { |
| 392 |
return evaluateBigDecimal(new BigDecimal(x)).doubleValue(); |
| 393 |
} |
| 394 |
|
| 395 |
public double logEvaluate(double x) { |
| 396 |
return BigDecimalUtils.ln(evaluateBigDecimal(new BigDecimal(x)),10).doubleValue(); |
| 397 |
} |
| 398 |
|
| 399 |
public double logEvaluateHorner(double x) { |
| 400 |
return logEvaluate(x); |
| 401 |
} |
| 402 |
|
| 403 |
protected BigDecimal evaluateBigDecimal(BigDecimal x) { |
| 404 |
BigDecimal result = new BigDecimal(0.0); |
| 405 |
BigDecimal xn = new BigDecimal(1.0); |
| 406 |
for(int n=0; n<=getDegree(); n++) { |
| 407 |
result = result.add(coefficient[n].multiply(xn)); |
| 408 |
xn = xn.multiply(x); |
| 409 |
} |
| 410 |
return result; |
| 411 |
} |
| 412 |
|
| 413 |
public double getCoefficient(int n) { |
| 414 |
return coefficient[n].doubleValue(); |
| 415 |
} |
| 416 |
|
| 417 |
public void setCoefficient(int n, double x) { |
| 418 |
coefficient[n] = new BigDecimal(x); |
| 419 |
} |
| 420 |
|
| 421 |
public Polynomial integrateWithLowerBound(double bound) { |
| 422 |
BigDouble integrand = integrate(); |
| 423 |
final BigDecimal x0 = integrand.evaluateBigDecimal(new BigDecimal(bound)); |
| 424 |
integrand.coefficient[0] = x0.multiply(new BigDecimal(-1.0)); |
| 425 |
return integrand; |
| 426 |
} |
| 427 |
|
| 428 |
public void setCoefficient(int n, BigDecimal x) { |
| 429 |
coefficient[n] = x; |
| 430 |
} |
| 431 |
|
| 432 |
BigDecimal[] coefficient; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public class Double extends Abstract { |
| 579 |
|
| 580 |
public Double(double[] coefficient) { |
| 581 |
this.coefficient = coefficient; |
| 582 |
} |
| 583 |
|
| 584 |
public Double copy() { |
| 585 |
return new Double(coefficient.clone()); |
| 586 |
} |
| 587 |
|
| 588 |
public Double(Polynomial polynomial) { |
| 589 |
this.coefficient = new double[polynomial.getDegree() + 1]; |
| 590 |
for (int n = 0; n <= polynomial.getDegree(); n++) |
| 591 |
coefficient[n] = polynomial.getCoefficient(n); |
| 592 |
} |
| 593 |
|
| 594 |
public void expand(double x) { |
| 595 |
final int degree = getDegree(); |
| 596 |
for(int i=0; i<=degree; i++) |
| 597 |
coefficient[i] = x * coefficient[i]; |
| 598 |
} |
| 599 |
|
| 600 |
public String getCoefficientString(int n) { |
| 601 |
return String.format(FORMAT, getCoefficient(n)); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
public int getDegree() { |
| 606 |
return coefficient.length - 1; |
| 607 |
} |
| 608 |
|
| 609 |
public double getCoefficient(int n) { |
| 610 |
return coefficient[n]; |
| 611 |
} |
| 612 |
|
| 613 |
public double logEvaluate(double x) { |
| 614 |
return Math.log(evaluate(x)); |
| 615 |
} |
| 616 |
|
| 617 |
public double logEvaluateQuick(double x, int n) { |
| 618 |
return Math.log(evaluateQuick(x,n)); |
| 619 |
} |
| 620 |
|
| 621 |
public double logEvaluateHorner(double x) { |
| 622 |
|
| 623 |
final int degree = getDegree(); |
| 624 |
final double logX = Math.log(x); |
| 625 |
boolean positive = coefficient[degree] > 0; |
| 626 |
double logResult; |
| 627 |
if (positive) |
| 628 |
logResult = Math.log(coefficient[degree]); |
| 629 |
else |
| 630 |
logResult = Math.log(-coefficient[degree]); |
| 631 |
for(int n=degree-1; n>=0; n--) { |
| 632 |
logResult += logX; |
| 633 |
boolean positiveCoefficient = coefficient[n] > 0; |
| 634 |
double logCoefficient; |
| 635 |
if (positiveCoefficient) |
| 636 |
logCoefficient = Math.log(coefficient[n]); |
| 637 |
else |
| 638 |
logCoefficient = Math.log(-coefficient[n]); |
| 639 |
if (!(positiveCoefficient ^ positive)) |
| 640 |
logResult = LogTricks.logSum(logResult,logCoefficient); |
| 641 |
else { |
| 642 |
if (logResult > logCoefficient) |
| 643 |
logResult = LogTricks.logDiff(logResult,logCoefficient); |
| 644 |
else { |
| 645 |
logResult = LogTricks.logDiff(logCoefficient,logResult); |
| 646 |
positive = !positive; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
if (!positive) |
| 651 |
return java.lang.Double.NaN; |
| 652 |
return logResult; |
| 653 |
} |
| 654 |
|
| 655 |
public Polynomial.Double multiply(Polynomial b) { |
| 656 |
double[] newCoefficient = new double[getDegree() + b.getDegree() + 1]; |
| 657 |
for (int n = 0; n <= getDegree(); n++) { |
| 658 |
for (int m = 0; m <= b.getDegree(); m++) { |
| 659 |
newCoefficient[n + m] += coefficient[n] * b.getCoefficient(m); |
| 660 |
} |
| 661 |
} |
| 662 |
return new Double(newCoefficient); |
| 663 |
} |
| 664 |
|
| 665 |
public Polynomial.Double integrate() { |
| 666 |
double[] newCoefficient = new double[getDegree() + 2]; |
| 667 |
for (int n = 0; n <= getDegree(); n++) { |
| 668 |
newCoefficient[n + 1] = coefficient[n] / (n + 1); |
| 669 |
} |
| 670 |
return new Double(newCoefficient); |
| 671 |
} |
| 672 |
|
| 673 |
public double evaluateHorners(double x) { |
| 674 |
|
| 675 |
final int degree = getDegree(); |
| 676 |
double result = coefficient[degree]; |
| 677 |
for (int n=degree-1; n>=0; n--) |
| 678 |
result = result*x + coefficient[n]; |
| 679 |
return result; |
| 680 |
} |
| 681 |
|
| 682 |
public double evaluateQuick(double x, int n) { |
| 683 |
int m = getDegree(); |
| 684 |
double xm = Math.pow(x,m); |
| 685 |
double result = xm * coefficient[m]; |
| 686 |
for (int i=n-1; i>0; i--) { |
| 687 |
xm /= x; |
| 688 |
m--; |
| 689 |
result += xm * coefficient[m]; |
| 690 |
} |
| 691 |
return result; |
| 692 |
} |
| 693 |
|
| 694 |
public double evaluate(double x) { |
| 695 |
double result = 0.0; |
| 696 |
double xn = 1; |
| 697 |
for (int n = 0; n <= getDegree(); n++) { |
| 698 |
result += xn * coefficient[n]; |
| 699 |
xn *= x; |
| 700 |
} |
| 701 |
return result; |
| 702 |
} |
| 703 |
|
| 704 |
public Polynomial integrateWithLowerBound(double bound) { |
| 705 |
Double integrand = integrate(); |
| 706 |
|
| 707 |
integrand.coefficient[0] = -integrand.evaluate(bound); |
| 708 |
return integrand; |
| 709 |
} |
| 710 |
|
| 711 |
public void setCoefficient(int n, double x) { |
| 712 |
coefficient[n] = x; |
| 713 |
} |
| 714 |
|
| 715 |
double[] coefficient; |
| 716 |
} |
| 717 |
|
| 718 |
public enum Type { |
| 719 |
DOUBLE, APDOUBLE, LOG_DOUBLE, BIG_DOUBLE |
| 720 |
} |
| 721 |
} |