# Function for reduced row Echelon form / Solving linear equations

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 rowCount = matrix.size;
var columnCount = matrix[0].size;
var pivot, factor;

block { |break|
rowCount.do { |r|
var i;

break.value(nil) ;
});

i = r;

{
i = i + 1;
block{|break2|
if(rowCount == i,
{
i = r;
break2.value(nil);
});
});
}});

matrix.swap(i, r);

if(pivot != 0,
{matrix.put(r, matrix[r]/pivot)}
);

rowCount.do{|i|
if(i != r, {
var change;
matrix.put(i, matrix[i] - (factor * matrix[r]));
//matrix.postln;
});
};

}
};
matrix;
};
)
(
a = [[80, 20, 0, 0.075, 10],[20, 80, 20, 0.03, 30],[0, 0, 160, 0.045, 20]];

~toReducedRowEchelonForm.value(a);
)
``````
1 Like

@erenutku97, you could consider contributing to the Matrix class found in the MathLib quark.

1 Like