{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In lecture 14 of mcs 320, we cover substitution, expansion, and factorization." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Substitution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we want to rewrite\n", "\n", "$$\n", " (x+y)^2 + \\frac{1}{(x+y)^2}\n", "$$\n", "\n", "into\n", "\n", "$$\n", " \\frac{(x+y)^4 + 1}{(x+y)^2}.\n", "$$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The problem is that we do not want to expand the numerator." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{\\left(x + y\\right)}^{2} + \\frac{1}{{\\left(x + y\\right)}^{2}}$$" ], "text/plain": [ "(x + y)^2 + 1/(x + y)^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x, y = var('x, y')\n", "p = (x+y)^2 + 1/(x+y)^2\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The method ``factor()`` puts the expression ``p`` on the common denominator:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + y^4 + 1)/(x + y)^2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.factor()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But we see that the numerator is expanded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To prevent ``(x+y)^4`` from expanding in the numerator, we substitute ``x+y`` by ``z``." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "z^2 + 1/z^2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = var('z')\n", "q = p.subs({x+y: z})\n", "q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Observe the syntax of the ``subs()`` method. The argument is the dictionary ``{x+y: z}`` has as key the expression we want to replace by ``z``, the corresponding value of the key." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(z^4 + 1)/z^2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = q.factor()\n", "r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``z`` shielded the expansion of ``(x+y)^4``." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((x + y)^4 + 1)/(x + y)^2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = r(z=x+y)\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The substition of a variable in an expression can be executed faster by *symbolic evaluation*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Sequential and Simultaneous Substitution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In sequential substitution, we replace variables one after the other. In a simultaneous substitution, all variables are replaced all at once." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a + 2*b + 3*c" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reset()\n", "a, b, c = var('a, b, c')\n", "p = a + 2*b + 3*c\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we want to replace ``a`` by ``b``, ``b`` by ``c``, and ``c`` by ``a``." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3*a + b + 2*c" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.subs({a: b, b: c, c: a})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3*a + b + 2*c" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p(a=b,b=c,c=a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above substitutions are simultaneous." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The substitutions below are sequential." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6*a" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.subs({a: b}).subs({b: c}).subs({c: a})" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6*a" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p(a=b)(b=c)(c=a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Collecting Terms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider writing\n", "\n", "$$\n", " (a + b + c) \\star (x^2 + x + 1)\n", "$$\n", "\n", "into\n", "\n", "$$\n", " (a + b + c) x^2 + (a + b + c) x + (a + b + c).\n", "$$" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x^2 + x + 1)*(a + b + c)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (a+b+c)*(x^2 + x + 1)\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Applying ``expand()`` on ``p`` leads to expression swell. To avoid the expression swell we can substitution ``(a+b+c)`` by ``z`` as we did before. This is a good exercise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An alternative is to work with a polynomial in ``x`` and coefficients in ``a``, ``b``, and ``c``." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(a + b + c)*x^2 + (a + b + c)*x + a + b + c" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ZZ[a,b,c][x](p)" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 4 }