{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we illustrate symbols, names, and references, the topic of lecture 6."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. An Example of a Symbolic Computation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us declare four variables, using the letters a, b, c, and d."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"a,b,c,d = var('a,b,c,d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define $a + b \\sqrt{2}$ and assign this expression to ``x``."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{2} b + a$$"
],
"text/plain": [
"sqrt(2)*b + a"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = a + b*sqrt(2)\n",
"x.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To ``y`` we assign the expression $c + d \\sqrt{2}$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{2} d + c$$"
],
"text/plain": [
"sqrt(2)*d + c"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y = c + d*sqrt(2)\n",
"y.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we assign the product of ``x`` with ``y`` to ``z``."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{\\left(\\sqrt{2} b + a\\right)} {\\left(\\sqrt{2} d + c\\right)}$$"
],
"text/plain": [
"(sqrt(2)*b + a)*(sqrt(2)*d + c)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"z = x*y\n",
"z.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, ``z = x*y`` is not expanded, we have to force the expansion by the application of the ``expand()`` method on ``z``."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{2} b c + \\sqrt{2} a d + a c + 2 \\, b d$$"
],
"text/plain": [
"sqrt(2)*b*c + sqrt(2)*a*d + a*c + 2*b*d"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ez = z.expand()\n",
"ez.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that the ``ez`` is not a simpler expression than the ``z``. To collect the terms in $\\sqrt{2}$, we select the coefficient of $\\sqrt{2}$ in the expression ``ez``."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}b c + a d$$"
],
"text/plain": [
"b*c + a*d"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f = ez.coefficient(sqrt(2))\n",
"f.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``f`` gives us the factor in the term with $\\sqrt{2}$ in the expression."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{2} {\\left(b c + a d\\right)}$$"
],
"text/plain": [
"sqrt(2)*(b*c + a*d)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g = f*sqrt(2)\n",
"show(g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to get the other term in the expression, we subtract the *expanded* ``g`` from ``ez`` to get the rest."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}a c + 2 \\, b d$$"
],
"text/plain": [
"a*c + 2*b*d"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"h = g.expand()\n",
"rest = ez - h\n",
"rest.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now we can assemble the final result."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}a c + 2 \\, b d + \\sqrt{2} {\\left(b c + a d\\right)}$$"
],
"text/plain": [
"a*c + 2*b*d + sqrt(2)*(b*c + a*d)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"final = rest + g\n",
"show(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. the Symbolic Ring"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the expression $\\sqrt{x^2}$."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{x^{2}}$$"
],
"text/plain": [
"sqrt(x^2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = var('x')\n",
"e = sqrt(x^2)\n",
"show(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why does ``sqrt(x^2)`` not simplify to ``x``? Well, recall that over the complex numbers, ``sqrt()`` is a double valued function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can force the choice of one of the answers, typically the positive one."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e.canonicalize_radical()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can declare letters as symbols. Also numbers can be considered as symbols."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sqrt{4}$$"
],
"text/plain": [
"sqrt(4)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sqrt4 = sqrt(SR(4), hold=True)\n",
"show(sqrt4)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sqrt4.unhold()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sin(pi)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\sin\\left(\\pi\\right)$$"
],
"text/plain": [
"sin(pi)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sinpi = sin(pi, hold=True)\n",
"show(sinpi)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sinpi.unhold()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Names and References"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"reset()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"y"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x, y = var('x, y')\n",
"x = y\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that ``x`` evaluates to ``y``, because after ``x = y``, ``x`` refers to ``y``."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y equals 3\n",
"x equals y\n"
]
}
],
"source": [
"y = 3\n",
"print('y equals', y)\n",
"print('x equals', x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that ``x`` still refers to ``y``, after the assignment of ``3`` to ``y``."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can force the full evaluation with ``eval()`` which takes a string on argument. The ``str(x)`` returns the string representation of the variable ``x``."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval(str(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What if we had started with ``y = 3``? How do we prevent the evaluation of ``y`` when we assign ``y`` to ``x``?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To be clear, we still want the same dependencies between the variables as before:\n",
"\n",
"$$\n",
" x \\longrightarrow y \\longrightarrow 3\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us rebegin by resetting all variables."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"reset()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y equals 3\n"
]
}
],
"source": [
"x, y = var('x, y')\n",
"y = 3\n",
"print('y equals', y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The key is to assign to ``x`` the *name* of ``y``."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x equals y\n"
]
}
],
"source": [
"x = 'y'\n",
"print('x equals', x)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval(str(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By placing quotes around ``y`` in the assignment to ``x`` we prevent ``y`` from evaluating to 3."
]
}
],
"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": 4
}