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