{ "cells": [ { "cell_type": "markdown", "id": "bf20212a-80f7-4066-a70e-58dea3f35a38", "metadata": {}, "source": [ "Submit your answers to gradescope before, or at 3:40pm." ] }, { "cell_type": "markdown", "id": "e8cfd805-36b2-41f9-a7c6-64d881bdec40", "metadata": {}, "source": [ "# Question 1" ] }, { "cell_type": "markdown", "id": "03aceab3-b08f-47cb-82c0-b39d11a5bee0", "metadata": {}, "source": [ "Let $N = \\exp(\\sqrt{5})$.\n", "\n", "1. Compute a rational approximation for $N$, accurate to 32 decimal places.\n", "\n", "2. Verify the accuracy of your rational approximation by computing the relative error." ] }, { "cell_type": "markdown", "id": "aa0d29bf-8e07-40a0-91c4-d826d4ba126b", "metadata": {}, "source": [ "## answer to question 1" ] }, { "cell_type": "code", "execution_count": 1, "id": "cf059dea-ae44-4b1b-8bf1-7703eba3c107", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 2, "id": "8bcdf7c1-ce85-4a1f-9195-0261218da93e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle e^{\\sqrt{5}}\\)" ], "text/latex": [ "$\\displaystyle e^{\\sqrt{5}}$" ], "text/plain": [ "e^sqrt(5)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "N = exp(sqrt(5))\n", "show(N)" ] }, { "cell_type": "code", "execution_count": 3, "id": "75533210-e752-4e46-bbd2-60efd3b1b33f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle \\frac{143063362936898051}{15290315468694576}\\)" ], "text/latex": [ "$\\displaystyle \\frac{143063362936898051}{15290315468694576}$" ], "text/plain": [ "143063362936898051/15290315468694576" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "q = QQ(N.n(digits=32))\n", "show(q)" ] }, { "cell_type": "markdown", "id": "d41c5a36-2974-4a74-9328-b5c6094ee304", "metadata": {}, "source": [ "To compute the relative error, we must use a precision of at least 32 decimal places. Let us use 33." ] }, { "cell_type": "code", "execution_count": 4, "id": "5b5c3a72-8a91-4100-bacd-1c1d084d573d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.29343035877327771428903377766835e-34" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e = abs(N - q)/N\n", "e.n(digits=33)" ] }, { "cell_type": "markdown", "id": "9ab9b636-baba-4ce8-b683-7f00ab528af5", "metadata": {}, "source": [ "Observing that the relative error is $10^{-34}$, we have indeed 32 decimal places correct." ] }, { "cell_type": "markdown", "id": "a483fc53-d094-4713-a7ce-fdfada77588b", "metadata": {}, "source": [ "# Question 2" ] }, { "cell_type": "markdown", "id": "fcc284b9-b3b2-4adc-8166-636a1c33f4f5", "metadata": {}, "source": [ "Consider the finite field with 7 elements.\n", "\n", "Show that $p = x^3 + 3 x^2 + 6$ is irreducible over this field.\n", "\n", "Declare $\\alpha$ as a formal root of $p$.\n", "What is $\\alpha^7$ in this finite field extension?" ] }, { "cell_type": "markdown", "id": "7cbe960e-4c8f-4c0c-9b0e-95ec34b8b6a2", "metadata": {}, "source": [ "## answer to question 2" ] }, { "cell_type": "code", "execution_count": 5, "id": "b2b84790-11f2-4f24-baa7-efcc9ae14634", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 6, "id": "7e38c56c-c9e8-499a-a08b-9781b68e1e13", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = polygen(GF(7))\n", "p = x^3 + 3*x^2 + 6\n", "p.is_irreducible()" ] }, { "cell_type": "code", "execution_count": 7, "id": "37896883-ddcb-4789-8467-173c2aea8ea4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Finite Field in alpha of size 7^3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K. = GF(7).extension(p)\n", "K" ] }, { "cell_type": "code", "execution_count": 8, "id": "4cc11b59-5cfd-4a4b-afc2-33f263a96068", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "alpha^2 + 2*alpha + 5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alpha^7" ] }, { "cell_type": "markdown", "id": "e0a6bc5d-b7ea-493f-bed7-9920fb512333", "metadata": {}, "source": [ "# Question 3" ] }, { "cell_type": "markdown", "id": "26124e9f-4afa-4105-9a0b-0c24765934eb", "metadata": {}, "source": [ "Compute a numeric factorization of $p = x^4 + 3 x^2 + x - 1$.\n", "\n", "Expand the factorization and compare the coefficients of the expanded form\n", "with the coefficients of $p$. \n", "\n", "What is the largest error on the coefficients?" ] }, { "cell_type": "markdown", "id": "d9900b31-cb02-4862-921c-da51849b3dca", "metadata": {}, "source": [ "## answer to question 3" ] }, { "cell_type": "code", "execution_count": 9, "id": "f483df22-d807-4563-9170-6a62e4c8a247", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 10, "id": "20e75507-6b21-4920-83aa-3c40b5c541c5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x - 0.425129513294570) * (x - 0.136327508783692 - 1.83095722695625*I) * (x - 0.136327508783692 + 1.83095722695625*I) * (x + 0.697784530861954)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = polygen(CC)\n", "p = x^4 + 3*x^2 + x - 1\n", "f = factor(p)\n", "f" ] }, { "cell_type": "code", "execution_count": 11, "id": "9f6186d7-47c2-43a9-96c2-543d6d8bfc4b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "x^4 + (1.11022302462516e-16)*x^3 + 3.00000000000000*x^2 + (1.00000000000000 + 1.38777878078145e-17*I)*x - 1.00000000000000 + 9.68370565487756e-18*I" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ef = f.expand()\n", "ef" ] }, { "cell_type": "code", "execution_count": 12, "id": "c8a103b4-81f5-4b27-8455-e39fb9a405d6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.11022302462516e-16)*x^3 + (4.44089209850063e-16)*x^2 + (4.44089209850063e-16 + 1.38777878078145e-17*I)*x + 9.68370565487756e-18*I" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "err = ef - p\n", "err" ] }, { "cell_type": "code", "execution_count": 13, "id": "c465f1bb-c406-4057-ad01-93308bb173b5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9.68370565487756e-18*I,\n", " 4.44089209850063e-16 + 1.38777878078145e-17*I,\n", " 4.44089209850063e-16,\n", " 1.11022302462516e-16]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "errcff = err.coefficients()\n", "errcff" ] }, { "cell_type": "markdown", "id": "b8e079aa-6543-4868-8330-a8fdc16c380a", "metadata": {}, "source": [ "The largest error is thus the absolute value of the largest coefficient of the difference between `ef` and `p`." ] }, { "cell_type": "code", "execution_count": 14, "id": "b089e519-c9e8-40fa-be9a-e47775f69843", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.44305997370834e-16" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max([abs(e) for e in errcff])" ] }, { "cell_type": "markdown", "id": "4237a0a7-5487-4f74-9b78-8928dcf61d66", "metadata": {}, "source": [ "The maximum error is $4.4 \\times 10^{-16}$, smaller than the hardware machine precision." ] }, { "cell_type": "markdown", "id": "3f81a6c5-9c1c-4035-b199-22a29bc9875a", "metadata": {}, "source": [ "# Question 4" ] }, { "cell_type": "markdown", "id": "bf572878-6f86-47fd-ae4f-53cec12d625f", "metadata": {}, "source": [ "Draw the binary expression tree defined by the fast callable object for\n", "\n", "$$\n", " (\\cos(b) - \\sin(c^2) + 2)/(b c - 2 a).\n", "$$" ] }, { "cell_type": "markdown", "id": "5284c117-77c6-4646-b4e0-18e19134efc0", "metadata": {}, "source": [ "## answer to question 4" ] }, { "cell_type": "code", "execution_count": 15, "id": "7e9c48fe-6bae-4716-ad0c-1e61dc764662", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 16, "id": "0a4ef6a4-5e13-4581-a4fd-23d63ae04b72", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('load_arg', 0), ('py_call', cos, 1), 'return']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fcos = fast_callable(cos(x), vars=['x'])\n", "fcos.op_list()" ] }, { "cell_type": "markdown", "id": "d01dcb54-10bc-4d38-85ce-917e85d3044c", "metadata": {}, "source": [ "Observe that `cos(x)` should be interpreted as a call to `py_call` with two arguments: `cos` and `x`." ] }, { "cell_type": "code", "execution_count": 17, "id": "a910fc9f-bf1a-41c7-ac98-9010cf9efdc5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "div(add(sub(v_1, ipow(v_2, 2)), 2), sub(mul(v_1, v_2), mul(2, v_0)))\n" ] } ], "source": [ "from sage.ext.fast_callable import ExpressionTreeBuilder\n", "etb = ExpressionTreeBuilder(vars=['a', 'b', 'c'])\n", "a = etb.var('a')\n", "b = etb.var('b')\n", "c = etb.var('c')\n", "e = (b - c^2 + 2)/(b*c - 2*a)\n", "print(e)" ] }, { "cell_type": "code", "execution_count": 18, "id": "e45aa5e5-97be-48d6-8be8-c245c57f42e6", "metadata": {}, "outputs": [], "source": [ "La = LabelledBinaryTree([], label='a')\n", "Lb = LabelledBinaryTree([], label='b')\n", "Lc = LabelledBinaryTree([], label='c')\n", "L2 = LabelledBinaryTree([], label='2')" ] }, { "cell_type": "code", "execution_count": 19, "id": "d426b06a-6c4e-42b9-a019-09b17fefa295", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " ___sub___\n", " / \\\n", " mul mul\n", " / \\ / \\\n", "b c 2 a" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Nbc = LabelledBinaryTree([Lb, Lc], label='mul')\n", "N2a = LabelledBinaryTree([L2, La], label='mul')\n", "denominator = LabelledBinaryTree([Nbc, N2a], label='sub')\n", "ascii_art(denominator)" ] }, { "cell_type": "code", "execution_count": 20, "id": "700e7b8f-be72-4cf3-967d-f5993d7a8327", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " ______sub_______\n", " / \\\n", " _py_call_ __py_call__\n", " / \\ / \\\n", "cos b sin ipow\n", " / \\\n", " c 2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Lcos = LabelledBinaryTree([], label='cos')\n", "Lsin = LabelledBinaryTree([], label='sin')\n", "Ncosb = LabelledBinaryTree([Lcos, Lb], label='py_call')\n", "Nc2 = LabelledBinaryTree([Lc, L2], label='ipow')\n", "Nsinc2 = LabelledBinaryTree([Lsin, Nc2], label='py_call')\n", "Nnumop1 = LabelledBinaryTree([Ncosb, Nsinc2], label='sub')\n", "ascii_art(Nnumop1)" ] }, { "cell_type": "code", "execution_count": 21, "id": "78f250ce-d86f-4da6-942a-e22348d3df8b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " ____________div____________\n", " / \\\n", " _____________add_____________ ___sub___\n", " / \\ / \\\n", " ______sub_______ 2 mul mul\n", " / \\ / \\ / \\\n", " _py_call_ __py_call__ b c 2 a\n", " / \\ / \\ \n", "cos b sin ipow \n", " / \\ \n", " c 2 " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerator = LabelledBinaryTree([Nnumop1, L2], label='add')\n", "tree = LabelledBinaryTree([numerator, denominator], label='div')\n", "ascii_art(tree)" ] }, { "cell_type": "markdown", "id": "60c1573b-36ca-4211-a289-0abd8390a45b", "metadata": {}, "source": [ "# Question 5" ] }, { "cell_type": "markdown", "id": "139dd403-48c5-45f9-926a-c62830122fe6", "metadata": {}, "source": [ "What does the preparser in SageMath do?\n", "\n", "Give an application of `preparse(x)`, with a good example \n", "for its argument `x`." ] }, { "cell_type": "markdown", "id": "1ecb39ce-944b-4a25-8bcc-597be320f0d5", "metadata": {}, "source": [ "## answer to question 5" ] }, { "cell_type": "code", "execution_count": 22, "id": "4269178c-3172-46f4-8036-8c9975a28d25", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "markdown", "id": "a846e198-1f97-44ba-8123-b7c0d4a3d692", "metadata": {}, "source": [ "The `preparse` interpretes strings within the context of SageMath." ] }, { "cell_type": "code", "execution_count": 23, "id": "8bff53af-6501-4650-b3bb-50c01791f0a1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14159265358979323846264338328'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Pi30 = pi.n(digits=30)\n", "strPi30 = str(Pi30)\n", "strPi30" ] }, { "cell_type": "code", "execution_count": 24, "id": "fef6450d-244f-4b22-bec9-18c00e239dce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"RealNumber('3.14159265358979323846264338328')\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preparse(strPi30)" ] }, { "cell_type": "markdown", "id": "d5463399-8e12-4d61-9ed7-d1481f49db56", "metadata": {}, "source": [ "Observe the `RealNumber()` as a result of the `preparse()` applied to the string representation of a 30-digit approximation for $\\pi$." ] }, { "cell_type": "markdown", "id": "36574236-42ec-4633-a58a-b031d9eb2f8a", "metadata": {}, "source": [ "# Question 6" ] }, { "cell_type": "markdown", "id": "76de6902-0672-4f45-8d53-ea9ad083dc5c", "metadata": {}, "source": [ "The\n", "``f = lambda n: float(sum([(k/n)*exp(k/n) for k in range(1,n)])/n)``\n", "\n", "computes the right hand side of\n", "$$\n", " \\qquad \\int_0^1 x e^x dx \\approx \\frac{1}{n} \n", " \\sum_{k=1}^{n-1} \\left( \\frac{k}{n} \\right) \\exp\\left( \\frac{k}{n} \\right).\n", "$$\n", "\n", "1. Time the execution of `f` for $n = 10000$.\n", " \n", " Explain why `f` is inefficient.\n", "\n", "2. Apply vectorization to improve the efficiency. Verify the correctness.\n", "\n", " Time the execution of the vectorized function for $n = 10000$, compare with timings of `f`." ] }, { "cell_type": "markdown", "id": "e9107b63-0a75-4342-beba-0ef0860ae7da", "metadata": {}, "source": [ "## answer to question 6" ] }, { "cell_type": "code", "execution_count": 25, "id": "d48b1651-617d-4151-9634-4de187100cc1", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 26, "id": "e1aeb5d0-72d0-4c16-bf92-f75a93ccb198", "metadata": {}, "outputs": [], "source": [ "f = lambda n: float(sum([(k/n)*exp(k/n) for k in range(1,n)])/n)" ] }, { "cell_type": "code", "execution_count": 27, "id": "bde1bc57-f7a2-49fd-b586-978046a32e22", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5 loops, best of 3: 767 ms per loop" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeit('f(10000)')" ] }, { "cell_type": "markdown", "id": "9317e118-daf1-47da-b0b1-2fd2c9d8415b", "metadata": {}, "source": [ "The evaluation of `f` is inefficient because the `exp()` is evaluated at exact rational numbers and kept as symbolic expressions, right before the final evaluation of the sum to a float." ] }, { "cell_type": "code", "execution_count": 28, "id": "96ed3a8d-6546-477c-b695-12d6588086f6", "metadata": {}, "outputs": [], "source": [ "def vf(n):\n", " \"\"\"\n", " Returns the result of the vectorized version of f.\n", " \"\"\"\n", " from numpy import arange, exp, sum\n", " arg = arange(1, n)/n\n", " return sum(arg*exp(arg))/n" ] }, { "cell_type": "markdown", "id": "4338bda5-0d49-4fd9-9c37-256d0e49653e", "metadata": {}, "source": [ "Let us verify the correctness." ] }, { "cell_type": "code", "execution_count": 29, "id": "584c5577-f1c5-46e1-9440-24b9c14b21d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9864455621121638" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(100)" ] }, { "cell_type": "code", "execution_count": 30, "id": "c24a69f2-12a3-4090-b438-3a10f694adf5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9864455621121637" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vf(100)" ] }, { "cell_type": "code", "execution_count": 31, "id": "ad366a8b-4a82-4f07-bfdf-12f346a52806", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "625 loops, best of 3: 56.8 μs per loop" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeit('vf(10000)')" ] }, { "cell_type": "markdown", "id": "0a8e2902-8667-4135-b5ab-214d14e6bf72", "metadata": {}, "source": [ "Observe the drop in units, from milliseconds to microseconds." ] }, { "cell_type": "markdown", "id": "6e022353-3cab-4db2-9db7-82ec1c9e34d3", "metadata": {}, "source": [ "# Question 7" ] }, { "cell_type": "markdown", "id": "9ca487b5-1e50-4fd2-96fc-bc533162709c", "metadata": {}, "source": [ "Let $q = (x^6 - 3 x^4 + 2 x + 1)/(x^6 + 2 x^5 - 3 x^4 + 3 x + 7)$.\n", "\n", "Compute a partial fraction decomposition of $q$ \n", "over the complex numbers.\n", "\n", "How many operations does it takes to evaluate $q$ compared\n", "the number of operations to evaluate its partial fraction decomposition?" ] }, { "cell_type": "code", "execution_count": 32, "id": "eec4e459-1110-423b-8553-04384ab1e884", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 33, "id": "fecb02e7-4cc6-4ca5-b1a1-6563264e99f9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle \\frac{x^{6} + 0.000000000000000 x^{5} - 3.00000000000000 x^{4} + 0.000000000000000 x^{3} + 0.000000000000000 x^{2} + 2.00000000000000 x + 1.00000000000000}{x^{6} + 2.00000000000000 x^{5} - 3.00000000000000 x^{4} + 0.000000000000000 x^{3} + 0.000000000000000 x^{2} + 3.00000000000000 x + 7.00000000000000}\\)" ], "text/latex": [ "$\\displaystyle \\frac{x^{6} + 0.000000000000000 x^{5} - 3.00000000000000 x^{4} + 0.000000000000000 x^{3} + 0.000000000000000 x^{2} + 2.00000000000000 x + 1.00000000000000}{x^{6} + 2.00000000000000 x^{5} - 3.00000000000000 x^{4} + 0.000000000000000 x^{3} + 0.000000000000000 x^{2} + 3.00000000000000 x + 7.00000000000000}$" ], "text/plain": [ "(x^6 - 3.00000000000000*x^4 + 2.00000000000000*x + 1.00000000000000)/(x^6 + 2.00000000000000*x^5 - 3.00000000000000*x^4 + 3.00000000000000*x + 7.00000000000000)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x = polygen(CC)\n", "q = (x^6 - 3*x^4 + 2*x + 1)/(x^6 + 2*x^5 - 3*x^4 + 3*x + 7)\n", "show(q)" ] }, { "cell_type": "code", "execution_count": 34, "id": "d89f74de-37dc-4358-85db-e00796e78342", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.00000000000000,\n", " [(-0.0990462615843695 + 0.137869235850092*I)/(x - 1.22782196703728 - 0.646108324775135*I),\n", " (-0.0990462615843695 - 0.137869235850092*I)/(x - 1.22782196703728 + 0.646108324775135*I),\n", " (-0.0766287244269281 + 0.117227796511901*I)/(x + 0.224737235232175 - 1.07662670516138*I),\n", " (-0.0766287244269281 - 0.117227796511901*I)/(x + 0.224737235232175 + 1.07662670516138*I),\n", " (-0.157894736842105)/(x + 1.00000000000000),\n", " (-1.49075529113530)/(x + 3.00616946361022)])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = q.partial_fraction_decomposition()\n", "f" ] }, { "cell_type": "markdown", "id": "4d0da033-c7b4-469b-89ea-ddb206cfadb0", "metadata": {}, "source": [ "Comparing the evaluation of `q` and `f`:\n", " \n", "1. The Horner form of the numerator and denominator of `q` requires 6 multiplications and 6 additions (subtractions), for a total of 12 multiplications and 12 additions (subtractions).\n", "\n", "2. The partial fraction decomposition `f` requires 6 divisions and 6 additions." ] }, { "cell_type": "markdown", "id": "4812edc4-e8c4-49d1-94ee-18bbd7c0c103", "metadata": {}, "source": [ "# Question 8" ] }, { "cell_type": "markdown", "id": "eea59726-9a68-4319-b3bf-c0301676f7ff", "metadata": {}, "source": [ "Transform\n", " $\\displaystyle (x-y)^4 + \\frac{(x+y)^3}{(x-y)^2}$\n", " into $\\displaystyle \\frac{(x-y)^6 + (x+y)^3}{(x-y)^2}$,\n", " without retyping." ] }, { "cell_type": "markdown", "id": "735d11eb-815d-4647-9e5e-87726c128751", "metadata": {}, "source": [ "## answer to question 8" ] }, { "cell_type": "code", "execution_count": 35, "id": "a19a15e9-ba1e-4ffb-a277-19387eaa1fcb", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 36, "id": "b30e967a-ac02-4ed0-ae36-3c2e0a841f68", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle {\\left(x - y\\right)}^{4} + \\frac{{\\left(x + y\\right)}^{3}}{{\\left(x - y\\right)}^{2}}\\)" ], "text/latex": [ "$\\displaystyle {\\left(x - y\\right)}^{4} + \\frac{{\\left(x + y\\right)}^{3}}{{\\left(x - y\\right)}^{2}}$" ], "text/plain": [ "(x - y)^4 + (x + y)^3/(x - y)^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x, y = var('x, y')\n", "q = (x-y)^4 + (x+y)^3/(x-y)^2\n", "show(q)" ] }, { "cell_type": "code", "execution_count": 37, "id": "1bad6389-32b2-435e-a552-c90f7ea3da24", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle u^{4} + \\frac{v^{3}}{u^{2}}\\)" ], "text/latex": [ "$\\displaystyle u^{4} + \\frac{v^{3}}{u^{2}}$" ], "text/plain": [ "u^4 + v^3/u^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "u, v = var('u, v')\n", "quv = q.subs({x-y: u, x+y: v})\n", "show(quv)" ] }, { "cell_type": "code", "execution_count": 38, "id": "9f3c940a-6cdf-4710-ade8-f1e0202d1d3a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle \\frac{u^{6} + v^{3}}{u^{2}}\\)" ], "text/latex": [ "$\\displaystyle \\frac{u^{6} + v^{3}}{u^{2}}$" ], "text/plain": [ "(u^6 + v^3)/u^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "equv = quv.numerator(normalize=True)/quv.denominator()\n", "show(equv)" ] }, { "cell_type": "code", "execution_count": 39, "id": "a2eef4f1-3265-45ed-95d8-3c83055c0ea6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle \\frac{{\\left(x - y\\right)}^{6} + {\\left(x + y\\right)}^{3}}{{\\left(x - y\\right)}^{2}}\\)" ], "text/latex": [ "$\\displaystyle \\frac{{\\left(x - y\\right)}^{6} + {\\left(x + y\\right)}^{3}}{{\\left(x - y\\right)}^{2}}$" ], "text/plain": [ "((x - y)^6 + (x + y)^3)/(x - y)^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "result = equv(u = x-y, v = x+y)\n", "show(result)" ] }, { "cell_type": "markdown", "id": "08631d7c-dc8f-4ff3-874b-50a4827bf323", "metadata": {}, "source": [ "# Question 9" ] }, { "cell_type": "markdown", "id": "ce87a84b-5368-4300-863b-26998c55947d", "metadata": {}, "source": [ "Are the expressions $\\displaystyle p = \\frac{x^2 + 7 x - 8}{x - 1}$ and $q = x + 8$ the same?\n", "\n", "Justify your answer by appropriate *symbolic* computations.\n", "\n", "Demonstrate the application of a *numerical* probability-one equality test." ] }, { "cell_type": "markdown", "id": "faeef771-c93c-40bf-affc-dd0731dd611d", "metadata": {}, "source": [ "## answer to question 9" ] }, { "cell_type": "code", "execution_count": 40, "id": "a3292dfb-9ae5-4af7-81eb-7f0406182297", "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "code", "execution_count": 41, "id": "db43e72b-3743-490b-9eb7-3e77bc6fce31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle \\frac{x^{2} + 7 \\, x - 8}{x - 1}\\)" ], "text/latex": [ "$\\displaystyle \\frac{x^{2} + 7 \\, x - 8}{x - 1}$" ], "text/plain": [ "(x^2 + 7*x - 8)/(x - 1)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x = var('x')\n", "p = (x^2 + 7*x - 8)/(x-1)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 42, "id": "1ee10c2c-42f4-44ad-ac38-3e733809577e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\(\\displaystyle x + 8\\)" ], "text/latex": [ "$\\displaystyle x + 8$" ], "text/plain": [ "x + 8" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "q = x + 8\n", "show(q)" ] }, { "cell_type": "markdown", "id": "d7859335-344b-454b-9daf-1c4b9baa1ea1", "metadata": {}, "source": [ "The symbolic test on equality applies normalization." ] }, { "cell_type": "code", "execution_count": 43, "id": "faac566d-6378-4af4-84f4-6c853425a1c6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "x + 8" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.numerator(normalize=True)" ] }, { "cell_type": "markdown", "id": "c9056960-9c31-4ae5-bc35-ac4f5f815d04", "metadata": {}, "source": [ "After normalizing `p` we see that `p` equals `q`." ] }, { "cell_type": "markdown", "id": "1a0019e0-b4fc-4423-90c9-c648c4944fd1", "metadata": {}, "source": [ "For a numerical test on equality, we generate a random number and compare the evaluation of p with the evaluation of q at that same random number." ] }, { "cell_type": "code", "execution_count": 44, "id": "2c53fed7-1592-43d3-ae31-b449f154e4f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.506755820044636" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rx = RR.random_element()\n", "rx" ] }, { "cell_type": "code", "execution_count": 45, "id": "cd60bcee-3ca4-4d1e-bd60-4c25e8739e98", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.49324417995536" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yp = p(x = rx)\n", "yp" ] }, { "cell_type": "code", "execution_count": 46, "id": "5dc60efe-dcb9-4636-8aa0-7164a1c09270", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.49324417995536" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yq = q(x = rx)\n", "yq" ] }, { "cell_type": "code", "execution_count": 47, "id": "8e8ce064-7940-4331-8639-3b353391f15e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.000000000000000" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(yp - yq)" ] }, { "cell_type": "markdown", "id": "5f0ceeee-3767-4935-9fbb-307cc3cdcfc9", "metadata": {}, "source": [ "The difference in function values is less than the machine precision. So, numerically `p` and `q` are the same." ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 10.3", "language": "sage", "name": "sagemath" }, "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.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }