rowEchelonForm method
Returns the row echelon form (REF) of the current matrix.
Algorithm:
- Initialize the result matrix as a copy of the current matrix.
- Iterate through the rows of the matrix.
- If a pivot element is found, swap the rows and normalize the row.
- 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;
}