Linear Algebra basics#
Credit: Ben Vanderlei’s Jupyter Guide to Linear Algebra and Damir Cavar notebooks under CC BY 4.0 with minor adaptations.
Supporting material#
This chapter’s supporting material is 3blue1brown Essence to Linear Algebra. You are encouraged to watch the whole playlist (it’s really good!) but specially relevant to this course are chapters 1 to 6 and chapters 14 to 16.
Concepts and Notation#
A scalar is an element in a vector, containing a real number value. In a vector space model or a vector mapping of (symbolic, qualitative, or quantitative) properties the scalar holds the concrete value or property of a variable.
A vector is an array, tuple, or ordered list of scalars (or elements) of size
A matrix is a list of vectors that all are of the same length.
We use the notation
A
A vector
Representing a row vector, that is a matrix with
Vector Spaces#
A vector space is a collection of objects, called vectors, together with definitions that allow for the addition of two vectors and the multiplication of a vector by a scalar. These operations produce other vectors in the collection and they satisfy a list of algebraic requirements such as associativity and commutativity. Although we will not consider the implications of each requirement here, we provide the list for reference.
For any vectors
The most familar example of vector spaces are the collections of single column arrays that we have been referring to as “vectors” throughout the previous chapter. The name given to the collection of all
The algebra of vectors by be visualized by interpreting the vectors as arrows. This is easiest to see with an example in
The vector
The algebra of vectors by be visualized by interpreting the vectors as arrows. This is easiest to see with an example in
The vector
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
options = {"head_width":0.1, "head_length":0.2, "length_includes_head":True}
ax.arrow(0,0,1,3,fc='b',ec='b',**options)
ax.text(1,2,'$U_1$')
ax.set_xlim(0,5)
ax.set_ylim(0,5)
ax.set_aspect('equal')
ax.grid(True,ls=':')
It is important to understand that it is the length and direction of this arrow that defines
fig, ax = plt.subplots()
ax.arrow(0,0,1,3,fc='b',ec='b',**options)
ax.arrow(3,0,1,3,fc='b',ec='b',**options)
ax.arrow(0,2,1,3,fc='b',ec='b',**options)
ax.arrow(2,1,1,3,fc='b',ec='b',**options)
ax.set_xlim(0,5)
ax.set_ylim(0,5)
ax.set_aspect('equal')
ax.grid(True,ls=':')
When we perform a scalar multiplication, such as
fig, ax = plt.subplots()
ax.arrow(0,0,1,3,fc='b',ec='b',**options)
ax.arrow(2,0,2,6,fc='r',ec='r',**options)
ax.text(1,2,'$U_1$')
ax.text(4,5,'$2U_1$')
ax.set_xlim(0,6)
ax.set_ylim(0,6)
ax.set_aspect('equal')
ax.grid(True,ls=':')
ax.set_xticks(np.arange(0,7,step = 1));
ax.set_yticks(np.arange(0,7,step = 1));
If the scalar is negative, we interpret the scalar multiplication as reversing the direction of the arrow, as well as changing the length.
fig, ax = plt.subplots()
ax.arrow(0,0,1,3,fc='b',ec='b',**options)
ax.arrow(4,6,-2,-6,fc='r',ec='r',**options)
ax.text(1,2,'$U_1$')
ax.text(3,1,'$-2U_1$')
ax.set_xlim(0,6)
ax.set_ylim(0,6)
ax.set_aspect('equal')
ax.grid(True,ls=':')
ax.set_xticks(np.arange(0,7,step = 1));
ax.set_yticks(np.arange(0,7,step = 1));
We can interpret the sum of two vectors as the result of aligning the two arrows tip to tail.
fig, ax = plt.subplots()
ax.arrow(0,0,1,3,fc='b',ec='b',**options)
ax.arrow(1,3,2,-1,fc='b',ec='b',**options)
ax.arrow(0,0,3,2,fc='r',ec='r',**options)
ax.text(1,2,'$U_1$')
ax.text(2,3,'$U_2$')
ax.text(2,1,'$U_1+U_2$')
ax.set_xlim(0,4)
ax.set_ylim(0,4)
ax.set_aspect('equal')
ax.grid(True,ls=':')
ax.set_xticks(np.arange(0,5,step = 1));
ax.set_yticks(np.arange(0,5,step = 1));
There are many other examples of vector spaces, but we will wait to provide these until after we have discussed more of the fundamental vector space concepts using
Linear Systems#
In this first chapter, we examine linear systems of equations and seek a method for their solution. We also introduce the machinery of matrix algebra which will be necessary in later chapters, and close with some applications.
A linear system of
Solutions to the linear system are collections of values for the unknowns that satisfy all all of the equations simultaneously. The set of all possible solutions for the system is known as its solution set.
Linear systems with two equations and two unknowns are a great starting point since we easily graph the sets of points that satisfy each equation in the
Example 1: System with a unique solution#
The solution set for each equation can be represented by a line, and the solution set for the linear system is represented by all points that lie on both lines. In this case the lines intersect at a single point and there is only one pair of values that satisfy both equations,
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-5,5,100)
fig, ax = plt.subplots()
ax.plot(x,(5-x)/3)
ax.plot(x,(5+x)/2)
ax.text(1,1.6,'$x_1+3x_2 = 5$')
ax.text(-3,0.5,'$x_1-2x_2 = -5$')
ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)
## This options specifies the ticks based the list of numbers provided.
ax.set_xticks(list(range(-4,5)))
ax.set_aspect('equal')
ax.grid(True,ls=':')
We are looking for a unique solution for the two variables
Exercises#
-Are the solution to the above system of equations scalars or vectors?
## Dicuss the question with your group mates. Ask us in case of doubts :)
Example 2: System with no solutions#
In this example the solution sets of the individual equations represent lines that are parallel. There is no pair of values that satisfy both equations simultaneously.
fig, ax = plt.subplots()
ax.plot(x,(5-x)/3)
ax.plot(x,-x/3)
ax.text(1,1.6,'$x_1+3x_2 = 5$')
ax.text(0.3,-1.4,'$x_1+3x_2 = 0$')
ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)
## This options specifies the ticks based the list of numbers provided.
ax.set_xticks(list(range(-4,5)))
ax.set_aspect('equal')
ax.grid(True,ls=':')
Example 3: System with an infinite number of solutions#
In the final example, the second equation is a multiple of the first equation. The solution set for both equations is represented by the same line and thus every point on the line is a solution to the linear system.
fig, ax = plt.subplots()
ax.plot(x,(5-x)/3)
ax.plot(x,(5-x)/3)
ax.text(1,1.6,'$x_1+3x_2 = 5$')
ax.text(-3,1.2,'$2x_1+6x_2 = 10$')
ax.set_xlim(-4,4)
ax.set_ylim(-2,6)
ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)
ax.set_xticks(list(range(-4,5)))
ax.set_aspect('equal')
ax.grid(True,ls=':')
These examples illustrate all of the possibile types of solution sets that might arise in a system of two equations with two unknowns. Either there will be exactly one solution, no solutions, or an infinite collection of solutions. A fundamental fact about linear systems is that their solution sets are always one of these three cases.
Inverse Matrices#
In this section we consider the idea of inverse matrices and describe a common method for their construction.
As a motivation for the idea, let’s again consider the system of linear equations written in the matrix form.
Again,
The answer is Not quite. We can make progress though by understanding that in the case that
We can extend this idea to the situation where
If
Notes about inverse matrices:
The matrix must be square in order for this definition to make sense. If
is not square, it is impossible for both and to be defined.Not all matrices have inverses. Matrices that do have inverses are called invertible matrices. Matrices that do not have inverses are called non-invertible, or singular, matrices.
If a matrix is invertible, its inverse is unique.
Now if we know
Then
Construction of an inverse matrix#
We take
Let’s think of the matrix product
Recall now that
Exercises#
Write down the matrix
using a numpy array
## Code solution here
# C = ...
Inverse matrices with SciPy#
The
from scipy import linalg
C_inverse = linalg.inv(C)
print(C_inverse)
## Your solution here
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[12], line 3
1 from scipy import linalg
----> 3 C_inverse = linalg.inv(C)
4 print(C_inverse)
NameError: name 'C' is not defined
Verify that C_inverse
is indeed the inverse matrix of C
by verifying that C@C_inverse
is the identity matrix
## Your solution here
Providing a non-invertible matrix to
Exercises#
Let
and be two random matrices. Demonstrate using Python that for the matrices.
## Code solution here
Discuss with your group mates what is the geometrical meaning of a matrix that can be inverted and a matrix that can not be inverted. Hint: remeber from the 3Blue1Brown videos that a matrix can represent a linear transformation.
Linear Combinations#
At the core of many ideas in linear algebra is the concept of a linear combination of vectors. To build a linear combination from a set of vectors
The scalars
Let’s define a collection of vectors to give concrete examples.
Now
The concept of linear combinations of vectors can be used to reinterpret the problem of solving linear systems of equations. Let’s consider the following system.
We’ve already discussed how this system can be written using matrix multiplication.
We’ve also seen how this matrix equation could be repackaged as a vector equation.
The connection to linear combinations now becomes clear if we consider the columns of the coefficient matrix as vectors. Finding the solution to the linear system of equations is equivalent to finding the linear combination of these column vectors that matches the vector on the right hand side of the equation.
Exercises#
Use scipy function linalg.solve() to solve the above sytem, that is, to find the values
and that makes the above equation true. Hint: You can check how to use scipy function in the scipy documentation.
## Your solution here
Use python to check that the obtained solution values for
## Your solution here
Linear Independence#
A set of vectors
it must be that
Example 1: Vectors in #
In order to determine if this set of vectors is linearly independent, we must examine the following vector equation.
Exercises#
Use scipy function linalg.solve() to solve the above sytem, that is, to find the values
and that makes the above equation true. Hint: You can check how to use scipy function in the scipy documentation.
## Hint: think of the geometrical interpretation of the system of two equations as two straight lines.
## Your solution here
Homogeneous systems#
A linear system is said to be homogeneous if it can be described with the matrix equation
In the previous examples we were solving the vector equation
Let us suppose that a homogeneous system
Consider the following system as an example.
We can look at the associated homogeneous system to determine if the columns of
Rank and Number of solutions of a system#
The rank of a system of linear equations is the maximal number of linearly independent vectors. If the rank is smaller than the number of vectores, then some vectors are linearly dependent: ie. they can be written as a linear combination of two other vectors.
If
If
If
If
Exercises#
Use the same scipy function as above to solve the homogenous and the non-homogenous systems above. Do they have the same set of solutions? Did you expect that?
## Code solution here.
Null space#
With the concept of homogeneous systems in place, we are ready to define the second fundamental subspace. If
The columns of a matrix
are linearly independent if and only if contains only the zero vector.The system
has at most one solution if and only if contains only the zero vector.
Making connections between the fundamental subspaces of
The connection between the null-space and the rank#
The Rank–nullity theorem tells us that there is the following relation between the rank and the nullity of a linar map
Equivalently, if you have a function
where the red area
Exercises#
Determine if the following set of vectors is linearly independent.
## Code solution here.
Hint: Let A be an n × n matrix. The following statements are equivalent:
1. A is invertible
2. Ax = b has a unique solution for all b in Rn.
3. Ax = 0 has only the solution x = 0.
4. rref(A) = In×n.
5. rank(A) = n.
6. nullity(A) = 0.
7. The column vectors of A span Rn.
8. The column vectors of A form a basis for Rn.
9. The column vectors of A are linearly independent.
10. The row vectors of A span Rn.
11. The row vectors of A form a basis for Rn.
12. The row vectors of A are linearly independent.
Orthogonalization#
Some of the most important applications of inner products involve finding and using sets of vectors that are mutually orthogonal. A set of nonzero vectors
Orthonormal sets must be linearly independent, so it makes sense to think of them as a basis for some vector subspace. Any collection of vectors from the standard bases of
In this section we will focus on a process called orthogonalization. Given a set of linearly independent vectors
One of the primary advantages of using orthonormal bases is that the calculation of coordinate vectors is greatly simplified. Recall that if we have a typical basis
All of the products
Projecting vectors onto vectors#
An important step in orthogonalization involves decomposing a vector
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
options = {"head_width":0.1, "head_length":0.2, "length_includes_head":True}
ax.arrow(0,0,2,3,fc='b',ec='b',**options)
ax.arrow(0,0,4,2,fc='b',ec='b',**options)
ax.arrow(0,0,2.8,1.4,fc='b',ec='r',**options)
ax.arrow(2.8,1.4,-0.8,1.6,fc='b',ec='r',**options)
ax.text(1,2,'$B$')
ax.text(3.2,1.2,'$V$')
ax.text(2,0.6,'$\hat{B}$')
ax.text(2.5,2.5,'$E$')
ax.text(1,1,'$\\theta$')
ax.set_xlim(0,5)
ax.set_xlabel('$x_1$')
ax.set_ylim(0,5)
ax.set_ylabel('$x_2$')
ax.set_aspect('equal')
ax.grid(True,ls=':')
The vector
To find the magnitude of
Combining these facts gives us
We can now construct
Finally, we can give a tidy formula by writing
Exercises#
Use numpy.dot() function to compute the vector
.Use the same function to compute the angle between
and
import numpy as np
B = np.array([2,3])
V = np.array([4,2])
## Your code here
Eigenvectors and eigenvalues#
“Eigenvalues are just the TLDR for a matrix”, @KyleMorgenstein
In this chapter we shift focus away from solving linear systems, and look closer at the effect of matrix multiplication. We restrict our attention now to square matrices, which define linear transformations from
Given a square
We will visualize examples in
Example 1: Matrix representing horizontal shear#
Let’s consider first the following matrix.
The multiplication by this matrix has the effect of a horizontal shear.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
options = {"head_width":0.1, "head_length":0.2, "length_includes_head":True}
ax.arrow(0,0,2,3,fc='b',ec='b',**options)
ax.arrow(0,0,4,3,fc='r',ec='r',**options)
ax.set_xlim(-1,5)
ax.set_ylim(-1,5)
ax.set_aspect('equal')
ax.set_xticks(np.arange(-1,6,step = 1))
ax.set_yticks(np.arange(-1,6,step = 1))
ax.text(0.8,2.2,'$X$')
ax.text(4,2.2,'$AX$')
ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)
ax.grid(True,ls=':')
For this example it is possible to deduce the eigenvalues and eigenvectors since the effect of the matrix is rather simple. Any vector that does not have its direction changed by this transformation is an eigenvector. In particular, we notice that any vector along the
We can calculate and observe that
We now consider if any other vectors whose directions are unchanged by the transformation.
After a bit of thought, we realize that any vector along the
Since
Example 2: Projection matrix#
For the next example, let’s consider a matrix which projects vectors orthogonally onto its one-dimensional column space.
B = np.array([[0.2, -0.4],[-0.4, 0.8]])
X = np.array([[1],[2]])
Y = np.array([[2],[1]])
print(B@X)
print(B@Y)
fig, ax = plt.subplots()
x=np.linspace(-6,6,100)
options = {"head_width":0.1, "head_length":0.2, "length_includes_head":True}
ax.arrow(0,0,1,2,fc='b',ec='b',**options)
ax.arrow(0,0,-0.6,1.2,fc='r',ec='r',**options)
ax.plot(x,-2*x,ls=':')
ax.set_xlim(-3,3)
ax.set_ylim(-2,4)
ax.set_aspect('equal')
ax.set_xticks(np.arange(-3,4,step = 1))
ax.set_yticks(np.arange(-2,5,step = 1))
ax.text(0.8,2.2,'$X$')
ax.text(-1,1.2,'$BX$')
ax.text(0.85,-1,'$\mathcal{C}(B)$')
ax.axvline(color='k',linewidth = 1)
ax.axhline(color='k',linewidth = 1)
ax.grid(True,ls=':')
In this example we can again determine the eigenvectors and corresponding eigenvalues by considering the transformation represented by the matrix. Any vectors in
Since
The other eigenvalue for this matrix may not be as easy to discover by examining the directions of various choices of
In this example, since
The eigenvalue corresponding to
Exercises#
Determine the eigenvalues and corresponding eigenvectors of the following matrix by considering the transformation that it represents. Check your answers by computing them with python
Hint: see how to use numpy.linalg.eig() in the numpy documentation
## Code solution here.