Tutorial 2 (AY24/25 Sem 1)¶
# Required imports
import sympy as sym
from ma1522 import Matrix, PartGen
A = Matrix.from_str("1 1 0 1; 0 1 1 0; 0 0 1 1", col_sep=" ", row_sep=";")
A
X = A.inverse("right", matrices=2, verbosity=1)
X
# matrices=2 separates X into two matrices, the particular and general solution to see the results clearly
Before RREF: [self | eye]
After RREF:
# Further factorisation
scalar_factor = X.gen_sol.scalar_factor(column=True)
scalar_factor
# Therefore, X can be expressed as:
PartGen(X.part_sol, scalar_factor)
(b)¶
Let $B = \begin{pmatrix} 1 & 0 & 1 \\ 1 & 1 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{pmatrix}$. Find a $3 \times 4$ matrix $Y$ such that $YB = I_3$.
B = Matrix.from_str("1 0 1; 1 1 0; 0 1 1; 0 0 1", col_sep=" ", row_sep=";")
B
Y = B.inverse("left", matrices=2, verbosity=1) # Find the right inverse of B
Y
Before RREF: [self^T | eye]
After RREF:
PartGen(Y.part_sol, Y.gen_sol.scalar_factor(column=False))
Question 3¶
(i) Reduce the following matrices $A$ to its reduced row-echelon form $R$.
(ii) For each of the elementary row operation, write the corresponding elementary matrix.
(iii) Write the matrices $A$ in the form $E_1 E_2 \ldots E_n R$ where $E_1, E_2, \ldots, E_n$ are elementary matrices and $R$ is the reduced row-echelon form of $A$.
(a)¶
$A = \begin{pmatrix} 5 & -2 & 6 & 0 \\ -2 & 1 & 3 & 1 \end{pmatrix}$.
A = Matrix.from_str("5 -2 6 0; -2 1 3 1")
A
# Tip: Use `copy()` to avoid modifying the original matrix so that
# the same cell can be ran multiple times without side effects.
(A.copy()
.reduce_row(1, -2/5, 0)
.scale_row(0, 1/5)
.scale_row(1, 5)
.reduce_row(0, -2/5, 1)
)
(b)¶
$A = \begin{pmatrix} -1 & 3 & -4 \\ 2 & 4 & 1 \\ -4 & 2 & -9 \end{pmatrix}$.
A = Matrix.from_str("-1 3 -4; 2 4 1; -4 2 -9")
A
(A.copy()
.reduce_row(1, -2, 0)
.reduce_row(2, 4, 0)
.reduce_row(2, -1, 1)
.scale_row(0, -1)
.scale_row(1, 1/10)
.reduce_row(0, -3, 1)
)
(c)¶
$A = \begin{pmatrix} 1 & -1 & 0 \\ 2 & -2 & 1 \\ 1 & 2 & 3 \end{pmatrix}$.
A = Matrix.from_str("1 -1 0; 2 -2 1; 1 2 3")
A
(A.copy()
.reduce_row(1, 2, 0)
.reduce_row(2, 1, 0)
.swap_row(1, 2)
.scale_row(1, sym.Rational(1, 3)) # To avoid floating point errors
.reduce_row(1, 1, 2)
.reduce_row(0, -1, 1)
)
Question 4¶
Determine if the following matrices are invertible. If the matrix is invertible, find its inverse.
(a)¶
$\begin{pmatrix} -1 & 3 \\ 3 & -2 \end{pmatrix}$.
mat = Matrix.from_str("-1 3; 3 -2")
mat.inverse(verbosity=2)
Left inverse found! Right inverse found! Before RREF: [self | eye]
After RREF:
(b)¶
$\begin{pmatrix} -1 & 3 & -4 \\ 2 & 4 & 1 \\ -4 & 2 & -9 \end{pmatrix}$.
mat = Matrix.from_str("-1 3 -4; 2 4 1; -4 2 -9")
try:
mat.inverse(verbosity=2) # without specifying the option, no rref will be shown
except ValueError as e:
print(f"Error: {e}")
Error: No inverse found! Rank: 2, Rows: 3, Columns: 3. Try pseudo-inverse: .pinv()
try:
mat.inverse("both", verbosity=2)
except ValueError as e:
print(f"Error: {e}")
Before RREF: [self | eye]
After RREF:
Error: No both inverse found! Try pseudo-inverse: .pinv()
Question 5¶
Write down the conditions so that the matrix $\begin{pmatrix} 1 & 1 & 1 \\ a & b & c \\ a^2 & b^2 & c^2 \end{pmatrix}$ is invertible.
a, b, c = sym.symbols("a b c")
mat = Matrix.from_str("1 1 1; a b c; a**2 b**2 c**2")
mat
inv = mat.inverse(verbosity=2)
inv
Left inverse found! Right inverse found! Before RREF: [self | eye]
After RREF:
# For inverse to exist, all the divisors must be non-zero
divisors = 1
for entry in inv.select_cols(0): # same divisor along columns
_, divisor = sym.fraction(entry)
divisors *= divisor
sym.solve(divisors)
# This means that a != b and a != c and b != c for the inverse to exist
# Alternative Method via determinant
det = mat.det()
det.factor()