Tutorial 09 (AY24/25 Sem 1)¶
# Required imports
import sympy as sym
from ma1522 import Matrix
Question 1¶
A father wishes to distribute an amount of money among his three sons Jack, Jim, and John. He wishes to distribute such that the following conditions are all satisfied.
(i) The amount Jack receives plus twice the amount Jim receives is $300.
(ii) The amount Jim receives plus the amount John receives is $300.
(iii) Jack receives $300 more than twice of what John receives.
(a)¶
Is it possible for the following conditions to all be satisfied?
mat = Matrix.from_str("1 2 0; 0 1 1; 1 0 -2")
aug = Matrix.from_str("300; 300; 300")
mat, aug
aug.is_subspace_of(mat, verbosity=2)
Check if span(self) is subspace of span(other) Before RREF: [other | self]
After RREF:
Span(self) is not a subspace of span(other).
False
(b)¶
If it is not possible, find a least square solution. (Make sure that your least square solution is feasible. For example, one cannot give a negative amount of money to anybody.)
sol = mat.solve_least_squares(aug, verbosity=2)
sol
Before RREF: [self.T @ self | self.T @ rhs]
After RREF
sol.sep_part_gen()
A = Matrix.from_str("1 1 0; 1 1 0; 1 1 1; 0 1 1")
A
# To get the full QR decomposition (as computed by MATLAB), set `full=True`
A.QRdecomposition(full=True, verbosity=0)
X = Matrix.from_str("1 1 2; 1 2 1; 2 1 1")
X
X**3 - 4 * X**2 - X + 4 * Matrix.eye(3)
(b)¶
Find the characteristic polynomial of $X$.
X.cpoly()
Question 4¶
For each of the following matrices $\mathbf{A}$, determine if $\mathbf{A}$ is diagonalizable. If $\mathbf{A}$ is diagonalizable, find an invertible $\mathbf{P}$ that diagonalizes $\mathbf{A}$ and determine $\mathbf{P}^{-1}\mathbf{A}\mathbf{P}$.
(a)¶
$\mathbf{A} = \begin{pmatrix} 1 & -3 & 3 \\ 3 & -5 & 3 \\ 6 & -6 & 4 \end{pmatrix}$
A = Matrix.from_str("1 -3 3; 3 -5 3; 6 -6 4")
A
sym.Matrix(A).is_diagonalizable()
True
# To check diagaonalization
# Take note that the `reals_only` argument is set to `True` by default
# in accordance with MA1522 syllabus. This overrides SymPy's default
# behavior of allowing complex eigenvalues.
A.is_diagonalizable(reals_only=True, verbosity=1)
Characteristic Polynomial is:
Check if algebraic multiplicity equals number of eigenvectors. Eigenvectors are:
True
# Full diagaonlisation procedure
A.diagonalize(reals_only=True, verbosity=1)
Characteristic Polynomial
After RREF:
Eigenvectors:
After RREF:
Eigenvectors:
(b)¶
$\mathbf{A} = \begin{pmatrix} 9 & 8 & 6 & 3 \\ 0 & -1 & 3 & -4 \\ 0 & 0 & 2 & 0 \\ 0 & 0 & 0 & 3 \end{pmatrix}$
A = Matrix.from_str("9 8 6 3; 0 -1 3 -4; 0 0 2 0; 0 0 0 3")
A
A.diagonalize(reals_only=True, verbosity=1)
Characteristic Polynomial
After RREF:
Eigenvectors:
After RREF:
Eigenvectors:
After RREF:
Eigenvectors:
After RREF:
Eigenvectors:
(c)¶
$\mathbf{A} = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 0 \\ 0 & 1 & 1 \end{pmatrix}$
A = Matrix.from_str("1 0 0; 1 1 0; 0 1 1")
A
# If matrix is not diagonalizable, it will raise a MatrixError
try:
A.diagonalize(reals_only=True, verbosity=1)
except sym.matrices.matrixbase.MatrixError as e:
print(f"MatrixError: {e}")
Characteristic Polynomial
After RREF:
Eigenvectors:
MatrixError: Matrix is not diagonalizable
(d)¶
$\mathbf{A} = \begin{pmatrix} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \end{pmatrix}$
A = Matrix.from_str("0 0 1 0; 0 0 0 1; 1 0 0 0; 0 1 0 0")
A
A.diagonalize(reals_only=True, verbosity=1)
Characteristic Polynomial
After RREF:
Eigenvectors:
After RREF:
Eigenvectors:
(e)¶
$\mathbf{A} = \begin{pmatrix} -1 & 1 & 1 \\ 1 & 1 & -1 \\ -4 & 2 & 3 \end{pmatrix}$
A = Matrix.from_str("-1 1 1; 1 1 -1; -4 2 3")
A
# `cpoly` returning a tuple of expressions means that the characteristic
# polynomial is not irreducible over the reals. As the rhs expression
# only has non-real roots. This means that the matrix is not
# diagaonalizable over the reals (with regards to MA1522 syllabus).
A.cpoly()
Matrix.eye(4).inv()
# By setting `reals_only=False`, we allow complex eigenvalues
# and hence the matrix can be diagonalized.
PDP = A.diagonalize(reals_only=False)
PDP