reducedRowEchelonForm method

Matrix reducedRowEchelonForm()

Returns the reduced row echelon form (RREF) of the current matrix.

Example:

final matrix = Matrix([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]);
final rrefMatrix = matrix.rref();
print(rrefMatrix);

Output:

1  0 -1
0  1  2
0  0  0

Implementation

Matrix reducedRowEchelonForm() {
  Matrix result = rowEchelonForm();
  int rowCount = result.rowCount;

  for (int r = rowCount - 1; r >= 0; r--) {
    int nonZeroIndex = _Utils.getFirstNonZeroIndex(result[r]);

    if (nonZeroIndex != -1) {
      for (int i = r - 1; i >= 0; i--) {
        result.addRow(r, i, -result[i][nonZeroIndex]);
      }
    }
  }

  return result;
}