Linear Algebra in Python
Linear Algebra in Python
blog
Linear Algebra
in Python
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Contents
• Linear Algebra
• Matrices and Vectors
• The NumPy Library
• Solving Linear Equations
• Python Examples
Linear Algebra
• Linear Algebra is central and important in almost all areas
of mathematics
• Linear Algebra is the “Mathematics of Data“
• Foundation: Vectors and Matrices
• Linear Equations
• Modern statistics and data analysis depends on Linear
Algebra
• Linear Algebra plays an important role in advanced
Control Engineering
• Linear Algebra plays an important role in Machine
Learning
Matrices
A matrix is a two-dimensional data structure where numbers are arranged into rows
and columns.
A general matrix is given by:
𝑎!! ⋯ 𝑎!"
𝐴= ⋮ ⋱ ⋮
𝑎#! ⋯ 𝑎#"
Where 𝑛 is number of rows and 𝑚 is number of columns.
Matrices are very important data structures for many mathematical and scientific
calculations.
Examples
Example of a 3 x 3 matrix:
1 5 3
𝐴= 4 6 6 Example of a 4 x 2 matrix:
3 8 9
1 5
Example of a 3 x 4 matrix: 4 5
𝐴=
3 2
1 5 3 4 7 8
𝐴= 4 5 7 8
7 8 9 3
Vectors
A vector is a one-dimensional data structure where numbers are arranged into rows
or columns.
A general vector is given by: 𝑥!
𝑥$
𝑥= ⋮
𝑥#
Where n is number of rows and m is number of columns.
Matrices and vectors are very important data structures for many mathematical and scientific
calculations.
Vectors
Assume the vector 𝑥: The Length of vector 𝑥 is given by:
𝑥!
𝑥" 𝑥 = 𝑥$𝑥 = 𝑥!" + 𝑥"" + ⋯ + 𝑥#"
𝑥= ⋮
𝑥# The length of a vector most makes sense
for 2 or 3 dimensional vectors.
The Transpose of vector 𝑥 is It can be visualized like this:
Example:
𝑥 = 𝑥!
$ 𝑥" ⋯ 𝑥#
𝑣 = 3, 4
a = np.array([[1],
[3],
Example of a 3 x 4 matrix: [7],
[2]])
1 3 7 2
𝐴= 5 8 −9 0 print("a =", a)
6 −7 11 12
A = np.array([[1, 3, 7, 2],
Example of vector:
[5, 8, -9, 0],
1 [6, -7, 11, 12]])
3
𝑎= 7
print("A =", A)
2
numpy.matrix
import numpy as np
Let's use the Matrix feature in the NumPy Library:
a = np.array([[1],
[3],
Example of a 3 x 4 matrix: [7],
1 3 7 2 [2]])
𝐴= 5 8 −9 0
6 −7 11 12 print("a =", a)
More Information:
https://numpy.org/doc/stable/reference/generated/numpy.matrix.html
Matrix Addition, Subtraction, Multiplication
0 1 1 0
Given the following: 𝐴= 𝐵=
−2 −3 3 −2
Matrix Addition:
0 1 1 0 0+1 1+0 1 1
𝐴+B= + = =
−2 −3 3 −2 −2 + 3 −3 + (−2) 1 −5
Matrix Subtraction:
0 1 1 0 0−1 1−0 −1 1
𝐴−B= − = =
−2 −3 3 −2 −2 − 3 −3 − (−2) −5 −1
Matrix Multiplication:
0 1 1 0 0;1+1;3 0 ; 0 + 1 ; (−2) 3 −2
𝐴B = == =
−2 −3 3 −2 −2 ; 1 − 3 ; 3 −2 ; 0 − 3 ; (−2) −11 6
Matrix Addition, Subtraction, Multiplication
Matrix Addition, Subtraction, Multiplication import numpy as np
B = np.array([[1, 0],
[3, -2]])
Matrix Addition, Subtraction, Multiplication
C = A + B
print("A+B =", C)
In this example we use numpy.array C = A - B
print("A-B =", C)
𝐴 × 𝐵 = 𝐶
𝑛×𝑚 𝑚×𝑝 𝑛×𝑝
The resulting matrix will
Inner dimensions be the outer dimensions
need to be the same
import numpy as np
1 (3×3) 7 8 9 3 [4],
[3]])
3 𝐷= 1 5 3 M = np.matmul(A,B)
print("M=", M)
(3×1) (1×3)
#M = np.matmul(B,A) # Not Working!
1 5 6
1 3 7 2
3 8 −7
𝐴 = 5 8 −9 0 𝐴D =
7 −9 11
6 −7 11 12
2 0 12
1 5
4 5 1 4 3 7
𝐵= 𝐵D =
3 2 5 5 2 8
7 8
Transpose of a Matrix import numpy as np
A = np.array([[1, 3, 7, 2],
[5, 8, -9, 0],
1 3 7 2 [6, -7, 11, 12]])
𝐴= 5 8 −9 0 print("A="); print(A)
6 −7 11 12
Atr = np.transpose(A)
Transpose of A=
𝐴$ = ?
[[ 1 5 6] print("Transpose of A="); print(Atr)
[ 3 8 -7]
1 5 [ 7 -9 11] B = np.array([[1, 5],
[ 2 0 12]] [4, 5],
4 5
𝐵= [3, 2],
3 2 [7, 8]])
7 8 print("B="); print(B)
Transpose of B=
[[1 4 3 7] Btr = np.transpose(B)
𝐵$ =? [5 5 2 8]]
print("Transpose of B="); print(Btr)
Determinant of a Matrix
𝑑𝑒𝑡 𝐴 = 𝐴
It will be a little more complicated and time-consuming work for systems with larger order,
so we need a programming language like python to handle this.
Determinant of a Matrix
Example:
−1 3 0
𝐴= 2 1 −5 1 −5 3 0 3 0
𝑑𝑒𝑡 𝐴 = (−1) −2 +1
1 4 −2 4 −2 4 −2 1 −5
This gives:
1 −5
= −2 − −20 = 18
4 −2
3 0
= −6 − 0 = −6
4 −2 3 0
= −15 − 0 = −15
1 −5
Finally:
B = np.array([[-1, 3, 0],
Python Solution:
[2, 1, -5],
-2.0000000000000004 [1, 4, -2]])
-21.00000000000001
Bdet = la.det(B)
print(Bdet)
Inverse Matrices
The inverse of a quadratic matrix 𝐴 is defined by: 𝐴&! Note: 𝐴𝐴&! = 𝐴&! 𝐴 = 𝐼
1 2 ! 4 −2 Where:
𝐴= 𝐴&! = '()(+)
3 4 −3 1 𝑑𝑒𝑡 𝐴 = 𝐴 = 1 * 4 − 3 * 2 = 4 − 6 = −2
! 4 −2 −2 1
This gives: 𝐴&! = =
&" −3 1 1.5 −0.5
It will be more complicated for systems with larger order, so we need a programming language
like python to handle this.
Inverse Matrices
Given the following Matrices: import numpy as np
import numpy.linalg as la
1 2
𝐴= −1 3 0
3 4 A = np.array([[1, 2],
𝐵= 2 1 −5 [3, 4]])
1 4 −2
−2 1
𝐴&! = Ainv = la.inv(A)
1.5 −0.5
𝐵 &! =?
Python Solution: print(Adet)
B = np.array([[-1, 3, 0],
[[-2. 1. ]
[2, 1, -5],
[ 1.5 -0.5]]
[1, 4, -2]])
[[-0.85714286 -0.28571429 0.71428571] Binv = la.inv(B)
[ 0.04761905 -0.0952381 0.23809524]
[-0.33333333 -0.33333333 0.33333333]]
print(Binv)
Eigenvalues
The Eigenvalues for a given matrix 𝐴 is: det 𝜆𝐼 − 𝐴 = 0
0 1
Example: 𝐴= 𝑑𝑒𝑡 𝐴 = 𝐴 = 𝑎!! 𝑎"" − 𝑎"! 𝑎!"
−2 −3
1 0 0 1 𝜆 0 0 1 𝜆 −1
𝜆 − = − =
0 1 −2 −3 0 𝜆 −2 −3 2 𝜆+3
−𝑏 ± 𝑏 " − 4𝑎𝑐
𝑥=
det 𝜆𝐼 − 𝐴 = 𝜆(𝜆 + 3) − (−1)(2) = 0 2𝑎
−3 ± 3" − 4 ; 1 ; (2) −3 ± 1
"
𝜆 + 3𝜆 + 2 = 0 𝜆= =
2;1 2 𝜆! = −1, 𝜆" = −2
In Python we use the function eig(). For more details:
https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html
Eigenvalues
import numpy as np
0 1 import numpy.linalg as la
𝐴=
−2 −3 A = np.array([[0, 1],
[-2, -3]])
−1
𝑒𝑖𝑔 𝐴 = Aeig, v = la.eig(A)
−2
print(Aeig)
𝐴𝐵 ≠ 𝐵𝐴
𝐴 𝐵𝐶 = 𝐴𝐵 𝐶
𝐴 + 𝐵 𝐶 = 𝐴𝐶 + 𝐵𝐶
𝐶 𝐴 + 𝐵 = 𝐶𝐴 + 𝐶𝐵
det 𝐴𝐵 = det 𝐴 det 𝐵
det 𝐴% = det(𝐴)
𝐴𝐴&! = 𝐴&! 𝐴 = 𝐼
import numpy as np
𝐴 + 𝐵 𝐶 = 𝐴𝐶 + 𝐵𝐶 R = np.matmul(B,A)
print("R=");print(R)
𝐶 𝐴 + 𝐵 = 𝐶𝐴 + 𝐶𝐵
print("A(BC) = (AB)C")
det 𝐴𝐵 = det 𝐴 det 𝐵 L = np.matmul(A,np.matmul(B,C))
det 𝐴D = det(𝐴) print("L=");print(L)
𝐴 + 𝐵 𝐶 = 𝐴𝐶 + 𝐵𝐶 R = np.matmul(A,C) + np.matmul(B,C)
print("R=");print(R)
𝐶 𝐴 + 𝐵 = 𝐶𝐴 + 𝐶𝐵
print("AA^(-1) = A^(-1)A = I")
det 𝐴𝐵 = det 𝐴 det 𝐵 L = np.matmul(A, la.inv(A))
det 𝐴D = det(𝐴) print("L=");print(L)
Solving Linear
Equations
Hans-Petter Halvorsen
Linear Equations
Given the following linear equations:
𝑎!! 𝑥! + 𝑎!" 𝑥" + 𝑎!# 𝑥# + ⋯ = 𝑏!
𝑎"! 𝑥! + 𝑎"! 𝑥" + 𝑎"# 𝑥# + ⋯ = 𝑏"
⋯
These equations can be set on the following general form:
𝐴𝑥 = 𝑏
Where A is a matrix, x is a vector with the unknowns and b Solution:
is a vector of constants
𝑥! 𝑏!
𝑎!! ⋯ 𝑎!- 𝑥" 𝑏" 𝑥 = 𝐴;/ 𝑏
𝐴= ⋮ ⋱ ⋮ 𝑥 = ⋮ 𝑏 =
⋮
𝑎#! ⋯ 𝑎#- 𝑥# 𝑏# (assuming 𝐴&! is possible )
Example
Given the following linear equations:
𝑥/ + 2𝑥0 = 5
3𝑥/ + 4𝑥0 = 6
We want to put the equations on the following general form:
𝐴𝑥 = 𝑏
This gives: The solution is given by:
!"
𝐴=
1 2
𝑏=
5 𝑥!
𝑥= 𝑥
𝑥=𝐴 𝑏
3 4 6 "
Example - Python
Python code:
import numpy as np
import numpy.linalg as la This gives the following solution:
[[-4. ]
A = np.array([[1, 2], [ 4.5]]
[3, 4]])
This means:
b = np.array([[5], 𝑥! = −4
[6]]) 𝑥" = 4.5
https://www.halvorsen.blog/documents/programming/python/
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog