|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MathPHP\LinearAlgebra\Decomposition; |
| 4 | + |
| 5 | +use MathPHP\Exception; |
| 6 | +use MathPHP\Functions\Support; |
| 7 | +use MathPHP\LinearAlgebra\Householder; |
| 8 | +use MathPHP\LinearAlgebra\NumericMatrix; |
| 9 | +use MathPHP\LinearAlgebra\MatrixFactory; |
| 10 | + |
| 11 | +/** |
| 12 | + * Hessenberg Decomposition |
| 13 | + * |
| 14 | + * A = QHQ* |
| 15 | + * |
| 16 | + * Where: |
| 17 | + * - Q is an orthogonal matrix |
| 18 | + * - H is an upper Hessenberg matrix (zeros below the first subdiagonal) |
| 19 | + * - Q* is the conjugate transpose of Q |
| 20 | + * |
| 21 | + * The Hessenberg decomposition is useful as a preprocessing step for eigenvalue algorithms, |
| 22 | + * as it preserves eigenvalues while reducing the matrix to a form that is easier to work with. |
| 23 | + * |
| 24 | + * @property-read NumericMatrix $Q Orthogonal transformation matrix |
| 25 | + * @property-read NumericMatrix $H Upper Hessenberg matrix |
| 26 | + */ |
| 27 | +class Hessenberg extends Decomposition |
| 28 | +{ |
| 29 | + /** @var NumericMatrix Orthogonal transformation matrix */ |
| 30 | + private $Q; |
| 31 | + |
| 32 | + /** @var NumericMatrix Upper Hessenberg matrix */ |
| 33 | + private $H; |
| 34 | + |
| 35 | + /** |
| 36 | + * Hessenberg constructor |
| 37 | + * |
| 38 | + * @param NumericMatrix $Q Orthogonal transformation matrix |
| 39 | + * @param NumericMatrix $H Upper Hessenberg matrix |
| 40 | + */ |
| 41 | + private function __construct(NumericMatrix $Q, NumericMatrix $H) |
| 42 | + { |
| 43 | + $this->Q = $Q; |
| 44 | + $this->H = $H; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Decompose a matrix into Hessenberg form using Householder reflections |
| 49 | + * Factory method to create Hessenberg objects. |
| 50 | + * |
| 51 | + * A = QHQ* |
| 52 | + * |
| 53 | + * Where: |
| 54 | + * - Q is an orthogonal matrix |
| 55 | + * - H is an upper Hessenberg matrix (zeros below the first subdiagonal) |
| 56 | + * - Q* is the conjugate transpose of Q |
| 57 | + * |
| 58 | + * Algorithm: |
| 59 | + * - Uses Householder reflections to systematically zero out elements below the first subdiagonal |
| 60 | + * - Each reflection preserves eigenvalues through similarity transformations |
| 61 | + * - Requires n-2 Householder transformations for an n×n matrix |
| 62 | + * |
| 63 | + * Numerical Stability: |
| 64 | + * - Uses consistent error tolerance throughout the decomposition process |
| 65 | + * - Householder transformations use the input matrix's error tolerance for consistent behavior |
| 66 | + * - The algorithm maintains numerical stability through orthogonal similarity transformations |
| 67 | + * |
| 68 | + * @param NumericMatrix $A Matrix to decompose |
| 69 | + * |
| 70 | + * @return Hessenberg |
| 71 | + * |
| 72 | + * @throws Exception\BadDataException |
| 73 | + * @throws Exception\IncorrectTypeException |
| 74 | + * @throws Exception\MathException |
| 75 | + * @throws Exception\MatrixException |
| 76 | + * @throws Exception\OutOfBoundsException |
| 77 | + * @throws Exception\VectorException |
| 78 | + */ |
| 79 | + public static function decompose(NumericMatrix $A): Hessenberg |
| 80 | + { |
| 81 | + if (!$A->isSquare()) { |
| 82 | + throw new Exception\MatrixException('Hessenberg decomposition only works on square matrices'); |
| 83 | + } |
| 84 | + |
| 85 | + $n = $A->getN(); |
| 86 | + $ε = $A->getError(); |
| 87 | + |
| 88 | + // For 1x1 matrices, return trivial decomposition |
| 89 | + if ($n < 2) { |
| 90 | + $Q = MatrixFactory::identity(1); |
| 91 | + $H = clone $A; |
| 92 | + $Q->setError($ε); |
| 93 | + $H->setError($ε); |
| 94 | + return new Hessenberg($Q, $H); |
| 95 | + } |
| 96 | + |
| 97 | + // Start with copies of the input matrix and identity matrix |
| 98 | + $H = MatrixFactory::createNumeric($A->getMatrix()); |
| 99 | + $Q = MatrixFactory::identity($n); |
| 100 | + |
| 101 | + // Apply Householder transformations to columns 0 through n-3 |
| 102 | + // (we need n-2 transformations total for an n×n matrix) |
| 103 | + for ($k = 0; $k < $n - 2; $k++) { |
| 104 | + // Extract the submatrix starting from row k+1, column k |
| 105 | + $subH = $H->submatrix($k + 1, $k, $n - 1, $k); |
| 106 | + |
| 107 | + // Skip if the column below the diagonal is already effectively zero |
| 108 | + if (Support::isZero($subH->frobeniusNorm(), $ε)) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + // Create Householder transformation for this column |
| 113 | + $reflector = Householder::transform($subH, $ε); |
| 114 | + |
| 115 | + // Embed the Householder matrix in a full-size identity matrix |
| 116 | + $fullReflector = self::embedHouseholderMatrix($reflector, $n, $k + 1); |
| 117 | + |
| 118 | + // Apply the transformation: H = QHQ' |
| 119 | + $H = $fullReflector->multiply($H)->multiply($fullReflector->transpose()); |
| 120 | + |
| 121 | + // Accumulate the orthogonal transformations: Q = QQ' |
| 122 | + $Q = $Q->multiply($fullReflector->transpose()); |
| 123 | + } |
| 124 | + |
| 125 | + // Propagate error tolerance to result matrices |
| 126 | + $Q->setError($ε); |
| 127 | + $H->setError($ε); |
| 128 | + |
| 129 | + return new Hessenberg($Q, $H); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Embed a Householder transformation matrix into a full-size identity matrix |
| 134 | + * |
| 135 | + * The Householder matrix is placed starting at the specified offset in both row and column dimensions. |
| 136 | + * |
| 137 | + * The implementation used is not the most efficient, |
| 138 | + * but it helps us visualize what it is, using matrix operations. |
| 139 | + * We prefer clarity and simplicity over performance. |
| 140 | + * |
| 141 | + * An alternative implementation would copy Householder matrix elements |
| 142 | + * to the appropriate position in identity matrix in a nested loop. |
| 143 | + * It is more efficient, but harder to visualize what the transformation is. |
| 144 | + * |
| 145 | + * $I = MatrixFactory::identity($n)->getMatrix(); |
| 146 | + * |
| 147 | + * for ($i = 0; $i < $householder->getM(); $i++) { |
| 148 | + * for ($j = 0; $j < $householder->getN(); $j++) { |
| 149 | + * $I[$offset + $i][$offset + $j] = $householder[$i][$j]; |
| 150 | + *. } |
| 151 | + * } |
| 152 | + * return MatrixFactory::createNumeric($I); |
| 153 | + * |
| 154 | + * @param NumericMatrix $householder The Householder transformation matrix |
| 155 | + * @param int $n The size of the full identity matrix |
| 156 | + * @param int $offset The starting position to embed the Householder matrix |
| 157 | + * |
| 158 | + * @return NumericMatrix Full-size matrix with embedded Householder transformation |
| 159 | + * |
| 160 | + * @throws Exception\MatrixException |
| 161 | + * @throws Exception\IncorrectTypeException |
| 162 | + */ |
| 163 | + private static function embedHouseholderMatrix(NumericMatrix $householder, int $n, int $offset): NumericMatrix |
| 164 | + { |
| 165 | + $I = MatrixFactory::identity($offset); |
| 166 | + |
| 167 | + // Create the top block: [ I | 0 ] |
| 168 | + $topBlock = $I->augment(MatrixFactory::zero($offset, $n - $offset)); |
| 169 | + |
| 170 | + // Create the bottom block: [ 0 | H ] |
| 171 | + $bottomBlock = MatrixFactory::zero($n - $offset, $offset)->augment($householder); |
| 172 | + |
| 173 | + // Stack them vertically: [ topBlock ] |
| 174 | + // [ bottomBlock ] |
| 175 | + return $topBlock->augmentBelow($bottomBlock); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Get Q or H matrix |
| 180 | + * |
| 181 | + * @param string $name |
| 182 | + * |
| 183 | + * @return NumericMatrix |
| 184 | + * |
| 185 | + * @throws Exception\MatrixException |
| 186 | + */ |
| 187 | + public function __get(string $name): NumericMatrix |
| 188 | + { |
| 189 | + switch ($name) { |
| 190 | + case 'Q': |
| 191 | + case 'H': |
| 192 | + return $this->$name; |
| 193 | + |
| 194 | + default: |
| 195 | + throw new Exception\MatrixException("Hessenberg class does not have a gettable property: $name"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments