Debian Codegraph @

src/dr/oldevomodel/substmodel/AbstractCovarionModel.java

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