Debian Codegraph @

src/dr/math/Polynomial.java

raw symbols SCIP
1 /*
2 * Polynomial.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;
27
28 //import org.apfloat.Apfloat;
29 //import org.apfloat.ApfloatMath;
30
31 import java.math.BigDecimal;
32 import java.math.MathContext;
33 import java.util.Arrays;
34
35 /**
36 * @author Marc A. Suchard
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) { // Sign difference, must subtract
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]; // Switch signs
188 }
189 } else { // Same signs, just add
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 // return -result.value;
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 // return -result.value;
249 }
250
251
252 public SignedLogDouble signedLogEvaluateHorners(double x) {
253 // Using Horner's Rule
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)) // Same sign
261 logResult = LogTricks.logSum(logResult,logCoefficient[n]);
262 else { // Different signs
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 // logResult += logX;
281 final double value = n*logX + logCoefficient[n];
282 if (!(positiveCoefficient[n] ^ positive)) // Same sign
283 logResult = LogTricks.logSum(logResult,value);
284 else { // Different signs
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 // public class APDouble extends Abstract {
436 //
437 // public String getCoefficientString(int n) {
438 // return coefficient[n].toString();
439 // }
440 //
441 // public APDouble copy() {
442 // return new APDouble(coefficient.clone());
443 // }
444 //
445 // public APDouble(double[] doubleCoefficient) {
446 // this.coefficient = new Apfloat[doubleCoefficient.length];
447 // for(int i=0; i<doubleCoefficient.length; i++)
448 // coefficient[i] = new Apfloat(doubleCoefficient[i]);
449 // }
450 //
451 // public APDouble(Apfloat[] coefficient) {
452 // this.coefficient = coefficient;
453 // }
454 //
455 // public int getDegree() {
456 // return coefficient.length - 1;
457 // }
458 //
459 // public APDouble multiply(Polynomial b) {
460 // if (!(b.getPolynomial() instanceof APDouble))
461 // throw new RuntimeException("Incompatiable polynomial types");
462 // APDouble bd = (APDouble) b.getPolynomial();
463 // Apfloat[] newCoefficient = new Apfloat[getDegree() + bd.getDegree()+1];
464 // for(int i=0; i<newCoefficient.length; i++)
465 // newCoefficient[i] = new Apfloat(0.0);
466 // for(int n=0; n<=getDegree(); n++) {
467 // for(int m=0; m<=bd.getDegree(); m++)
468 // newCoefficient[n+m] = newCoefficient[n+m].add(coefficient[n].multiply(bd.coefficient[m]));
469 // }
470 // return new APDouble(newCoefficient);
471 // }
472 //
473 // public APDouble integrate() {
474 // Apfloat[] newCoefficient = new Apfloat[getDegree()+2];
475 // for(int n=0; n<=getDegree(); n++) {
476 // newCoefficient[n+1] = coefficient[n].divide(new Apfloat(n+1));
477 // }
478 // newCoefficient[0] = new Apfloat(0.0);
479 // return new APDouble(newCoefficient);
480 // }
481 //
482 // public double evaluate(double x) {
483 // return evaluateAPDouble(new Apfloat(x)).doubleValue();
484 // }
485 //
486 // public double logEvaluate(double x) {
487 // Apfloat result = evaluateAPDouble(new Apfloat((x)));
488 // if (result.doubleValue() == 0)
489 // return java.lang.Double.NEGATIVE_INFINITY;
490 // return ApfloatMath.log(result).doubleValue();
491 // }
492 //
493 // public double logEvaluateHorner(double x) {
494 // return logEvaluateInLogSpace(x);
495 // }
496 //
497 // private static double log(Apfloat x) {
498 // double log = ApfloatMath.log(x).doubleValue();
499 // if (java.lang.Double.isInfinite(log))
500 // throw new RuntimeException("Still infinite");
501 // return log;
502 // }
503 //
504 // private static boolean positive(Apfloat x) {
505 // return x.signum() != -1;
506 // }
507 //
508 // public double logEvaluateInLogSpace(double x) {
509 // // Using Horner's Rule
510 // final double logX = Math.log(x);
511 // final int degree = getDegree();
512 // boolean positive = positive(coefficient[degree]);
513 // double logResult;
514 // if (positive)
515 // logResult = log(coefficient[degree]);
516 // else
517 // logResult = log(coefficient[degree].negate());
518 // for(int n=degree-1; n>=0; n--) {
519 // logResult += logX;
520 // if (coefficient[n].signum() != 0) {
521 // final boolean nextPositive = positive(coefficient[n]);
522 // double logNextValue;
523 // if (nextPositive)
524 // logNextValue = log(coefficient[n]);
525 // else
526 // logNextValue = log(coefficient[n].negate());
527 // if(!(nextPositive ^ positive)) // Same sign
528 // logResult = LogTricks.logSum(logResult,logNextValue);
529 // else { // Different signs
530 // if (logResult > logNextValue)
531 // logResult = LogTricks.logDiff(logResult,logNextValue);
532 // else {
533 // logResult = LogTricks.logDiff(logNextValue,logResult);
534 // positive = !positive;
535 // }
536 // }
537 // }
538 // }
539 // if (!positive)
540 // logResult = -logResult;
541 // return logResult;
542 // }
543 //
544 // protected Apfloat evaluateAPDouble(Apfloat x) {
545 // Apfloat result = new Apfloat(0.0);
546 // Apfloat xn = new Apfloat(1.0);
547 // for(int n=0; n<=getDegree(); n++) {
548 // result = result.add(coefficient[n].multiply(xn));
549 // xn = xn.multiply(x);
550 // }
551 // // TODO Rewrite using Horner's Rule
552 // return result;
553 // }
554 //
555 // public double getCoefficient(int n) {
556 // return coefficient[n].doubleValue();
557 // }
558 //
559 // public void setCoefficient(int n, double x) {
560 // coefficient[n] = new Apfloat(x);
561 // }
562 //
563 // public Polynomial integrateWithLowerBound(double bound) {
564 // APDouble integrand = integrate();
565 // final Apfloat x0 = integrand.evaluateAPDouble(new Apfloat(bound));
566 // integrand.coefficient[0] = x0.multiply(new Apfloat(-1.0));
567 // return integrand;
568 // }
569 //
570 // public void setCoefficient(int n, Apfloat x) {
571 // coefficient[n] = x;
572 // }
573 //
574 // Apfloat[] coefficient;
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 // Uses Horner's Rule in log scale
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)) // Same sign
640 logResult = LogTricks.logSum(logResult,logCoefficient);
641 else { // Different signs
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 // Uses Horner's Rule
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 // System.err.println("integrand = "+integrand);
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//, RATIONAL
720 }
721 }