Exercises – Long Day#
Exercise 1: Intro to Systems of Linear Equations with SymPy#
In Exercise 6 from Short Day in Week 7 the following three systems of linear equations over \(\mathbb R\) were investigated:
We now want to investigate these three systems again, this time using the CAS tool SymPy. Open a Jupyter notebook and begin by running the command from sympy import *
. The command enables the use of SymPy commands.
Question a#
The augmented matrix \(\mathbf T_1\) of linear equation system no. 1 can be defined in SymPy as follows:
T1=Matrix([[1,1,1,1],[1,2,4,1],[1,3,9,1]])
.
Try this command. To see the matrix on your screen, just type T1
. To compute its reduced row-echelon form, type T1.rref()
. Note that “rref” is simply an abbreviation of “reduced row-echelon form”. How should we interpret the output?
Answer
The output is
(Matrix([[1,0,0,1],[0,1,0,0],[0,0,1,0]]),(0,1,2))
.
The first part of the output, Matrix([[1,0,0,1],[0,1,0,0],[0,0,1,0]])
, is the reduced row-echelon form of matrix \(\mathbf T_1\). The other part, (0,1,2)
, tells which columns of the reduced row-echelon form that contain pivot elements. Remember that Python uses zero-indexing, meaning that it in this case is the 1st, 2nd, and 3rd columns of the reduced row-echelon form that contain pivot elements.
If you use the command T1.rref(pivots=False)
, then you will only see the reduced row-echelon form of \(\mathbf T_1\). Try this command as well.
Question b#
The solution can be read directly from the reduced row-echelon form of \(\mathbf T_1\). One can also solve the system directly in SymPy. First, the system’s coefficient matrix A=Matrix([[1,1,1],[1,2,4],[1,3,9]])
must be defined as well as the system’s right-hand side b=Matrix([[1],[1],[1]])
. Now test the command linsolve((A,b))
and interpret the output.
Answer
The output is the solution set to linear equation system no. 1.
Question c#
Now try on your own to go through the same steps as in Questions a and b, this time for equation system no. 2. Which columns of the reduced row-echelon form of its augmented matrix \(\mathbf T_2\) contain a pivot element? What is the solution set to the system?
Hint
The command T2=Matrix([[1,1,1,1],[1,2,4,1],[1,3,7,1]])
defines the augmented matrix of the second system.
Answer
The reduced row-echelon form of \(\mathbf T_2\) only has two pivot elements, found in its 1st and 2nd columns. SymPy’s suggestion for a solution set is \(\{(2\tau_0+1,-3\tau_0,\tau_0)\}\). Note how SymPy uses \(\tau_0\) to denote a free parameter (\(\tau_0\), \(\tau_1\) etc. when multiple free parameters are needed). Also note that the solution set of the system actually is \(\{(2\tau_0+1,-3\tau_0,\tau_0) \, \mid \, \tau_0 \in \mathbb{R}\}.\)
Question d#
Now repeat this one more time for equation system no. 3. Which columns of the reduced row-echelon form of its augmented matrix contain pivot elements? What is the solution set to the system?
Answer
The reduced row-echelon form of the system’s augmented matrix has three pivot elements, found in its 1st, 2nd, and 4th columns. SymPy correctly outputs \(\emptyset\), the empty set, as the solution set.
Exercise 2: Linear Combinations#
We are given the following vectors in \(\mathbb{R}^3\):
Question a#
Compute by hand the following linear combinations of the three vectors \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}.\)
\(4{\mathbf v}.\)
\(2{\mathbf u}-3{\mathbf w}\).
\(3{\mathbf u}-2{\mathbf v}+2{\mathbf w}\).
Answer
- \[\begin{split}4{\mathbf v}=\left[ \begin{array}{c} 28\\ 16\\ 12\\ \end{array} \right]. \end{split}\]
Question b#
Determine whether the vectors \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}\) are linearly independent based on the answers from Question a.
Hint
In Definition 7.1.1 in the textbook you can find a description of what it means for vectors to be linearly independent/dependent. Equations (7-3) and (7-4) can also be useful to have a look at.
Answer
The vectors \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}\) are not linearly independent (they are linearly dependent) since no. 3 from Question a as well as Equation (7-4) from the textbook show that they can be written as a linear combination with non-zero coefficients.
Question c#
Show that the vectors \({\mathbf u}\) and \({\mathbf v}\) are linearly independent.
Hint
First, consider Equation (7-3) in the textbook. If \(c_1,c_2 \in \mathbb{R}\) fulfill that
can we then show that \(c_1=c_2=0\)?
Answer
The vectors \({\mathbf u}\) and \({\mathbf v}\) are linearly independent since they cannot be written as a linear combination with non-zero coefficients.
Exercise 3: Linearly Independent Vectors in \(\mathbb{R}^4\)#
The following three vectors in \(\mathbb{R}^4\) are given:
Question a#
Show that the vectors \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}\) are linearly independent.
Hint
Assume that
What can we say about \(c_1, c_2,\) and \(c_3\)?
Question b#
Determine a vector \({\mathbf b} \in \mathbb{R}^4\) such that the vectors \({\mathbf u}, {\mathbf v}, {\mathbf w},\) and \({\mathbf b}\) are linearly independent.
Question c#
Assume that \({\mathbf c} \in \mathbb{R}^4\) fulfills that the vectors \({\mathbf u}, {\mathbf v}, {\mathbf w},\) and \({\mathbf c}\) are linearly independent. Show that if so, then vector \(\mathbf c\) cannot be written as a linear combination of \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}\).
Hint
What we are showing is logically equivalent to the proposition: if \({\mathbf c}\) can be written as a linear combination of \({\mathbf u}, {\mathbf v},\) and \({\mathbf w}\) then the vectors \({\mathbf u}, {\mathbf v}, {\mathbf w},\) and \({\mathbf c}\) are linearly dependent.
Hint
If \({\mathbf c}=c_1\cdot {\mathbf u}+c_2\cdot {\mathbf v}+c_3\cdot {\mathbf w}\) for some \(c_1,c_2,c_3 \in \mathbb{R}\), then it applies that
Exercise 4: Linear Independency and Reduced Row-Echelon Form#
We are given the following four vectors in \(\mathbb{C}^4\):
Question a#
Define in SymPy the \(4 \times 4\) matrix \(\mathbf A\) that has columns \({\mathbf v}_1, {\mathbf v}_2, {\mathbf v}_3,\) and \({\mathbf v}_4\). Use SymPy to compute the reduced row-echelon form of the matrix. Use this row-echelon form to determine the rank of \({\mathbf A}\).
Answer
The reduced row-echelon form of \(\mathbf A\) is
This means that the reduced row-echelon form of \(\mathbf A\) is \({\mathbf I}_4\). The echelon form has four pivot elements, meaning that \(\rho(\mathbf A)=4.\)
Question b#
Are the given vectors \({\mathbf v}_1, {\mathbf v}_2, {\mathbf v}_3,\) and \({\mathbf v}_4\) linearly independent?
Hint
Theorem 7.1.3 in the textbook can be of use here.
Answer
Yes, they are linearly independent.
Question 5: Matrix Product#
We are given the following three matrices:
Question a#
Which of the nine combinations of matrix products \({\mathbf A}\cdot{\mathbf A}\), \(\, {\mathbf A}\cdot{\mathbf B}\), \(\, {\mathbf A}\cdot{\mathbf C}\), \(\, {\mathbf B}\cdot{\mathbf A}\), \(\, {\mathbf B}\cdot{\mathbf B}\), \(\, {\mathbf B}\cdot{\mathbf C}\), \(\, {\mathbf C}\cdot{\mathbf A}\), \(\, {\mathbf C}\cdot{\mathbf B}\), \(\, {\mathbf C}\cdot{\mathbf C}\) are defined?
Hint
According to Definition 7.2.2 in the textbook, one can only multiply two matrices if the number of columns in the first matrix is equal to the number of rows in the second matrix.
Answer
Only the following matrix products can be carried out: \({\mathbf A}\cdot{\mathbf B}, \, {\mathbf A}\cdot{\mathbf C}, \, {\mathbf B}\cdot{\mathbf A}, \, {\mathbf C}\cdot{\mathbf B}, \, {\mathbf C}\cdot{\mathbf C}\).
Question b#
Compute by hand the matrix products \({\mathbf A}\cdot{\mathbf B}\) and \({\mathbf B}\cdot{\mathbf A}\). Does it apply that \({\mathbf A}\cdot{\mathbf B}={\mathbf B}\cdot{\mathbf A}\)?
Answer
It is clear from the answer that \({\mathbf A}\cdot{\mathbf B} \neq {\mathbf B}\cdot{\mathbf A}\). The matrices \({\mathbf A}\cdot{\mathbf B}\) and \({\mathbf B}\cdot{\mathbf A}\) do not even have the same size!
Question c#
Compute the product of the scalar \(-1/2\) and the matrix \(\mathbf C\). In other words, compute \((-1/2) \cdot {\mathbf C}\).
Answer
Question d#
Determine whether the following sums of matrices are defined, and compute those that are: \((-1/2) \cdot {\mathbf C}+{\mathbf A} \cdot {\mathbf B}\) and \((-1/2) \cdot {\mathbf C}+{\mathbf B} \cdot {\mathbf A}.\)
Answer
The sum \((-1/2) \cdot {\mathbf C}+{\mathbf A} \cdot {\mathbf B}\) is not defined since \((-1/2) \cdot {\mathbf C}\) is a \(3 \times 3\) matrix while \({\mathbf A} \cdot {\mathbf B}\) is a \(2 \times 2\) matrix.
The sum \((-1/2) \cdot {\mathbf C}+{\mathbf B} \cdot {\mathbf A}\) is defined since the matrices \((-1/2) \cdot {\mathbf C}\) and \({\mathbf A} \cdot {\mathbf B}\) have the same size. The sum is:
Exercise 6: Matrix-Vector Product and Systems of Linear Equations#
We are given the linear equation system
Question a#
Write the system in the form as in Equation (7-5) in Chapter 7 of the textbook.
Answer
Question b#
We define
Check that the given linear equation system has the augmented matrix \([{\mathbf A}|{\mathbf b}]\). In Exercise 3b from Short Day in Week 7 we checked by hand that the linear equation system with augmented matrix \([{\mathbf A}|{\mathbf b}]\) has the following particular solution:
Check this again, this time by computing the product \({\mathbf A}\cdot{\mathbf v}\) in SymPy.
Hint
First define the matrix \(\mathbf A \in \mathbb{R}^{4 \times 4}\) and the vector \(\mathbf v \in \mathbb{R}^4\) in SymPy. Such a product of a matrix \(\mathbf A\) and a vector \(\mathbf v\) is computed in SymPy with the command A*v
.
Question c#
Let \({\mathbf A}\) and \({\mathbf v}\) be the same as before. The product \({\mathbf v}\cdot{\mathbf A}\) is not defined. What happens if you try to compute the product in SymPy anyways?
Answer
You should get an error message ending with Matrix size mismatch: (4, 1) * (4, 4)
, which makes sense since a vector \(\mathbf v \in \mathbb{R}^4\) can be considered a \(4 \times 1\) matrix, while \(\mathbf A\) is a \(4 \times 4\) matrix.
Exercise 7: Computing Inverse Matrices#
We are given the following square matrix:
Question a#
Determine by hand whether the matrix \({\mathbf A}^{-1}\) exists, and if so then compute it (still by hand).
Hint
One can follow the procedure as explained just before Example 7.3.1 in the textbook. Examples 7.3.1 and 7.3.2 are useful if you would like to see some examples.
Answer
The reduced row-echelon form of the matrix \([{\mathbf A}|{\mathbf I}_3]\) is
Thus \({\mathbf A}^{-1}\) exists, and we find it to be:
Question b#
Check your computations with SymPy.
Hint
If A
is a matrix defined in SymPy, then the command A**(-1)
will compute the inverse matrix, if it exists. If the inverse matrix does not exist, then you will get an error message.
Exercise 8: Inverse Matrices and Systems of Linear Equations#
Let \({\mathbf A} \in \mathbb{C}^{n \times n}\) be a square matrix and assume that \({\mathbf A}\) is an invertible matrix. Furthermore, let \({\mathbf b} \in \mathbb{C}^n\) be a vector.
Question a#
Show that the vector \({\mathbf A}^{-1}\cdot {\mathbf b} \in \mathbb{C}^n\) is a solution to the linear equation system with augmented matrix \([{\mathbf A}|{\mathbf b}].\)
Hint
First write the equation system in matrix form as in Equation (7-5) in the textbook.
Hint
In matrix form the system looks as follows:
Is this fulfilled if
Question b#
Show that the vector \({\mathbf A}^{-1} \cdot {\mathbf b}\) is the only solution to the linear equation system with augmented matrix \([{\mathbf A}|{\mathbf b}].\)
Hint
Multiply the equation
with \({\mathbf A}^{-1}\) on both sides of the equal side from the left.
Exercise 9: Equation Systems with a Variable Coefficient#
We are for every real value of \(a\) given the linear equation system
Question a#
State the augmented matrix of the system.
Question b#
What is the solution to the system in the specific case where \(a=-2\).
Answer
There is no solution to the system when \(a=-2.\)
Question c#
Determine the solution to the linear equation system for all \(a\).
Hint
Perform usual row operations, but be careful when you want to divide by an expression that includes the unknown. Note down those cases of \(a\) that would result in a division by zero. Then treat those special-cases separately afterwards.
Answer
For \(a\in\mathbb R\setminus\{-2,1\}\) the solution is \((x_1,x_2,x_3)=(\frac1{a+2},\frac1{a+2},\frac1{a+2})\), and for \(a=1\) the solution is \((x_1,x_2,x_3)=(1,0,0)+t_1(-1,1,0)+t_2(-1,0,1)\quad,t_1,t_2\in\mathbb R\). There are no solutions for \(a=-2\).
Question d#
Can SymPy solve the exercise?
Answer
If you use SymPy’s .rref()
or linsolve()
, then SymPy will ignore the special-cases that you caught in your manual work in Question c. So, even though SymPy does give some of the solutions as its output, it does not give all solutions - but even worse, you are not even aware that there are other solutions than those in the output for some values of \(a\). We must therefore be very careful when using such CAS tools in cases where unknowns are involved.