Skip to content

Commit f386771

Browse files
committed
REF: scale by smallest non-zero element
Since we're technically manipulating a linear system, it seems that the property of the final result remaining the same despite scaling applies. If scaling by the inverse of the smallest element, the matrix error becomes relative to the magnitudes of the numbers rather than an absolute quantity. This intuition seems true because all of the tests pass
1 parent 3ec7c55 commit f386771

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/LinearAlgebra/Reduction/RowEchelonForm.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ public static function gaussianElimination(NumericMatrix $A): array
127127
$swaps = 0;
128128
$ε = $A->getError();
129129

130+
// Scale the matrix by its smallest non-zero element
131+
$min = PHP_INT_MAX;
132+
for ($i = 0; $i < $m; $i++) {
133+
for ($j = 0; $j < $n; $j++) {
134+
$elem = \abs($A[$i][$j]);
135+
if (Support::isNotZero($elem, $ε) and $elem < $min) {
136+
$min = $elem;
137+
}
138+
}
139+
}
140+
141+
$R = $A->scalarMultiply(1/$min)->getMatrix();
142+
130143
for ($k = 0; $k < $size; $k++) {
131144
// Find column max
132145
$i_max = $k;

0 commit comments

Comments
 (0)