Using Python in Linear Algebra
Python provides a powerful and flexible environment for exploring
linear algebra concepts. In this lecture, we will use Python to:
1 Create and manipulate matrices
2 Solve systems of linear equations
3 Access and modify individual matrix elements
4 Compute the reduced row echelon form (RREF)
5 Perform matrix operations (addition, multiplication, division,
transposition)
6 Find the inverse of a matrix (when it exists)
7 Explore special matrices (identity, diagonal, symmetric, etc.)
Dr. Song AMAT 240 Fall 2025 1 / 21
Packages
1 numpy
2 matplotlib
3 math
4 sympy
5 scipy
Dr. Song AMAT 240 Fall 2025 2 / 21
Defining a matrix in Python
1 1 1
A = 1 2 4
1 3 9
Dr. Song AMAT 240 Fall 2025 3 / 21
Defining a matrix in Python
1 1 1
A = 1 2 4
1 3 9
>>>import numpy as np
>>>from numpy import array
>>>from numpy import linalg as LA
>>>A=array([[1,1,1],[1,2,4],[1,3,9]],dtype=float);
>>>print("Matrix A is ",A)
>>>print(A)
Dr. Song AMAT 240 Fall 2025 3 / 21
Solving linear systems with Python
Use Python to solve the following linear system:
8x1 + 3x2 − 2x3 = 9
−4x1 + 7x2 + 5x3 = 15
3x1 + 4x2 − 12x3 = 35
Dr. Song AMAT 240 Fall 2025 4 / 21
Solving linear systems with Python
Use Python to solve the following linear system:
8x1 + 3x2 − 2x3 = 9
−4x1 + 7x2 + 5x3 = 15
3x1 + 4x2 − 12x3 = 35
8 3 −2 9
A = −4 7 5 , b = 15
3 4 −12 35
Dr. Song AMAT 240 Fall 2025 4 / 21
Solving linear systems with Python
8 3 −2 9
A = −4 7
5 , b = 15
3 4 −12 35
>>> A=array([[8, 3, -2],[-4, 7, 5], [3, 4, -12]], dtyp
>>> b=array([[9], [15], [35]], dtype=float)
>>> Sol1 = [Link](A, b)
>>> print("The solution is:",Sol1 )
>>> Sol1Roundup = [Link](Sol1, 4)
>>> print("Rounded solution:", Sol1Roundup)
Dr. Song AMAT 240 Fall 2025 4 / 21
Solving linear systems with Python
8 3 −2 9
A = −4 7 5 , b = 15
3 4 −12 35
>>> A=array([[8, 3, -2],[-4, 7, 5], [3, 4, -12]], dtyp
>>> b=array([[9], [15], [35]], dtype=float)
>>> Sol1 = [Link](A, b)
>>> print("The solution is:",Sol1 )
>>> Sol1Roundup = [Link](Sol1, 4)
>>> print("Rounded solution:", Sol1Roundup)
Note: A must be an invertible matrix when using [Link](A, b).
Dr. Song AMAT 240 Fall 2025 4 / 21
Solving linear systems with Python
Exercise 45 of Section 1.2: Solve the system by using Python.
a0 + a1 + a2 = 12
1 1 1 12
a0 + 2a1 + 4a2 = 15 A = 1 2 4 , b = 15
1 3 9 16
a0 + 3a1 + 9a2 = 16
Dr. Song AMAT 240 Fall 2025 5 / 21
Solving linear systems with Python
Exercise 45 of Section 1.2: Solve the system by using Python.
a0 + a1 + a2 = 12
1 1 1 12
a0 + 2a1 + 4a2 = 15 A = 1 2 4 , b = 15
1 3 9 16
a0 + 3a1 + 9a2 = 16
>>>b=array([[12],[15],[16]],dtype=float)
>>>Sol = [Link](A, b)
>>>print("The solution is ",Sol)
Dr. Song AMAT 240 Fall 2025 5 / 21
Matrix shape and size in numpy
>>> A=array([[1,0.5,10],[3,21,15]],dtype=float);
>>> [Link] # Return the shape of A.
# If A is m by n, [Link] returns (m,n)
>>> [Link](A) #The same as [Link].
>>> [Link] #the total number of elements.
>>> [Link](A) #The same as [Link].
Dr. Song AMAT 240 Fall 2025 6 / 21
Accessing matrix elements in numny
1 1 1 12
Let Ag = 1 2 4 15
1 3 9 16
Dr. Song AMAT 240 Fall 2025 7 / 21
Accessing matrix elements in numny
1 1 1 12
Let Ag = 1 2 4 15
1 3 9 16
>>> Ag=[Link]((A,b))
>>>AgCopy=[Link]()
>>>> Ag[0,0]
>>>> Ag[0,2]
>>>> Ag[0,:]
>>>> Ag[1,:]
>>>> Ag[:,1]
>>>> Ag[:,2]
Dr. Song AMAT 240 Fall 2025 7 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
>>> Ag=array([[1.0,1,1,12],[1,2,4,15],[1,3,9,16]]);
>>> Ag[0,:]
array([[ 1, 1, 1, 12]])
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
>>> Ag[1,:]=(-1)*Ag[0,:]+Ag[1,:]
>>> Ag
matrix([[ 1, 1, 1, 12],
[ 0, 1, 3, 3],
[ 1, 3, 9, 16]])
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
>>> Ag[2,:]=(-1)*Ag[0,:]+Ag[2,:]
>>> Ag
array([[ 1, 1, 1, 12],
[ 0, 1, 3, 3],
[ 0, 2, 8, 4]])
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
>>> Ag[2,:]=(-2)*Ag[1,:]+Ag[2,:]
>>> Ag
array([[ 1, 1, 1, 12],
[ 0, 1, 3, 3],
[ 0, 0, 2, -2]])
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15
1 3 9 16
>>> Ag[1,:]=(-3/2)*Ag[2,:]+Ag[1,:]
>>> Ag[0,:]=(-1)*Ag[1,:]+Ag[0,:]
>>> Ag[2,:]=Ag[2,:]/2
>>> Ag[0,:]=(-1)*Ag[2,:]+Ag[0,:]
>>> print("The RREF of Ag is ",Ag)
Dr. Song AMAT 240 Fall 2025 8 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15. Continue our elementary row operations we
1 3 9 16
eventually obtain the RREF of Ag.
Dr. Song AMAT 240 Fall 2025 9 / 21
Performing elementary row operations in Python
1 1 1 12
Ag = 1 2 4 15. Continue our elementary row operations we
1 3 9 16
eventually obtain the RREF of Ag.
>>> Ag[1,:]=(-3/2)*Ag[2,:]+Ag[1,:]
>>> Ag[0,:]=(-1)*Ag[1,:]+Ag[0,:]
>>> Ag[2,:]=Ag[2,:]/2
>>> Ag[0,:]=(-1)*Ag[2,:]+Ag[0,:]
>>> print("The RREF of Ag is ",Ag)
Dr. Song AMAT 240 Fall 2025 9 / 21
Finding the RREF of a Matrix
8 3 −2
Find the RREF of A = −4 7 5 .
3 4 −12
Dr. Song AMAT 240 Fall 2025 10 / 21
Finding the RREF of a Matrix
8 3 −2
Find the RREF of A = −4 7 5 .
3 4 −12
>>> from sympy import Matrix
>>> A= Matirx([[8, 3, -2],[-4, 7, 5],\
[3, 4, -12]],dtype=float)
>>> [Link]() # Reduced row echelon form
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]), (0, 1, 2))
Dr. Song AMAT 240 Fall 2025 10 / 21
Finding the RREF of a Matrix
1 1 −2
Example 2: Find the RREF of C = 3 0 0 .
1 0 0
Dr. Song AMAT 240 Fall 2025 10 / 21
Finding the RREF of a Matrix
1 1 −2
Example 2: Find the RREF of C = 3 0 0 .
1 0 0
>>> C=Matrix([[1.0,1,-2],[3,0,0],[1,0,0]])
>>> [Link]() # Reduced row echelon form
(Matrix([
[1, 0, 0],
[0, 1, -2],
[0, 0, 0]]), (0, 1))
Dr. Song AMAT 240 Fall 2025 10 / 21
Converting numpy arrays to sympy matrices
>>> matrixA=Matrix(A)
>>> [Link]() # Reduced row echelon form
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]), (0, 1, 2))
Dr. Song AMAT 240 Fall 2025 11 / 21
Converting numpy arrays to sympy matrices
>>> matrixA=Matrix(A)
>>> [Link]() # Reduced row echelon form
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]), (0, 1, 2))
>>> matrixAg=Matrix(AgCopy)
>>> [Link]()
(Matrix([
[1, 0, 0, 7.0],
[0, 1, 0, 6.0],
[0, 0, 1, -1.0]]), (0, 1, 2))
Dr. Song AMAT 240 Fall 2025 11 / 21
Matrix multiplication
#in numpy
>>> A = array([[1,2],[3,4]]);
>>> B=array([[-2.1,9,0.3],[23,78,49]])
>>> [Link](A,B)
array([[ 43.9, 165. , 98.3],
[ 85.7, 339. , 196.9]])
Dr. Song AMAT 240 Fall 2025 12 / 21
Matrix multiplication
#in numpy
>>> A = array([[1,2],[3,4]]);
>>> B=array([[-2.1,9,0.3],[23,78,49]])
>>> [Link](A,B)
array([[ 43.9, 165. , 98.3],
[ 85.7, 339. , 196.9]])
>>>A@B
array([[ 43.9, 165. , 98.3],
[ 85.7, 339. , 196.9]])
Dr. Song AMAT 240 Fall 2025 12 / 21
Element-wise vs matrix multiplication
>>> A1=array([[1,-1,2],[3,4,5]]);
>>> B1=array([[-2.1,9,0.3],[23,78,49]])
>>> A1*B1 #Element-wise multiplication
>>> A1@B1 #Undefined
Dr. Song AMAT 240 Fall 2025 13 / 21
Element-wise multiplication (*) and division (/)
>>> A1=[Link]([[1,2],[3,4]])
>>> [Link](A1,A1) #Elementwise
>>> B1=[Link]([[2,-2],[10,5]])
>>> [Link](A1,B1) #Elementwise multiplication
>>> A1*B1 #Same as [Link](A1,B1)
>>> A1*A1 #=[Link](A1,A1)
>>> A1**2 #=[Link](A1,A1)
Dr. Song AMAT 240 Fall 2025 14 / 21
Element-wise multiplication (*) and division (/)
>>> A2=array([[-1,2],[3,8]])
>>> A2*A2
array([[ 1, 4],
[ 9, 64]])
>>> A2**2
array([[ 1, 4],
[ 9, 64]])
>>> A2@A2
array([[ 7, 14],
[21, 70]])
Dr. Song AMAT 240 Fall 2025 15 / 21
Element-wise multiplication (*) and division (/)
>>> A2=array([[-1,2],[3,8]])
>>> A2*A2
array([[ 1, 4],
[ 9, 64]])
>>> A2**2
array([[ 1, 4],
[ 9, 64]])
>>> A2@A2
array([[ 7, 14],
[21, 70]])
>>> A/B #Elementwise division
>>> A/A
>>> (A+B)/B
Dr. Song AMAT 240 Fall 2025 15 / 21
Column-wise normalization by last row
Divide each columnof matrix A by its last element:
−2 12 4 5
6
3 −3 −3
2 18 6 6
10 −8 4 2
Dr. Song AMAT 240 Fall 2025 16 / 21
Column-wise normalization by last row
Divide each columnof matrix Aby its last element:
−2 12 4 5 −0.2 −1.5 1 2.5
6
3 −3 −3 Result: 0.6 −0.375 −0.75 −1.5
2 18 6 6 0.2 −2.25 1.5 3
10 −8 4 2 1 1 1 1
Dr. Song AMAT 240 Fall 2025 16 / 21
Column-wise normalization by last row
Divide each columnof matrix Aby its last element:
−2 12 4 5 −0.2 −1.5 1 2.5
6
3 −3 −3 Result: 0.6 −0.375 −0.75 −1.5
2 18 6 6 0.2 −2.25 1.5 3
10 −8 4 2 1 1 1 1
>>> G=array([[-2, 12, 4, 5],\
[6, 3, -3, -3],\
[2, 18, 6, 6],\
[10, -8, 4, 2]])
>>> G/G[3,:] #Divide each column by its last element
array([[-0.2 , -1.5 , 1. , 2.5 ],
[ 0.6 , -0.375, -0.75 , -1.5 ],
[ 0.2 , -2.25 , 1.5 , 3. ],
[ 1. , 1. , 1. , 1. ]])
Dr. Song AMAT 240 Fall 2025 16 / 21
Transpose
>>> B=array([[-2.1,9,0.3],[23,78,49]])
>>> B.T #return the transpose of B
Dr. Song AMAT 240 Fall 2025 17 / 21
Creating special matrices in numpy
[Link](n) returns the n × n identity matrix.
Dr. Song AMAT 240 Fall 2025 18 / 21
Creating special matrices in numpy
[Link](n) returns the n × n identity matrix.
[Link]((m, n)) returns an m × n matrix of all zeros.
Dr. Song AMAT 240 Fall 2025 18 / 21
Creating special matrices in numpy
[Link](n) returns the n × n identity matrix.
[Link]((m, n)) returns an m × n matrix of all zeros.
[Link]((m, n)) returns an m × n matrix of all ones.
Dr. Song AMAT 240 Fall 2025 18 / 21
Creating special matrices in numpy
[Link](n) returns the n × n identity matrix.
[Link]((m, n)) returns an m × n matrix of all zeros.
[Link]((m, n)) returns an m × n matrix of all ones.
[Link]([d 1, d 2, ..., d n]) returns a diagonal matrix with
entries d1 , d2 , . . . , dn .
Dr. Song AMAT 240 Fall 2025 18 / 21
Creating special matrices in numpy
[Link](n) returns the n × n identity matrix.
[Link]((m, n)) returns an m × n matrix of all zeros.
[Link]((m, n)) returns an m × n matrix of all ones.
[Link]([d 1, d 2, ..., d n]) returns a diagonal matrix with
entries d1 , d2 , . . . , dn .
[Link]([Link]) returns a zero matrix with the same shape as
A.
Dr. Song AMAT 240 Fall 2025 18 / 21
Creating special matrices in numpy
Dr. Song AMAT 240 Fall 2025 18 / 21
Examples of special matrices
>>> I3 = [Link](3) # 3 by 3 identity matrix
>>> [Link]((2, 3)) # 2 by 3 zero matrix
>>> [Link]((3, 5)) # 3 by 5 matrix of all ones
>>> [Link]([2.4, 3, -8])
#Diagonal matrix with entries 2.4, 3, -8
>>> [Link]((2, 3, 5))
#3D matrix of shape (2, 3, 5) filled with ones
>>> [Link](0, 1, (3, 2, 2))
#3D matrix with standard normal entries
Dr. Song AMAT 240 Fall 2025 19 / 21
Example of transpose and multiplication
5 1 1 0.2 1
Let A = 0 2 −3 and B = 3.5 −2.3. Compute BT (A − 2I3 ).
4 0 2 4 −1
Dr. Song AMAT 240 Fall 2025 20 / 21
Example of transpose and multiplication
5 1 1 0.2 1
Let A = 0 2 −3 and B = 3.5 −2.3. Compute BT (A − 2I3 ).
4 0 2 4 −1
>>> A=array([[5,1,1],[0,2,-3],[4,0,2]])
>>> B=array([[0.2,1],[3.5,-2],[4,-1]])
>>> B.T@(A-2*[Link](3))
Dr. Song AMAT 240 Fall 2025 20 / 21
Example of transpose and multiplication
5 1 1 0.2 1
Let A = 0 2 −3 and B = 3.5 −2.3. Compute BT (A − 2I3 ).
4 0 2 4 −1
>>> A=array([[5,1,1],[0,2,-3],[4,0,2]])
>>> B=array([[0.2,1],[3.5,-2],[4,-1]])
>>> B.T@(A-2*[Link](3))
Answer
array([[ 16.6, 0.2, -10.3],
[ -1. , 1. , 7. ]])
Dr. Song AMAT 240 Fall 2025 20 / 21
Exercises
1.0758 −5.3894 1.919 2.6850 3.4169
Let A = −2.1046 −0.9550 −0.9724 , B = −1.0051 0.6385 , C =
5.4971 1.5719 0.8258 0.0750 −2.0326
11 11 0.1 0.5 0.5 1 0 0 0.2
11 11 , P = 0.6 0.35 0.15 , I3 = 0 1 0 , v = 0.5 , u = 1.2 .
4.5
11 11 0.3 0.15 0.35 0 0 1 0.3
Use Python to compute the following expressions. Round all numerical results to the
4th decimal place.
1 Compute AB and find the (3,2)-entry of AB.
2 Find the RREF of B.
3 Compute PAPB, BT A, (A − 10I3 )B + 5C and B(2u) − 3.2v.
4 Let D = A−1 . Write down the following three entries of D: d11 , d21 , and d32 .
5 Assume that matrix C has already been defined in Python. Write the code that
computes the dimension of matrix C.
6 Solve the linear system Px = v.
7 Compute Pv, P2 v, P4 v.
Dr. Song AMAT 240 Fall 2025 21 / 21