rowEchelonForm method

Matrix rowEchelonForm()

Returns the row echelon form (REF) of the current matrix.

Algorithm:

  1. Initialize the result matrix as a copy of the current matrix.
  2. Iterate through the rows of the matrix.
  3. If a pivot element is found, swap the rows and normalize the row.
  4. Eliminate the elements below the pivot.

Example:

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

Output:

1  2  3
0 -3 -6
0  0  0

Implementation

Matrix rowEchelonForm() {
  Matrix result = _Utils.toNumMatrix(copy());
  int lead = 0;
  int rowCount = result.rowCount;
  int columnCount = result.columnCount;

  for (int r = 0; r < rowCount; r++) {
    if (lead >= columnCount) {
      break;
    }
    int i = r;
    while (result[i][lead] == 0) {
      i++;
      if (i == rowCount) {
        i = r;
        lead++;
        if (lead == columnCount) {
          return result;
        }
      }
    }
    result.swapRows(i, r);
    if (result[r][lead] != 0) {
      result.scaleRow(r, Complex.one() / result[r][lead]);
    }
    for (i = r + 1; i < rowCount; i++) {
      result.addRow(r, i, -result[i][lead]);
    }
    lead++;
  }

  return result;
}