Debian Codegraph @

src/dr/math/ConjugateDirectionSearch.java

raw symbols SCIP
1 /*
2 * ConjugateDirectionSearch.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
29 /**
30 * methods for minimization of a real-valued function of
31 * several variables without using derivatives
32 * - algorithm is based on Brent's modification of a conjugate direction
33 * search method proposed by Powell (praxis)
34 * Richard P. Brent. 1973. Algorithms for finding zeros and extrema
35 *
36 * @author Korbinian Strimmer
37 */
38 public class ConjugateDirectionSearch extends MultivariateMinimum
39 {
40 //
41 // Public stuff
42 //
43
44 /**
45 * constructor
46 */
47 public ConjugateDirectionSearch()
48 {
49 }
50
51
52 // Variables that control aspects of the inner workings of the
53 // minimization algorithm. Setting them is optional, they
54 // are all set to some reasonable default values given below.
55
56 /**
57 * controls the printed output from the routine
58 * (0 -> no output, 1 -> print only starting and final values,
59 * 2 -> detailed map of the minimization process,
60 * 3 -> print also eigenvalues and vectors of the
61 * search directions), the default value is 0
62 */
63 public int prin = 0;
64
65 /**
66 * step is a steplength parameter and should be set equal
67 * to the expected distance from the solution.
68 * exceptionally small or large values of step lead to
69 * slower convergence on the first few iterations
70 * the default value for step is 1.0
71 */
72 public double step = 1.0;
73
74 /**
75 * scbd is a scaling parameter. 1.0 is the default and
76 * indicates no scaling. if the scales for the different
77 * parameters are very different, scbd should be set to
78 * a value of about 10.0.
79 */
80 public double scbd = 1.0;
81
82 /**
83 * illc should be set to true
84 * if the problem is known to
85 * be ill-conditioned. the default is false. this
86 * variable is automatically set, when the problem
87 * is found to to be ill-conditioned during iterations.
88 */
89 public boolean illc = false;
90
91 // implementation of abstract method
92
93 public void optimize(MultivariateFunction f, double[] xvector, double tolfx, double tolx) {
94 optimize(f,xvector,tolfx,tolx,null);
95 }
96
97 public void optimize(MultivariateFunction f, double[] xvector, double tolfx, double tolx, MinimiserMonitor monitor)
98 {
99 t = tolx;
100
101 fun = f;
102 x = xvector;
103
104 checkBounds(x);
105 h = step;
106
107 dim = fun.getNumArguments();
108
109 d = new double[dim];
110 y = new double[dim];
111 z = new double[dim];
112 q0 = new double[dim];
113 q1 = new double[dim];
114 v = new double[dim][dim];
115 tflin = new double[dim];
116
117 small = MachineAccuracy.EPSILON*MachineAccuracy.EPSILON;
118 vsmall = small*small;
119 large = 1.0/small;
120 vlarge = 1.0/vsmall;
121 ldfac = (illc ? 0.1 : 0.01);
122 nl = kt = 0;
123 numFun = 1;
124 fx = fun.evaluate(x);
125
126 stopCondition(fx, x, tolfx, tolx, true);
127
128 qf1 = fx;
129 t2 = small + Math.abs(t);
130 t = t2;
131 dmin = small;
132
133 if (h < 100.0*t) h = 100.0*t;
134 ldt = h;
135 for (i = 0; i < dim; i++)
136 {
137 for (j = 0; j < dim; j++)
138 {
139 v[i][j] = (i == j ? 1.0 : 0.0);
140 }
141 }
142 d[0] = 0.0;
143 qd0 = 0.0;
144 for (i = 0; i < dim; i++)
145 q1[i] = x[i];
146
147 if (prin > 1)
148 {
149 System.out.println("\n------------- enter function praxis -----------\n");
150 System.out.println("... current parameter settings ...");
151 System.out.println("... scaling ... " + scbd);
152 System.out.println("... tolx ... " + t);
153 System.out.println("... tolfx ... " + tolfx);
154 System.out.println("... maxstep ... " + h);
155 System.out.println("... illc ... " + illc);
156 System.out.println("... maxFun ... " + maxFun);
157 }
158 if (prin > 0)
159 System.out.println();
160
161 while(true)
162 {
163 sf = d[0];
164 s = d[0] = 0.0;
165
166 /* minimize along first direction */
167
168 min1 = d[0];
169 min2 = s;
170 min(0, 2, fx, false);
171 d[0] = min1;
172 s = min2;
173
174 if (s <= 0.0)
175 for (i = 0; i < dim; i++)
176 {
177 v[i][0] = -v[i][0];
178 }
179 if ((sf <= (0.9 * d[0])) || ((0.9 * sf) >= d[0]))
180 for (i=1; i < dim; i++)
181 d[i] = 0.0;
182
183 boolean gotoFret = false;
184 for (k=1; k < dim; k++)
185 {
186 for (i=0; i< dim; i++)
187 {
188 y[i] = x[i];
189 }
190 sf = fx;
191 illc = illc || (kt > 0);
192
193 boolean gotoNext;
194 do
195 {
196 kl = k;
197 df = 0.0;
198 if (illc)
199 { /* random step to get off resolution valley */
200 for (i=0; i < dim; i++)
201 {
202 z[i] = (0.1 * ldt + t2 * Math.pow(10.0,(double)kt)) * (MathUtils.nextDouble() - 0.5);
203 s = z[i];
204 for (j=0; j < dim; j++)
205 {
206 x[j] += s * v[j][i];
207 }
208 }
209
210 checkBounds(x);
211
212 fx = fun.evaluate(x);
213 numFun++;
214 }
215
216 /* minimize along non-conjugate directions */
217 for (k2=k; k2 < dim; k2++)
218 {
219 sl = fx;
220 s = 0.0;
221
222 min1 = d[k2];
223 min2 = s;
224 min(k2, 2, fx, false);
225 d[k2] = min1;
226 s = min2;
227
228 if (illc)
229 {
230 double szk = s + z[k2];
231 s = d[k2] * szk*szk;
232 }
233 else
234 s = sl - fx;
235 if (df < s)
236 {
237 df = s;
238 kl = k2;
239 }
240 }
241
242 if (!illc && (df < Math.abs(100.0 * MachineAccuracy.EPSILON * fx)))
243 {
244 illc = true;
245 gotoNext = true;
246 }
247 else
248 gotoNext = false;
249 } while (gotoNext);
250
251
252
253 if ((k == 1) && (prin > 1))
254 vecprint("\n... New Direction ...", d);
255 /* minimize along conjugate directions */
256 for (k2=0; k2<=k-1; k2++)
257 {
258 s = 0.0;
259
260 min1 = d[k2];
261 min2 = s;
262 min(k2, 2, fx, false);
263 d[k2] = min1;
264 s = min2;
265 }
266 f1 = fx;
267 fx = sf;
268 lds = 0.0;
269 for (i=0; i<dim; i++)
270 {
271 sl = x[i];
272 x[i] = y[i];
273 y[i] = sl - y[i];
274 sl = y[i];
275 lds = lds + sl*sl;
276 }
277 checkBounds(x);
278
279 lds = Math.sqrt(lds);
280 if (lds > small)
281 {
282 for (i=kl-1; i>=k; i--)
283 {
284 for (j=0; j < dim; j++)
285 v[j][i+1] = v[j][i];
286 d[i+1] = d[i];
287 }
288 d[k] = 0.0;
289 for (i=0; i < dim; i++)
290 v[i][k] = y[i] / lds;
291
292 min1 = d[k];
293 min2 = lds;
294 min(k, 4, f1, true);
295 d[k] = min1;
296 lds = min2;
297
298 if (lds <= 0.0)
299 {
300 lds = -lds;
301 for (i=0; i< dim; i++)
302 v[i][k] = -v[i][k];
303 }
304 }
305 ldt = ldfac * ldt;
306 if (ldt < lds)
307 ldt = lds;
308 if (prin > 1)
309 print();
310 if(monitor!=null) {
311
312 monitor.newMinimum(fx,x);
313 }
314
315 if(stopCondition(fx, x, tolfx, tolx, false))
316 {
317 kt++;
318 }
319 else
320 {
321 kt = 0;
322 }
323 if (kt > 1)
324 {
325 gotoFret = true;
326 break;
327 }
328
329 }
330
331 if (gotoFret) break;
332
333
334 /* try quadratic extrapolation in case */
335 /* we are stuck in a curved valley */
336 quadr();
337 dn = 0.0;
338 for (i=0; i < dim; i++)
339 {
340 d[i] = 1.0 / Math.sqrt(d[i]);
341 if (dn < d[i])
342 dn = d[i];
343 }
344 if (prin > 2)
345 matprint("\n... New Matrix of Directions ...",v);
346 for (j=0; j < dim; j++)
347 {
348 s = d[j] / dn;
349 for (i=0; i < dim; i++)
350 v[i][j] *= s;
351 }
352 if (scbd > 1.0)
353 { /* scale axis to reduce condition number */
354 s = vlarge;
355 for (i=0; i < dim; i++)
356 {
357 sl = 0.0;
358 for (j=0; j < dim; j++)
359 sl += v[i][j]*v[i][j];
360 z[i] = Math.sqrt(sl);
361 if (z[i] < MachineAccuracy.SQRT_SQRT_EPSILON)
362 z[i] = MachineAccuracy.SQRT_SQRT_EPSILON;
363 if (s > z[i])
364 s = z[i];
365 }
366 for (i=0; i < dim; i++)
367 {
368 sl = s / z[i];
369 z[i] = 1.0 / sl;
370 if (z[i] > scbd)
371 {
372 sl = 1.0 / scbd;
373 z[i] = scbd;
374 }
375 }
376 }
377 for (i=1; i < dim; i++)
378 for (j=0; j<=i-1; j++)
379 {
380 s = v[i][j];
381 v[i][j] = v[j][i];
382 v[j][i] = s;
383 }
384 minfit(dim, MachineAccuracy.EPSILON, vsmall, v, d);
385 if (scbd > 1.0)
386 {
387 for (i=0; i < dim; i++)
388 {
389 s = z[i];
390 for (j=0; j < dim; j++)
391 v[i][j] *= s;
392 }
393 for (i=0; i < dim; i++)
394 {
395 s = 0.0;
396 for (j=0; j < dim; j++)
397 s += v[j][i]*v[j][i];
398 s = Math.sqrt(s);
399 d[i] *= s;
400 s = 1.0 / s;
401 for (j=0; j < dim; j++)
402 v[j][i] *= s;
403 }
404 }
405 for (i=0; i < dim; i++)
406 {
407 if ((dn * d[i]) > large)
408 d[i] = vsmall;
409 else if ((dn * d[i]) < small)
410 d[i] = vlarge;
411 else
412 d[i] = Math.pow(dn * d[i],-2.0);
413 }
414 sort(); /* the new eigenvalues and eigenvectors */
415 dmin = d[dim-1];
416 if (dmin < small)
417 dmin = small;
418 illc = (MachineAccuracy.SQRT_EPSILON * d[0]) > dmin;
419 if ((prin > 2) && (scbd > 1.0))
420 vecprint("\n... Scale Factors ...",z);
421 if (prin > 2)
422 vecprint("\n... Eigenvalues of A ...",d);
423 if (prin > 2)
424 matprint("\n... Eigenvectors of A ...",v);
425
426 if ((maxFun > 0) && (nl > maxFun))
427 {
428 if (prin > 0)
429 System.out.println("\n... maximum number of function calls reached ...");
430 break;
431 }
432 }
433
434 if (prin > 0)
435 {
436 vecprint("\n... Final solution is ...", x);
437 System.out.println("\n... Function value reduced to " + fx + " ...");
438 System.out.println("... after " + numFun + " function calls.");
439 }
440
441 //return (fx);
442 }
443
444
445 //
446 // Private stuff
447 //
448
449 // some global variables
450 private int i, j, k, k2, nl, kl, kt;
451 private double s, sl, dn, dmin,
452 fx, f1, lds, ldt, sf, df,
453 qf1, qd0, qd1, qa, qb, qc, small, vsmall, large,
454 vlarge, ldfac, t2;
455
456 // need to be initialised
457 private double[] d;
458 private double[] y;
459 private double[] z;
460 private double[] q0;
461 private double[] q1;
462 private double[][] v;
463
464 private double[] tflin;
465
466 private int dim;
467 private double[] x;
468 private MultivariateFunction fun;
469
470 // these will be set by praxis to the global control parameters
471 private double h, t;
472
473 // sort d and v in descending order
474 private void sort()
475 {
476 int k, i, j;
477 double s;
478
479 for (i=0; i < dim-1; i++)
480 {
481 k = i; s = d[i];
482 for (j=i+1; j < dim; j++)
483 {
484 if (d[j] > s)
485 {
486 k = j;
487 s = d[j];
488 }
489 }
490 if (k > i)
491 {
492 d[k] = d[i];
493 d[i] = s;
494 for (j=0; j < dim; j++)
495 {
496 s = v[j][i];
497 v[j][i] = v[j][k];
498 v[j][k] = s;
499 }
500 }
501 }
502 }
503
504 private void vecprint(String s, double[] x)
505 {
506 System.out.println(s);
507 for (int i=0; i < x.length; i++)
508 System.out.print(x[i] + " ");
509 System.out.println();
510 }
511
512 private void print() /* print a line of traces */
513 {
514 System.out.println();
515 System.out.println("... function value reduced to ... " + fx);
516 System.out.println("... after " + numFun + " function calls ...");
517 System.out.println("... including " + nl + " linear searches ...");
518 vecprint("... current values of x ...", x);
519 }
520
521 private void matprint(String s, double[][] v)
522 {
523 System.out.println(s);
524 for (int k=0; k<v.length; k++)
525 {
526 for (int i=0; i<v.length; i++)
527 {
528 System.out.print(v[k][i] + " ");
529 }
530 System.out.println();
531 }
532 }
533
534 private double flin(double l, int j)
535 {
536 if (j != -1)
537 { /* linear search */
538 for (int i = 0; i < dim; i++)
539 tflin[i] = x[i] + l*v[i][j];
540 }
541 else
542 { /* search along parabolic space curve */
543 qa = l*(l-qd1)/(qd0*(qd0+qd1));
544 qb = (l+qd0)*(qd1-l)/(qd0*qd1);
545 qc = l*(l+qd0)/(qd1*(qd0+qd1));
546 for (int i = 0; i < dim; i++)
547 {
548 tflin[i] = qa*q0[i]+qb*x[i]+qc*q1[i];
549 }
550 }
551
552 checkBounds(tflin);
553
554 numFun++;
555 return fun.evaluate(tflin);
556 }
557
558 private void checkBounds(double[] p)
559 {
560 for (int i = 0; i < dim; i++)
561 {
562 if (p[i] < fun.getLowerBound(i))
563 {
564 p[i] = fun.getLowerBound(i);
565 }
566 if (p[i] > fun.getUpperBound(i))
567 {
568 p[i] = fun.getUpperBound(i);
569 }
570 }
571 }
572
573 private double min1;
574 private double min2;
575
576 private void min(int j, int nits, double f1, boolean fk)
577 {
578 int k;
579 double x2, xm, f0, f2, fm, d1, t2, s, sf1, sx1;
580
581 sf1 = f1; sx1 = min2;
582 k = 0; xm = 0.0; fm = f0 = fx;
583 boolean dz = (min1 < MachineAccuracy.EPSILON);
584
585 /* find step size */
586 s = 0;
587 for (int i=0; i < dim; i++)
588 {
589 s += x[i]*x[i];
590 }
591 s = Math.sqrt(s);
592 if (dz)
593 {
594 t2 = MachineAccuracy.SQRT_SQRT_EPSILON*
595 Math.sqrt(Math.abs(fx)/dmin + s*ldt) +
596 MachineAccuracy.SQRT_EPSILON*ldt;
597 }
598 else
599 {
600 t2 = MachineAccuracy.SQRT_SQRT_EPSILON*
601 Math.sqrt(Math.abs(fx)/(min1) + s*ldt) +
602 MachineAccuracy.SQRT_EPSILON*ldt;
603 }
604 s = s*MachineAccuracy.SQRT_SQRT_EPSILON + t;
605 if (dz && t2 > s)
606 t2 = s;
607 if (t2 < small)
608 t2 = small;
609 if (t2 > 0.01*h)
610 t2 = 0.01 * h;
611 if (fk && f1 <= fm)
612 {
613 xm = min2;
614 fm = f1;
615 }
616 if (!fk || Math.abs(min2) < t2)
617 {
618 min2 = (min2 > 0 ? t2 : -t2);
619 f1 = flin(min2, j);
620 }
621 if (f1 <= fm)
622 {
623 xm = min2;
624 fm = f1;
625 }
626
627 boolean gotoNext;
628 do
629 {
630 if (dz)
631 {
632 x2 = (f0 < f1 ? -(min2) : 2*(min2));
633 f2 = flin(x2, j);
634 if (f2 <= fm)
635 {
636 xm = x2;
637 fm = f2;
638 }
639 min1 = (x2*(f1-f0) - (min2)*(f2-f0))/((min2)*x2*((min2)-x2));
640 }
641 d1 = (f1-f0)/(min2) - min2*min1;
642 dz = true;
643 if (min1 <= small)
644 {
645 x2 = (d1 < 0 ? h : -h);
646 }
647 else
648 {
649 x2 = - 0.5*d1/(min1);
650 }
651 if (Math.abs(x2) > h)
652 x2 = (x2 > 0 ? h : -h);
653
654 f2 = flin(x2, j);
655
656 gotoNext = false;
657
658 while ((k < nits) && (f2 > f0))
659 {
660 k++;
661 if ((f0 < f1) && (min2*x2 > 0.0))
662 {
663 gotoNext = true;
664 break;
665 }
666
667
668 x2 *= 0.5;
669 f2 = flin(x2, j);
670 }
671 } while (gotoNext);
672
673
674 nl++;
675 if (f2 > fm)
676 x2 = xm;
677 else fm = f2;
678 if (Math.abs(x2*(x2-min2)) > small)
679 {
680 min1 = (x2*(f1-f0) - min2*(fm-f0))/(min2*x2*(min2-x2));
681 }
682 else
683 {
684 if (k > 0) min1 = 0;
685 }
686 if (min1 <= small)
687 min1 = small;
688 min2 = x2; fx = fm;
689 if (sf1 < fx)
690 {
691 fx = sf1;
692 min2 = sx1;
693 }
694 if (j != -1)
695 {
696 for (i=0; i < dim; i++)
697 {
698 x[i] += (min2)*v[i][j];
699 }
700 checkBounds(x);
701 }
702 }
703
704 // Look for a minimum along the curve q0, q1, q2
705 private void quadr()
706 {
707 int i;
708 double l, s;
709
710 s = fx; fx = qf1; qf1 = s; qd1 = 0.0;
711 for (i=0; i < dim; i++)
712 {
713 s = x[i]; l = q1[i]; x[i] = l; q1[i] = s;
714 qd1 = qd1 + (s-l)*(s-l);
715 }
716 s = 0.0; qd1 = Math.sqrt(qd1); l = qd1;
717 if (qd0>0.0 && qd1>0.0 &&nl>=3*dim*dim)
718 {
719 min1 = s;
720 min2 = l;
721 min(-1, 2, qf1, true);
722 s = min1;
723 l = min2;
724
725 qa = l*(l-qd1)/(qd0*(qd0+qd1));
726 qb = (l+qd0)*(qd1-l)/(qd0*qd1);
727 qc = l*(l+qd0)/(qd1*(qd0+qd1));
728 }
729 else
730 {
731 fx = qf1; qa = qb = 0.0; qc = 1.0;
732 }
733 qd0 = qd1;
734 for (i=0; i<dim; i++)
735 {
736 s = q0[i]; q0[i] = x[i];
737 x[i] = qa*s + qb*x[i] + qc*q1[i];
738 }
739
740 checkBounds(x);
741 }
742
743 // Singular value decomposition
744 private void minfit(int n, double eps, double tol, double[][] ab, double[] q)
745 {
746 int l=0, kt, l2, i, j, k;
747 double c, f, g, h, s, x, y, z;
748
749 double[] e = new double[dim];
750
751
752 /* householder's reduction to bidiagonal form */
753 x = g = 0.0;
754 for (i=0; i<n; i++)
755 {
756 e[i] = g; s = 0.0; l = i+1;
757 for (j=i; j<n; j++)
758 s += ab[j][i] * ab[j][i];
759 if (s < tol)
760 {
761 g = 0.0;
762 }
763 else
764 {
765 f = ab[i][i];
766 if (f < 0.0)
767 g = Math.sqrt(s);
768 else
769 g = -Math.sqrt(s);
770 h = f*g - s; ab[i][i] = f - g;
771 for (j=l; j<n; j++)
772 {
773 f = 0.0;
774 for (k=i; k<n; k++)
775 f += ab[k][i] * ab[k][j];
776 f /= h;
777 for (k=i; k<n; k++)
778 ab[k][j] += f * ab[k][i];
779 }
780 }
781 q[i] = g; s = 0.0;
782 if (i < n)
783 for (j=l; j<n; j++)
784 s += ab[i][j] * ab[i][j];
785 if (s < tol)
786 {
787 g = 0.0;
788 }
789 else
790 {
791 f = ab[i][i+1];
792 if (f < 0.0)
793 g = Math.sqrt(s);
794 else
795 g = - Math.sqrt(s);
796 h = f*g - s;
797 ab[i][i+1] = f - g;
798 for (j=l; j<n; j++)
799 e[j] = ab[i][j]/h;
800 for (j=l; j<n; j++)
801 {
802 s = 0;
803 for (k=l; k<n; k++)
804 s += ab[j][k]*ab[i][k];
805 for (k=l; k<n; k++)
806 ab[j][k] += s * e[k];
807 }
808 }
809 y = Math.abs(q[i]) + Math.abs(e[i]);
810 if (y > x)
811 x = y;
812 }
813 /* accumulation of right hand transformations */
814 for (i=n-1; i >= 0; i--)
815 {
816 if (g != 0.0)
817 {
818 h = ab[i][i+1]*g;
819 for (j=l; j<n; j++) ab[j][i] = ab[i][j] / h;
820 for (j=l; j<n; j++)
821 {
822 s = 0.0;
823 for (k=l; k<n; k++) s += ab[i][k] * ab[k][j];
824 for (k=l; k<n; k++) ab[k][j] += s * ab[k][i];
825 }
826 }
827 for (j=l; j<n; j++)
828 ab[i][j] = ab[j][i] = 0.0;
829 ab[i][i] = 1.0; g = e[i]; l = i;
830 }
831 /* diagonalization to bidiagonal form */
832 eps *= x;
833 boolean converged = false;
834 for (k=n-1; k>= 0; k--)
835 {
836 kt = 0;
837
838 do
839 {
840 kt++;
841
842 boolean skipNext = false;
843 for (l2=k; l2>=0; l2--)
844 {
845 l = l2;
846 if (Math.abs(e[l]) <= eps)
847 {
848 skipNext = true;
849 break;
850 }
851
852 if (Math.abs(q[l-1]) <= eps)
853 break;
854 }
855
856 if (skipNext == false)
857 {
858
859 c = 0.0; s = 1.0;
860 for (i=l; i<=k; i++)
861 {
862 f = s * e[i]; e[i] *= c;
863 if (Math.abs(f) <= eps)
864 break;
865 g = q[i];
866 if (Math.abs(f) < Math.abs(g))
867 {
868 double fg = f/g;
869 h = Math.abs(g)*Math.sqrt(1.0+fg*fg);
870 }
871 else
872 {
873 double gf = g/f;
874 h = (f!=0.0 ? Math.abs(f)*Math.sqrt(1.0+gf*gf) : 0.0);
875 }
876 q[i] = h;
877 if (h == 0.0)
878 {
879 h = 1.0; g = 1.0;
880 }
881 c = g/h; s = -f/h;
882 }
883 }
884
885 z = q[k];
886 if (l == k)
887 {
888 converged = true;
889 break;
890 }
891
892
893 /* shift from bottom 2x2 minor */
894 x = q[l]; y = q[k-l]; g = e[k-1]; h = e[k];
895 f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2.0*h*y);
896 g = Math.sqrt(f*f+1.0);
897 if (f <= 0.0)
898 f = ((x-z)*(x+z) + h*(y/(f-g)-h))/x;
899 else
900 f = ((x-z)*(x+z) + h*(y/(f+g)-h))/x;
901 /* next qr transformation */
902 s = c = 1.0;
903 for (i=l+1; i<=k; i++)
904 {
905 g = e[i]; y = q[i]; h = s*g; g *= c;
906 if (Math.abs(f) < Math.abs(h))
907 {
908 double fh = f/h;
909 z = Math.abs(h) * Math.sqrt(1.0 + fh*fh);
910 }
911 else
912 {
913 double hf = h/f;
914 z = (f!=0.0 ? Math.abs(f)*Math.sqrt(1.0+hf*hf) : 0.0);
915 }
916 e[i-1] = z;
917 if (z == 0.0)
918 f = z = 1.0;
919 c = f/z; s = h/z;
920 f = x*c + g*s; g = - x*s + g*c; h = y*s;
921 y *= c;
922 for (j=0; j<n; j++)
923 {
924 x = ab[j][i-1]; z = ab[j][i];
925 ab[j][i-1] = x*c + z*s;
926 ab[j][i] = - x*s + z*c;
927 }
928 if (Math.abs(f) < Math.abs(h))
929 {
930 double fh = f/h;
931 z = Math.abs(h) * Math.sqrt(1.0 + fh*fh);
932 }
933 else
934 {
935 double hf = h/f;
936 z = (f!=0.0 ? Math.abs(f)*Math.sqrt(1.0+hf*hf) : 0.0);
937 }
938 q[i-1] = z;
939 if (z == 0.0)
940 z = f = 1.0;
941 c = f/z; s = h/z;
942 f = c*g + s*y; x = - s*g + c*y;
943 }
944 e[l] = 0.0; e[k] = f; q[k] = x;
945
946
947 } while (kt <= 30);
948
949 if (!converged)
950 {
951 e[k] = 0.0;
952 System.out.println("\n+++ qr failed\n");
953 System.exit(1);
954 }
955
956
957 if (z < 0.0)
958 {
959 q[k] = - z;
960 for (j=0; j<n; j++)
961 ab[j][k] = - ab[j][k];
962 }
963 }
964 }
965 }