{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Factoring Polynomials with SymPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "from ipywidgets import interact\n", "from IPython.display import display" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "from sympy import Symbol, Eq, factor, init_printing\n", "init_printing(use_latex='mathjax')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "x = Symbol('x')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "def factorit(n):\n", " display(Eq(x**n-1, factor(x**n-1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how the output of the `factorit` function is properly formatted LaTeX." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/latex": [ "$$x^{12} - 1 = \\left(x - 1\\right) \\left(x + 1\\right) \\left(x^{2} + 1\\right) \\left(x^{2} - x + 1\\right) \\left(x^{2} + x + 1\\right) \\left(x^{4} - x^{2} + 1\\right)$$" ], "text/plain": [ " 12 ⎛ 2 ⎞ ⎛ 2 ⎞ ⎛ 2 ⎞ ⎛ 4 2 ⎞\n", "x - 1 = (x - 1)⋅(x + 1)⋅⎝x + 1⎠⋅⎝x - x + 1⎠⋅⎝x + x + 1⎠⋅⎝x - x + 1⎠" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "factorit(12)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4d968e2b95894fcdbd857fb26f9dd940", "version_major": 2, "version_minor": 0 }, "text/html": [ "<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n", "<p>\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n", " Widgets Documentation</a> for setup instructions.\n", "</p>\n", "<p>\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n", " it may mean that your frontend doesn't currently support widgets.\n", "</p>\n" ], "text/plain": [ "interactive(children=(IntSlider(value=21, description='n', max=40, min=2), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "interact(factorit, n=(2,40));" ] } ], "metadata": { "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.6.8" } }, "nbformat": 4, "nbformat_minor": 4 }