{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SymPy Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example is related to the article [*Computer Algebra with Jupyter and SymPy*](https://jaantollander.com/2019-11-16-computer-algebra.html). It contains a showcase of the essential features of SymPy. The Sympy webpage contains the full list of [features](https://www.sympy.org/en/features.html) and [documentation](https://docs.sympy.org/latest/index.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports and Printing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import SymPy and initialize the printing of SymPy expressions with MathJax." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "# init_printing(\"mathjax\") # sympy expression printing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import SymEngine and initialize the printing of SymEngine expressions with MathJax. SymEngine is a fast symbolic manipulation library written in C++ instead of Python. SymEngine has Python wrappers that integrate with Sympy, although Symengine does not yet support all of SymPy's functionality. We can override some of the SymPy functions with SymEngine variants for improved performance. It is wise to import only computationally critical functions." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#from symengine import * # overrides some sympy functions\n", "#init_printing(\"mathjax\") # symengine expression printing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define Symbols and Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`symbols`, `Symbol`, `Function`, `sympify`, `S`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "x = symbols(\"x\")\n", "a,b,c = symbols(\"a,b,c\", real=True)\n", "i,j,n = symbols(\"i,j,n\", integer=True)\n", "f = symbols(\"f\", cls=Function)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 1)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sympify(1), S(1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle i$" ], "text/plain": [ "I" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "I # imaginary unit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Rational`, `Sum`, `Product`, `I`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{2}$" ], "text/plain": [ "1/2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rational(1, 2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\sum_{i=1}^{n} x^{i}$" ], "text/plain": [ "Sum(x**i, (i, 1, n))" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sum(x**i, (i, 1, n))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\prod_{i=1}^{n} \\left(i + x\\right)$" ], "text/plain": [ "Product(i + x, (i, 1, n))" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Product(x+i, (i, 1, n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluate Expressions Numerically" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`N`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle e^{5}$" ], "text/plain": [ "exp(5)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp(5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 148.41$" ], "text/plain": [ "148.41" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(_, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manipulate Expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`expand`, `collect`, `factor`, `simplify`, `.subs`, `.doit`" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(- a + x\\right) \\left(- b + x\\right)$" ], "text/plain": [ "(-a + x)*(-b + x)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(x-a)*(x-b)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a b - a x - b x + x^{2}$" ], "text/plain": [ "a*b - a*x - b*x + x**2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expand(_)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a \\left(b - x\\right) - b x + x^{2}$" ], "text/plain": [ "a*(b - x) - b*x + x**2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(_, a)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(- a + x\\right) \\left(- b + x\\right)$" ], "text/plain": [ "(-a + x)*(-b + x)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factor(_)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(- a + c\\right) \\left(- b + c\\right)$" ], "text/plain": [ "(-a + c)*(-b + c)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "_.subs(x, c)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(a - c\\right) \\left(b - c\\right)$" ], "text/plain": [ "(a - c)*(b - c)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(_)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\sum_{i=1}^{n} x^{i}$" ], "text/plain": [ "Sum(x**i, (i, 1, n))" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sum(x**i, (i, 1, n))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\begin{cases} n & \\text{for}\\: x = 1 \\\\\\frac{x - x^{n + 1}}{1 - x} & \\text{otherwise} \\end{cases}$" ], "text/plain": [ "Piecewise((n, Eq(x, 1)), ((x - x**(n + 1))/(1 - x), True))" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "_.doit()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Eq`" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a x^{2} + b x = c$" ], "text/plain": [ "Eq(a*x**2 + b*x, c)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Eq(a*x**2+b*x, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`solve`" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a x^{2} + b x + c$" ], "text/plain": [ "a*x**2 + b*x + c" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*x**2 + b*x + c " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve(_, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Systems of Equations" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "z = symbols(\"z_{0:2}\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{z_{0}: -a/2 - sqrt(a**2 + 4*b)/2, z_{1}: -a/2 + sqrt(a**2 + 4*b)/2},\n", " {z_{0}: -a/2 + sqrt(a**2 + 4*b)/2, z_{1}: -a/2 - sqrt(a**2 + 4*b)/2}]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve([z[0] + z[1] + a, z[0] * z[1] + b], z, dict=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Recurrence Relations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`rsolve`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{aligned}\n", "y_0&=1, \\\\\n", "y_1&=4, \\\\\n", "y_n &= 2y_{n-1} + 5y_{n-2}\n", "\\end{aligned}\n", "$$" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(\\frac{1}{2} - \\frac{\\sqrt{6}}{4}\\right) \\left(1 - \\sqrt{6}\\right)^{n} + \\left(\\frac{1}{2} + \\frac{\\sqrt{6}}{4}\\right) \\left(1 + \\sqrt{6}\\right)^{n}$" ], "text/plain": [ "(1/2 - sqrt(6)/4)*(1 - sqrt(6))**n + (1/2 + sqrt(6)/4)*(1 + sqrt(6))**n" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = Function('y')\n", "f = y(n) - 2*y(n-1) - 5*y(n-2)\n", "rsolve(f, y(n), [1, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Differential Calculus" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`limit`, `series`, `diff`, `integrate`, `Derivative`, `Integral`" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{x}$" ], "text/plain": [ "1/x" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(log(x))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{\\log{\\left(x^{2} + x + 1 \\right)}}{2} + \\frac{\\sqrt{3} \\operatorname{atan}{\\left(\\frac{2 \\sqrt{3} x}{3} + \\frac{\\sqrt{3}}{3} \\right)}}{3}$" ], "text/plain": [ "log(x**2 + x + 1)/2 + sqrt(3)*atan(2*sqrt(3)*x/3 + sqrt(3)/3)/3" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate((x**2-1)/(x**3-1))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{\\sqrt{3} \\pi}{18} + \\frac{\\log{\\left(3 \\right)}}{2}$" ], "text/plain": [ "sqrt(3)*pi/18 + log(3)/2" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate((x**2-1)/(x**3-1), (x, 0, 1))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x - \\frac{x^{3}}{6} + \\frac{x^{5}}{120} + O\\left(x^{6}\\right)$" ], "text/plain": [ "x - x**3/6 + x**5/120 + O(x**6)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "series(sin(x), x)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1$" ], "text/plain": [ "1" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "limit(sin(x)/x, x, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Differential Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`dsolve`" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "f = Function(\"f\")" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a \\frac{d}{d x} f{\\left(x \\right)} + b f{\\left(x \\right)} + c + \\frac{d^{2}}{d x^{2}} f{\\left(x \\right)}$" ], "text/plain": [ "a*Derivative(f(x), x) + b*f(x) + c + Derivative(f(x), (x, 2))" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(f(x), x, 2) + a*diff(f(x), x) + b*f(x) + c" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle f{\\left(x \\right)} = C_{1} e^{\\frac{x \\left(- a - \\sqrt{a^{2} - 4 b}\\right)}{2}} + C_{2} e^{\\frac{x \\left(- a + \\sqrt{a^{2} - 4 b}\\right)}{2}} - \\frac{c}{b}$" ], "text/plain": [ "Eq(f(x), C1*exp(x*(-a - sqrt(a**2 - 4*b))/2) + C2*exp(x*(-a + sqrt(a**2 - 4*b))/2) - c/b)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dsolve(_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }