In Coursera’s Mathematics for Machine Learning: Linear Algebra class, we learned all about matrices. One of my favorite is the row echelon form or REF.

I like it ‘cuz it sounds fancy and Trekkie-like.

Anyways, we had to write a Python application that converts a 4×4 matrix into row echelon form. There’s also a feature in there that catches errors in case of extra special matrices like singular matrices.

def fixRowTwo(A) : A[2] = A[2] - A[0] * A[2,0] A[2] = A[2] - A[1] * A[2,1] if A[2,2] == 0 : A[2] = A[2] + A[3] A[2] = A[2] - A[0] * A[2,0] A[2] = A[2] - A[1] * A[2,1] if A[2,2] == 0 : raise MatrixIsSingular() A[2] = A[2] / A[2,2] return A

You can view the full code on Github.