{ "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", "