{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Lecture 2 of Summer 2023 MCS 320 Introduction to Symbolic Computation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Computing with Symbols" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SageMath is a computer algebra system, capable of working with symbols.\n", "For example, the transcendental constant $\\pi$ is known to SageMath as ``pi``.\n", "Applying the ``sin()`` function to $\\pi$ shows the true value of $\\sin(\\pi)$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin(pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The language of SageMath is based on Python. Python is a dynamically typed language. The type of each object determines the opererations that can be performed on the object.\n", "We query the type of an object with ``type()``." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that SageMath is case sensitive ``pi`` $\\not=$ ``Pi``." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numerical Approximations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can compute a numerical approximation for $\\pi$, with the application of the method ``n()`` on the object ``pi``, which is of type symbolic expression.\n", "In the argument of ``n()`` we can specify the number of decimal places with ``digits=``." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592654\n", "\n" ] } ], "source": [ "Pi10 = pi.n(digits=10)\n", "print(Pi10)\n", "print(type(Pi10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number we obtained is not a Python float, but of type ``RealNumber`` of the multiprecision software GNU MPFR library. Let us see by how much ``Pi10`` differs from $\\pi$." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pi - 3.141592654\n", "\n" ] } ], "source": [ "delta = pi - Pi10\n", "print(delta)\n", "print(type(delta))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The answer above is exact and correct, but it is not so helpful,\n", "we were hoping to see a number to verify that our ``Pi10`` approximates $\\pi$ really with 10 decimal places accuracy. Let us compute a numerical approximation of ``delta``." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0000000000\n", "3.6379788071e-12\n" ] } ], "source": [ "print(delta.n(digits=10))\n", "print(delta.n(digits=11))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the 10-digit accurate rational approximation for pi: 833719/265381\n", "\n" ] } ], "source": [ "Pi18q = QQ(Pi10)\n", "print('the 10-digit accurate rational approximation for pi:', Pi18q)\n", "print(type(Pi18q))" ] } ], "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 }