Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

9.1: Sympy RREF function

( \newcommand{\kernel}{\mathrm{null}\,}\)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#  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');
#  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:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

B = np.matrix([[ 50, 13, 30 ], [100, 26, 60 ],  [20.5, 25, 650]])
sym.Matrix(B).rref()
# 'Run' this cell to see the output
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()

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

sym.Matrix(B).rref()[0]
# 'Run' this cell to see the output
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×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.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here. 
#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:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

rref,_ = sym.Matrix(G).rref()
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:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Replace the zeros with your answers
carpenter_week1 = 0
carpenter_week2 = 0
carpenter_week3 = 0
blacksmith_week1 = 0
blacksmith_week2 = 0
blacksmith_week3 = 0
#Replace the zeros with your answers
carpenter_week1 = 0
carpenter_week2 = 0
carpenter_week3 = 0
blacksmith_week1 = 0
blacksmith_week2 = 0
blacksmith_week3 = 0

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

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');
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.

Support Center

How can we help?