{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "980d3f19-4ea9-4352-b280-c610dcf92086", "metadata": {}, "outputs": [], "source": [ "using Latexify\n", "using LaTeXStrings\n", "using LinearAlgebra\n", "using Random\n", "using SymPy\n", "using RowEchelon\n", "using Plots\n", "gr()\n", "\n", "# Defaults for Latexify\n", "set_default(env=:raw, starred=true)\n", "\n", "# Use the same seed for all random problems\n", "seed0 = 1233\n", "myseed() = Random.seed!(seed0);\n", "myseed(s) = Random.seed!(seed0+s);\n", "\n", "# Often used dimensions\n", "n0 = 6;\n", "n1 = 3;\n", "\n", "# range of entries in a randomatrix\n", "S0 = -1:5;\n", "\n", "# Some helper functions for making plots\n", "xs_ys(vs) = Tuple(eltype(vs[1])[vs[i][j] for i in 1:length(vs)] for j in eachindex(first(vs)));\n", "xs_ys(v,vs...) = xs_ys([v, vs...]);\n", "xs_ys(r::Function, a, b, n=100) = xs_ys(r.(range(a, stop=b, length=n)));\n", "\n", "tofloat(x) = convert(Float64, x);\n", "\n", "# Generate a singular matrix\n", "function randsing(S, m, n)\n", " while true\n", " A = rand(S, (m, n))\n", " if det(A) == 0\n", " return A\n", " end\n", " end\n", "end;\n", "\n", "randsing(n) = randsing(S0, n, n)\n", "randsing(S, n) = randsing(S, n, n)\n", "\n", "# Generate a non-singular matrix\n", "function randnonsing(S, m, n)\n", " while true\n", " A = rand(S, (m, n))\n", " if det(A) != 0\n", " return A\n", " end\n", " end\n", "end;\n", "\n", "randnonsing(n) = randnonsing(S0, n, n)\n", "randnonsing(S, n) = randnonsing(S, n, n)\n", "\n", "# LaTeX helper functions\n", "\n", "# Generate LaTeX of an empty matrix for student to fill in\n", "function emptymat(m, n)\n", " r = fill(\"\\\\underline{\\\\hspace{1cm}}\", n)\n", " r = join(r, \"& \")\n", " m = fill(r, m)\n", " m = join(m, \"\\\\\\\\\\n\")\n", " return \"\\\\begin{bmatrix}\" * m * \"\\\\end{bmatrix}\"\n", "end;\n", "emptymat(n) = emptymat(n, n);\n", "\n", "# Generate LaTeX of a partially revealed matrix\n", "function partialmat(mat, rows)\n", " (m, n) = size(mat)\n", " r = fill(\"\\\\underline{\\\\hspace{1cm}}\", n)\n", " r = join(r, \"& \")\n", " r *= \"\\\\\\\\\\n\"\n", " s = \"\"\n", " for i in 1:m\n", " if i in rows\n", " r1 = join([latexify(x) for x in mat[i, :]],\"& \")\n", " r1 *= \"\\\\\\\\\\n\"\n", " s *= r1 \n", " else\n", " s *= r\n", " end\n", " end\n", " return \"\\\\begin{bmatrix}\" * s * \"\\\\end{bmatrix}\"\n", "end;\n", "\n", "# Generate a long empty space for student to write proofs\n", "answerblock(h) = LaTeXString(\"\"\"Answer:\n", "\n", "\\\\vspace{$(h)pt}\n", "\"\"\");\n", "answerblock() = answerblock(180)\n", "\n", "# Print a matrix to a set of vectors in LaTeX\n", "function tovecset(mat)\n", " ptstext = join([latexify(c) for c in eachcol(mat)], \", \")\n", " return L\"$$\\left\\{%$ptstext\\right\\}$$\"\n", "end;" ] }, { "cell_type": "markdown", "id": "e170c0fe", "metadata": {}, "source": [ "\n", "" ] }, { "cell_type": "markdown", "id": "3c6f7c5f-aa8d-4644-9c4d-242771868bf8", "metadata": {}, "source": [ "Full Name: $\\underline{\\hspace{5cm}}$ NetID: $\\underline{\\hspace{5cm}}$" ] }, { "cell_type": "markdown", "id": "d3475f01", "metadata": {}, "source": [ "Honor Pledge: I will neither give nor receive aid on this assessment." ] }, { "cell_type": "markdown", "id": "5ed52b79-e27d-4002-9393-e517ffd2aa4b", "metadata": {}, "source": [ "Signature: $\\underline{\\hspace{5cm}}$" ] }, { "cell_type": "markdown", "id": "7bd83b56-5b57-4902-8628-ca9aa4fdf6f4", "metadata": {}, "source": [ "Note: \n", "\n", "1. Don't look inside until the exam starts.\n", "2. Each question counts as 1 pt.\n", "3. When answering multiple choice questions, only check the boxes before correct answers.\n", "4. A proof must be written with **correct** English grammar and mathematical notations to receive full points.\n", "\n", "Good luck!" ] }, { "cell_type": "raw", "id": "079d4a98-0863-4a61-bf76-890224bff881", "metadata": {}, "source": [ "\\begin{figure}\n", "\\centering\n", "\\includegraphics[width=0.6\\linewidth]{images/art.png}\n", "\\end{figure}\n", "\n", "\\pagebreak{}" ] }, { "cell_type": "markdown", "id": "915a1382", "metadata": {}, "source": [ "# Solution sets\n", "\n", "Solve the linear system with the augmented matrix" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0023b97", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "-1 & 3 & 1 & 18 \\\\\n", "1 & 1 & 2 & 8 \\\\\n", "1 & 5 & 5 & 34 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n" ], "text/plain": [ "L\"\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "-1 & 3 & 1 & 18 \\\\\n", "1 & 1 & 2 & 8 \\\\\n", "1 & 5 & 5 & 34 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n", "\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Random.seed!(seed0)\n", "A = randsing(3)\n", "x0 = rand(S0, 3)\n", "y = A * x0\n", "B = hcat(A, y)\n", "latexify(B, env=:eq)" ] }, { "cell_type": "code", "execution_count": 3, "id": "9b0ea605-89ec-490b-bcd6-de8fa2968847", "metadata": {}, "outputs": [], "source": [ "xs = [Sym(\"x$i\") for i in 1:3]\n", "B1 = sympy.Matrix([A zeros(Int, 3)])\n", "sol = [sympy.linsolve(B1, xs[1], xs[2], xs[3]).args[1].args...];" ] }, { "cell_type": "code", "execution_count": 4, "id": "db9eedb0-35a2-4e22-b7b7-907f24f61aca", "metadata": {}, "outputs": [], "source": [ "sol1 = latexify(sol[1].subs(xs[3], 1));" ] }, { "cell_type": "markdown", "id": "45c804d4", "metadata": {}, "source": [ "Answer:\n", "\n", "The solution set is of the form" ] }, { "cell_type": "code", "execution_count": 5, "id": "504abfa9", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "\\begin{bmatrix}\n", "-1 \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", "+\n", "s\n", "\\begin{bmatrix}\n", "\\frac{-5}{4} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "s \\in \\dsR\n", "$$\n" ], "text/plain": [ "L\"$$\n", "\\begin{bmatrix}\n", "-1 \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", "+\n", "s\n", "\\begin{bmatrix}\n", "\\frac{-5}{4} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "s \\in \\dsR\n", "$$\n", "\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"$$\n", "\\begin{bmatrix}\n", "%$(x0[1]) \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", "+\n", "s\n", "\\begin{bmatrix}\n", "%$(sol1) \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} \\\\\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "s \\in \\dsR\n", "$$\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "d88de561", "metadata": {}, "source": [ "# Linear independence" ] }, { "cell_type": "markdown", "id": "47694406", "metadata": {}, "source": [ "Find the value of $h$ which makes the columns of the following matrix linearly **dependent**" ] }, { "cell_type": "code", "execution_count": 6, "id": "39345d20", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "3 & -1 & 1 & 2 \\\\\n", "1 & 3 & 1 & 3 \\\\\n", "1 & 0 & h & -1 \\\\\n", "1 & 3 & 2 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n" ], "text/plain": [ "L\"\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "3 & -1 & 1 & 2 \\\\\n", "1 & 3 & 1 & 3 \\\\\n", "1 & 0 & h & -1 \\\\\n", "1 & 3 & 2 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n", "\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Random.seed!(seed0 + 5)\n", "A = sympy.Matrix(rand(-1:3,(4,4)))\n", "h = Sym(\"h\")\n", "A[3,3] = h\n", "latexify(A, env=:eq)" ] }, { "cell_type": "markdown", "id": "f59bec31", "metadata": {}, "source": [ "Answer: $h = \\underline{\\hspace{5cm}}$" ] }, { "cell_type": "code", "execution_count": 7, "id": "a070636f", "metadata": {}, "outputs": [], "source": [ "sympy.solve(det(A));" ] }, { "cell_type": "markdown", "id": "b3e7b6e3", "metadata": {}, "source": [ "# Linear transformations\n", "\n", "Which statements are always true?\n", "\n", "* [ ] A linear transformation $T: \\dsR^n \\mapsto \\dsR^m$ is completely determined by its effect on the columns of the $n \\times n$ identity matrix.\n", "* [ ] If $T: \\dsR^2 \\mapsto \\dsR^2$ rotates vectors about the origin through an angle $\\phi$, then $T$ is a linear transformation.\n", "* [ ] When two linear transformations are performed one after another, the combined effect is always a linear transformation.\n", "* [ ] Every linear transformation from $\\dsR^n$ to $\\dsR^m$ is also a matrix transformation.\n", "* [ ] A mapping $T : \\dsR^n \\mapsto \\dsR^m$ is one-to-one if each vector in $\\dsR^n$ maps onto a unique vector in $\\dsR^m$." ] }, { "cell_type": "markdown", "id": "791f6185-921b-401d-ad28-ac3e4793c411", "metadata": {}, "source": [ "# Linear transformations\n", "\n", "Find the matrix which transforms the picture on the left to the one on the right." ] }, { "cell_type": "code", "execution_count": 8, "id": "ceb0d199-0e59-474e-89a3-91a6efbdb590", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myseed(seed0+2)\n", "pts = hcat(rand(1:4, (2,5)))\n", "T = randnonsing(0:2, 2)\n", "pts1 = T * pts\n", "pic = Plots.plot(Shape(pts[1, :], pts[2, :]),\n", " apsect_ratio = 1,\n", " legend = nothing,\n", " framestyle = :origin,\n", " size = (400, 400)\n", ")\n", "savefig(pic, \"images/art.png\")\n", "xup = maximum(pts1[1, :])\n", "yup = maximum(pts1[2, :])\n", "pic1 = Plots.plot(Shape(pts1[1, :], pts1[2, :]),\n", " aspect_ratio = 1,\n", " legend = nothing,\n", " framestyle = :origin,\n", " xticks = 0:1:xup,\n", " yticks = 0:1:yup,\n", ")\n", "\n", "plot(pic, pic1, layout=grid(1, 2, widths=[0.4, 0.6]), size=(1000, 400))" ] }, { "cell_type": "markdown", "id": "9f544296-e728-4ac0-a93f-02cc170db666", "metadata": {}, "source": [ "Answer:" ] }, { "cell_type": "code", "execution_count": 9, "id": "1fd46c20-f453-475c-9482-de12c5c1cba4", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}$$" ], "text/plain": [ "L\"$$\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}$$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"$$%$(emptymat(2))$$\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "dc0d71d5-9c25-4c6f-8367-b491f86735e8", "metadata": {}, "outputs": [], "source": [ "@syms a, b, c, d\n", "T1 = [a b; c d]\n", "solve(vcat([Eq.(T1*pts[:, i], pts1[:, i]) for i in 1:2]...));" ] }, { "cell_type": "markdown", "id": "7af824cf", "metadata": {}, "source": [ "# Invertible matrices" ] }, { "cell_type": "markdown", "id": "c2a4f9f2", "metadata": {}, "source": [ "Find the inverse of" ] }, { "cell_type": "code", "execution_count": 11, "id": "bbc52d36", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*}\n", "\\left[\n", "\\begin{array}{ccccc}\n", "1 & 0 & 0 & 0 & 0 \\\\\n", "1 & 2 & 0 & 0 & 0 \\\\\n", "1 & 2 & 3 & 0 & 0 \\\\\n", "1 & 2 & 3 & 4 & 0 \\\\\n", "1 & 2 & 3 & 4 & 5 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n" ], "text/plain": [ "L\"\\begin{equation*}\n", "\\left[\n", "\\begin{array}{ccccc}\n", "1 & 0 & 0 & 0 & 0 \\\\\n", "1 & 2 & 0 & 0 & 0 \\\\\n", "1 & 2 & 3 & 0 & 0 \\\\\n", "1 & 2 & 3 & 4 & 0 \\\\\n", "1 & 2 & 3 & 4 & 5 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n", "\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myseed()\n", "A = zeros(Int, (5, 5))\n", "for i in 1:5\n", " for j in 1:i\n", " A[i, j] =j\n", " end\n", "end\n", "latexify(A, env=:eq)" ] }, { "cell_type": "code", "execution_count": 12, "id": "fe86cc38", "metadata": {}, "outputs": [], "source": [ "sympy.Matrix(A)^(-1);" ] }, { "cell_type": "markdown", "id": "ecde551f", "metadata": {}, "source": [ "Hint: Do some experiments and make a guess." ] }, { "cell_type": "markdown", "id": "d4ab1525", "metadata": {}, "source": [ "Answer:" ] }, { "cell_type": "code", "execution_count": 13, "id": "7700aa6c", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}$$" ], "text/plain": [ "\"\\$\\$\\\\begin{bmatrix}\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{\" ⋯ 143 bytes ⋯ \"erline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\end{bmatrix}\\$\\$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"$$%$(emptymat(5,5))$$\"" ] }, { "cell_type": "markdown", "id": "1b27db8d", "metadata": {}, "source": [ "# LU factorization" ] }, { "cell_type": "code", "execution_count": 14, "id": "e232a971", "metadata": {}, "outputs": [], "source": [ "myseed(seed0+1)\n", "U = UpperTriangular(randnonsing(2:6, 3))\n", "U = [U; zeros(Int, (1,3))]\n", "L = LowerTriangular(randnonsing(-1:5, 4))\n", "A = L*U\n", "for i in 1:4\n", " L[i, i] = 1\n", "end\n", "L\n", "x = rand(-1:3, 3);\n", "b = A * x;" ] }, { "cell_type": "markdown", "id": "ee9f4bf0", "metadata": {}, "source": [ "Using the given LU-factorization to solve the linear system $A x = b$ where" ] }, { "cell_type": "code", "execution_count": 15, "id": "46ef41d6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "A = \n", "\\left[\n", "\\begin{array}{cccc}\n", "1 & 0 & 0 & 0 \\\\\n", "0 & 1 & 0 & 0 \\\\\n", "2 & 2 & 1 & 0 \\\\\n", "0 & 0 & 4 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "\\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 2 \\\\\n", "0 & 5 & 3 \\\\\n", "0 & 0 & 2 \\\\\n", "0 & 0 & 0 \\\\\n", "\\end{array}\n", "\\right]\n", ",\n", "\\qquad\n", "b\n", "=\n", "\\left[\n", "\\begin{array}{c}\n", "36 \\\\\n", "6 \\\\\n", "18 \\\\\n", "-8 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n" ], "text/plain": [ "L\"$$\n", "A = \n", "\\left[\n", "\\begin{array}{cccc}\n", "1 & 0 & 0 & 0 \\\\\n", "0 & 1 & 0 & 0 \\\\\n", "2 & 2 & 1 & 0 \\\\\n", "0 & 0 & 4 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "\\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 2 \\\\\n", "0 & 5 & 3 \\\\\n", "0 & 0 & 2 \\\\\n", "0 & 0 & 0 \\\\\n", "\\end{array}\n", "\\right]\n", ",\n", "\\qquad\n", "b\n", "=\n", "\\left[\n", "\\begin{array}{c}\n", "36 \\\\\n", "6 \\\\\n", "18 \\\\\n", "-8 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n", "\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"\n", "$$\n", "A = \n", "%$(latexify(L))\n", "%$(latexify(U))\n", ",\n", "\\qquad\n", "b\n", "=\n", "%$(latexify(b))\n", "$$\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 16, "id": "e241bf22-013a-4ca0-8acc-24884342e5ca", "metadata": {}, "outputs": [], "source": [ "xs = [Sym(\"x$i\") for i in 1:4]\n", "B1 = sympy.Matrix([A b])\n", "sol = [sympy.linsolve(B1, xs[1], xs[2], xs[3]).args[1].args...];" ] }, { "cell_type": "markdown", "id": "9f6e0eeb", "metadata": {}, "source": [ "Answer:" ] }, { "cell_type": "code", "execution_count": 17, "id": "becb9f77", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "x = \n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$\n" ], "text/plain": [ "L\"$$\n", "x = \n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$\n", "\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"\n", "$$\n", "x = \n", "%$(emptymat(3, 1))\n", "$$\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "ceba74dc", "metadata": {}, "outputs": [], "source": [ "A1 = sympy.Matrix([A zeros(Int, 4)])\n", "sympy.linsolve(A1);" ] }, { "cell_type": "markdown", "id": "a0eb3caa", "metadata": {}, "source": [ "# Determinants\n", "\n", "Let $A$ be an $n \\times n$ matrix.\n", "Which of the following statements are always true?\n", "\n", "* [ ] The determinant of $A$ is the product of the pivots in any echelon form $U$ of $A$, multiplied by $(−1)^r$, where r is the number of row interchanges made during row reduction from $A$ to $U$.\n", "* [ ] If the columns of $A$ are linearly dependent, then $\\det A = 0$.\n", "* [ ] Adding a multiple of one row to another does not affect the determinant of a matrix.\n", "* [ ] If $C$ is a $2 \\times 2$ matrix with a zero determinant, then one column of $C$ is a multiple of the other.\n", "* [ ] $\\det(AA^T) \\ge 0$." ] }, { "cell_type": "markdown", "id": "78ab568d", "metadata": {}, "source": [ "# Volumes" ] }, { "cell_type": "markdown", "id": "35de9abd", "metadata": {}, "source": [ "Compute the volume of the tetrahedron with vertcies" ] }, { "cell_type": "code", "execution_count": 19, "id": "8cbf34b0", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left\\{\\left[\n", "\\begin{array}{c}\n", "-2 \\\\\n", "-1 \\\\\n", "0 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "-3 \\\\\n", "0 \\\\\n", "-2 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "-3 \\\\\n", "-2 \\\\\n", "-3 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "0 \\\\\n", "-2 \\\\\n", "-1 \\\\\n", "\\end{array}\n", "\\right]\\right\\}$$" ], "text/plain": [ "L\"$$\\left\\{\\left[\n", "\\begin{array}{c}\n", "-2 \\\\\n", "-1 \\\\\n", "0 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "-3 \\\\\n", "0 \\\\\n", "-2 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "-3 \\\\\n", "-2 \\\\\n", "-3 \\\\\n", "\\end{array}\n", "\\right], \\left[\n", "\\begin{array}{c}\n", "0 \\\\\n", "-2 \\\\\n", "-1 \\\\\n", "\\end{array}\n", "\\right]\\right\\}$$\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myseed(seed0 + 1)\n", "pts = rand(-3:1, (3,4))\n", "(x, y, z) = eachrow(pts)\n", "tovecset(pts)" ] }, { "cell_type": "markdown", "id": "cb524d84", "metadata": {}, "source": [ "Answer: $\\underline{\\hspace{5cm}}$" ] }, { "cell_type": "code", "execution_count": 20, "id": "a009f1eb", "metadata": {}, "outputs": [], "source": [ "pts1 = copy(pts)\n", "for i in 2:4\n", " pts1[:, i] -= pts1[:, 1]\n", "end\n", "pts1[:, 1] = zeros(3)\n", "pts1\n", "abs(det(pts1[:, 2:end]));" ] }, { "cell_type": "markdown", "id": "5b6c6cc8-3f7d-4bb9-81bc-e94909d75071", "metadata": {}, "source": [ "# Ranks\n", "\n", "Let $u, v \\in \\dsR^n$. Prove that $\\mathrm{rank}(u v^T) \\le 1$." ] }, { "cell_type": "code", "execution_count": 21, "id": "8f49d54b-0da6-4b8d-af84-455aea481a73", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Answer:\n", "\n", "\\vspace{150pt}\n" ], "text/plain": [ "L\"Answer:\n", "\n", "\\vspace{150pt}\n", "\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answerblock(150)" ] }, { "cell_type": "markdown", "id": "446c85e1", "metadata": {}, "source": [ "# Vector space\n", "\n", "Let $\\dsR^{3 \\times 3}$ be the vector space of $3 \\times 3$ matrices.\n", "Which of the following subsets of $\\dsR^{3 \\times 3}$ are subspaces of $\\dsR^{3 \\times 3}$?\n", "\n", "* [ ] The $3 \\times 3$ matrices A such that the vector $(0, 8, 6)$ is in the null space of $A$.\n", "* [ ] The $3 \\times 3$ matrices whose entries are all integers\n", "* [ ] The $3 \\times 3$ matrices in reduced row-echelon form \n", "* [ ] The diagonal $3 \\times 3$ matrices\n", "* [ ] The $3 \\times 3$ matrices of rank $3$" ] }, { "cell_type": "markdown", "id": "3fea136c-4bea-480d-a812-d41d5fa7d5b6", "metadata": {}, "source": [ "# Change of basis\n", "\n", "Let $\\mathcal B$ and $\\mathcal C$ be two bases of $\\dsR^3$ forme by the columns of $B$ and $C$ respectively where" ] }, { "cell_type": "code", "execution_count": 22, "id": "fb2ea2c5-6008-4b18-9ff8-569f7def5bc0", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$B = \\left[\n", "\\begin{array}{cc}\n", "3 & 3 \\\\\n", "4 & 2 \\\\\n", "\\end{array}\n", "\\right], \\qquad C = \\left[\n", "\\begin{array}{cc}\n", "1 & 5 \\\\\n", "5 & 5 \\\\\n", "\\end{array}\n", "\\right]$$" ], "text/plain": [ "L\"$$B = \\left[\n", "\\begin{array}{cc}\n", "3 & 3 \\\\\n", "4 & 2 \\\\\n", "\\end{array}\n", "\\right], \\qquad C = \\left[\n", "\\begin{array}{cc}\n", "1 & 5 \\\\\n", "5 & 5 \\\\\n", "\\end{array}\n", "\\right]$$\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myseed()\n", "B = randnonsing(2)\n", "C = randnonsing(2)\n", "L\"$$B = %$(latexify(B)), \\qquad C = %$(latexify(C))$$\"" ] }, { "cell_type": "markdown", "id": "79d41054-1213-470a-803b-fe3c41381bf0", "metadata": {}, "source": [ "Find the change-of-coordinates matrix from $\\mathcal B$ to $\\mathcal C$ \n", "and the change-of-coordinates matrix from $\\mathcal C$ to $\\mathcal B$." ] }, { "cell_type": "code", "execution_count": 23, "id": "6fbcc386-5530-48a8-a575-5dd4b60693e4", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Answer:\n", "\n", "$$\n", "\\underset{\\mathcal C \\leftarrow \\mathcal B}{P}\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", ",\n", "\\qquad\n", "\\underset{\\mathcal B \\leftarrow \\mathcal C}{P}\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$\n" ], "text/plain": [ "L\"Answer:\n", "\n", "$$\n", "\\underset{\\mathcal C \\leftarrow \\mathcal B}{P}\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", ",\n", "\\qquad\n", "\\underset{\\mathcal B \\leftarrow \\mathcal C}{P}\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$\n", "\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"Answer:\n", "\n", "$$\n", "\\underset{\\mathcal C \\leftarrow \\mathcal B}{P}\n", "=\n", "%$(emptymat(2))\n", ",\n", "\\qquad\n", "\\underset{\\mathcal B \\leftarrow \\mathcal C}{P}\n", "=\n", "%$(emptymat(2))\n", "$$\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 24, "id": "28393260-86c8-4c65-923c-15047086ffff", "metadata": {}, "outputs": [], "source": [ "latexify(rationalize.(B \\ C), env=:eq);" ] }, { "cell_type": "code", "execution_count": 25, "id": "41c0831a-db42-45e1-ab56-2b2ec0c147ee", "metadata": {}, "outputs": [], "source": [ "latexify(rationalize.(C \\ B), env=:eq);" ] }, { "cell_type": "markdown", "id": "5b8ccc5e", "metadata": {}, "source": [ "# Eigenvalues" ] }, { "cell_type": "markdown", "id": "2e597da9", "metadata": {}, "source": [ "Find $\\lambda$, the largest eigenvalue of" ] }, { "cell_type": "code", "execution_count": 26, "id": "378ab2ce", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "2 & 2 & 0 & 0 \\\\\n", "3 & -1 & 0 & 0 \\\\\n", "0 & 0 & -2 & 4 \\\\\n", "0 & 0 & 5 & 4 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n" ], "text/plain": [ "L\"\\begin{equation*}\n", "\\left[\n", "\\begin{array}{cccc}\n", "2 & 2 & 0 & 0 \\\\\n", "3 & -1 & 0 & 0 \\\\\n", "0 & 0 & -2 & 4 \\\\\n", "0 & 0 & 5 & 4 \\\\\n", "\\end{array}\n", "\\right]\n", "\\end{equation*}\n", "\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Random.seed!(seed0)\n", "A11 = rand(-5:5, (2,2))\n", "A22 = rand(-5:5, (2,2))\n", "B = zeros(Int, (2,2))\n", "A = [A11 B; B A22]\n", "latexify(A, env=:eq)" ] }, { "cell_type": "code", "execution_count": 27, "id": "8bfe26cb", "metadata": {}, "outputs": [], "source": [ "A1 = sympy.Matrix(A)\n", "λ = maximum(keys(A1.eigenvals()));" ] }, { "cell_type": "code", "execution_count": 28, "id": "6d338139", "metadata": {}, "outputs": [], "source": [ "v = Dict([t[1]=>t[3][1] for t in A1.eigenvects()])[λ];" ] }, { "cell_type": "code", "execution_count": 29, "id": "e1a0de30-63a8-4b7e-911e-9d5daa90b4d1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Find also $v$, the eigenvector for $\\lambda$ which has $1$ as its last entry." ], "text/plain": [ "L\"Find also $v$, the eigenvector for $\\lambda$ which has $1$ as its last entry.\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"Find also $v$, the eigenvector for $\\lambda$ which has $%$(v[4])$ as its last entry.\"" ] }, { "cell_type": "markdown", "id": "61510b9e-b582-4c62-b693-d603bbe168f1", "metadata": {}, "source": [ "Answer:" ] }, { "cell_type": "code", "execution_count": 30, "id": "8e1973c9-53e8-4e6d-aa36-e011383a71e0", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "\\lambda\n", "=\n", "\\underline{\\hspace{3cm}} \n", ",\n", "\\qquad\n", "\\begin{bmatrix}\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " 1 \\\\\n", "\\end{bmatrix}\n", "$$\n" ], "text/plain": [ "L\"$$\n", "\\lambda\n", "=\n", "\\underline{\\hspace{3cm}} \n", ",\n", "\\qquad\n", "\\begin{bmatrix}\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " 1 \\\\\n", "\\end{bmatrix}\n", "$$\n", "\"" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"\n", "$$\n", "\\lambda\n", "=\n", "\\underline{\\hspace{3cm}} \n", ",\n", "\\qquad\n", "\\begin{bmatrix}\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " \\underline{\\hspace{3cm}} \\\\\n", " %$(v[4]) \\\\\n", "\\end{bmatrix}\n", "$$\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 31, "id": "5d6f4178", "metadata": {}, "outputs": [], "source": [ "A1 = sympy.Matrix(A)\n", "λ = maximum(keys(A1.eigenvals()));" ] }, { "cell_type": "code", "execution_count": 32, "id": "c6e87417", "metadata": {}, "outputs": [], "source": [ "v = Dict([t[1]=>t[3][1] for t in A1.eigenvects()])[λ];" ] }, { "cell_type": "markdown", "id": "352cace7", "metadata": {}, "source": [ "# Diagonalization" ] }, { "cell_type": "code", "execution_count": 33, "id": "bbc0329f", "metadata": {}, "outputs": [], "source": [ "Random.seed!(seed0)\n", "A = sympy.Matrix(randnonsing(3))\n", "eivals =[λ for λ in keys(A.eigenvals())]\n", "evals = [latexify(λ) for λ in eivals];\n", "etext = join(evals, \", \");\n", "(P, D) = A.diagonalize();" ] }, { "cell_type": "code", "execution_count": 34, "id": "47a86d8a-c141-45f2-9518-2c3534a20934", "metadata": {}, "outputs": [], "source": [ "P;" ] }, { "cell_type": "code", "execution_count": 35, "id": "d0d314e9-f27a-4bb9-bf36-7f82dcc1ab63", "metadata": {}, "outputs": [], "source": [ "D;" ] }, { "cell_type": "code", "execution_count": 36, "id": "f133fb7e", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Find an invertible matrice $P$ and $\\lambda_1, \\lambda_2, \\lambda_3 \\in \\dsR$ \n", "such that $|\\lambda_1| < |\\lambda_2| < |\\lambda_3|$ and\n", "$$\n", "P\n", "\\begin{bmatrix}\n", "\\lambda_1 & 0 & 0 \\\\\n", "0 & \\lambda_2 & 0 \\\\\n", "0 & 0 & \\lambda_2\\\\\n", "\\end{bmatrix}\n", "P^{-1}\n", "=\n", "\\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 5 \\\\\n", "4 & 1 & 5 \\\\\n", "3 & 5 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n" ], "text/plain": [ "L\"Find an invertible matrice $P$ and $\\lambda_1, \\lambda_2, \\lambda_3 \\in \\dsR$ \n", "such that $|\\lambda_1| < |\\lambda_2| < |\\lambda_3|$ and\n", "$$\n", "P\n", "\\begin{bmatrix}\n", "\\lambda_1 & 0 & 0 \\\\\n", "0 & \\lambda_2 & 0 \\\\\n", "0 & 0 & \\lambda_2\\\\\n", "\\end{bmatrix}\n", "P^{-1}\n", "=\n", "\\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 5 \\\\\n", "4 & 1 & 5 \\\\\n", "3 & 5 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n", "\"" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#L\"\"\"The matrix $A = %$(latexify(A))$ has eigenvalues $(%$etext)$.\n", "L\"\"\"Find an invertible matrice $P$ and $\\lambda_1, \\lambda_2, \\lambda_3 \\in \\dsR$ \n", "such that $|\\lambda_1| < |\\lambda_2| < |\\lambda_3|$ and\n", "$$\n", "P\n", "\\begin{bmatrix}\n", "\\lambda_1 & 0 & 0 \\\\\n", "0 & \\lambda_2 & 0 \\\\\n", "0 & 0 & \\lambda_2\\\\\n", "\\end{bmatrix}\n", "P^{-1}\n", "=\n", "%$(latexify(A))\n", "$$\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "c888a2db", "metadata": {}, "source": [ "Answer" ] }, { "cell_type": "code", "execution_count": 37, "id": "89ba3981", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "P\n", "=\n", "\\begin{bmatrix}\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "2 & 1 & 1\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "D\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$" ], "text/plain": [ "L\"$$\n", "P\n", "=\n", "\\begin{bmatrix}\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "2 & 1 & 1\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "D\n", "=\n", "\\begin{bmatrix}\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\end{bmatrix}\n", "$$\"" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"$$\n", "P\n", "=\n", "\\begin{bmatrix}\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "\\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} & \\underline{\\hspace{2cm}} \\\\\n", "%$(P[3,1]) & %$(P[3,2]) & %$(P[3,3])\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "D\n", "=\n", "%$(emptymat(3))\n", "$$\"\"\"" ] }, { "cell_type": "markdown", "id": "f17b8a4d", "metadata": {}, "source": [ "# Diagonalization" ] }, { "cell_type": "markdown", "id": "7161c118", "metadata": {}, "source": [ "Let $A, P$ and $D$ be $n \\times n$ matrices.\n", "Which of the following statements are always true?" ] }, { "cell_type": "markdown", "id": "9f55a8f8", "metadata": {}, "source": [ "* [ ] $A$ is diagonalizable if $A=PDP^{-1}$ for some matrix $D$ and some invertible matrix $P$.\n", "* [ ] If $\\dsR^n$ has a basis of eigenvectors of $A$, then $A$ is diagonalizable.\n", "* [ ] If $A$ is diagonalizable, then $A$ is invertible.\n", "* [ ] $A$ is diagonalizable if $A$ has $n$ eigenvectors.\n", "* [ ] If $A$ is invertible, then $A$ is diagonalizable." ] }, { "cell_type": "markdown", "id": "3c87e512", "metadata": {}, "source": [ "# Linear transformation" ] }, { "cell_type": "code", "execution_count": 38, "id": "5e12db5e", "metadata": {}, "outputs": [], "source": [ "Random.seed!(seed0)\n", "A = randnonsing(3)\n", "A * [3, -8, 0];" ] }, { "cell_type": "markdown", "id": "39b084c9", "metadata": {}, "source": [ "Let $\\mathcal{B} = \\{b_1, b_2, b_3\\}$ be a basis for a vector space $V$. \n", "Find $T(3 b_1 - 8 b_2)$ when $T$ is a linear transformation from $V$ to $V$ whose matrix relative to $\\mathcal B$ is" ] }, { "cell_type": "code", "execution_count": 39, "id": "0cb5979b", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "[T]_{\\mathcal B} = \\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 5 \\\\\n", "4 & 1 & 5 \\\\\n", "3 & 5 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n" ], "text/plain": [ "L\"$$\n", "[T]_{\\mathcal B} = \\left[\n", "\\begin{array}{ccc}\n", "3 & 2 & 5 \\\\\n", "4 & 1 & 5 \\\\\n", "3 & 5 & 1 \\\\\n", "\\end{array}\n", "\\right]\n", "$$\n", "\"" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"\n", "$$\n", "[T]_{\\mathcal B} = %$(latexify(A))\n", "$$\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "0f8f4d4c", "metadata": {}, "source": [ "Answer:\n", "\n", "$$\n", "T(3 b_1 - 8 b_2)\n", "=\n", "\\underline{\\hspace{1cm}} b_1 \n", "+ \n", "\\underline{\\hspace{1cm}} b_2\n", "+\n", "\\underline{\\hspace{1cm}} b_3 \n", "$$" ] }, { "cell_type": "markdown", "id": "a6f7cdc0", "metadata": {}, "source": [ "# Discrete dynamic systems" ] }, { "cell_type": "code", "execution_count": 40, "id": "1f1e85f8", "metadata": {}, "outputs": [], "source": [ "myseed()\n", "A = round.(rand(Float64, (2,2)), digits=1);" ] }, { "cell_type": "code", "execution_count": 41, "id": "e9a4bd91", "metadata": {}, "outputs": [], "source": [ "eigvals(A);" ] }, { "cell_type": "code", "execution_count": 42, "id": "e0ed1be2", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "The origin in the discrete dynamic system $x_{k+1} = \\left[\n", "\\begin{array}{cc}\n", "0.7 & 0.6 \\\\\n", "0.7 & 0.4 \\\\\n", "\\end{array}\n", "\\right]x_k$ is a" ], "text/plain": [ "L\"The origin in the discrete dynamic system $x_{k+1} = \\left[\n", "\\begin{array}{cc}\n", "0.7 & 0.6 \\\\\n", "0.7 & 0.4 \\\\\n", "\\end{array}\n", "\\right]x_k$ is a\"" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"The origin in the discrete dynamic system $x_{k+1} = %$(latexify(A))x_k$ is a\"" ] }, { "cell_type": "code", "execution_count": 43, "id": "83dc1ce0-a046-483f-bfdc-b4689b3a9cda", "metadata": {}, "outputs": [], "source": [ "V = eigvecs(A);" ] }, { "cell_type": "code", "execution_count": 44, "id": "4e8600bb-4431-4b10-9f31-b9e1f37c81d0", "metadata": {}, "outputs": [], "source": [ "pic = scatter((0,0), legend=nothing)\n", "for p in Iterators.product(-1:1,-1:1)\n", "xs = 100 * V * [(p)...]\n", "for i in 1:10\n", " xnext = A*xs[:, end]\n", " xs = hcat(xs, xnext)\n", "end\n", "scatter!(xs[1, :], xs[2, :])\n", "plot!(xs[1, :], xs[2, :], arrow=true)\n", "end\n", "pic;" ] }, { "cell_type": "markdown", "id": "6aaf501c", "metadata": {}, "source": [ "* [ ] repeller\n", "* [ ] attractor\n", "* [ ] saddle point\n", "* [ ] pivot point\n", "* [ ] non above" ] }, { "cell_type": "markdown", "id": "5bd9e468-71c7-4a36-bf21-f1582a78462d", "metadata": {}, "source": [ "# Orthogonal projection\n", "\n", "Which of the following statements are true?\n", "\n", "* [ ] If the columns of an $m \\times n$ matrix $A$ are orthonormal, then the linear mapping $x \\mapsto Ax$ preserves lengths.\n", "* [ ] Every orthogonal set in $\\dsR^n$ is linearly independent.\n", "* [ ] The orthogonal projection of $y$ onto $v$ is the same as the orthogonal projection of $y$ onto $c v$ whenever $c \\ne 0$.\n", "* [ ] An orthogonal matrix is invertible.\n", "* [ ] If a set $S = \\{u_1, \\ldots, u_p\\}$ has the property that $u_i \\cdot u_j = 0$ whenever $i \\ne j$, then S is an orthogonal set." ] }, { "cell_type": "markdown", "id": "3a48479e-5407-420c-ba9c-f5c827076186", "metadata": {}, "source": [ "# QR Factorization\n", "\n", "Find a QR Factorization of $A$ where" ] }, { "cell_type": "code", "execution_count": 45, "id": "1f91c4ca", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$A = \\left[\n", "\\begin{array}{ccc}\n", "1 & 2 & 1 \\\\\n", "1 & 2 & 2 \\\\\n", "1 & 2 & -1 \\\\\n", "0 & 0 & 0 \\\\\n", "0 & -1 & 2 \\\\\n", "\\end{array}\n", "\\right]$$" ], "text/plain": [ "L\"$$A = \\left[\n", "\\begin{array}{ccc}\n", "1 & 2 & 1 \\\\\n", "1 & 2 & 2 \\\\\n", "1 & 2 & -1 \\\\\n", "0 & 0 & 0 \\\\\n", "0 & -1 & 2 \\\\\n", "\\end{array}\n", "\\right]$$\"" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Random.seed!(seed0)\n", "A = rand(-1:2, (5, 3))\n", "Al = latexify(A)\n", "L\"$$A = %$Al$$\"" ] }, { "cell_type": "code", "execution_count": 46, "id": "e932aad5-ddbe-4a94-ac2f-01257e03ed79", "metadata": {}, "outputs": [], "source": [ "(x1, x2, x3) = collect(eachcol(A))\n", "v1 = x1\n", "v2 = x2 - dot(v1, x2)//dot(v1, v1)*v1\n", "v3 = x3 - dot(v1, x3)//dot(v1, v1)*v1 - dot(v2, x3)//dot(v2, v2)*v2\n", "W = hcat(v1, v2, v3)\n", "latexify(W, env=:eq)\n", "\n", "W1 = [sqrt(Sym(dot(W[:,i], W[:,i]))) for i in 1:3]\n", "Q1 = hcat([W[:, i] / W1[i] for i in 1:3]...);" ] }, { "cell_type": "code", "execution_count": 47, "id": "8d740aa3", "metadata": {}, "outputs": [], "source": [ "R1 = Q1'*A;" ] }, { "cell_type": "code", "execution_count": 48, "id": "00665540-d416-4d60-9717-0a365b2a96ad", "metadata": {}, "outputs": [], "source": [ "A == Q1 * R1;" ] }, { "cell_type": "markdown", "id": "3b495962-f209-4fa0-972a-0b54783c1fa5", "metadata": {}, "source": [ "Answer:" ] }, { "cell_type": "code", "execution_count": 49, "id": "3d4eead5-513d-4a6d-998e-d25d098737f6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\n", "Q = \\begin{bmatrix}\\frac{\\sqrt{3}}{3}& 0& \\frac{\\sqrt{42}}{42}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\end{bmatrix}\n", ",\n", "\\qquad\n", "R = \\begin{bmatrix}\\sqrt{3}& 2 \\cdot \\sqrt{3}& \\frac{2 \\cdot \\sqrt{3}}{3}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}& \\underline{\\hspace{1cm}}\\\\\n", "\\end{bmatrix}\n", ".\n", "$$\n" ], "text/plain": [ "\"\\$\\$\\nQ = \\\\begin{bmatrix}\\\\frac{\\\\sqrt{3}}{3}& 0& \\\\frac{\\\\sqrt{42}}{42}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{\" ⋯ 119 bytes ⋯ \"matrix}\\n,\\n\\\\qquad\\nR = \\\\begin{bmatrix}\\\\sqrt{3}& 2 \\\\cdot \\\\sqrt{3}& \\\\frac{2 \\\\cdot \\\\sqrt{3}}{3}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}& \\\\underline{\\\\hspace{1cm}}\\\\\\\\\\n\\\\end{bmatrix}\\n.\\n\\$\\$\\n\"" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L\"\"\"\n", "$$\n", "Q = %$(partialmat(Q1, [1]))\n", ",\n", "\\qquad\n", "R = %$(partialmat(R1, [1]))\n", ".\n", "$$\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "87d7e60c", "metadata": {}, "source": [ "# Least-squares lines" ] }, { "cell_type": "markdown", "id": "abba71cb", "metadata": {}, "source": [ "Find the equation $y = \\beta_0 + \\beta_1 x$ of the least-squares line that best fit the given data" ] }, { "cell_type": "code", "execution_count": 50, "id": "95088c19", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myseed()\n", "ys = rand(0:5, 4)\n", "xs = range(1, 4, step=1)\n", "pic = scatter(xs, ys, label=\"data\", size=(200, 200), framestyle=:origin)\n", "#savefig(pic, \"images/data.png\")\n", "#pic" ] }, { "cell_type": "markdown", "id": "d9c5fde6", "metadata": {}, "source": [ "Answer: $(\\beta_0, \\beta_1) = (\\underline{\\hspace{2cm}}, \\underline{\\hspace{2cm}})$" ] }, { "cell_type": "code", "execution_count": 51, "id": "8e4c89f2", "metadata": {}, "outputs": [], "source": [ "## draw the picture\n", "\n", "xs1 = fill(1, 4)\n", "X = hcat(xs1, xs)\n", "β = round.(X \\ ys, digits=6);" ] }, { "cell_type": "code", "execution_count": 52, "id": "02614a1e", "metadata": {}, "outputs": [], "source": [ "line(x) = dot(β, [1 x])\n", "@syms x\n", "plot(pic, line, label = L\"y = %$(latexify(line(x)))\");" ] }, { "cell_type": "markdown", "id": "f5866eef", "metadata": {}, "source": [ "# Orthonormal bases\n", "\n", "An orthogonal matrix is a square invertible matrix $U$ such that $U^{-1} = U^T$. Such a matrix has orthonormal columns.\n", "\n", "Let $U$ be an $n \\times n$ orthogonal matrix. Show that the rows of $U$ form an orthonormal basis of $\\dsR^n$." ] }, { "cell_type": "code", "execution_count": 53, "id": "3f5277c4-fbc6-455e-bf8e-2a41aed6677e", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Answer:\n", "\n", "\\vspace{180pt}\n" ], "text/plain": [ "L\"Answer:\n", "\n", "\\vspace{180pt}\n", "\"" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answerblock()" ] }, { "cell_type": "code", "execution_count": 54, "id": "e579dd77-66f0-416d-8480-29e827465f64", "metadata": {}, "outputs": [], "source": [ "myseed()\n", "A = randnonsing(5)\n", "V = A[:, 1]\n", "for i in 2:5\n", " x = A[:, i]\n", " x0 = copy(x)\n", " for j in 1:i-1\n", " v = V[:, j]\n", " x0 -= dot(x, v)/dot(v, v) * v\n", " end\n", " V = hcat(V, x0)\n", "end\n", "for i in 1:5\n", " v = V[:, i]\n", " V[:, i] = v / norm(v)\n", "end\n", "V\n", "norm.(eachcol(V));" ] }, { "cell_type": "code", "execution_count": 55, "id": "1c0bc151-6f9e-4d8e-a1e6-e179b77654c3", "metadata": {}, "outputs": [], "source": [ "norm.(eachrow(V));" ] }, { "cell_type": "markdown", "id": "80bf2e42", "metadata": {}, "source": [ "# Bonus question" ] }, { "cell_type": "markdown", "id": "07baa790", "metadata": {}, "source": [ "Define a linear transformation \n", "$T : \\mathbb{P}^2 \\mapsto \\dsR^2$ \n", "by $T(p) = \\begin{bmatrix}p(0) \\\\ p(0)\\end{bmatrix}$. \n", "Find polynomials $p_1$ and $p_2$ that span the kernel of $T$, and describe the range of $T$." ] }, { "cell_type": "code", "execution_count": 56, "id": "46a1b424-6f12-4012-9ee7-2de1170ec682", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Answer:\n", "\n", "\\vspace{180pt}\n" ], "text/plain": [ "L\"Answer:\n", "\n", "\\vspace{180pt}\n", "\"" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answerblock()" ] }, { "cell_type": "markdown", "id": "efe680b1-aca5-4d9f-8b10-63fb65556d25", "metadata": {}, "source": [ "# Bonus question" ] }, { "cell_type": "markdown", "id": "bd5bff24-91b3-45b6-a243-744c747d6462", "metadata": {}, "source": [ "Guess the inverse of the $n \\times n$ matrix\n", "\n", "$$\n", "\\begin{bmatrix}\n", " 1 & 0 & 0 & 0 & \\dots & 0 \\\\\n", " 1 & 1 & 0 & 0 & \\dots & 0 \\\\\n", " 1 & 1 & 1 & 0 & \\dots & 0 \\\\\n", " 1 & 1 & 1 & 1 & \\dots & 0 \\\\\n", " \\vdots & \\vdots & \\vdots & \\vdots & & 0 \\\\\n", " 1 & 1 & 1 & \\dots & 1 & 0 \\\\\n", " 1 & 1 & 1 & \\dots & 1 & 1 \\\\\n", "\\end{bmatrix}\n", "$$\n", "\n", "and prove that your guess is correct." ] }, { "cell_type": "code", "execution_count": 57, "id": "8e939cf4-0aa9-4d3c-9c4c-9abb550e174f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Answer:\n", "\n", "\\vspace{350pt}\n" ], "text/plain": [ "L\"Answer:\n", "\n", "\\vspace{350pt}\n", "\"" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answerblock(350)" ] }, { "cell_type": "raw", "id": "d5a23e16-1168-454c-ba6c-89b62bf61439", "metadata": {}, "source": [ "\\rule{\\textwidth}{0.4pt}\n", "\\begin{center}\n", "Good bye! I will see you around.\n", "\\end{center}" ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "authors": [ { "name": "Instructor: Xing Shi Cai" } ], "date": "Feb 23, 2022", "hide_input": false, "kernelspec": { "display_name": "Julia 1.7.1", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.1" }, "title": "MATH 202 Linear Algebra --- Exam 3", "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "221.6px" }, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": true }, "nbformat": 4, "nbformat_minor": 5 }