Debian Codegraph @

src/dr/oldevomodel/substmodel/AbstractSubstitutionModel.java

raw symbols SCIP
1 /*
2 * AbstractSubstitutionModel.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.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.inference.model.AbstractModel;
30 import dr.inference.model.Model;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Variable;
33 import dr.math.MachineAccuracy;
34
35 import java.util.LinkedList;
36 import java.util.List;
37
38 /**
39 * An abstract base class for substitution models.
40 *
41 * This class needs serious documentation about the expectations from deriving classes.
42 *
43 *
44 * @author Andrew Rambaut
45 * @author Alexei Drummond
46 * @version $Id: AbstractSubstitutionModel.java,v 1.41 2005/05/24 20:25:58 rambaut Exp $
47 */
48
49 public abstract class AbstractSubstitutionModel extends AbstractModel implements SubstitutionModel {
50
51 public static final String MODEL = "model";
52
53 protected DataType dataType = null;
54
55 protected FrequencyModel freqModel;
56 protected double[] relativeRates;
57
58 public double[] getRelativeRates() {
59 return relativeRates;
60 }
61
62 protected double[] storedRelativeRates;
63
64 protected int stateCount;
65 protected int rateCount;
66
67 protected boolean eigenInitialised = false;
68 protected boolean updateMatrix = true;
69 protected boolean storedUpdateMatrix = true;
70
71 AbstractSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel) {
72 super(name);
73
74 this.dataType = dataType;
75
76 setStateCount(dataType.getStateCount());
77
78 if (freqModel != null) {
79 // freqModel can be null at this point but must be
80 // in place by the time setupMatrix is called.
81
82 if (freqModel.getDataType() != dataType) {
83 throw new IllegalArgumentException("Datatypes do not match!");
84 }
85
86 this.freqModel = freqModel;
87 addModel(freqModel);
88
89 checkFrequencies();
90 }
91
92 updateMatrix = true;
93 }
94
95 public int getStateCount() {
96 return stateCount;
97 }
98
99 private void setStateCount(int stateCount) {
100 eigenInitialised = false;
101
102 this.stateCount = stateCount;
103 rateCount = ((stateCount - 1) * stateCount) / 2;
104
105 relativeRates = new double[rateCount];
106 storedRelativeRates = new double[rateCount];
107 for (int i = 0; i < rateCount; i++) {
108 relativeRates[i] = 1.0;
109 }
110 }
111
112 // *****************************************************************
113 // Interface Model
114 // *****************************************************************
115
116 protected void handleModelChangedEvent(Model model, Object object, int index) {
117 // frequencyModel changed!
118 updateMatrix = true;
119 // if below is not true then the derived model registered more models - then it needs to override
120 // this method
121 assert( model == freqModel ) ;
122 frequenciesChanged();
123 }
124
125 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
126 // relativeRates changed
127 updateMatrix = true;
128 ratesChanged();
129 }
130
131 protected void storeState() {
132
133 storedUpdateMatrix = updateMatrix;
134
135 System.arraycopy(relativeRates, 0, storedRelativeRates, 0, rateCount);
136
137 System.arraycopy(Eval, 0, storedEval, 0, stateCount);
138 for (int i = 0; i < stateCount; i++) {
139 System.arraycopy(Ievc[i], 0, storedIevc[i], 0, stateCount);
140 System.arraycopy(Evec[i], 0, storedEvec[i], 0, stateCount);
141 }
142
143 }
144
145 /**
146 * Restore the additional stored state
147 */
148 protected void restoreState() {
149
150 updateMatrix = storedUpdateMatrix;
151
152 // To restore all this stuff just swap the pointers...
153 double[] tmp1 = storedRelativeRates;
154 storedRelativeRates = relativeRates;
155 relativeRates = tmp1;
156
157 tmp1 = storedEval;
158 storedEval = Eval;
159 Eval = tmp1;
160
161 double[][] tmp2 = storedIevc;
162 storedIevc = Ievc;
163 Ievc = tmp2;
164
165 tmp2 = storedEvec;
166 storedEvec = Evec;
167 Evec = tmp2;
168
169 }
170
171 protected void acceptState() {
172 } // nothing to do
173
174 // called when any registered model changes
175 abstract protected void frequenciesChanged();
176
177 // called when any registered variable changes
178 abstract protected void ratesChanged();
179
180 abstract protected void setupRelativeRates();
181
182 public FrequencyModel getFrequencyModel() {
183 return freqModel;
184 }
185
186 /**
187 * @return the data type
188 */
189 public DataType getDataType() {
190 return dataType;
191 }
192
193 /**
194 * get the complete transition probability matrix for the given distance
195 *
196 * @param distance the expected number of substitutions
197 * @param matrix an array to store the matrix
198 */
199 public void getTransitionProbabilities(double distance, double[] matrix) {
200 int i, j, k;
201 double temp;
202
203 // this must be synchronized to avoid being called simultaneously by
204 // two different likelihood threads - AJD
205 synchronized (this) {
206 if (updateMatrix) {
207 setupMatrix();
208 }
209 }
210
211 // implemented a pool of iexp matrices to support multiple threads
212 // without creating a new matrix each call. - AJD
213 double[][] iexp = popiexp();
214 for (i = 0; i < stateCount; i++) {
215 temp = Math.exp(distance * Eval[i]);
216 for (j = 0; j < stateCount; j++) {
217 iexp[i][j] = Ievc[i][j] * temp;
218 }
219 }
220
221 int u = 0;
222 for (i = 0; i < stateCount; i++) {
223 for (j = 0; j < stateCount; j++) {
224 temp = 0.0;
225 for (k = 0; k < stateCount; k++) {
226 temp += Evec[i][k] * iexp[k][j];
227 }
228
229 matrix[u] = Math.abs(temp);
230 u++;
231 }
232 }
233 pushiexp(iexp);
234 }
235
236 /**
237 * This function returns the Eigen vectors.
238 *
239 * @return the array
240 */
241 public double[][] getEigenVectors() {
242 synchronized (this) {
243 if (updateMatrix) {
244 setupMatrix();
245 }
246 }
247 return Evec;
248 }
249
250 /**
251 * This function returns the inverse Eigen vectors.
252 *
253 * @return the array
254 */
255 public double[][] getInverseEigenVectors() {
256 synchronized (this) {
257 if (updateMatrix) {
258 setupMatrix();
259 }
260 }
261 return Ievc;
262 }
263
264 /**
265 * This function returns the Eigen values.
266 */
267 public double[] getEigenValues() {
268 synchronized (this) {
269 if (updateMatrix) {
270 setupMatrix();
271 }
272 }
273 return Eval;
274 }
275
276 /**
277 * setup substitution matrix
278 */
279 public void setupMatrix() {
280 setupRelativeRates();
281
282 if (!eigenInitialised)
283 initialiseEigen();
284
285 int i, j, k = 0;
286
287 // Set the instantaneous rate matrix
288 for (i = 0; i < stateCount; i++) {
289 for (j = i + 1; j < stateCount; j++) {
290 amat[i][j] = relativeRates[k] * freqModel.getFrequency(j);
291 amat[j][i] = relativeRates[k] * freqModel.getFrequency(i);
292 k += 1;
293 }
294 }
295 makeValid(amat, stateCount);
296 normalize(amat, freqModel.getFrequencies());
297
298 // copy q matrix for unit testing
299 for (i = 0; i < amat.length; i++) {
300 System.arraycopy(amat[i], 0, q[i], 0, amat[i].length);
301 }
302
303 // compute eigenvalues and eigenvectors
304 elmhes(amat, ordr, stateCount);
305 eltran(amat, Evec, ordr, stateCount);
306 hqr2(stateCount, 1, stateCount, amat, Evec, Eval, evali);
307 luinverse(Evec, Ievc, stateCount);
308
309 updateMatrix = false;
310 }
311
312 // Make it a valid rate matrix (make sum of rows = 0)
313 void makeValid(double[][] matrix, int dimension) {
314 for (int i = 0; i < dimension; i++) {
315 double sum = 0.0;
316 for (int j = 0; j < dimension; j++) {
317 if (i != j)
318 sum += matrix[i][j];
319 }
320 matrix[i][i] = -sum;
321 }
322 }
323
324 /**
325 * Normalize rate matrix to one expected substitution per unit time
326 *
327 * @param matrix the matrix to normalize to one expected substitution
328 * @param pi the equilibrium distribution of states
329 */
330 void normalize(double[][] matrix, double[] pi) {
331 double subst = 0.0;
332 int dimension = pi.length;
333
334 for (int i = 0; i < dimension; i++)
335 subst += -matrix[i][i] * pi[i];
336
337 for (int i = 0; i < dimension; i++) {
338 for (int j = 0; j < dimension; j++) {
339 matrix[i][j] = matrix[i][j] / subst;
340 }
341 }
342 }
343
344 /**
345 * Ensures that frequencies are not smaller than MINFREQ and
346 * that two frequencies differ by at least 2*MINFDIFF.
347 * This avoids potential problems later when eigenvalues
348 * are computed.
349 */
350 private void checkFrequencies() {
351 // required frequency difference
352 double MINFDIFF = 1.0E-10;
353
354 // lower limit on frequency
355 double MINFREQ = 1.0E-10;
356
357 int maxi = 0;
358 double sum = 0.0;
359 double maxfreq = 0.0;
360 for (int i = 0; i < stateCount; i++) {
361 double freq = freqModel.getFrequency(i);
362 if (freq < MINFREQ) freqModel.setFrequency(i, MINFREQ);
363 if (freq > maxfreq) {
364 maxfreq = freq;
365 maxi = i;
366 }
367 sum += freqModel.getFrequency(i);
368 }
369 double diff = 1.0 - sum;
370 freqModel.setFrequency(maxi, freqModel.getFrequency(maxi) + diff);
371
372 for (int i = 0; i < stateCount - 1; i++) {
373 for (int j = i + 1; j < stateCount; j++) {
374 if (freqModel.getFrequency(i) == freqModel.getFrequency(j)) {
375 freqModel.setFrequency(i, freqModel.getFrequency(i) + MINFDIFF);
376 freqModel.setFrequency(j, freqModel.getFrequency(j) - MINFDIFF);
377 }
378 }
379 }
380 }
381
382 /**
383 * allocate memory for the Eigen routines
384 */
385 protected void initialiseEigen() {
386
387 Eval = new double[stateCount];
388 Evec = new double[stateCount][stateCount];
389 Ievc = new double[stateCount][stateCount];
390
391 storedEval = new double[stateCount];
392 storedEvec = new double[stateCount][stateCount];
393 storedIevc = new double[stateCount][stateCount];
394
395 amat = new double[stateCount][stateCount];
396 q = new double[stateCount][stateCount];
397
398 ordr = new int[stateCount];
399 evali = new double[stateCount];
400
401 eigenInitialised = true;
402 updateMatrix = true;
403 }
404
405 // Eigenvalues, eigenvectors, and inverse eigenvectors
406 protected double[] Eval;
407 protected double[] storedEval;
408 protected double[][] Evec;
409 protected double[][] storedEvec;
410 protected double[][] Ievc;
411 protected double[][] storedIevc;
412
413 List<double[][]> iexpPool = new LinkedList<double[][]>();
414
415 private int[] ordr;
416 private double[] evali;
417 double amat[][];
418 private double q[][];
419
420 public double[][] getQ() {
421 return q;
422 }
423
424 protected synchronized double[][] popiexp() {
425
426 if (iexpPool.size() == 0) {
427 iexpPool.add(new double[stateCount][stateCount]);
428 }
429 return iexpPool.remove(0);
430 }
431
432 protected synchronized void pushiexp(double[][] iexp) {
433 iexpPool.add(0, iexp);
434 }
435
436 private void elmhes(double[][] a, int[] ordr, int n) {
437 int m, j, i;
438 double y, x;
439
440 for (i = 0; i < n; i++) {
441 ordr[i] = 0;
442 }
443 for (m = 2; m < n; m++) {
444 x = 0.0;
445 i = m;
446 for (j = m; j <= n; j++) {
447 if (Math.abs(a[j - 1][m - 2]) > Math.abs(x)) {
448 x = a[j - 1][m - 2];
449 i = j;
450 }
451 }
452 ordr[m - 1] = i;
453 if (i != m) {
454 for (j = m - 2; j < n; j++) {
455 y = a[i - 1][j];
456 a[i - 1][j] = a[m - 1][j];
457 a[m - 1][j] = y;
458 }
459 for (j = 0; j < n; j++) {
460 y = a[j][i - 1];
461 a[j][i - 1] = a[j][m - 1];
462 a[j][m - 1] = y;
463 }
464 }
465 if (x != 0.0) {
466 for (i = m; i < n; i++) {
467 y = a[i][m - 2];
468 if (y != 0.0) {
469 y /= x;
470 a[i][m - 2] = y;
471 for (j = m - 1; j < n; j++) {
472 a[i][j] -= y * a[m - 1][j];
473 }
474 for (j = 0; j < n; j++) {
475 a[j][m - 1] += y * a[j][i];
476 }
477 }
478 }
479 }
480 }
481 }
482
483 // Helper variables for mcdiv
484 private double cr, ci;
485
486 private void mcdiv(double ar, double ai, double br, double bi) {
487 double s, ars, ais, brs, bis;
488
489 s = Math.abs(br) + Math.abs(bi);
490 ars = ar / s;
491 ais = ai / s;
492 brs = br / s;
493 bis = bi / s;
494 s = brs * brs + bis * bis;
495 cr = (ars * brs + ais * bis) / s;
496 ci = (ais * brs - ars * bis) / s;
497 }
498
499 void hqr2(int n, int low, int hgh, double[][] h, double[][] zz,
500 double[] wr, double[] wi) throws ArithmeticException {
501 int i, j, k, l = 0, m, en, na, itn, its;
502 double p = 0, q = 0, r = 0, s = 0, t, w, x = 0, y, ra, sa, vi, vr, z = 0, norm, tst1, tst2;
503 boolean notLast;
504
505
506 norm = 0.0;
507 k = 1;
508 /* store isolated roots and compute matrix norm */
509 for (i = 0; i < n; i++) {
510 for (j = k - 1; j < n; j++) {
511 norm += Math.abs(h[i][j]);
512 }
513 k = i + 1;
514 if (i + 1 < low || i + 1 > hgh) {
515 wr[i] = h[i][i];
516 wi[i] = 0.0;
517 }
518 }
519 en = hgh;
520 t = 0.0;
521 itn = n * 30;
522 while (en >= low) { /* search for next eigenvalues */
523 its = 0;
524 na = en - 1;
525 while (en >= 1) {
526 /* look for single small sub-diagonal element */
527 boolean fullLoop = true;
528 for (l = en; l > low; l--) {
529 s = Math.abs(h[l - 2][l - 2]) + Math.abs(h[l - 1][l - 1]);
530 if (s == 0.0) {
531 s = norm;
532 }
533 tst1 = s;
534 tst2 = tst1 + Math.abs(h[l - 1][l - 2]);
535 if (tst2 == tst1) {
536 fullLoop = false;
537 break;
538 }
539 }
540 if (fullLoop) {
541 l = low;
542 }
543
544 x = h[en - 1][en - 1]; /* form shift */
545 if (l == en || l == na) {
546 break;
547 }
548 if (itn == 0) {
549 /* eigenvalues have not converged */
550 throw new ArithmeticException();
551 }
552 y = h[na - 1][na - 1];
553 w = h[en - 1][na - 1] * h[na - 1][en - 1];
554 /* form exceptional shift */
555 if (its == 10 || its == 20) {
556 t += x;
557 for (i = low - 1; i < en; i++) {
558 h[i][i] -= x;
559 }
560 s = Math.abs(h[en - 1][na - 1]) + Math.abs(h[na - 1][en - 3]);
561 x = 0.75 * s;
562 y = x;
563 w = -0.4375 * s * s;
564 }
565 its++;
566 itn--;
567 /* look for two consecutive small sub-diagonal elements */
568 for (m = en - 2; m >= l; m--) {
569 z = h[m - 1][m - 1];
570 r = x - z;
571 s = y - z;
572 p = (r * s - w) / h[m][m - 1] + h[m - 1][m];
573 q = h[m][m] - z - r - s;
574 r = h[m + 1][m];
575 s = Math.abs(p) + Math.abs(q) + Math.abs(r);
576 p /= s;
577 q /= s;
578 r /= s;
579 if (m == l) {
580 break;
581 }
582 tst1 = Math.abs(p) * (Math.abs(h[m - 2][m - 2]) + Math.abs(z) + Math.abs(h[m][m]));
583 tst2 = tst1 + Math.abs(h[m - 1][m - 2]) * (Math.abs(q) + Math.abs(r));
584 if (tst2 == tst1) {
585 break;
586 }
587 }
588 for (i = m + 2; i <= en; i++) {
589 h[i - 1][i - 3] = 0.0;
590 if (i != m + 2) {
591 h[i - 1][i - 4] = 0.0;
592 }
593 }
594 for (k = m; k <= na; k++) {
595 notLast = k != na;
596 if (k != m) {
597 p = h[k - 1][k - 2];
598 q = h[k][k - 2];
599 r = 0.0;
600 if (notLast) {
601 r = h[k + 1][k - 2];
602 }
603 x = Math.abs(p) + Math.abs(q) + Math.abs(r);
604 if (x != 0.0) {
605 p /= x;
606 q /= x;
607 r /= x;
608 }
609 }
610 if (x != 0.0) {
611 if (p < 0.0) { /* sign */
612 s = -Math.sqrt(p * p + q * q + r * r);
613 } else {
614 s = Math.sqrt(p * p + q * q + r * r);
615 }
616 if (k != m) {
617 h[k - 1][k - 2] = -s * x;
618 } else if (l != m) {
619 h[k - 1][k - 2] = -h[k - 1][k - 2];
620 }
621 p += s;
622 x = p / s;
623 y = q / s;
624 z = r / s;
625 q /= p;
626 r /= p;
627 if (!notLast) {
628 for (j = k - 1; j < n; j++) { /* row modification */
629 p = h[k - 1][j] + q * h[k][j];
630 h[k - 1][j] -= p * x;
631 h[k][j] -= p * y;
632 }
633 j = (en < (k + 3)) ? en : (k + 3); /* min */
634 for (i = 0; i < j; i++) { /* column modification */
635 p = x * h[i][k - 1] + y * h[i][k];
636 h[i][k - 1] -= p;
637 h[i][k] -= p * q;
638 }
639 /* accumulate transformations */
640 for (i = low - 1; i < hgh; i++) {
641 p = x * zz[i][k - 1] + y * zz[i][k];
642 zz[i][k - 1] -= p;
643 zz[i][k] -= p * q;
644 }
645 } else {
646 for (j = k - 1; j < n; j++) { /* row modification */
647 p = h[k - 1][j] + q * h[k][j] + r * h[k + 1][j];
648 h[k - 1][j] -= p * x;
649 h[k][j] -= p * y;
650 h[k + 1][j] -= p * z;
651 }
652 j = (en < (k + 3)) ? en : (k + 3); /* min */
653 for (i = 0; i < j; i++) { /* column modification */
654 p = x * h[i][k - 1] + y * h[i][k] + z * h[i][k + 1];
655 h[i][k - 1] -= p;
656 h[i][k] -= p * q;
657 h[i][k + 1] -= p * r;
658 }
659 /* accumulate transformations */
660 for (i = low - 1; i < hgh; i++) {
661 p = x * zz[i][k - 1] + y * zz[i][k] +
662 z * zz[i][k + 1];
663 zz[i][k - 1] -= p;
664 zz[i][k] -= p * q;
665 zz[i][k + 1] -= p * r;
666 }
667 }
668 }
669 } /* for k */
670 } /* while infinite loop */
671 if (l == en) { /* one root found */
672 h[en - 1][en - 1] = x + t;
673 wr[en - 1] = h[en - 1][en - 1];
674 wi[en - 1] = 0.0;
675 en = na;
676 continue;
677 }
678 y = h[na - 1][na - 1];
679 w = h[en - 1][na - 1] * h[na - 1][en - 1];
680 p = (y - x) / 2.0;
681 q = p * p + w;
682 z = Math.sqrt(Math.abs(q));
683 h[en - 1][en - 1] = x + t;
684 x = h[en - 1][en - 1];
685 h[na - 1][na - 1] = y + t;
686 if (q >= 0.0) { /* real pair */
687 if (p < 0.0) { /* sign */
688 z = p - Math.abs(z);
689 } else {
690 z = p + Math.abs(z);
691 }
692 wr[na - 1] = x + z;
693 wr[en - 1] = wr[na - 1];
694 if (z != 0.0) {
695 wr[en - 1] = x - w / z;
696 }
697 wi[na - 1] = 0.0;
698 wi[en - 1] = 0.0;
699 x = h[en - 1][na - 1];
700 s = Math.abs(x) + Math.abs(z);
701 p = x / s;
702 q = z / s;
703 r = Math.sqrt(p * p + q * q);
704 p /= r;
705 q /= r;
706 for (j = na - 1; j < n; j++) { /* row modification */
707 z = h[na - 1][j];
708 h[na - 1][j] = q * z + p * h[en - 1][j];
709 h[en - 1][j] = q * h[en - 1][j] - p * z;
710 }
711 for (i = 0; i < en; i++) { /* column modification */
712 z = h[i][na - 1];
713 h[i][na - 1] = q * z + p * h[i][en - 1];
714 h[i][en - 1] = q * h[i][en - 1] - p * z;
715 }
716 /* accumulate transformations */
717 for (i = low - 1; i < hgh; i++) {
718 z = zz[i][na - 1];
719 zz[i][na - 1] = q * z + p * zz[i][en - 1];
720 zz[i][en - 1] = q * zz[i][en - 1] - p * z;
721 }
722 } else { /* complex pair */
723 wr[na - 1] = x + p;
724 wr[en - 1] = x + p;
725 wi[na - 1] = z;
726 wi[en - 1] = -z;
727 }
728 en -= 2;
729 } /* while en >= low */
730 /* backsubstitute to find vectors of upper triangular form */
731 if (norm != 0.0) {
732 for (en = n; en >= 1; en--) {
733 p = wr[en - 1];
734 q = wi[en - 1];
735 na = en - 1;
736 if (q == 0.0) {/* real vector */
737 m = en;
738 h[en - 1][en - 1] = 1.0;
739 if (na != 0) {
740 for (i = en - 2; i >= 0; i--) {
741 w = h[i][i] - p;
742 r = 0.0;
743 for (j = m - 1; j < en; j++) {
744 r += h[i][j] * h[j][en - 1];
745 }
746 if (wi[i] < 0.0) {
747 z = w;
748 s = r;
749 } else {
750 m = i + 1;
751 if (wi[i] == 0.0) {
752 t = w;
753 if (t == 0.0) {
754 tst1 = norm;
755 t = tst1;
756 do {
757 t = 0.01 * t;
758 tst2 = norm + t;
759 }
760 while (tst2 > tst1);
761 }
762 h[i][en - 1] = -(r / t);
763 } else { /* solve real equations */
764 x = h[i][i + 1];
765 y = h[i + 1][i];
766 q = (wr[i] - p) * (wr[i] - p) + wi[i] * wi[i];
767 t = (x * s - z * r) / q;
768 h[i][en - 1] = t;
769 if (Math.abs(x) > Math.abs(z))
770 h[i + 1][en - 1] = (-r - w * t) / x;
771 else
772 h[i + 1][en - 1] = (-s - y * t) / z;
773 }
774 /* overflow control */
775 t = Math.abs(h[i][en - 1]);
776 if (t != 0.0) {
777 tst1 = t;
778 tst2 = tst1 + 1.0 / tst1;
779 if (tst2 <= tst1) {
780 for (j = i; j < en; j++) {
781 h[j][en - 1] /= t;
782 }
783 }
784 }
785 }
786 }
787 }
788 } else if (q > 0.0) {
789 m = na;
790 if (Math.abs(h[en - 1][na - 1]) > Math.abs(h[na - 1][en - 1])) {
791 h[na - 1][na - 1] = q / h[en - 1][na - 1];
792 h[na - 1][en - 1] = (p - h[en - 1][en - 1]) / h[en - 1][na - 1];
793 } else {
794 mcdiv(0.0, -h[na - 1][en - 1], h[na - 1][na - 1] - p, q);
795 h[na - 1][na - 1] = cr;
796 h[na - 1][en - 1] = ci;
797 }
798 h[en - 1][na - 1] = 0.0;
799 h[en - 1][en - 1] = 1.0;
800 if (en != 2) {
801 for (i = en - 3; i >= 0; i--) {
802 w = h[i][i] - p;
803 ra = 0.0;
804 sa = 0.0;
805 for (j = m - 1; j < en; j++) {
806 ra += h[i][j] * h[j][na - 1];
807 sa += h[i][j] * h[j][en - 1];
808 }
809 if (wi[i] < 0.0) {
810 z = w;
811 r = ra;
812 s = sa;
813 } else {
814 m = i + 1;
815 if (wi[i] == 0.0) {
816 mcdiv(-ra, -sa, w, q);
817 h[i][na - 1] = cr;
818 h[i][en - 1] = ci;
819 } else { /* solve complex equations */
820 x = h[i][i + 1];
821 y = h[i + 1][i];
822 vr = (wr[i] - p) * (wr[i] - p);
823 vr = vr + wi[i] * wi[i] - q * q;
824 vi = (wr[i] - p) * 2.0 * q;
825 if (vr == 0.0 && vi == 0.0) {
826 tst1 = norm * (Math.abs(w) + Math.abs(q) + Math.abs(x) +
827 Math.abs(y) + Math.abs(z));
828 vr = tst1;
829 do {
830 vr = 0.01 * vr;
831 tst2 = tst1 + vr;
832 }
833 while (tst2 > tst1);
834 }
835 mcdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi);
836 h[i][na - 1] = cr;
837 h[i][en - 1] = ci;
838 if (Math.abs(x) > Math.abs(z) + Math.abs(q)) {
839 h[i + 1]
840 [na - 1] = (q * h[i][en - 1] -
841 w * h[i][na - 1] - ra) / x;
842 h[i + 1][en - 1] = (-sa - w * h[i][en - 1] -
843 q * h[i][na - 1]) / x;
844 } else {
845 mcdiv(-r - y * h[i][na - 1], -s - y * h[i][en - 1], z, q);
846 h[i + 1][na - 1] = cr;
847 h[i + 1][en - 1] = ci;
848 }
849 }
850 /* overflow control */
851 t = (Math.abs(h[i][na - 1]) > Math.abs(h[i][en - 1])) ?
852 Math.abs(h[i][na - 1]) : Math.abs(h[i][en - 1]);
853 if (t != 0.0) {
854 tst1 = t;
855 tst2 = tst1 + 1.0 / tst1;
856 if (tst2 <= tst1) {
857 for (j = i; j < en; j++) {
858 h[j][na - 1] /= t;
859 h[j][en - 1] /= t;
860 }
861 }
862 }
863 }
864 }
865 }
866 }
867 }
868 /* end back substitution. vectors of isolated roots */
869 for (i = 0; i < n; i++) {
870 if (i + 1 < low || i + 1 > hgh) {
871 for (j = i; j < n; j++) {
872 zz[i][j] = h[i][j];
873 }
874 }
875 }
876 /* multiply by transformation matrix to give vectors of
877 * original full matrix. */
878 for (j = n - 1; j >= low - 1; j--) {
879 m = ((j + 1) < hgh) ? (j + 1) : hgh; /* min */
880 for (i = low - 1; i < hgh; i++) {
881 z = 0.0;
882 for (k = low - 1; k < m; k++) {
883 z += zz[i][k] * h[k][j];
884 }
885 zz[i][j] = z;
886 }
887 }
888 }
889 }
890
891 private void eltran(double[][] a, double[][] zz, int[] ordr, int n) {
892 int i, j, m;
893
894 for (i = 0; i < n; i++) {
895 for (j = i + 1; j < n; j++) {
896 zz[i][j] = 0.0;
897 zz[j][i] = 0.0;
898 }
899 zz[i][i] = 1.0;
900 }
901 if (n <= 2) {
902 return;
903 }
904 for (m = n - 1; m >= 2; m--) {
905 for (i = m; i < n; i++) {
906 zz[i][m - 1] = a[i][m - 2];
907 }
908 i = ordr[m - 1];
909 if (i != m) {
910 for (j = m - 1; j < n; j++) {
911 zz[m - 1][j] = zz[i - 1][j];
912 zz[i - 1][j] = 0.0;
913 }
914 zz[i - 1][m - 1] = 1.0;
915 }
916 }
917 }
918
919 void luinverse(double[][] inmat, double[][] imtrx, int size) throws IllegalArgumentException {
920 int i, j, k, l, maxi = 0, idx, ix, jx;
921 double sum, tmp, maxb, aw;
922 int[] index;
923 double[] wk;
924 double[][] omtrx;
925
926
927 index = new int[size];
928 omtrx = new double[size][size];
929
930 /* copy inmat to omtrx */
931 for (i = 0; i < size; i++) {
932 for (j = 0; j < size; j++) {
933 omtrx[i][j] = inmat[i][j];
934 }
935 }
936
937 wk = new double[size];
938 aw = 1.0;
939 for (i = 0; i < size; i++) {
940 maxb = 0.0;
941 for (j = 0; j < size; j++) {
942 if (Math.abs(omtrx[i][j]) > maxb) {
943 maxb = Math.abs(omtrx[i][j]);
944 }
945 }
946 if (maxb == 0.0) {
947 /* Singular matrix */
948 System.out.println("Singular matrix encountered");
949 throw new IllegalArgumentException("Singular matrix");
950 }
951 wk[i] = 1.0 / maxb;
952 }
953 for (j = 0; j < size; j++) {
954 for (i = 0; i < j; i++) {
955 sum = omtrx[i][j];
956 for (k = 0; k < i; k++) {
957 sum -= omtrx[i][k] * omtrx[k][j];
958 }
959 omtrx[i][j] = sum;
960 }
961 maxb = 0.0;
962 for (i = j; i < size; i++) {
963 sum = omtrx[i][j];
964 for (k = 0; k < j; k++) {
965 sum -= omtrx[i][k] * omtrx[k][j];
966 }
967 omtrx[i][j] = sum;
968 tmp = wk[i] * Math.abs(sum);
969 if (tmp >= maxb) {
970 maxb = tmp;
971 maxi = i;
972 }
973 }
974 if (j != maxi) {
975 for (k = 0; k < size; k++) {
976 tmp = omtrx[maxi][k];
977 omtrx[maxi][k] = omtrx[j][k];
978 omtrx[j][k] = tmp;
979 }
980 aw = -aw;
981 wk[maxi] = wk[j];
982 }
983 index[j] = maxi;
984 if (omtrx[j][j] == 0.0) {
985 omtrx[j][j] = MachineAccuracy.EPSILON;
986 }
987 if (j != size - 1) {
988 tmp = 1.0 / omtrx[j][j];
989 for (i = j + 1; i < size; i++) {
990 omtrx[i][j] *= tmp;
991 }
992 }
993 }
994 for (jx = 0; jx < size; jx++) {
995 for (ix = 0; ix < size; ix++) {
996 wk[ix] = 0.0;
997 }
998 wk[jx] = 1.0;
999 l = -1;
1000 for (i = 0; i < size; i++) {
1001 idx = index[i];
1002 sum = wk[idx];
1003 wk[idx] = wk[i];
1004 if (l != -1) {
1005 for (j = l; j < i; j++) {
1006 sum -= omtrx[i][j] * wk[j];
1007 }
1008 } else if (sum != 0.0) {
1009 l = i;
1010 }
1011 wk[i] = sum;
1012 }
1013 for (i = size - 1; i >= 0; i--) {
1014 sum = wk[i];
1015 for (j = i + 1; j < size; j++) {
1016 sum -= omtrx[i][j] * wk[j];
1017 }
1018 wk[i] = sum / omtrx[i][i];
1019 }
1020 for (ix = 0; ix < size; ix++) {
1021 imtrx[ix][jx] = wk[ix];
1022 }
1023 }
1024 wk = null;
1025 index = null;
1026 omtrx = null;
1027 }
1028 }