Tutorial 07 (AY24/25 Sem 1)¶
# Required imports
import sympy as sym
from ma1522 import Matrix
# set n = 4
a = Matrix.create_unk_matrix(r=4, symbol="a")
x = Matrix.create_unk_matrix(r=4, symbol="x")
a, x
(b)¶
Find the solution set of the linear system $$\begin{cases} x_1 &+& 3x_2 &-& 2x_3 && &=& 0 \\ 2x_1 &+& 6x_2 &-& 5x_3 &-& 2x_4 &=& 0 \\ && && 5x_3 &+& 10x_4 &=& 0 \end{cases}$$
mat = Matrix.from_str("1 3 -2 0; 2 6 -5 -2; 0 0 5 10")
aug = Matrix.zeros(rows=3, cols=1)
mat, aug
sol = mat.solve(aug)
sol
sol.sep_unk()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[5], line 1 ----> 1 sol.sep_unk() AttributeError: 'list' object has no attribute 'sep_unk'
# Alternatively, use nullspace to find the solution set
mat.nullspace(verbosity=1)
Before RREF: [self]
After RREF:
(c)¶
Find a nonzero vector $\mathbf{v} \in \mathbb{R}^4$ such that $\mathbf{a}_1 \cdot \mathbf{v} = 0$, $\mathbf{a}_2 \cdot \mathbf{v} = 0$, and $\mathbf{a}_3 \cdot \mathbf{v} = 0$, where $$\mathbf{a}_1 = \begin{pmatrix} 1 \\ 3 \\ -2 \\ 0 \end{pmatrix}, \quad \mathbf{a}_2 = \begin{pmatrix} 2 \\ 6 \\ -5 \\ -2 \end{pmatrix}, \quad \mathbf{a}_3 = \begin{pmatrix} 0 \\ 0 \\ 5 \\ 10 \end{pmatrix}$$
A = Matrix.from_str("1 3 -2 0; 2 6 -5 -2; 0 0 5 10").T
v = Matrix.create_unk_matrix(r=4, symbol="v")
A, v
# Fast method to find v without showing the steps
sol = sym.solve(A.T @ v, v)
v.subs(sol)
# Alternative method using `orthogonal_complement`
mat = A.orthogonal_complement(verbosity=1)
mat
Before RREF: [self]
After RREF:
free_params = Matrix.create_unk_matrix(r=2)
mat @ free_params
Question 3¶
Let $\mathbf{v}_1 = \begin{pmatrix} 1 \\ 2 \\ \ -1 \end{pmatrix}$, $\mathbf{v}_2 = \begin{pmatrix} 1 \\ 0 \\ 1 \end{pmatrix}$, and $\mathbf{V} = \begin{pmatrix} \mathbf{v}_1 & \mathbf{v}_2 \end{pmatrix}$.
(a)¶
Compute $\mathbf{v}_1 \cdot \mathbf{v}_1$, $\mathbf{v}_1 \cdot \mathbf{v}_2$, $\mathbf{v}_2 \cdot \mathbf{v}_1$ and $\mathbf{v}_2 \cdot \mathbf{v}_2$.
v1 = Matrix.from_str("1; 2; -1")
v2 = Matrix.from_str("1; 0; 1")
v1, v2
# use `dot` method for explicit dot product
v1.dot(v1), v1.dot(v2), v2.dot(v1), v2.dot(v2)
(b)¶
Compute $\mathbf{V}^T \mathbf{V}$. What do the entries of $\mathbf{V}^T \mathbf{V}$ represent?
V = v1.row_join(v2)
V.T @ V
Question 4¶
Let $W$ be a subspace of $\mathbb{R}^n$. The orthogonal complement of $W$, denoted as $W^{\perp}$, is defined to be $$W^{\perp} := \{ \mathbf{v} \in \mathbb{R}^n : \mathbf{v} \cdot \mathbf{w} = 0 \text{ for all } \mathbf{w} \in W \}$$
Let $\mathbf{w}_1 = \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \\ 1 \end{pmatrix}$, $\mathbf{w}_2 = \begin{pmatrix} 1 \\ 2 \\ -1 \\ -2 \\ 0 \end{pmatrix}$, and $\mathbf{w}_3 = \begin{pmatrix} 1 \\ -1 \\ 1 \\ -1 \\ 0 \end{pmatrix}$, and $W = \text{span}\{\mathbf{w}_1, \mathbf{w}_2, \mathbf{w}_3\}$.
(a)¶
Show that $S = \{\mathbf{w}_1, \mathbf{w}_2, \mathbf{w}_3\}$ is linearly independent.
S = Matrix.from_str("1 1 1 1 1; 1 2 -1 -2 0; 1 -1 1 -1 0").T
S
S.is_linearly_independent(verbosity=2)
Before RREF: self
After RREF:
Check if Number of columns (3) == Number of pivot columns (3)
True
(b)¶
Show that $S$ is orthogonal.
S.is_vec_orthogonal(verbosity=1)
Check if [self^T @ self] is a diagonal matrix
True
(c)¶
Show that $W^{\perp}$ is a subspace of $\mathbb{R}^5$ by showing that it is a span of a set. What is the dimension? (Hint: See Question 1.)
W_perp = S.orthogonal_complement(verbosity=1)
W_perp
Before RREF: [self]
After RREF:
(d)¶
Obtain an orthonormal set $T$ by normalizing $\mathbf{w}_1$, $\mathbf{w}_2$, $\mathbf{w}_3$.
# `normalized` is a mutable method, so use `copy` to avoid modifying S
T = S.copy().normalized()
T
# Use ScalarFactor so that it is easier to read the matrix
T = S.copy().normalized(factor=True)
T
(e)¶
Let $\mathbf{v} = \begin{pmatrix} 2 \\ 0 \\ 1 \\ 1 \\ -1 \end{pmatrix}$. Find the projection of $\mathbf{v}$ onto $W$.
v = Matrix.from_str("2; 0; 1; 1; -1")
W = S # W is the subspace spanned by S
# `proj_comp` solves least squares solution under the hood and
# does not require W to be a set of orthonormal column vectors
v_proj = v.proj_comp(W, verbosity=1)
self.T @ self is invertible. The lest squares solution is unique.
Projected component: Au
Normal component: b - b_proj
(f)¶
Let $\mathbf{v}_W$ be the projection of $\mathbf{v}$ onto $W$. Show that $\mathbf{v} - \mathbf{v}_W$ is in $W^{\perp}$.
v_norm = v - v_proj
aug_mat = W_perp.row_join(v_norm)
aug_mat
aug_mat.rref() # consistent system, so v_norm is in W_perp
Question 5¶
Let $S = \{\mathbf{u}_1, \mathbf{u}_2, \mathbf{u}_3, \mathbf{u}_4\}$ where $$\mathbf{u}_1 = \begin{pmatrix} 1 \\ 2 \\ 2 \\ -1 \end{pmatrix}, \quad \mathbf{u}_2 = \begin{pmatrix} 1 \\ 1 \\ -1 \\ 1 \end{pmatrix}, \quad \mathbf{u}_3 = \begin{pmatrix} -1 \\ 1 \\ -1 \\ -1 \end{pmatrix}, \quad \mathbf{u}_4 = \begin{pmatrix} -2 \\ 1 \\ 1 \\ 2 \end{pmatrix}$$
(a)¶
Check that $S$ is an orthogonal basis for $\mathbb{R}^4$.
S = Matrix.from_str("1 2 2 -1; 1 1 -1 1; -1 1 -1 -1; -2 1 1 2").T
S
S.is_vec_orthogonal(verbosity=1)
Check if [self^T @ self] is a diagonal matrix
True
(b)¶
Is it possible to find a nonzero vector $\mathbf{w}$ in $\mathbb{R}^4$ such that $S \cup {\mathbf{w}}$ is an orthogonal set?
w = Matrix.orthogonal_complement(S, verbosity=1) # No orthogonal complement found
Before RREF: [self]
After RREF:
/home/runner/work/linear-algebra/linear-algebra/src/ma1522/symbolic.py:2870: UserWarning: Only trivial nullspace (0-vector) detected! return Matrix.from_list(self.transpose().nullspace(verbosity))
(c)¶
Obtain an orthonormal set $T$ by normalizing $\mathbf{u}_1$, $\mathbf{u}_2$, $\mathbf{u}_3$, $\mathbf{u}_4$.
T = S.copy().normalized(factor=True)
T
(d)¶
Let $\mathbf{v} = \begin{pmatrix} 0 \\ 1 \\ 2 \\ 3 \end{pmatrix}$. Find $[\mathbf{v}]_S$ and $[\mathbf{v}]_T$.
v = Matrix.from_str("0; 1; 2; 3")
v
v_S = v.coords_relative(S, verbosity=2)
v_S
Before RREF: [to | self]
After RREF:
T = T.eval() # Convert the ScalarFactor to a regular matrix
v_T = v.coords_relative(T, verbosity=2)
v_T
Before RREF: [to | self]
After RREF:
(e)¶
Suppose $\mathbf{w}$ is a vector in $\mathbb{R}^4$ such that $[\mathbf{w}]_S = \begin{pmatrix} 1 \\ 2 \\ 1 \\ 1 \end{pmatrix}$. Find $[\mathbf{w}]_T$.
w_S = Matrix.from_str("1; 2; 1; 1")
w_S
# Quick and dirty way to find w_T
w = S @ w_S
w
w_T = w.coords_relative(T, verbosity=2)
w_T
Before RREF: [to | self]
After RREF:
# Alternative way via `transition_matrix`
S_to_T = S.transition_matrix(T, verbosity=2)
S_to_T
Before RREF: [to | self]
After RREF:
w_T = S_to_T @ w_S
w_T