protected function pivotize() : Matrix
{
$n = $this->n;
$P = MatrixFactory::identity($n);
$A = $this->A;
// Set initial column max to diagonal element Aᵢᵢ
for ($i = 0; $i < $n; $i++) {
$max = $A[$i][$i];
$row = $i;
// Check for column element below Aᵢᵢ that is bigger
for ($j = $i; $j < $n; $j++) {
if ($A[$j][$i] > $max) {
$max = $A[$j][$i];
$row = $j;
}
}
// Swap rows if a larger column element below Aᵢᵢ was found
if ($i != $row) {
$P = $P->rowInterchange($i, $row);
}
}
return $P;
}