{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Linear Algebra - Exercises" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sympy as sp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate the determinant and inverse of $W$\n", "\n", "$W=\\begin{bmatrix}1&&2&&-1\\\\1&&0&&1\\\\0&&1&&0\\end{bmatrix}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"det W =\", (1 * (0 - 1)) - (2 * (0 - 0)) + (-1 * (1 - 0)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To calculate the inverse we can leverage the fact that $WW^{-1}=I$.\n", "\n", "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.\n", "\n", "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}$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "W = sp.Matrix([[1, 2, -1], [1, 0, 1], [0, 1, 0]])\n", "WI = W.row_join(sp.Matrix.eye(3))\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R2 = R2 - R1\n", "WI = WI.elementary_row_op(\"n->n+km\", row=1, k=-1, row1=1, row2=0)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R2 = -0.5R2\n", "WI = WI.elementary_row_op(\"n->kn\", row=1, k=-0.5)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R1 = R1 - 2R2\n", "WI = WI.elementary_row_op(\"n->n+km\", row=0, k=-2, row1=0, row2=1)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R3 = R3 - R2\n", "WI = WI.elementary_row_op(\"n->n+km\", row=2, k=-1, row1=2, row2=1)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R1 = R1 - R3\n", "WI = WI.elementary_row_op(\"n->n+km\", row=0, k=-1, row1=0, row2=2)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# R2 = R2 + R3\n", "WI = WI.elementary_row_op(\"n->n+km\", row=1, k=1, row1=1, row2=2)\n", "WI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "WI = WI[-3:, -3:]\n", "WI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can verify $WW^{-1}=I$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "W.multiply(WI)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate the characteristic polynomial, eigenvalues and eigenvectors of $A$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A = \\begin{bmatrix}2&&1\\\\-3&&6\\end{bmatrix}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$A = \\begin{bmatrix}2-\\lambda&&1\\\\-3&&6-\\lambda\\end{bmatrix}$\n", "\n", "$det(A-\\lambda I) = (2-\\lambda)(6-\\lambda)-(1)(-3)$\n", "\n", "$\\lambda^2 - 8\\lambda + 12 + 3$\n", "\n", "$\\lambda^2 - 8\\lambda + 15$\n", "\n", "$(5 - \\lambda)(3 -\\lambda)$\n", "\n", "$\\lambda = 5, \\lambda = 3$\n", "\n", "For $\\lambda$ = 5:\n", "\n", "$\\begin{bmatrix}-3&&1\\\\-3&&1\\end{bmatrix}$\n", "\n", "$\\begin{bmatrix}-3&&1\\\\0&&0\\end{bmatrix}$\n", "\n", "$\\begin{cases}-3v_1+v_2\\\\0=0\\end{cases}$\n", "\n", "$\\begin{cases}3v_1=v_2\\\\0=0\\end{cases}$\n", "\n", "$\\vec{v}_{\\lambda=5} = \\begin{bmatrix}1\\\\3\\end{bmatrix}$\n", "\n", "For $\\lambda$ = 3:\n", "\n", "$\\begin{bmatrix}-1&&1\\\\-3&&3\\end{bmatrix}$\n", "\n", "$\\begin{bmatrix}-1&&1\\\\0&&0\\end{bmatrix}$\n", "\n", "$\\begin{cases}-v_1+v_2\\\\0=0\\end{cases}$\n", "\n", "$\\begin{cases}v_1=v_2\\\\0=0\\end{cases}$\n", "\n", "$\\vec{v}_{\\lambda=3} = \\begin{bmatrix}1\\\\1\\end{bmatrix}$\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }