Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/LinearAlgebra/Reduction/RowEchelonForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public static function gaussianElimination(NumericMatrix $A): array
$swaps = 0;
$ε = $A->getError();

// Scale the matrix by its smallest non-zero element
$min = PHP_INT_MAX;
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
$elem = \abs($A[$i][$j]);
if (Support::isNotZero($elem, $ε) and $elem < $min) {
$min = $elem;
}
}
}

$R = $A->scalarMultiply(1/$min)->getMatrix();

for ($k = 0; $k < $size; $k++) {
// Find column max
$i_max = $k;
Expand Down
38 changes: 38 additions & 0 deletions tests/LinearAlgebra/Reduction/ReducedRowEchelonFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,44 @@ public function dataProviderForRref(): array
[0, 0, 0, 0, 0],
],
],
// Large magnitude case
[
[
[-2.82878905765*10**6, 0, 0, 0, 0, 0],
[0, -2.82878905765*10**6, 0, 0, 0, 1729.7],
[0, 0, -2.82878905765*10**6, 0, -1729.7, 0],
[0, 0, 0, -2.82879105765*10**6, 0, 0],
[0, 0, -1729.7, 0, -1.05765, 0],
[0, 1729.7, 0, 0, 0, -1.05765]
],
[
[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]
]
],
// Scaled version of large magnitude case
[
[
[-0.99999966687087, 0, 0, 0, 0, 0],
[0, -0.99999966687087, 0, 0, 0, 0.00061146285160793],
[0, 0, -0.99999966687087, 0, -0.00061146285160793, 0],
[0, 0, 0, -1.0000003738869, 0, 0],
[0, 0, -0.00061146285160793, 0, -3.7388694340557*10**-7, 0],
[0, 0.00061146285160793, 0, 0, 0, -3.7388694340557*10**-7]
],
[
[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]
]
]
];
}

Expand Down