Last active
January 21, 2020 07:31
-
-
Save anmolj7/dea53b7820a382a5c6d813893a8dd469 to your computer and use it in GitHub Desktop.
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": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Collecting sympy\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/ce/5b/acc12e3c0d0be685601fc2b2d20ed18dc0bf461380e763afc9d0a548deb0/sympy-1.5.1-py2.py3-none-any.whl (5.6MB)\n", | |
"\u001b[K 100% |████████████████████████████████| 5.6MB 311kB/s eta 0:00:01\n", | |
"\u001b[?25hCollecting mpmath>=0.19 (from sympy)\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/ca/63/3384ebb3b51af9610086b23ea976e6d27d6d97bf140a76a365bd77a3eb32/mpmath-1.1.0.tar.gz (512kB)\n", | |
"\u001b[K 100% |████████████████████████████████| 522kB 254kB/s ta 0:00:01\n", | |
"\u001b[?25hBuilding wheels for collected packages: mpmath\n", | |
" Running setup.py bdist_wheel for mpmath ... \u001b[?25ldone\n", | |
"\u001b[?25h Stored in directory: /home/emperoraj/.cache/pip/wheels/63/9d/8e/37c3f6506ed3f152733a699e92d8e0c9f5e5f01dea262f80ad\n", | |
"Successfully built mpmath\n", | |
"Installing collected packages: mpmath, sympy\n", | |
"Successfully installed mpmath-1.1.0 sympy-1.5.1\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip3 install sympy --user" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sympy\n", | |
"sympy.init_printing(use_latex=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Creating symbolic variables\n", | |
"x, y, u, v = sympy.symbols('x y u v') # At this point I wonder who's the guy\n", | |
"# who started the convention of using x and y in math xD Also, I am used to \n", | |
"# u and v because of blackpenredpen" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Let's solve this simple limit :P\n", | |
"$$\\lim_{x \\to 0^+}\\left(\\frac{\\sin{\\left(x \\right)}}{x}\\right)$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 1$" | |
], | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.limit(sympy.sin(x)/x, x, 0)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Starting with \"simple\" derrivatives " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"function = sympy.sin(180*x)**2*sympy.exp(2*x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle e^{2 x} \\sin^{2}{\\left(180 x \\right)}$" | |
], | |
"text/plain": [ | |
" 2⋅x 2 \n", | |
"ℯ ⋅sin (180⋅x)" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dv = sympy.diff(function, x, 1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 2 e^{2 x} \\sin^{2}{\\left(180 x \\right)} + 360 e^{2 x} \\sin{\\left(180 x \\right)} \\cos{\\left(180 x \\right)}$" | |
], | |
"text/plain": [ | |
" 2⋅x 2 2⋅x \n", | |
"2⋅ℯ ⋅sin (180⋅x) + 360⋅ℯ ⋅sin(180⋅x)⋅cos(180⋅x)" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dv #Yeah, simple actually :P" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"function = sympy.exp(x**x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle e^{x^{x}}$" | |
], | |
"text/plain": [ | |
" ⎛ x⎞\n", | |
" ⎝x ⎠\n", | |
"ℯ " | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle x^{x} \\left(\\log{\\left(x \\right)} + 1\\right) e^{x^{x}}$" | |
], | |
"text/plain": [ | |
" ⎛ x⎞\n", | |
" x ⎝x ⎠\n", | |
"x ⋅(log(x) + 1)⋅ℯ " | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.diff(function, x, 1) #Math issooooo beautifullllll" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Lets subsitute the value of x with 9" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 2 e^{2 x} \\sin^{2}{\\left(180 x \\right)} + 360 e^{2 x} \\sin{\\left(180 x \\right)} \\cos{\\left(180 x \\right)}$" | |
], | |
"text/plain": [ | |
" 2⋅x 2 2⋅x \n", | |
"2⋅ℯ ⋅sin (180⋅x) + 360⋅ℯ ⋅sin(180⋅x)⋅cos(180⋅x)" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 360 e^{18} \\sin{\\left(1620 \\right)} \\cos{\\left(1620 \\right)} + 2 e^{18} \\sin^{2}{\\left(1620 \\right)}$" | |
], | |
"text/plain": [ | |
" 18 18 2 \n", | |
"360⋅ℯ ⋅sin(1620)⋅cos(1620) + 2⋅ℯ ⋅sin (1620)" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dv.subs(x, 9)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle -9958200450.54737$" | |
], | |
"text/plain": [ | |
"-9958200450.54737" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Evaluting .. \n", | |
"dv.subs(x, 9).evalf() # I don't know if it's the right answer and I won't bother to check :P" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Now let's integrate the derrivative and see if we can get back to the origin function we defined" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle 2 e^{2 x} \\sin^{2}{\\left(180 x \\right)} + 360 e^{2 x} \\sin{\\left(180 x \\right)} \\cos{\\left(180 x \\right)}$" | |
], | |
"text/plain": [ | |
" 2⋅x 2 2⋅x \n", | |
"2⋅ℯ ⋅sin (180⋅x) + 360⋅ℯ ⋅sin(180⋅x)⋅cos(180⋅x)" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle e^{2 x} \\sin^{2}{\\left(180 x \\right)}$" | |
], | |
"text/plain": [ | |
" 2⋅x 2 \n", | |
"ℯ ⋅sin (180⋅x)" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.integrate(dv, x) #Works as expected :P" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Let's try what it answers when we integrate x^x which everybody knows is not possible :P" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"function = x**x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle x^{x}$" | |
], | |
"text/plain": [ | |
" x\n", | |
"x " | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\int x^{x}\\, dx$" | |
], | |
"text/plain": [ | |
"⌠ \n", | |
"⎮ x \n", | |
"⎮ x dx\n", | |
"⌡ " | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.integrate(function, x)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Uh huh, sympy be like: <img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSei1-zpMPPO7Dmmr3XBC_afud4wJL2Cb95qGP19NNhkb7iDA5k\">" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\frac{d}{d x} y{\\left(x \\right)} = \\operatorname{acos}{\\left(x \\right)}$" | |
], | |
"text/plain": [ | |
"d \n", | |
"──(y(x)) = acos(x)\n", | |
"dx " | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# A simple ODE\n", | |
"x, l = sympy.symbols('x lambda')\n", | |
"\n", | |
"y = sympy.Function('y')(x)\n", | |
"\n", | |
"dydx = y.diff(x)\n", | |
"expression = sympy.Eq(dydx, sympy.acos(x))\n", | |
"expression" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle y{\\left(x \\right)} = C_{1} + x \\operatorname{acos}{\\left(x \\right)} - \\sqrt{1 - x^{2}}$" | |
], | |
"text/plain": [ | |
" ________\n", | |
" ╱ 2 \n", | |
"y(x) = C₁ + x⋅acos(x) - ╲╱ 1 - x " | |
] | |
}, | |
"execution_count": 78, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.dsolve(expression)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\frac{\\partial}{\\partial t} u{\\left(x,t \\right)} = \\frac{\\partial}{\\partial x} u{\\left(x,t \\right)}$" | |
], | |
"text/plain": [ | |
"∂ ∂ \n", | |
"──(u(x, t)) = ──(u(x, t))\n", | |
"∂t ∂x " | |
] | |
}, | |
"execution_count": 82, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x,t = sympy.symbols('x t')\n", | |
"u = sympy.Function('u')\n", | |
"pde = sympy.Eq(sympy.diff(u(x,t),t), sympy.diff(u(x,t),x))\n", | |
"pde" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle u{\\left(x,t \\right)} = F{\\left(t + x \\right)}$" | |
], | |
"text/plain": [ | |
"u(x, t) = F(t + x)" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sympy.pdsolve(pde)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Itegration using scipy module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from scipy import integrate\n", | |
"from numpy import exp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f = lambda x: exp(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\left( 145.69487727411754, \\ 1.6175380731966177e-12\\right)$" | |
], | |
"text/plain": [ | |
"(145.69487727411754, 1.6175380731966177e-12)" | |
] | |
}, | |
"execution_count": 91, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"integrate.quad(f, 1, 5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### The difference between integration with scipy and with sympy is the symbol computation i.e, sympy just ouputs the evaluation, which is not what we require in some cases. Suppose I made a smart glass with OCR which could solve questions, I'd rather want sympy instead of scipy to cheat in an exam. If I need to compute something, I can just use the evalf function in sympy, it would just make my life easier XD" | |
] | |
} | |
], | |
"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.7.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment