Linear Algebra - Exercises#

[1]:
import sympy as sp

Calculate the determinant and inverse of \(W\)#

\(W=\begin{bmatrix}1&&2&&-1\\1&&0&&1\\0&&1&&0\end{bmatrix}\)

[2]:
print("det W =", (1 * (0 - 1)) - (2 * (0 - 0)) + (-1 * (1 - 0)))
det W = -2

To calculate the inverse we can leverage the fact that \(WW^{-1}=I\).

To find \(W^{-1}\) we can solve the system of equation formed by the matrix multiplication of \(W\) (coefficients) with \(W^{-1}\) (variables) and \(I\) are the constants on the RHS.

Or we can use the augmented matrices method, which basically entails augmenting the matrix \(W\) with \(I\) and applying row operations so the LHS becomes the reduced echelon form of \(W\) and the RHS side becomes \(W^{-1}\).

[3]:
W = sp.Matrix([[1, 2, -1], [1, 0, 1], [0, 1, 0]])
WI = W.row_join(sp.Matrix.eye(3))
WI
[3]:
$\displaystyle \left[\begin{matrix}1 & 2 & -1 & 1 & 0 & 0\\1 & 0 & 1 & 0 & 1 & 0\\0 & 1 & 0 & 0 & 0 & 1\end{matrix}\right]$
[4]:
# R2 = R2 - R1
WI = WI.elementary_row_op("n->n+km", row=1, k=-1, row1=1, row2=0)
WI
[4]:
$\displaystyle \left[\begin{matrix}1 & 2 & -1 & 1 & 0 & 0\\0 & -2 & 2 & -1 & 1 & 0\\0 & 1 & 0 & 0 & 0 & 1\end{matrix}\right]$
[5]:
# R2 = -0.5R2
WI = WI.elementary_row_op("n->kn", row=1, k=-0.5)
WI
[5]:
$\displaystyle \left[\begin{matrix}1 & 2 & -1 & 1 & 0 & 0\\0 & 1.0 & -1.0 & 0.5 & -0.5 & 0\\0 & 1 & 0 & 0 & 0 & 1\end{matrix}\right]$
[6]:
# R1 = R1 - 2R2
WI = WI.elementary_row_op("n->n+km", row=0, k=-2, row1=0, row2=1)
WI
[6]:
$\displaystyle \left[\begin{matrix}1 & 0 & 1.0 & 0 & 1.0 & 0\\0 & 1.0 & -1.0 & 0.5 & -0.5 & 0\\0 & 1 & 0 & 0 & 0 & 1\end{matrix}\right]$
[7]:
# R3 = R3 - R2
WI = WI.elementary_row_op("n->n+km", row=2, k=-1, row1=2, row2=1)
WI
[7]:
$\displaystyle \left[\begin{matrix}1 & 0 & 1.0 & 0 & 1.0 & 0\\0 & 1.0 & -1.0 & 0.5 & -0.5 & 0\\0 & 0 & 1.0 & -0.5 & 0.5 & 1\end{matrix}\right]$
[8]:
# R1 = R1 - R3
WI = WI.elementary_row_op("n->n+km", row=0, k=-1, row1=0, row2=2)
WI
[8]:
$\displaystyle \left[\begin{matrix}1 & 0 & 0 & 0.5 & 0.5 & -1\\0 & 1.0 & -1.0 & 0.5 & -0.5 & 0\\0 & 0 & 1.0 & -0.5 & 0.5 & 1\end{matrix}\right]$
[9]:
# R2 = R2 + R3
WI = WI.elementary_row_op("n->n+km", row=1, k=1, row1=1, row2=2)
WI
[9]:
$\displaystyle \left[\begin{matrix}1 & 0 & 0 & 0.5 & 0.5 & -1\\0 & 1.0 & 0 & 0 & 0 & 1\\0 & 0 & 1.0 & -0.5 & 0.5 & 1\end{matrix}\right]$
[10]:
WI = WI[-3:, -3:]
WI
[10]:
$\displaystyle \left[\begin{matrix}0.5 & 0.5 & -1\\0 & 0 & 1\\-0.5 & 0.5 & 1\end{matrix}\right]$

Now we can verify \(WW^{-1}=I\)

[11]:
W.multiply(WI)
[11]:
$\displaystyle \left[\begin{matrix}1.0 & 0 & 0\\0 & 1.0 & 0\\0 & 0 & 1\end{matrix}\right]$

Calculate the characteristic polynomial, eigenvalues and eigenvectors of \(A\)#

\(A = \begin{bmatrix}2&&1\\-3&&6\end{bmatrix}\)

\(A = \begin{bmatrix}2-\lambda&&1\\-3&&6-\lambda\end{bmatrix}\)

\(det(A-\lambda I) = (2-\lambda)(6-\lambda)-(1)(-3)\)

\(\lambda^2 - 8\lambda + 12 + 3\)

\(\lambda^2 - 8\lambda + 15\)

\((5 - \lambda)(3 -\lambda)\)

\(\lambda = 5, \lambda = 3\)

For \(\lambda\) = 5:

\(\begin{bmatrix}-3&&1\\-3&&1\end{bmatrix}\)

\(\begin{bmatrix}-3&&1\\0&&0\end{bmatrix}\)

\(\begin{cases}-3v_1+v_2\\0=0\end{cases}\)

\(\begin{cases}3v_1=v_2\\0=0\end{cases}\)

\(\vec{v}_{\lambda=5} = \begin{bmatrix}1\\3\end{bmatrix}\)

For \(\lambda\) = 3:

\(\begin{bmatrix}-1&&1\\-3&&3\end{bmatrix}\)

\(\begin{bmatrix}-1&&1\\0&&0\end{bmatrix}\)

\(\begin{cases}-v_1+v_2\\0=0\end{cases}\)

\(\begin{cases}v_1=v_2\\0=0\end{cases}\)

\(\vec{v}_{\lambda=3} = \begin{bmatrix}1\\1\end{bmatrix}\)