August 15, 2025
Examples: Reduce the following matrices into their reduced row echelon form.
We can rewrite linear systems of equations as matrices
We can use our three permissible row-reduction operations (scaling, swapping, and replacement) to reduce these matrices to simpler forms
Goal for Today: Learn to use Python for basic calculations, vector and matrix math, and row-reduction (note: you’ll still need to show that you can do row-reduction by hand)
Python is a general-purpose programming language, which means it’s used for a wide range of tasks
In MAT350, we’ll use Python as a tool to help us work more efficiently with mathematical objects like vectors and matrices.
Because understanding how to row-reduce by hand is still an important skill, you’ll be required to pass a row-reduction “Gateway Exam” in this course.
After that, you’ll often be encouraged to let Python do the heavy lifting for you.
This will free up your cognitive energy to focus on understanding and applying new concepts, rather than spending all your time crunching through row operations.
Navigate to colab.research.google.com while logged into your Google/Gmail account
Click the pop-up to create a new notebook
Colab Notebooks
Note: If you would prefer to use Python in another environment (VScode, spyder, etc.), then you are welcome to do so. I won’t help troubleshooting individual environments though.
Colab notebooks are Jupyter notebooks run directly from your Google Drive
The notebooks consist of text/markdown cells (white background) and code cells (grey background)
shift
and hitting enter
/return
We can use python for all basic mathematical operations – addition (+
), subtraction (-
), multiplication (*
), division (/
), and exponentiation (**
).
*
operator. While writing 5(8 - 6)
makes perfect sense mathematically, python will think you are trying to “plug \(6 - 8\) into a function named \(5\)”.Example: Try evaluating each of the following expressions using the code cell below.
On Printing: Perhaps you noticed that only the result of the last line in the code cell is printed out. You’ll need to explicitly wrap your expression in print()
if you want earlier results printed out as well. Go back and try it!
In any programming language, we often want to store values into variables for future use. Python is no different.
In python, we store values into variables with the =
assignment operator.
=
sign and the value to store in that variable goes to the right.Examples:
x = 5
assigns the integer 5
to the variable x
my_list = [1, 4, 9, 16]
assigns the list [1, 4, 9, 16]
to the variable my_list
.Python will infer the data type required to store an object. We do not need to initialize variables in python, nor do we need to declare their classes. This is a convenience that can potentially cause problems.
When choosing variable names, try to be descriptive.
Consider the following tips and recommendations.
Variable names should start with a letter and can include letters, numbers, and underscores, but no spaces.
Variable names are case sensitive, so height
and Height
are different variables.
There are several conventions for typesetting variable names, for example, this_is_snake_case
and ThisIsCamelCase
– choose one and stick to it.
Find a balance between descriptive and concise variable names
eqn1_sol
is a better choice for variable name than the_solution_we_found_for_equation_1
.To keep Python lightweight, not all of Python’s functionality is made available to you by default.
You’ll need to import modules in order to complete many specialized tasks.
{sympy}
(SYmbolic Mathematics in PYthon) module since it has functionality for nearly everything we encounter in an introduction to Linear Algebra.{numpy}
(NUMerical PYthon) module which handles numerical operations on vectors and matrices very well.When importing modules, we can either load the entire module (ie. import sympy
) or we can just load a single function from that module (ie. from numpy.linalg import inv
).
Running import sympy
will load the {sympy}
module to the current Python session
With {sympy}
imported, we can call and utilize functionality from that module.
sympy.Matrix()
to tell Python to go to the {sympy}
module and find the Matrix()
function.Because we need to reference the module name when calling functionality from it, Python users often alias their module names when importing in order to reduce typing.
import sympy as sp
and then can use sp.Matrix()
instead{sympy}
The code cell above defines and prints the matrix \(A = \begin{bmatrix} 1 & 5 & 9 & 22\\ 0 & 2 & 17 & -11\end{bmatrix}\)
Example: Open a code cell and use Python and {sympy}
to define the matrix \(B = \begin{bmatrix} -2 & 8 & 5\\
0 & 7 & -11/2\\
2 & 1/2 & 5/8\\
1/2 & -4 & 6\end{bmatrix}\)
{sympy}
The code cell above defines and prints the matrix \(A = \begin{bmatrix} 1 & 5 & 9 & 22\\ 0 & 2 & 17 & -11\end{bmatrix}\)
Example: Open a code cell and use Python and {sympy}
to define the matrix \(B = \begin{bmatrix} -2 & 8 & 5\\
0 & 7 & -11/2\\
2 & 1/2 & 5/8\\
1/2 & -4 & 6\end{bmatrix}\)
Example: Recall that we can think of a vector as a matrix with a single column. Use the code cell below to define the vector \(\vec{v} = \begin{bmatrix} -2\\ 1\\ 3/2\end{bmatrix}\)
+
and -
operators as long as our objects have been defined using {sympy}
.Example: Use Python to compute the sums and differences below.
Example: Use Python to compute the sums and differences below.
(Note: Scalar multiplication can be performed as usual, using the *
operator)
Matrix multiplication is not an elementwise operation. +Instead, to compute the product \(AB\), we take the dot products between rows of \(A\) and columns of \(B\) to construct the corresponding entries of \(AB\).
In Python, as long as we’ve defined our matrices using {sympy}
, then we can use the usual *
operator to execute matrix multiplication.
Example: Define the following matrices in Python and calculate the desired products. What happens in scenarios where the matrix product is not defined because of dimension incompatibility?
We can also use Python to compute vector products like the dot product and cross product. Given vectors u
and v
defined,
u.dot(v)
.u.cross(v)
.Example: Consider the vectors \(\vec{u} = \begin{bmatrix} -3\\ 1\\ 1\end{bmatrix}\) and \(\vec{v} = \begin{bmatrix} 0\\ -2\\ -1\end{bmatrix}\). Use the code cell below to
Without a doubt, the most useful functionality for you this entire semester will be the ability to quickly row reduce a matrix.
As long as the matrix \(A\) has been defined using {sympy}
, we can use the .rref()
method to obtain the reduced row echelon form of \(A\).
Example: Consider the matrix \(A = \begin{bmatrix} 3 & -2 & 0 & 1\\ 0 & 2 & 5 & -6\\ 4 & 4 & -2 & -1\\ -2 & -3 & 0 & 9\\ 1 & 0 & 0 & -4\end{bmatrix}\). We define the matrix and obtain its reduced row echelon form below.
Note: The resulting output is the reduced row echelon form of the matrix, along with a tuple containing the pivot columns of the matrix.
Example: For the linear systems below, construct the corresponding augmented coefficient matrices, define them in Python using {sympy}
, use the .rref()
method to obtain their reduced row echelon form, and then describe the solution set for each system.
\[(A)~~\left\{\begin{array}{rcr} x + 2y -z & = & 4\\ 2x - y + 3z & = & 1\\ -3x + y +2z & = & -5\end{array}\right.~~~~~(B) \left\{\begin{array}{rcr} x_1 + x_2 + x_3 & = & 2\\ 2x_1 - x_2 + 3x_3 & = & 5\end{array}\right.\]
\[(C)~~\left\{\begin{array}{rcr}x_1 + x_2 + x_3 & = & 3\\ 2x_1 - x_2 + x_3 & = & 1\\ x_1 + 2x_2 - x_3 & = & 4\\ 3x_1 + x_2 + 2x_3 & = & 7\end{array}\right.~~~~~(D)~~\left\{\begin{array}{rcr}x_1 + 2x_2 - x_3 & = & 1\\ 2x_1 + 4x_2 -2x_3 & = & 2\\ -x_1 - 2x_2 + x_3 & = & -1\\ 3x_1 + 6x_2 - 3x_3 & = & 3\end{array}\right.\]
\[(E)~~\left\{\begin{array}{rcr} 2x_1 - 3x_2 & = & 6\\ 8x_1 - 12x_2 & = & 24\end{array}\right.\]
Being able to carry out matrix and vector operations (including row reduction) by hand is essential for building understanding and intuition about what results should look like. In this course, you should work problems by hand until you are completely confident with the procedures. Once that foundation is solid, feel free to use Python to save time on routine steps.
Note. You will still be expected to demonstrate hand-calculation skills throughout the course.
\[\Huge{\text{Finish Homework 2}}\] \[\Huge{\text{on MyOpenMath}}\]
\(\Huge{\text{Pivots and Solutions}}\)