Here’s a function that converts a matrix to its reduced row echelon form in case someone needs it for solving linear equations through Gaussian ellimination.
The original algorithm is from: https://rosettacode.org/wiki/Reduced_row_echelon_form
(
~toReducedRowEchelonForm = { |matrix|
var lead = 0;
var rowCount = matrix.size;
var columnCount = matrix[0].size;
var pivot, factor;
block { |break|
rowCount.do { |r|
var i;
if (columnCount <= lead, {
break.value(nil) ;
});
i = r;
while({matrix[i][lead] == 0},
{
i = i + 1;
block{|break2|
if(rowCount == i,
{
i = r;
lead = lead + 1;
if (columnCount == lead, {
break2.value(nil);
});
});
}});
matrix.swap(i, r);
pivot = matrix[r][lead];
if(pivot != 0,
{matrix.put(r, matrix[r]/pivot)}
);
rowCount.do{|i|
if(i != r, {
var change;
factor = matrix[i][lead];
matrix.put(i, matrix[i] - (factor * matrix[r]));
//matrix.postln;
});
};
lead = lead + 1;
}
};
matrix;
};
)
(
a = [[80, 20, 0, 0.075, 10],[20, 80, 20, 0.03, 30],[0, 0, 160, 0.045, 20]];
~toReducedRowEchelonForm.value(a);
)