Tutorial 6 (AY24/25 Sem 1)¶
# Required imports
import sympy as sym
from ma1522 import Matrix
S = Matrix.from_str("1 2 -1; 0 2 1; 0 -1 3").T
S
S.get_linearly_independent_vectors(verbosity=1)
Before RREF: [self]
After RREF:
Select columns of self corresponding to pivot positions.
(b)¶
Suppose $\mathbf{w} = \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix}$. Find the coordinate vector of $\mathbf{w}$ relative to $S$.
w = Matrix.from_str("1; 1; 1")
w.coords_relative(S, verbosity=2)
Before RREF: [to | self]
After RREF:
(c)¶
Let $T = \{\mathbf{v}_1, \mathbf{v}_2, \mathbf{v}_3\}$ be another basis for $\mathbb{R}^3$ where $\mathbf{v}_1 = \begin{pmatrix} 1 \\ 5 \\ 4 \end{pmatrix}$, $\mathbf{v}_2 = \begin{pmatrix} -1 \\ 3 \\ 7 \end{pmatrix}$, $\mathbf{v}_3 = \begin{pmatrix} 2 \\ 2 \\ 4 \end{pmatrix}$. Find the transition matrix from $T$ to $S$.
T = Matrix.from_str("1 5 4; -1 3 7; 2 2 4").T
P_T_to_S = T.transition_matrix(S, verbosity=2)
P_T_to_S
Before RREF: [to | self]
After RREF:
(d)¶
Find the transition matrix from $S$ to $T$.
P_S_to_T = S.transition_matrix(T, verbosity=2)
P_S_to_T
Before RREF: [to | self]
After RREF:
## Alternatively
P_S_to_T = P_T_to_S.inverse()
P_S_to_T
(e)¶
Use the vector $\mathbf{w}$ in Part (b). Find the coordinate vector of $\mathbf{w}$ relative to $T$.
w.coords_relative(T, verbosity=2)
Before RREF: [to | self]
After RREF:
A = Matrix.from_str("1 -1 1; 1 1 -1; -1 -1 1")
b = Matrix.from_str("2; 1; 0")
try:
b.coords_relative(A, verbosity=2)
except ValueError as e:
print(f"Error: {e}") # b is not in the column space of A
Before RREF: [to | self]
After RREF:
Error: No solution found due to inconsistent system.
(b)¶
Let $\mathbf{A} = \begin{pmatrix} 1 & 9 & 1 \\ -1 & 3 & 1 \\ 1 & 1 & 1 \end{pmatrix}$ and $\mathbf{b} = \begin{pmatrix}5 & 1 & -1\end{pmatrix}$. Is $\mathbf{b}$ in the row space of $\mathbf{A}$? If it is, express it as a linear combination of the rows of $\mathbf{A}$.
A = Matrix.from_str("1 9 1; -1 3 1; 1 1 1")
b = Matrix.from_str("5 1 -1")
scalars = b.T.coords_relative(A.T, verbosity=2)
Before RREF: [to | self]
After RREF:
scalars # coordinates relative gives the coefficients of the linear combination
(c)¶
Let $\mathbf{A} = \begin{pmatrix} 1 & 2 & 0 & 1 \\ 0 & 1 & 2 & 1 \\ 1 & 2 & 1 & 3 \\ 0 & 1 & 2 & 2 \end{pmatrix}$. Is the row space and column space of $\mathbf{A}$ the whole $\mathbb{R}^4$?
A = Matrix.from_str("1 2 0 1; 0 1 2 1; 1 2 1 3; 0 1 2 2")
A
A.is_same_subspace(verbosity=2) # other defaults to R^n
Check rref(self) does not have zero rows Before RREF: self
After RREF:
True
A.T.is_same_subspace(verbosity=2) # other defaults to R^n
Check rref(self) does not have zero rows Before RREF: self
After RREF:
True
Problem 4¶
For each of the following matrices $\mathbf{A}$,
(i) Find a basis for the row space of $\mathbf{A}$.
(ii) Find a basis for the column space of $\mathbf{A}$.
(iii) Find a basis for the nullspace of $\mathbf{A}$.
(iv) Hence determine $\text{rank}(\mathbf{A})$, $\text{nullity}(\mathbf{A})$ and verify the dimension theorem for matrices.
(v) Is $\mathbf{A}$ full rank?
(a)¶
$\mathbf{A} = \begin{pmatrix} 1 & 2 & 5 & 3 \\ 1 & -4 & -1 & -9 \\ -1 & 0 & -3 & 1 \\ 2 & 1 & 7 & 0 \\ 0 & 1 & 1 & 2 \end{pmatrix}$
A = Matrix.from_str("1 2 5 3; 1 -4 -1 -9; -1 0 -3 1; 2 1 7 0; 0 1 1 2")
A
# Row space
A.get_linearly_independent_vectors(colspace=False, verbosity=2)
Before RREF: [self^T]
After RREF:
Select rows of self corresponding to pivot positions.
# Column space
A.get_linearly_independent_vectors(colspace=True, verbosity=2)
Before RREF: [self]
After RREF:
Select columns of self corresponding to pivot positions.
A.nullspace(verbosity=2)
Before RREF: [self]
After RREF:
A.rank(), A.nullity()
(b)¶
$\mathbf{A} = \begin{pmatrix} 1 & 3 & 7 \\ 2 & 1 & 8 \\ 3 & -5 & -1 \\ 2 & -2 & 2 \\ 1 & 1 & 5 \end{pmatrix}$
A = Matrix.from_str("1 3 7; 2 1 8; 3 -5 -1; 2 -2 2; 1 1 5")
A
# Alternatively, you can use `simplify_basis` to get a simple basis
# that is not necessarily a row or column of the original matrix.
rowspace = A.simplify_basis(colspace=False, verbosity=2)
rowspace
Before RREF: self
After RREF:
colspace = A.simplify_basis(colspace=True, verbosity=2)
colspace
Before RREF: self^T
After RREF:
A.nullspace(verbosity=2) # UserWarning if the nullspace is trivial
Before RREF: [self]
After RREF:
/tmp/ipykernel_2385/40416061.py:1: UserWarning: Only trivial nullspace (0-vector) detected! A.nullspace(verbosity=2) # UserWarning if the nullspace is trivial
A.rank(), A.nullity()
Question 5¶
Let $W$ be a subspace of $\mathbb{R}^5$ spanned by the following vectors $$\mathbf{u}_1 = \begin{pmatrix} 1 \\ -2 \\ 0 \\ 0 \\ 3 \end{pmatrix}, \quad \mathbf{u}_2 = \begin{pmatrix} 2 \\ -5 \\ -3 \\ -2 \\ 6 \end{pmatrix}, \quad \mathbf{u}_3 = \begin{pmatrix} 0 \\ 5 \\ 15 \\ 10 \\ 0 \end{pmatrix}, \quad \mathbf{u}_4 = \begin{pmatrix} 2 \\ 1 \\ 15 \\ 8 \\ 6 \end{pmatrix}$$
(a)¶
Find a basis for $W$.
W = Matrix.from_str("1 -2 0 0 3; 2 -5 -3 -2 6; 0 5 15 10 0; 2 1 15 8 6").T
W
W.get_linearly_independent_vectors(colspace=True, verbosity=2)
Before RREF: [self]
After RREF:
Select columns of self corresponding to pivot positions.
# OR
W.simplify_basis(colspace=True, verbosity=2)
Before RREF: self^T
After RREF:
(c)¶
Extend the basis $W$ found in (a) to a basis for $\mathbb{R}^5$.
W.extend_basis(verbosity=2) # span_subspace=None extends to R^n by default
Before RREF: [self | span_subspace]
After RREF:
Select columns of rref([self | span_subspace]) corresponding to pivot positions.
# Alternatively, use the `simplify_basis` method first for a simpler basis overall
W.simplify_basis(colspace=True, verbosity=0).extend_basis(verbosity=2)
Before RREF: [self | span_subspace]
After RREF:
Select columns of rref([self | span_subspace]) corresponding to pivot positions.
Question 6¶
Let $S = \left\{ \begin{pmatrix} 1 \\ 0 \\ 1 \\ 3 \end{pmatrix}, \begin{pmatrix} 2 \\ -1 \\ 0 \\ 1 \end{pmatrix}, \begin{pmatrix} -1 \\ 3 \\ 5 \\ 12 \end{pmatrix}, \begin{pmatrix} 0 \\ 1 \\ 2 \\ 5 \end{pmatrix}, \begin{pmatrix} 3 \\ -1 \\ 1 \\ 4 \end{pmatrix} \right\}$ and $V = \text{span}(S)$. Find a subset $S' \subseteq S$ such that $S'$ forms a basis for $V$.
V = Matrix.from_str("1 0 1 3; 2 -1 0 1; -1 3 5 12; 0 1 2 5; 3 -1 1 4").T
V
S_prime = V.get_linearly_independent_vectors(colspace=True, verbosity=2)
S_prime
Before RREF: [self]
After RREF:
Select columns of self corresponding to pivot positions.