Conditions | 71 |
Paths | > 20000 |
Total Lines | 377 |
Code Lines | 272 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
398 | private function hqr2() |
||
399 | { |
||
400 | // Initialize |
||
401 | $nn = $this->n; |
||
402 | $n = $nn - 1; |
||
403 | $low = 0; |
||
404 | $high = $nn - 1; |
||
405 | $eps = pow(2.0, -52.0); |
||
406 | $exshift = 0.0; |
||
407 | $p = $q = $r = $s = $z = 0; |
||
408 | // Store roots isolated by balanc and compute matrix norm |
||
409 | $norm = 0.0; |
||
410 | |||
411 | for ($i = 0; $i < $nn; ++$i) { |
||
412 | if (($i < $low) or ($i > $high)) { |
||
413 | $this->d[$i] = $this->H[$i][$i]; |
||
414 | $this->e[$i] = 0.0; |
||
415 | } |
||
416 | for ($j = max($i - 1, 0); $j < $nn; ++$j) { |
||
417 | $norm = $norm + abs($this->H[$i][$j]); |
||
418 | } |
||
419 | } |
||
420 | |||
421 | // Outer loop over eigenvalue index |
||
422 | $iter = 0; |
||
423 | while ($n >= $low) { |
||
424 | // Look for single small sub-diagonal element |
||
425 | $l = $n; |
||
426 | while ($l > $low) { |
||
427 | $s = abs($this->H[$l - 1][$l - 1]) + abs($this->H[$l][$l]); |
||
428 | if ($s == 0.0) { |
||
429 | $s = $norm; |
||
430 | } |
||
431 | if (abs($this->H[$l][$l - 1]) < $eps * $s) { |
||
432 | break; |
||
433 | } |
||
434 | --$l; |
||
435 | } |
||
436 | // Check for convergence |
||
437 | // One root found |
||
438 | if ($l == $n) { |
||
439 | $this->H[$n][$n] = $this->H[$n][$n] + $exshift; |
||
440 | $this->d[$n] = $this->H[$n][$n]; |
||
441 | $this->e[$n] = 0.0; |
||
442 | --$n; |
||
443 | $iter = 0; |
||
444 | // Two roots found |
||
445 | } elseif ($l == $n - 1) { |
||
446 | $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; |
||
447 | $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; |
||
448 | $q = $p * $p + $w; |
||
449 | $z = sqrt(abs($q)); |
||
450 | $this->H[$n][$n] = $this->H[$n][$n] + $exshift; |
||
451 | $this->H[$n - 1][$n - 1] = $this->H[$n - 1][$n - 1] + $exshift; |
||
452 | $x = $this->H[$n][$n]; |
||
453 | // Real pair |
||
454 | if ($q >= 0) { |
||
455 | if ($p >= 0) { |
||
456 | $z = $p + $z; |
||
457 | } else { |
||
458 | $z = $p - $z; |
||
459 | } |
||
460 | $this->d[$n - 1] = $x + $z; |
||
461 | $this->d[$n] = $this->d[$n - 1]; |
||
462 | if ($z != 0.0) { |
||
463 | $this->d[$n] = $x - $w / $z; |
||
464 | } |
||
465 | $this->e[$n - 1] = 0.0; |
||
466 | $this->e[$n] = 0.0; |
||
467 | $x = $this->H[$n][$n - 1]; |
||
468 | $s = abs($x) + abs($z); |
||
469 | $p = $x / $s; |
||
470 | $q = $z / $s; |
||
471 | $r = sqrt($p * $p + $q * $q); |
||
472 | $p = $p / $r; |
||
473 | $q = $q / $r; |
||
474 | // Row modification |
||
475 | for ($j = $n - 1; $j < $nn; ++$j) { |
||
476 | $z = $this->H[$n - 1][$j]; |
||
477 | $this->H[$n - 1][$j] = $q * $z + $p * $this->H[$n][$j]; |
||
478 | $this->H[$n][$j] = $q * $this->H[$n][$j] - $p * $z; |
||
479 | } |
||
480 | // Column modification |
||
481 | for ($i = 0; $i <= $n; ++$i) { |
||
482 | $z = $this->H[$i][$n - 1]; |
||
483 | $this->H[$i][$n - 1] = $q * $z + $p * $this->H[$i][$n]; |
||
484 | $this->H[$i][$n] = $q * $this->H[$i][$n] - $p * $z; |
||
485 | } |
||
486 | // Accumulate transformations |
||
487 | for ($i = $low; $i <= $high; ++$i) { |
||
488 | $z = $this->V[$i][$n - 1]; |
||
489 | $this->V[$i][$n - 1] = $q * $z + $p * $this->V[$i][$n]; |
||
490 | $this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z; |
||
491 | } |
||
492 | // Complex pair |
||
493 | } else { |
||
494 | $this->d[$n - 1] = $x + $p; |
||
495 | $this->d[$n] = $x + $p; |
||
496 | $this->e[$n - 1] = $z; |
||
497 | $this->e[$n] = -$z; |
||
498 | } |
||
499 | $n = $n - 2; |
||
500 | $iter = 0; |
||
501 | // No convergence yet |
||
502 | } else { |
||
503 | // Form shift |
||
504 | $x = $this->H[$n][$n]; |
||
505 | $y = 0.0; |
||
506 | $w = 0.0; |
||
507 | if ($l < $n) { |
||
508 | $y = $this->H[$n - 1][$n - 1]; |
||
509 | $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; |
||
510 | } |
||
511 | // Wilkinson's original ad hoc shift |
||
512 | if ($iter == 10) { |
||
513 | $exshift += $x; |
||
514 | for ($i = $low; $i <= $n; ++$i) { |
||
515 | $this->H[$i][$i] -= $x; |
||
516 | } |
||
517 | $s = abs($this->H[$n][$n - 1]) + abs($this->H[$n - 1][$n - 2]); |
||
518 | $x = $y = 0.75 * $s; |
||
519 | $w = -0.4375 * $s * $s; |
||
520 | } |
||
521 | // MATLAB's new ad hoc shift |
||
522 | if ($iter == 30) { |
||
523 | $s = ($y - $x) / 2.0; |
||
524 | $s = $s * $s + $w; |
||
525 | if ($s > 0) { |
||
526 | $s = sqrt($s); |
||
527 | if ($y < $x) { |
||
528 | $s = -$s; |
||
529 | } |
||
530 | $s = $x - $w / (($y - $x) / 2.0 + $s); |
||
531 | for ($i = $low; $i <= $n; ++$i) { |
||
532 | $this->H[$i][$i] -= $s; |
||
533 | } |
||
534 | $exshift += $s; |
||
535 | $x = $y = $w = 0.964; |
||
536 | } |
||
537 | } |
||
538 | // Could check iteration count here. |
||
539 | $iter = $iter + 1; |
||
540 | // Look for two consecutive small sub-diagonal elements |
||
541 | $m = $n - 2; |
||
542 | while ($m >= $l) { |
||
543 | $z = $this->H[$m][$m]; |
||
544 | $r = $x - $z; |
||
545 | $s = $y - $z; |
||
546 | $p = ($r * $s - $w) / $this->H[$m + 1][$m] + $this->H[$m][$m + 1]; |
||
547 | $q = $this->H[$m + 1][$m + 1] - $z - $r - $s; |
||
548 | $r = $this->H[$m + 2][$m + 1]; |
||
549 | $s = abs($p) + abs($q) + abs($r); |
||
550 | $p = $p / $s; |
||
551 | $q = $q / $s; |
||
552 | $r = $r / $s; |
||
553 | if ($m == $l) { |
||
554 | break; |
||
555 | } |
||
556 | if ( |
||
557 | abs($this->H[$m][$m - 1]) * (abs($q) + abs($r)) < |
||
558 | $eps * (abs($p) * (abs($this->H[$m - 1][$m - 1]) + abs($z) + abs($this->H[$m + 1][$m + 1]))) |
||
559 | ) { |
||
560 | break; |
||
561 | } |
||
562 | --$m; |
||
563 | } |
||
564 | for ($i = $m + 2; $i <= $n; ++$i) { |
||
565 | $this->H[$i][$i - 2] = 0.0; |
||
566 | if ($i > $m + 2) { |
||
567 | $this->H[$i][$i - 3] = 0.0; |
||
568 | } |
||
569 | } |
||
570 | // Double QR step involving rows l:n and columns m:n |
||
571 | for ($k = $m; $k <= $n - 1; ++$k) { |
||
572 | $notlast = ($k != $n - 1); |
||
573 | if ($k != $m) { |
||
574 | $p = $this->H[$k][$k - 1]; |
||
575 | $q = $this->H[$k + 1][$k - 1]; |
||
576 | $r = ($notlast ? $this->H[$k + 2][$k - 1] : 0.0); |
||
577 | $x = abs($p) + abs($q) + abs($r); |
||
578 | if ($x != 0.0) { |
||
579 | $p = $p / $x; |
||
580 | $q = $q / $x; |
||
581 | $r = $r / $x; |
||
582 | } |
||
583 | } |
||
584 | if ($x == 0.0) { |
||
585 | break; |
||
586 | } |
||
587 | $s = sqrt($p * $p + $q * $q + $r * $r); |
||
588 | if ($p < 0) { |
||
589 | $s = -$s; |
||
590 | } |
||
591 | if ($s != 0) { |
||
592 | if ($k != $m) { |
||
593 | $this->H[$k][$k - 1] = -$s * $x; |
||
594 | } elseif ($l != $m) { |
||
595 | $this->H[$k][$k - 1] = -$this->H[$k][$k - 1]; |
||
596 | } |
||
597 | $p = $p + $s; |
||
598 | $x = $p / $s; |
||
599 | $y = $q / $s; |
||
600 | $z = $r / $s; |
||
601 | $q = $q / $p; |
||
602 | $r = $r / $p; |
||
603 | // Row modification |
||
604 | for ($j = $k; $j < $nn; ++$j) { |
||
605 | $p = $this->H[$k][$j] + $q * $this->H[$k + 1][$j]; |
||
606 | if ($notlast) { |
||
607 | $p = $p + $r * $this->H[$k + 2][$j]; |
||
608 | $this->H[$k + 2][$j] = $this->H[$k + 2][$j] - $p * $z; |
||
609 | } |
||
610 | $this->H[$k][$j] = $this->H[$k][$j] - $p * $x; |
||
611 | $this->H[$k + 1][$j] = $this->H[$k + 1][$j] - $p * $y; |
||
612 | } |
||
613 | // Column modification |
||
614 | $iMax = min($n, $k + 3); |
||
615 | for ($i = 0; $i <= $iMax; ++$i) { |
||
616 | $p = $x * $this->H[$i][$k] + $y * $this->H[$i][$k + 1]; |
||
617 | if ($notlast) { |
||
618 | $p = $p + $z * $this->H[$i][$k + 2]; |
||
619 | $this->H[$i][$k + 2] = $this->H[$i][$k + 2] - $p * $r; |
||
620 | } |
||
621 | $this->H[$i][$k] = $this->H[$i][$k] - $p; |
||
622 | $this->H[$i][$k + 1] = $this->H[$i][$k + 1] - $p * $q; |
||
623 | } |
||
624 | // Accumulate transformations |
||
625 | for ($i = $low; $i <= $high; ++$i) { |
||
626 | $p = $x * $this->V[$i][$k] + $y * $this->V[$i][$k + 1]; |
||
627 | if ($notlast) { |
||
628 | $p = $p + $z * $this->V[$i][$k + 2]; |
||
629 | $this->V[$i][$k + 2] = $this->V[$i][$k + 2] - $p * $r; |
||
630 | } |
||
631 | $this->V[$i][$k] = $this->V[$i][$k] - $p; |
||
632 | $this->V[$i][$k + 1] = $this->V[$i][$k + 1] - $p * $q; |
||
633 | } |
||
634 | } // ($s != 0) |
||
635 | } // k loop |
||
636 | } // check convergence |
||
637 | } // while ($n >= $low) |
||
638 | |||
639 | // Backsubstitute to find vectors of upper triangular form |
||
640 | if ($norm == 0.0) { |
||
641 | return; |
||
642 | } |
||
643 | |||
644 | for ($n = $nn - 1; $n >= 0; --$n) { |
||
645 | $p = $this->d[$n]; |
||
646 | $q = $this->e[$n]; |
||
647 | // Real vector |
||
648 | if ($q == 0) { |
||
649 | $l = $n; |
||
650 | $this->H[$n][$n] = 1.0; |
||
651 | for ($i = $n - 1; $i >= 0; --$i) { |
||
652 | $w = $this->H[$i][$i] - $p; |
||
653 | $r = 0.0; |
||
654 | for ($j = $l; $j <= $n; ++$j) { |
||
655 | $r = $r + $this->H[$i][$j] * $this->H[$j][$n]; |
||
656 | } |
||
657 | if ($this->e[$i] < 0.0) { |
||
658 | $z = $w; |
||
659 | $s = $r; |
||
660 | } else { |
||
661 | $l = $i; |
||
662 | if ($this->e[$i] == 0.0) { |
||
663 | if ($w != 0.0) { |
||
664 | $this->H[$i][$n] = -$r / $w; |
||
665 | } else { |
||
666 | $this->H[$i][$n] = -$r / ($eps * $norm); |
||
667 | } |
||
668 | // Solve real equations |
||
669 | } else { |
||
670 | $x = $this->H[$i][$i + 1]; |
||
671 | $y = $this->H[$i + 1][$i]; |
||
672 | $q = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i]; |
||
673 | $t = ($x * $s - $z * $r) / $q; |
||
674 | $this->H[$i][$n] = $t; |
||
675 | if (abs($x) > abs($z)) { |
||
676 | $this->H[$i + 1][$n] = (-$r - $w * $t) / $x; |
||
677 | } else { |
||
678 | $this->H[$i + 1][$n] = (-$s - $y * $t) / $z; |
||
679 | } |
||
680 | } |
||
681 | // Overflow control |
||
682 | $t = abs($this->H[$i][$n]); |
||
683 | if (($eps * $t) * $t > 1) { |
||
684 | for ($j = $i; $j <= $n; ++$j) { |
||
685 | $this->H[$j][$n] = $this->H[$j][$n] / $t; |
||
686 | } |
||
687 | } |
||
688 | } |
||
689 | } |
||
690 | // Complex vector |
||
691 | } elseif ($q < 0) { |
||
692 | $l = $n - 1; |
||
693 | // Last vector component imaginary so matrix is triangular |
||
694 | if (abs($this->H[$n][$n - 1]) > abs($this->H[$n - 1][$n])) { |
||
695 | $this->H[$n - 1][$n - 1] = $q / $this->H[$n][$n - 1]; |
||
696 | $this->H[$n - 1][$n] = -($this->H[$n][$n] - $p) / $this->H[$n][$n - 1]; |
||
697 | } else { |
||
698 | $this->cdiv(0.0, -$this->H[$n - 1][$n], $this->H[$n - 1][$n - 1] - $p, $q); |
||
699 | $this->H[$n - 1][$n - 1] = $this->cdivr; |
||
700 | $this->H[$n - 1][$n] = $this->cdivi; |
||
701 | } |
||
702 | $this->H[$n][$n - 1] = 0.0; |
||
703 | $this->H[$n][$n] = 1.0; |
||
704 | for ($i = $n - 2; $i >= 0; --$i) { |
||
705 | // double ra,sa,vr,vi; |
||
706 | $ra = 0.0; |
||
707 | $sa = 0.0; |
||
708 | for ($j = $l; $j <= $n; ++$j) { |
||
709 | $ra = $ra + $this->H[$i][$j] * $this->H[$j][$n - 1]; |
||
710 | $sa = $sa + $this->H[$i][$j] * $this->H[$j][$n]; |
||
711 | } |
||
712 | $w = $this->H[$i][$i] - $p; |
||
713 | if ($this->e[$i] < 0.0) { |
||
714 | $z = $w; |
||
715 | $r = $ra; |
||
716 | $s = $sa; |
||
717 | } else { |
||
718 | $l = $i; |
||
719 | if ($this->e[$i] == 0) { |
||
720 | $this->cdiv(-$ra, -$sa, $w, $q); |
||
721 | $this->H[$i][$n - 1] = $this->cdivr; |
||
722 | $this->H[$i][$n] = $this->cdivi; |
||
723 | } else { |
||
724 | // Solve complex equations |
||
725 | $x = $this->H[$i][$i + 1]; |
||
726 | $y = $this->H[$i + 1][$i]; |
||
727 | $vr = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i] - $q * $q; |
||
728 | $vi = ($this->d[$i] - $p) * 2.0 * $q; |
||
729 | if ($vr == 0.0 & $vi == 0.0) { |
||
730 | $vr = $eps * $norm * (abs($w) + abs($q) + abs($x) + abs($y) + abs($z)); |
||
731 | } |
||
732 | $this->cdiv($x * $r - $z * $ra + $q * $sa, $x * $s - $z * $sa - $q * $ra, $vr, $vi); |
||
733 | $this->H[$i][$n - 1] = $this->cdivr; |
||
734 | $this->H[$i][$n] = $this->cdivi; |
||
735 | if (abs($x) > (abs($z) + abs($q))) { |
||
736 | $this->H[$i + 1][$n - 1] = (-$ra - $w * $this->H[$i][$n - 1] + $q * $this->H[$i][$n]) / $x; |
||
737 | $this->H[$i + 1][$n] = (-$sa - $w * $this->H[$i][$n] - $q * $this->H[$i][$n - 1]) / $x; |
||
738 | } else { |
||
739 | $this->cdiv(-$r - $y * $this->H[$i][$n - 1], -$s - $y * $this->H[$i][$n], $z, $q); |
||
740 | $this->H[$i + 1][$n - 1] = $this->cdivr; |
||
741 | $this->H[$i + 1][$n] = $this->cdivi; |
||
742 | } |
||
743 | } |
||
744 | // Overflow control |
||
745 | $t = max(abs($this->H[$i][$n - 1]), abs($this->H[$i][$n])); |
||
746 | if (($eps * $t) * $t > 1) { |
||
747 | for ($j = $i; $j <= $n; ++$j) { |
||
748 | $this->H[$j][$n - 1] = $this->H[$j][$n - 1] / $t; |
||
749 | $this->H[$j][$n] = $this->H[$j][$n] / $t; |
||
750 | } |
||
751 | } |
||
752 | } // end else |
||
753 | } // end for |
||
754 | } // end else for complex case |
||
755 | } // end for |
||
756 | |||
757 | // Vectors of isolated roots |
||
758 | for ($i = 0; $i < $nn; ++$i) { |
||
759 | if ($i < $low | $i > $high) { |
||
760 | for ($j = $i; $j < $nn; ++$j) { |
||
761 | $this->V[$i][$j] = $this->H[$i][$j]; |
||
762 | } |
||
763 | } |
||
764 | } |
||
765 | |||
766 | // Back transformation to get eigenvectors of original matrix |
||
767 | for ($j = $nn - 1; $j >= $low; --$j) { |
||
768 | for ($i = $low; $i <= $high; ++$i) { |
||
769 | $z = 0.0; |
||
770 | $kMax = min($j, $high); |
||
771 | for ($k = $low; $k <= $kMax; ++$k) { |
||
772 | $z = $z + $this->V[$i][$k] * $this->H[$k][$j]; |
||
773 | } |
||
774 | $this->V[$i][$j] = $z; |
||
775 | } |
||
864 |