Created
August 24, 2022 13:12
-
-
Save ritog/09d4bd1c4bf8014c4de48e25ce0f0f89 to your computer and use it in GitHub Desktop.
polynomial factoring with SymPy (a python package)
This file contains 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": [ | |
{ | |
"cell_type": "markdown", | |
"id": "e3aebf06", | |
"metadata": {}, | |
"source": [ | |
"# Factorizing Polynomial with Sympy\n", | |
"*~ example by [Rito Ghosh](https://www.linkedin.com/in/ritobrata-ghosh/)*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "48541d8a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# importing required functions from package\n", | |
"from sympy import symbols, factor" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "c67c56a2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# declaring symbols\n", | |
"a, b, c = symbols('a b c')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "2318e26f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'(a^2*c^9+14*a^2*c^8-2*a^2*c^7-528 *a^2*c^6-639*a ^2*c^5+3706*a^2*c^4+9608*a^2*c^3+8144*a^2*c^2+2640*a^2*c+288*a^2)/(2*(c^2+4*c+3)^2*(2*c^3+12*c^2-21*c-10)^2)'" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# the expression of polynomial to fatorize\n", | |
"expr = '(a^2*c^9+14*a^2*c^8-2*a^2*c^7-528 *a^2*c^6-639*a ^2*c^5+3706*a^2*c^4+9608*a^2*c^3+8144*a^2*c^2+2640*a^2*c+288*a^2)/(2*(c^2+4*c+3)^2*(2*c^3+12*c^2-21*c-10)^2)'\n", | |
"expr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "5df5f38f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\frac{a^{2} \\left(c + 2\\right) \\left(c^{4} + 6 c^{3} - 31 c^{2} - 52 c - 12\\right)^{2}}{2 \\left(c + 1\\right)^{2} \\left(c + 3\\right)^{2} \\left(2 c^{3} + 12 c^{2} - 21 c - 10\\right)^{2}}$" | |
], | |
"text/plain": [ | |
"a**2*(c + 2)*(c**4 + 6*c**3 - 31*c**2 - 52*c - 12)**2/(2*(c + 1)**2*(c + 3)**2*(2*c**3 + 12*c**2 - 21*c - 10)**2)" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# calling the `factor` function\n", | |
"factor(expr)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "85aea0ce", | |
"metadata": {}, | |
"source": [ | |
"### Requirements (including Jupyter)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "7eb60d9e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"name: sptest\n", | |
"channels:\n", | |
" - defaults\n", | |
"dependencies:\n", | |
" - _libgcc_mutex=0.1=main\n", | |
" - _openmp_mutex=5.1=1_gnu\n", | |
" - anyio=3.5.0=py310h06a4308_0\n", | |
" - argon2-cffi=21.3.0=pyhd3eb1b0_0\n", | |
" - argon2-cffi-bindings=21.2.0=py310h7f8727e_0\n", | |
" - asttokens=2.0.5=pyhd3eb1b0_0\n", | |
" - attrs=21.4.0=pyhd3eb1b0_0\n", | |
" - babel=2.9.1=pyhd3eb1b0_0\n", | |
" - backcall=0.2.0=pyhd3eb1b0_0\n", | |
" - beautifulsoup4=4.11.1=py310h06a4308_0\n", | |
" - bleach=4.1.0=pyhd3eb1b0_0\n", | |
" - brotlipy=0.7.0=py310h7f8727e_1002\n", | |
" - bzip2=1.0.8=h7b6447c_0\n", | |
" - ca-certificates=2022.07.19=h06a4308_0\n", | |
" - certifi=2022.6.15=py310h06a4308_0\n", | |
" - cffi=1.15.1=py310h74dc2b5_0\n", | |
" - charset-normalizer=2.0.4=pyhd3eb1b0_0\n", | |
" - cryptography=37.0.1=py310h9ce1e76_0\n", | |
" - dbus=1.13.18=hb2f20db_0\n", | |
" - debugpy=1.5.1=py310h295c915_0\n", | |
" - decorator=5.1.1=pyhd3eb1b0_0\n", | |
" - defusedxml=0.7.1=pyhd3eb1b0_0\n", | |
" - entrypoints=0.4=py310h06a4308_0\n", | |
" - executing=0.8.3=pyhd3eb1b0_0\n", | |
" - expat=2.4.4=h295c915_0\n", | |
" - fontconfig=2.13.1=h6c09931_0\n", | |
" - freetype=2.11.0=h70c0345_0\n", | |
" - giflib=5.2.1=h7b6447c_0\n", | |
" - glib=2.69.1=h4ff587b_1\n", | |
" - gmp=6.2.1=h295c915_3\n", | |
" - gmpy2=2.1.2=py310heeb90bb_0\n", | |
" - gst-plugins-base=1.14.0=h8213a91_2\n", | |
" - gstreamer=1.14.0=h28cd5cc_2\n", | |
" - icu=58.2=he6710b0_3\n", | |
" - idna=3.3=pyhd3eb1b0_0\n", | |
" - ipykernel=6.9.1=py310h06a4308_0\n", | |
" - ipython=8.4.0=py310h06a4308_0\n", | |
" - ipython_genutils=0.2.0=pyhd3eb1b0_1\n", | |
" - ipywidgets=7.6.5=pyhd3eb1b0_1\n", | |
" - jedi=0.18.1=py310h06a4308_1\n", | |
" - jinja2=3.0.3=pyhd3eb1b0_0\n", | |
" - jpeg=9e=h7f8727e_0\n", | |
" - json5=0.9.6=pyhd3eb1b0_0\n", | |
" - jsonschema=4.4.0=py310h06a4308_0\n", | |
" - jupyter=1.0.0=py310h06a4308_8\n", | |
" - jupyter_client=7.2.2=py310h06a4308_0\n", | |
" - jupyter_console=6.4.3=pyhd3eb1b0_0\n", | |
" - jupyter_core=4.10.0=py310h06a4308_0\n", | |
" - jupyter_server=1.18.1=py310h06a4308_0\n", | |
" - jupyterlab=3.4.4=py310h06a4308_0\n", | |
" - jupyterlab_pygments=0.1.2=py_0\n", | |
" - jupyterlab_server=2.12.0=py310h06a4308_0\n", | |
" - jupyterlab_widgets=1.0.0=pyhd3eb1b0_1\n", | |
" - krb5=1.19.2=hac12032_0\n", | |
" - ld_impl_linux-64=2.38=h1181459_1\n", | |
" - lerc=3.0=h295c915_0\n", | |
" - libclang=10.0.1=default_hb85057a_2\n", | |
" - libdeflate=1.8=h7f8727e_5\n", | |
" - libedit=3.1.20210910=h7f8727e_0\n", | |
" - libevent=2.1.12=h8f2d780_0\n", | |
" - libffi=3.3=he6710b0_2\n", | |
" - libgcc-ng=11.2.0=h1234567_1\n", | |
" - libgomp=11.2.0=h1234567_1\n", | |
" - libllvm10=10.0.1=hbcb73fb_5\n", | |
" - libpng=1.6.37=hbc83047_0\n", | |
" - libpq=12.9=h16c4e8d_3\n", | |
" - libsodium=1.0.18=h7b6447c_0\n", | |
" - libstdcxx-ng=11.2.0=h1234567_1\n", | |
" - libtiff=4.4.0=hecacb30_0\n", | |
" - libuuid=1.0.3=h7f8727e_2\n", | |
" - libwebp=1.2.2=h55f646e_0\n", | |
" - libwebp-base=1.2.2=h7f8727e_0\n", | |
" - libxcb=1.15=h7f8727e_0\n", | |
" - libxkbcommon=1.0.1=hfa300c1_0\n", | |
" - libxml2=2.9.14=h74e7548_0\n", | |
" - libxslt=1.1.35=h4e12654_0\n", | |
" - lz4-c=1.9.3=h295c915_1\n", | |
" - markupsafe=2.1.1=py310h7f8727e_0\n", | |
" - matplotlib-inline=0.1.2=pyhd3eb1b0_2\n", | |
" - mistune=0.8.4=py310h7f8727e_1000\n", | |
" - mpc=1.1.0=h10f8cd9_1\n", | |
" - mpfr=4.0.2=hb69a4c5_1\n", | |
" - nbclassic=0.3.5=pyhd3eb1b0_0\n", | |
" - nbclient=0.5.13=py310h06a4308_0\n", | |
" - nbconvert=6.4.4=py310h06a4308_0\n", | |
" - nbformat=5.3.0=py310h06a4308_0\n", | |
" - ncurses=6.3=h5eee18b_3\n", | |
" - nest-asyncio=1.5.5=py310h06a4308_0\n", | |
" - notebook=6.4.12=py310h06a4308_0\n", | |
" - nspr=4.33=h295c915_0\n", | |
" - nss=3.74=h0370c37_0\n", | |
" - openssl=1.1.1q=h7f8727e_0\n", | |
" - packaging=21.3=pyhd3eb1b0_0\n", | |
" - pandocfilters=1.5.0=pyhd3eb1b0_0\n", | |
" - parso=0.8.3=pyhd3eb1b0_0\n", | |
" - pcre=8.45=h295c915_0\n", | |
" - pexpect=4.8.0=pyhd3eb1b0_3\n", | |
" - pickleshare=0.7.5=pyhd3eb1b0_1003\n", | |
" - pip=22.1.2=py310h06a4308_0\n", | |
" - ply=3.11=py310h06a4308_0\n", | |
" - prometheus_client=0.14.1=py310h06a4308_0\n", | |
" - prompt-toolkit=3.0.20=pyhd3eb1b0_0\n", | |
" - prompt_toolkit=3.0.20=hd3eb1b0_0\n", | |
" - ptyprocess=0.7.0=pyhd3eb1b0_2\n", | |
" - pure_eval=0.2.2=pyhd3eb1b0_0\n", | |
" - pycparser=2.21=pyhd3eb1b0_0\n", | |
" - pygments=2.11.2=pyhd3eb1b0_0\n", | |
" - pyopenssl=22.0.0=pyhd3eb1b0_0\n", | |
" - pyparsing=3.0.4=pyhd3eb1b0_0\n", | |
" - pyqt=5.15.7=py310h6a678d5_1\n", | |
" - pyrsistent=0.18.0=py310h7f8727e_0\n", | |
" - pysocks=1.7.1=py310h06a4308_0\n", | |
" - python=3.10.4=h12debd9_0\n", | |
" - python-dateutil=2.8.2=pyhd3eb1b0_0\n", | |
" - python-fastjsonschema=2.15.1=pyhd3eb1b0_0\n", | |
" - pytz=2022.1=py310h06a4308_0\n", | |
" - pyzmq=23.2.0=py310h6a678d5_0\n", | |
" - qt-main=5.15.2=h327a75a_7\n", | |
" - qt-webengine=5.15.9=hd2b0992_4\n", | |
" - qtconsole=5.3.1=py310h06a4308_1\n", | |
" - qtpy=2.0.1=pyhd3eb1b0_0\n", | |
" - qtwebkit=5.212=h4eab89a_4\n", | |
" - readline=8.1.2=h7f8727e_1\n", | |
" - requests=2.28.1=py310h06a4308_0\n", | |
" - send2trash=1.8.0=pyhd3eb1b0_1\n", | |
" - setuptools=63.4.1=py310h06a4308_0\n", | |
" - sip=6.6.2=py310h6a678d5_0\n", | |
" - six=1.16.0=pyhd3eb1b0_1\n", | |
" - sniffio=1.2.0=py310h06a4308_1\n", | |
" - soupsieve=2.3.1=pyhd3eb1b0_0\n", | |
" - sqlite=3.39.2=h5082296_0\n", | |
" - stack_data=0.2.0=pyhd3eb1b0_0\n", | |
" - sympy=1.10.1=py310h06a4308_0\n", | |
" - terminado=0.13.1=py310h06a4308_0\n", | |
" - testpath=0.6.0=py310h06a4308_0\n", | |
" - tk=8.6.12=h1ccaba5_0\n", | |
" - toml=0.10.2=pyhd3eb1b0_0\n", | |
" - tornado=6.1=py310h7f8727e_0\n", | |
" - traitlets=5.1.1=pyhd3eb1b0_0\n", | |
" - typing-extensions=4.3.0=py310h06a4308_0\n", | |
" - typing_extensions=4.3.0=py310h06a4308_0\n", | |
" - tzdata=2022a=hda174b7_0\n", | |
" - urllib3=1.26.11=py310h06a4308_0\n", | |
" - wcwidth=0.2.5=pyhd3eb1b0_0\n", | |
" - webencodings=0.5.1=py310h06a4308_1\n", | |
" - websocket-client=0.58.0=py310h06a4308_4\n", | |
" - wheel=0.37.1=pyhd3eb1b0_0\n", | |
" - widgetsnbextension=3.5.2=py310h06a4308_0\n", | |
" - xz=5.2.5=h7f8727e_1\n", | |
" - zeromq=4.3.4=h2531618_0\n", | |
" - zlib=1.2.12=h7f8727e_2\n", | |
" - zstd=1.5.2=ha4553b6_0\n", | |
" - pip:\n", | |
" - mpmath==1.2.1\n", | |
" - pyqt5-sip==12.11.0\n" | |
] | |
} | |
], | |
"source": [ | |
"%%bash\n", | |
"\n", | |
"conda env export -n sptest > environment.yml\n", | |
"head -n -1 environment.yml" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment