Special Matrices

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.

Published by

Ednalyn C. De Dios

I’ve always been enamored with code and I love data science because of its inherent power to solve real problems. Having grown up in the Philippines, served in the United States Navy, and worked in the nonprofit sector, I am driven to make the world a better place. I have started and participated in numerous campaigns that aim to reduce domestic violence and child abuse in the community.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.