| Conditions | 71 |
| Paths | > 20000 |
| Total Lines | 414 |
| Code Lines | 270 |
| Lines | 48 |
| Ratio | 11.59 % |
| 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 |
||
| 551 | private function hqr2(): void |
||
| 552 | { |
||
| 553 | // Initialize |
||
| 554 | $nn = $this->n; |
||
| 555 | $n = $nn - 1; |
||
| 556 | $low = 0; |
||
| 557 | $high = $nn - 1; |
||
| 558 | $eps = pow(2.0, -52.0); |
||
| 559 | $exshift = 0.0; |
||
| 560 | $p = $q = $r = $s = $z = 0; |
||
| 561 | // Store roots isolated by balanc and compute matrix norm |
||
| 562 | $norm = 0.0; |
||
| 563 | |||
| 564 | for ($i = 0; $i < $nn; ++$i) { |
||
| 565 | if (($i < $low) or ($i > $high)) { |
||
| 566 | $this->d[$i] = $this->H[$i][$i]; |
||
| 567 | $this->e[$i] = 0.0; |
||
| 568 | } |
||
| 569 | |||
| 570 | for ($j = max($i - 1, 0); $j < $nn; ++$j) { |
||
| 571 | $norm = $norm + abs($this->H[$i][$j]); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | // Outer loop over eigenvalue index |
||
| 576 | $iter = 0; |
||
| 577 | while ($n >= $low) { |
||
| 578 | // Look for single small sub-diagonal element |
||
| 579 | $l = $n; |
||
| 580 | while ($l > $low) { |
||
| 581 | $s = abs($this->H[$l - 1][$l - 1]) + abs($this->H[$l][$l]); |
||
| 582 | if ($s == 0.0) { |
||
| 583 | $s = $norm; |
||
| 584 | } |
||
| 585 | |||
| 586 | if (abs($this->H[$l][$l - 1]) < $eps * $s) { |
||
| 587 | break; |
||
| 588 | } |
||
| 589 | |||
| 590 | --$l; |
||
| 591 | } |
||
| 592 | |||
| 593 | // Check for convergence |
||
| 594 | // One root found |
||
| 595 | if ($l == $n) { |
||
| 596 | $this->H[$n][$n] = $this->H[$n][$n] + $exshift; |
||
| 597 | $this->d[$n] = $this->H[$n][$n]; |
||
| 598 | $this->e[$n] = 0.0; |
||
| 599 | --$n; |
||
| 600 | $iter = 0; |
||
| 601 | // Two roots found |
||
| 602 | } elseif ($l == $n - 1) { |
||
| 603 | $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; |
||
| 604 | $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; |
||
| 605 | $q = $p * $p + $w; |
||
| 606 | $z = sqrt(abs($q)); |
||
| 607 | $this->H[$n][$n] = $this->H[$n][$n] + $exshift; |
||
| 608 | $this->H[$n - 1][$n - 1] = $this->H[$n - 1][$n - 1] + $exshift; |
||
| 609 | $x = $this->H[$n][$n]; |
||
| 610 | // Real pair |
||
| 611 | if ($q >= 0) { |
||
| 612 | if ($p >= 0) { |
||
| 613 | $z = $p + $z; |
||
| 614 | } else { |
||
| 615 | $z = $p - $z; |
||
| 616 | } |
||
| 617 | |||
| 618 | $this->d[$n - 1] = $x + $z; |
||
| 619 | $this->d[$n] = $this->d[$n - 1]; |
||
| 620 | if ($z != 0.0) { |
||
| 621 | $this->d[$n] = $x - $w / $z; |
||
| 622 | } |
||
| 623 | |||
| 624 | $this->e[$n - 1] = 0.0; |
||
| 625 | $this->e[$n] = 0.0; |
||
| 626 | $x = $this->H[$n][$n - 1]; |
||
| 627 | $s = abs($x) + abs($z); |
||
| 628 | $p = $x / $s; |
||
| 629 | $q = $z / $s; |
||
| 630 | $r = sqrt($p * $p + $q * $q); |
||
| 631 | $p = $p / $r; |
||
| 632 | $q = $q / $r; |
||
| 633 | // Row modification |
||
| 634 | for ($j = $n - 1; $j < $nn; ++$j) { |
||
| 635 | $z = $this->H[$n - 1][$j]; |
||
| 636 | $this->H[$n - 1][$j] = $q * $z + $p * $this->H[$n][$j]; |
||
| 637 | $this->H[$n][$j] = $q * $this->H[$n][$j] - $p * $z; |
||
| 638 | } |
||
| 639 | |||
| 640 | // Column modification |
||
| 641 | for ($i = 0; $i <= $n; ++$i) { |
||
| 642 | $z = $this->H[$i][$n - 1]; |
||
| 643 | $this->H[$i][$n - 1] = $q * $z + $p * $this->H[$i][$n]; |
||
| 644 | $this->H[$i][$n] = $q * $this->H[$i][$n] - $p * $z; |
||
| 645 | } |
||
| 646 | |||
| 647 | // Accumulate transformations |
||
| 648 | for ($i = $low; $i <= $high; ++$i) { |
||
| 649 | $z = $this->V[$i][$n - 1]; |
||
| 650 | $this->V[$i][$n - 1] = $q * $z + $p * $this->V[$i][$n]; |
||
| 651 | $this->V[$i][$n] = $q * $this->V[$i][$n] - $p * $z; |
||
| 652 | } |
||
| 653 | |||
| 654 | // Complex pair |
||
| 655 | } else { |
||
| 656 | $this->d[$n - 1] = $x + $p; |
||
| 657 | $this->d[$n] = $x + $p; |
||
| 658 | $this->e[$n - 1] = $z; |
||
| 659 | $this->e[$n] = -$z; |
||
| 660 | } |
||
| 661 | |||
| 662 | $n = $n - 2; |
||
| 663 | $iter = 0; |
||
| 664 | // No convergence yet |
||
| 665 | } else { |
||
| 666 | // Form shift |
||
| 667 | $x = $this->H[$n][$n]; |
||
| 668 | $y = 0.0; |
||
| 669 | $w = 0.0; |
||
| 670 | if ($l < $n) { |
||
| 671 | $y = $this->H[$n - 1][$n - 1]; |
||
| 672 | $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; |
||
| 673 | } |
||
| 674 | |||
| 675 | // Wilkinson's original ad hoc shift |
||
| 676 | if ($iter == 10) { |
||
| 677 | $exshift += $x; |
||
| 678 | for ($i = $low; $i <= $n; ++$i) { |
||
| 679 | $this->H[$i][$i] -= $x; |
||
| 680 | } |
||
| 681 | |||
| 682 | $s = abs($this->H[$n][$n - 1]) + abs($this->H[$n - 1][$n - 2]); |
||
| 683 | $x = $y = 0.75 * $s; |
||
| 684 | $w = -0.4375 * $s * $s; |
||
| 685 | } |
||
| 686 | |||
| 687 | // MATLAB's new ad hoc shift |
||
| 688 | if ($iter == 30) { |
||
| 689 | $s = ($y - $x) / 2.0; |
||
| 690 | $s = $s * $s + $w; |
||
| 691 | if ($s > 0) { |
||
| 692 | $s = sqrt($s); |
||
| 693 | if ($y < $x) { |
||
| 694 | $s = -$s; |
||
| 695 | } |
||
| 696 | |||
| 697 | $s = $x - $w / (($y - $x) / 2.0 + $s); |
||
| 698 | for ($i = $low; $i <= $n; ++$i) { |
||
| 699 | $this->H[$i][$i] -= $s; |
||
| 700 | } |
||
| 701 | |||
| 702 | $exshift += $s; |
||
| 703 | $x = $y = $w = 0.964; |
||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | // Could check iteration count here. |
||
| 708 | $iter = $iter + 1; |
||
| 709 | // Look for two consecutive small sub-diagonal elements |
||
| 710 | $m = $n - 2; |
||
| 711 | while ($m >= $l) { |
||
| 712 | $z = $this->H[$m][$m]; |
||
| 713 | $r = $x - $z; |
||
| 714 | $s = $y - $z; |
||
| 715 | $p = ($r * $s - $w) / $this->H[$m + 1][$m] + $this->H[$m][$m + 1]; |
||
| 716 | $q = $this->H[$m + 1][$m + 1] - $z - $r - $s; |
||
| 717 | $r = $this->H[$m + 2][$m + 1]; |
||
| 718 | $s = abs($p) + abs($q) + abs($r); |
||
| 719 | $p = $p / $s; |
||
| 720 | $q = $q / $s; |
||
| 721 | $r = $r / $s; |
||
| 722 | if ($m == $l) { |
||
| 723 | break; |
||
| 724 | } |
||
| 725 | |||
| 726 | if (abs($this->H[$m][$m - 1]) * (abs($q) + abs($r)) < |
||
| 727 | $eps * (abs($p) * (abs($this->H[$m - 1][$m - 1]) + abs($z) + abs($this->H[$m + 1][$m + 1])))) { |
||
| 728 | break; |
||
| 729 | } |
||
| 730 | |||
| 731 | --$m; |
||
| 732 | } |
||
| 733 | |||
| 734 | for ($i = $m + 2; $i <= $n; ++$i) { |
||
| 735 | $this->H[$i][$i - 2] = 0.0; |
||
| 736 | if ($i > $m + 2) { |
||
| 737 | $this->H[$i][$i - 3] = 0.0; |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | // Double QR step involving rows l:n and columns m:n |
||
| 742 | for ($k = $m; $k <= $n - 1; ++$k) { |
||
| 743 | $notlast = ($k != $n - 1); |
||
| 744 | if ($k != $m) { |
||
| 745 | $p = $this->H[$k][$k - 1]; |
||
| 746 | $q = $this->H[$k + 1][$k - 1]; |
||
| 747 | $r = ($notlast ? $this->H[$k + 2][$k - 1] : 0.0); |
||
| 748 | $x = abs($p) + abs($q) + abs($r); |
||
| 749 | if ($x != 0.0) { |
||
| 750 | $p = $p / $x; |
||
| 751 | $q = $q / $x; |
||
| 752 | $r = $r / $x; |
||
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | if ($x == 0.0) { |
||
| 757 | break; |
||
| 758 | } |
||
| 759 | |||
| 760 | $s = sqrt($p * $p + $q * $q + $r * $r); |
||
| 761 | if ($p < 0) { |
||
| 762 | $s = -$s; |
||
| 763 | } |
||
| 764 | |||
| 765 | if ($s != 0) { |
||
| 766 | if ($k != $m) { |
||
| 767 | $this->H[$k][$k - 1] = -$s * $x; |
||
| 768 | } elseif ($l != $m) { |
||
| 769 | $this->H[$k][$k - 1] = -$this->H[$k][$k - 1]; |
||
| 770 | } |
||
| 771 | |||
| 772 | $p = $p + $s; |
||
| 773 | $x = $p / $s; |
||
| 774 | $y = $q / $s; |
||
| 775 | $z = $r / $s; |
||
| 776 | $q = $q / $p; |
||
| 777 | $r = $r / $p; |
||
| 778 | // Row modification |
||
| 779 | for ($j = $k; $j < $nn; ++$j) { |
||
| 780 | $p = $this->H[$k][$j] + $q * $this->H[$k + 1][$j]; |
||
| 781 | if ($notlast) { |
||
| 782 | $p = $p + $r * $this->H[$k + 2][$j]; |
||
| 783 | $this->H[$k + 2][$j] = $this->H[$k + 2][$j] - $p * $z; |
||
| 784 | } |
||
| 785 | |||
| 786 | $this->H[$k][$j] = $this->H[$k][$j] - $p * $x; |
||
| 787 | $this->H[$k + 1][$j] = $this->H[$k + 1][$j] - $p * $y; |
||
| 788 | } |
||
| 789 | |||
| 790 | // Column modification |
||
| 791 | for ($i = 0; $i <= min($n, $k + 3); ++$i) { |
||
| 792 | $p = $x * $this->H[$i][$k] + $y * $this->H[$i][$k + 1]; |
||
| 793 | if ($notlast) { |
||
| 794 | $p = $p + $z * $this->H[$i][$k + 2]; |
||
| 795 | $this->H[$i][$k + 2] = $this->H[$i][$k + 2] - $p * $r; |
||
| 796 | } |
||
| 797 | |||
| 798 | $this->H[$i][$k] = $this->H[$i][$k] - $p; |
||
| 799 | $this->H[$i][$k + 1] = $this->H[$i][$k + 1] - $p * $q; |
||
| 800 | } |
||
| 801 | |||
| 802 | // Accumulate transformations |
||
| 803 | for ($i = $low; $i <= $high; ++$i) { |
||
| 804 | $p = $x * $this->V[$i][$k] + $y * $this->V[$i][$k + 1]; |
||
| 805 | if ($notlast) { |
||
| 806 | $p = $p + $z * $this->V[$i][$k + 2]; |
||
| 807 | $this->V[$i][$k + 2] = $this->V[$i][$k + 2] - $p * $r; |
||
| 808 | } |
||
| 809 | |||
| 810 | $this->V[$i][$k] = $this->V[$i][$k] - $p; |
||
| 811 | $this->V[$i][$k + 1] = $this->V[$i][$k + 1] - $p * $q; |
||
| 812 | } |
||
| 813 | } // ($s != 0) |
||
| 814 | } // k loop |
||
| 815 | } // check convergence |
||
| 816 | } // while ($n >= $low) |
||
| 817 | |||
| 818 | // Backsubstitute to find vectors of upper triangular form |
||
| 819 | if ($norm == 0.0) { |
||
| 820 | return; |
||
| 821 | } |
||
| 822 | |||
| 823 | for ($n = $nn - 1; $n >= 0; --$n) { |
||
| 824 | $p = $this->d[$n]; |
||
| 825 | $q = $this->e[$n]; |
||
| 826 | // Real vector |
||
| 827 | if ($q == 0) { |
||
| 828 | $l = $n; |
||
| 829 | $this->H[$n][$n] = 1.0; |
||
| 830 | for ($i = $n - 1; $i >= 0; --$i) { |
||
| 831 | $w = $this->H[$i][$i] - $p; |
||
| 832 | $r = 0.0; |
||
| 833 | for ($j = $l; $j <= $n; ++$j) { |
||
| 834 | $r = $r + $this->H[$i][$j] * $this->H[$j][$n]; |
||
| 835 | } |
||
| 836 | |||
| 837 | if ($this->e[$i] < 0.0) { |
||
| 838 | $z = $w; |
||
| 839 | $s = $r; |
||
| 840 | } else { |
||
| 841 | $l = $i; |
||
| 842 | if ($this->e[$i] == 0.0) { |
||
| 843 | if ($w != 0.0) { |
||
| 844 | $this->H[$i][$n] = -$r / $w; |
||
| 845 | } else { |
||
| 846 | $this->H[$i][$n] = -$r / ($eps * $norm); |
||
| 847 | } |
||
| 848 | |||
| 849 | // Solve real equations |
||
| 850 | } else { |
||
| 851 | $x = $this->H[$i][$i + 1]; |
||
| 852 | $y = $this->H[$i + 1][$i]; |
||
| 853 | $q = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i]; |
||
| 854 | $t = ($x * $s - $z * $r) / $q; |
||
| 855 | $this->H[$i][$n] = $t; |
||
| 856 | if (abs($x) > abs($z)) { |
||
| 857 | $this->H[$i + 1][$n] = (-$r - $w * $t) / $x; |
||
| 858 | } else { |
||
| 859 | $this->H[$i + 1][$n] = (-$s - $y * $t) / $z; |
||
| 860 | } |
||
| 861 | } |
||
| 862 | |||
| 863 | // Overflow control |
||
| 864 | $t = abs($this->H[$i][$n]); |
||
| 865 | if (($eps * $t) * $t > 1) { |
||
| 866 | for ($j = $i; $j <= $n; ++$j) { |
||
| 867 | $this->H[$j][$n] = $this->H[$j][$n] / $t; |
||
| 868 | } |
||
| 869 | } |
||
| 870 | } |
||
| 871 | } |
||
| 872 | |||
| 873 | // Complex vector |
||
| 874 | } elseif ($q < 0) { |
||
| 875 | $l = $n - 1; |
||
| 876 | // Last vector component imaginary so matrix is triangular |
||
| 877 | if (abs($this->H[$n][$n - 1]) > abs($this->H[$n - 1][$n])) { |
||
| 878 | $this->H[$n - 1][$n - 1] = $q / $this->H[$n][$n - 1]; |
||
| 879 | $this->H[$n - 1][$n] = -($this->H[$n][$n] - $p) / $this->H[$n][$n - 1]; |
||
| 880 | } else { |
||
| 881 | $this->cdiv(0.0, -$this->H[$n - 1][$n], $this->H[$n - 1][$n - 1] - $p, $q); |
||
| 882 | $this->H[$n - 1][$n - 1] = $this->cdivr; |
||
| 883 | $this->H[$n - 1][$n] = $this->cdivi; |
||
| 884 | } |
||
| 885 | |||
| 886 | $this->H[$n][$n - 1] = 0.0; |
||
| 887 | $this->H[$n][$n] = 1.0; |
||
| 888 | for ($i = $n - 2; $i >= 0; --$i) { |
||
| 889 | // double ra,sa,vr,vi; |
||
| 890 | $ra = 0.0; |
||
| 891 | $sa = 0.0; |
||
| 892 | for ($j = $l; $j <= $n; ++$j) { |
||
| 893 | $ra = $ra + $this->H[$i][$j] * $this->H[$j][$n - 1]; |
||
| 894 | $sa = $sa + $this->H[$i][$j] * $this->H[$j][$n]; |
||
| 895 | } |
||
| 896 | |||
| 897 | $w = $this->H[$i][$i] - $p; |
||
| 898 | if ($this->e[$i] < 0.0) { |
||
| 899 | $z = $w; |
||
| 900 | $r = $ra; |
||
| 901 | $s = $sa; |
||
| 902 | } else { |
||
| 903 | $l = $i; |
||
| 904 | if ($this->e[$i] == 0) { |
||
| 905 | $this->cdiv(-$ra, -$sa, $w, $q); |
||
| 906 | $this->H[$i][$n - 1] = $this->cdivr; |
||
| 907 | $this->H[$i][$n] = $this->cdivi; |
||
| 908 | } else { |
||
| 909 | // Solve complex equations |
||
| 910 | $x = $this->H[$i][$i + 1]; |
||
| 911 | $y = $this->H[$i + 1][$i]; |
||
| 912 | $vr = ($this->d[$i] - $p) * ($this->d[$i] - $p) + $this->e[$i] * $this->e[$i] - $q * $q; |
||
| 913 | $vi = ($this->d[$i] - $p) * 2.0 * $q; |
||
| 914 | if ($vr == 0.0 & $vi == 0.0) { |
||
| 915 | $vr = $eps * $norm * (abs($w) + abs($q) + abs($x) + abs($y) + abs($z)); |
||
| 916 | } |
||
| 917 | |||
| 918 | $this->cdiv($x * $r - $z * $ra + $q * $sa, $x * $s - $z * $sa - $q * $ra, $vr, $vi); |
||
| 919 | $this->H[$i][$n - 1] = $this->cdivr; |
||
| 920 | $this->H[$i][$n] = $this->cdivi; |
||
| 921 | if (abs($x) > (abs($z) + abs($q))) { |
||
| 922 | $this->H[$i + 1][$n - 1] = (-$ra - $w * $this->H[$i][$n - 1] + $q * $this->H[$i][$n]) / $x; |
||
| 923 | $this->H[$i + 1][$n] = (-$sa - $w * $this->H[$i][$n] - $q * $this->H[$i][$n - 1]) / $x; |
||
| 924 | } else { |
||
| 925 | $this->cdiv(-$r - $y * $this->H[$i][$n - 1], -$s - $y * $this->H[$i][$n], $z, $q); |
||
| 926 | $this->H[$i + 1][$n - 1] = $this->cdivr; |
||
| 927 | $this->H[$i + 1][$n] = $this->cdivi; |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | // Overflow control |
||
| 932 | $t = max(abs($this->H[$i][$n - 1]), abs($this->H[$i][$n])); |
||
| 933 | if (($eps * $t) * $t > 1) { |
||
| 934 | for ($j = $i; $j <= $n; ++$j) { |
||
| 935 | $this->H[$j][$n - 1] = $this->H[$j][$n - 1] / $t; |
||
| 936 | $this->H[$j][$n] = $this->H[$j][$n] / $t; |
||
| 937 | } |
||
| 938 | } |
||
| 939 | } // end else |
||
| 940 | } // end for |
||
| 941 | } // end else for complex case |
||
| 942 | } // end for |
||
| 943 | |||
| 944 | // Vectors of isolated roots |
||
| 945 | for ($i = 0; $i < $nn; ++$i) { |
||
| 946 | if ($i < $low | $i > $high) { |
||
| 947 | for ($j = $i; $j < $nn; ++$j) { |
||
| 948 | $this->V[$i][$j] = $this->H[$i][$j]; |
||
| 949 | } |
||
| 950 | } |
||
| 951 | } |
||
| 952 | |||
| 953 | // Back transformation to get eigenvectors of original matrix |
||
| 954 | for ($j = $nn - 1; $j >= $low; --$j) { |
||
| 955 | for ($i = $low; $i <= $high; ++$i) { |
||
| 956 | $z = 0.0; |
||
| 957 | for ($k = $low; $k <= min($j, $high); ++$k) { |
||
| 958 | $z = $z + $this->V[$i][$k] * $this->H[$k][$j]; |
||
| 959 | } |
||
| 960 | |||
| 961 | $this->V[$i][$j] = $z; |
||
| 962 | } |
||
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 |