Created
February 28, 2020 17:01
-
-
Save DBremen/4e1823ebab02a2aad081739b902134bf to your computer and use it in GitHub Desktop.
Solving polynomials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import sympy as sym\nfrom IPython.display import display, Math", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def disp(expr):\n display(Math(sym.latex(expr)))", | |
"execution_count": 22, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Adding polynomials" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x = sym.symbols('x')\n\np1 = 2*x**3 + x**2 - x\np2 = x**3 - x**4 - 4*x**2\ndisplay(Math('(%s) + (%s) = %s' %(sym.latex(p1), sym.latex(p2), sym.latex(p1+p2))))", | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle (2 x^{3} + x^{2} - x) + (- x^{4} + x^{3} - 4 x^{2}) = - x^{4} + 3 x^{3} - 3 x^{2} - x$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "p1 = sym.Poly(2*x**3 + x**2 - x)\ndisplay(p1)", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "Poly(2*x**3 + x**2 - x, x, domain='ZZ')", | |
"text/latex": "$\\displaystyle \\operatorname{Poly}{\\left( 2 x^{3} + x^{2} - x, x, domain=\\mathbb{Z} \\right)}$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "p1.eval(2)", | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 25, | |
"data": { | |
"text/plain": "18", | |
"text/latex": "$\\displaystyle 18$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "p1.coeffs()", | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 26, | |
"data": { | |
"text/plain": "[2, 1, -1]" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Multiplying polynomials" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "p1 = 4*x**2 - 2*x\np2 = x**3 - 1\ndisp(sym.expand(p1*p2))\ndisp(p1*p2)", | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle 4 x^{5} - 2 x^{4} - 4 x^{2} + 2 x$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\left(4 x^{2} - 2 x\\right) \\left(x^{3} - 1\\right)$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Divide polynomials" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "p1 = 4*x**5 - x\np2 = 2*x**3 - x\ndisplay(Math('\\\\frac{%s}{%s} = %s' %(sym.latex(p1), sym.latex(p2), sym.latex(sym.simplify(p1/p2)))))", | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\frac{4 x^{5} - x}{2 x^{3} - x} = 2 x^{2} + 1$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x,y = sym.symbols('x y')\nnums = range(5,16)\np1 = x**6 + (2*x**4) + (6*x) -y\np2 = x**3 + 3\nfor num in nums:\n solution = sym.simplify((p1/p2).subs(y,num))\n hasDenominator = sym.fraction(solution)[1] == 1\n display(Math('\\\\text{Input: }%g\\\\text{, Solution:}%s\\\\text{ , has denominator: %s}' %(num,sym.latex(solution),hasDenominator)))", | |
"execution_count": 64, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }5\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 5}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }6\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 6}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }7\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 7}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }8\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 8}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }9\\text{, Solution:}x^{3} + 2 x - 3\\text{ , has denominator: True}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }10\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 10}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }11\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 11}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }12\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 12}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }13\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 13}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }14\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 14}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Math object>", | |
"text/latex": "$\\displaystyle \\text{Input: }15\\text{, Solution:}\\frac{x^{6} + 2 x^{4} + 6 x - 15}{x^{3} + 3}\\text{ , has denominator: False}$" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.7.3", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Solving polynomials", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment