Skip to main content
Mathematics LibreTexts

9.1: Sympy RREF function

  • Page ID
    64301
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    #  Load Useful Python Libraries
    import numpy as np
    import sympy as sym
    sym.init_printing(use_unicode=True)
    from urllib.request import urlretrieve
    urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
                'answercheck.py');

    In class we talked about the Python sympy library which has a “reduced row echelon form” (rref) function that runs a much more efficient version of the Gauss-Jordan function. To use the rref function you must first convert your matrix into a sympy.Matrix and then run the function. For example, lets do this for the following matrix \(B\):

    B = np.matrix([[ 50, 13, 30 ], [100, 26, 60 ],  [20.5, 25, 650]])
    sym.Matrix(B).rref()
    # 'Run' this cell to see the output

    This function outputs two values (a matrix and a tuple). For the purposes of this class we only care about the matrix. I generally use the following syntax when using rref()

    sym.Matrix(B).rref()[0]
    # 'Run' this cell to see the output
    Question

    Although we do not use it often in this course, what does the second output of the rref mean (i.e. what does (0,1) mean?

    hint: read the documentation for rref.

    How lets consider the multi-week example from a previous assignment, where:

    Week 1:

    \(c + b = 30\)

    \(20c + 25b = 690\)

    Week 2:

    \(c + b = 35\)

    \(20c + 25b = 750\)

    Week 3:

    \(c + b = 30\)

    \(20c + 25b = 650\)

    Do This

    Write a \(2 \times 5\) augmented matrix representing the 6 equations above. (you can just copy and paste this from the pre-class if you got it right there), Name your Matrix \(G\) to verify your answer using the checkanswer function below.

    #Put your answer to the above question here. 

    The following function will apply the rref function to the matrix \(G\) and store it in a variable called, wait for it, rref:

    rref,_ = sym.Matrix(G).rref()
    rref
    Question

    Given the above, How many hours did Giselle work as a capenter for the three weeks and how many hours did she work as a blacksmith. Fill in your answers below to check if you are correct:

    #Replace the zeros with your answers
    carpenter_week1 = 0
    carpenter_week2 = 0
    carpenter_week3 = 0
    blacksmith_week1 = 0
    blacksmith_week2 = 0
    blacksmith_week3 = 0
    from answercheck import checkanswer
    
    hours = [[carpenter_week1, carpenter_week2, carpenter_week3],
             [blacksmith_week1, blacksmith_week2, blacksmith_week3]]
    hours = np.matrix(hours).astype('float')
    
    checkanswer.matrix(hours,'b2d4a73cac3c95204f5ed743b507093a');

    This page titled 9.1: Sympy RREF function is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.