{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy\n",
    "from sympy import Symbol, Function, cos, diff\n",
    "from sympy.codegen.ast import Assignment, Print\n",
    "from sympy.integrals.integrals import integrate, Integral\n",
    "from sympy.plotting import plot\n",
    "from sympy.plotting.pygletplot import PygletPlot as Plot\n",
    "from IPython.display import Markdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle f{\\left(x \\right)} = x^{2}$"
      ],
      "text/plain": [
       "Eq(f(x), x**2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{f'}{\\left(x \\right)} = 2 x$"
      ],
      "text/plain": [
       "Eq(f'(x), 2*x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{f''}{\\left(x \\right)} = 2$"
      ],
      "text/plain": [
       "Eq(f''(x), 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = Symbol('x')\n",
    "\n",
    "def f(x):\n",
    "    return x**2\n",
    "\n",
    "display(sympy.Eq(Function('f')(x), f(x)))\n",
    "display(sympy.Eq(Function('f\\'')(Symbol('x')), diff(f(x))))\n",
    "display(sympy.Eq(Function('f\\'\\'')(Symbol('x')), diff(diff(f(x)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\int \\left(x^{2} + x + 1\\right)\\, dx$"
      ],
      "text/plain": [
       "Integral(x**2 + x + 1, x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{x^{3}}{3} + \\frac{x^{2}}{2} + x$"
      ],
      "text/plain": [
       "x**3/3 + x**2/2 + x"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = Symbol('x')\n",
    "i = Integral(x**2 + x + 1, x)\n",
    "sympy.solvers.solve(i, x)\n",
    "display(i)\n",
    "display(i.doit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\int \\cos{\\left(x \\right)}\\, dx$"
      ],
      "text/plain": [
       "Integral(cos(x), x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sin{\\left(x \\right)}$"
      ],
      "text/plain": [
       "sin(x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "i = Integral(cos(x), x)\n",
    "display(i)\n",
    "display(i.doit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{-1, 1\\right\\}$"
      ],
      "text/plain": [
       "{-1, 1}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Symbol('x')\n",
    "y = Symbol('y')\n",
    "sympy.FiniteSet(*sympy.solvers.solve(sympy.Eq(x**2, 1), x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "![](plot.svg)"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 24080 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sympy import symbols\n",
    "from sympy.plotting import plot\n",
    "x = symbols('x')\n",
    "p1 = plot(x**2, show=False, label='$f(x)=x^2$', legend=True)\n",
    "p2 = plot(x, label='$g(x)=x$', show=False)\n",
    "p2.extend(plot(-x**2, label='$h(x)=-x$', show=False))\n",
    "p1.extend(p2)\n",
    "p1.save('plot.svg')\n",
    "Markdown('![](plot.svg)')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "96fc3968b0459c7bb0304f66d2259bbe1d74f60096901795baa92b56e1ba9010"
  },
  "kernelspec": {
   "display_name": "Python 3.10.2 ('AN2_Analysis_2-DUdzbZjr')",
   "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.10.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
