#!/usr/bin/env python # coding: utf-8 # # Symbolic mathematics with Sympy # ## Vince Knight (original repo - [here](https://github.com/drvinceknight/Python-Mathematics-Handbook)) # # Forked and solved for future reference # [Sympy](http://www.sympy.org/en/index.html) is described as a: # # > "... Python library for symbolic mathematics." # # This means it can be used to: # # - Manipulate symbolic expressions; # - Solve symbolic equations; # - Carry out symbolic Calculus; # - Plot symbolic function. # # It has other capabilities that we will not go in to in this handbook. But you can read more about it here: http://www.sympy.org/en/index.html # # ## Manipulating symbolic expressions # # Before we can start using the library to manipulate expressions, we need to import it. # In[1]: from sympy import * # The above imports the library and gives us access to it's commands using the shortand `sym` which is conventially used. # # If we wanted to get Python to check that $x - x = 0$ we would get an error if we did not tell Python what $x$ was: # x - x # This is where Sympy comes in, we can tell Python to create $x$ as a symbolic variable: # In[2]: x = symbols('x') # Now we can calculate $x - x$: # In[3]: x - x # We can create and manipulate expressions in Sympy. Let us for example verify: # # $$(a + b) ^ 2 = a ^ 2 + 2ab + b ^2$$ # # First, we create the symbolic variables $a, b$: # In[4]: a, b = symbols('a, b') # Now let's create our expression: # In[5]: expr = (a + b) ** 2 expr # **Note** we can get Sympy to use LaTeX so that the output looks nice in a notebook: # In[6]: init_printing() # In[7]: expr # Let us expand our expression: # In[8]: expr.expand() # Note that we can also get Sympy to produce the LaTeX code for future use: # In[9]: latex(expr.expand()) # --- # **EXERCISE** Use Sympy to verify the following expressions: # # - $(a - b) ^ 2 = a ^ 2 - 2 a b + b^2$ # - $a ^ 2 - b ^ 2 = (a - b) (a + b)$ (instead of using `expand`, try `factor`) # In[10]: expr = (a - b) **2 expr.expand() # In[11]: expr = a ** 2 - b **2 expr.factor() # ## Solving symbolic equations # We can use Sympy to solve symbolic expression. For example let's find the solution in $x$ of the quadratic equation: # # $$a x ^ 2 + b x + c = 0$$ # In[12]: # We only really need to define `c` but doing them all again. a, b, c, x = symbols('a, b, c, x') # The Sympy command for solving equations is `solveset`. The first argument is an expression for which the root will be found. The second argument is the value that we are solving for. # In[13]: solveset(a * x ** 2 + b * x + c, x) # --- # **EXERCISE** Use Sympy to find the solutions to the generic cubic equation: # # $$a x ^ 3 + b x ^ 2 + c x + d = 0$$ # # --- # In[14]: a, b, c, d, x = symbols('a, b, c, d, x') # In[15]: solveset(a*x**3+b*x**2+c*x+d,x) # It is possible to pass more arguments to `solveset` for example to constrain the solution space. Let us see what the solution of the following is in $\mathbb{R}$: # # $$x^2=-1$$ # In[16]: solveset(x ** 2 + 1, x, domain=S.Reals) # --- # **EXERCISE** Use Sympy to find the solutions to the following equations: # # - $x ^ 2 == 2$ in $\mathbb{N}$; # - $x ^ 3 + 2 x = 0$ in $\mathbb{R}$. # # --- # In[17]: x = symbols('x') # In[18]: solveset(x**2-2, domain= S.Naturals) # In[19]: solveset(x**3 + 2 , domain=S.Reals) # ## Symbolic calculus # # We can use Sympy to compute limits. Let us calculate: # # $$\lim_{x\to 0^+}\frac{1}{x}$$ # In[20]: limit(1/x, x, 0, dir="+") # --- # **EXERCISE** Compute the following limits: # # 1. $\lim_{x\to 0^-}\frac{1}{x}$ # 2. $\lim_{x\to 0}\frac{1}{x^2}$ # # --- # In[21]: limit(1/x,x,0,dir="-") # In[22]: limit(1/x**2,x,0,dir='+') # We can use also Sympy to differentiate and integrate. Let us experiment with differentiating the following expression: # # $$x ^ 2 - \cos(x)$$ # In[23]: diff(x ** 2 - cos(x), x) # Similarly we can integrate: # In[24]: integrate(x ** 2 - cos(x), x) # In[25]: integrate(exp(-x**2), x) # We can also carry out definite integrals: # In[26]: integrate(x ** 2 - cos(x), (x, 0, 5)) # --- # # **EXERCISE** Use Sympy to calculate the following: # # 1. $\frac{d\sin(x ^2)}{dx}$ # 2. $\frac{d(x ^2 + xy - \ln(y))}{dy}$ # 3. $\int e^x \cos(x)\;dx$ # 4. $\int_0^5 e^{2x}\;dx$ # In[27]: x, y = symbols('x,y') # In[28]: diff(sin(x**2),x) # In[29]: diff(x**2+x*y+ln(y),y) # In[30]: diff(x**2+x*y+ln(y),x) # In[31]: integrate(exp(x)*cos(x)) # In[32]: integrate(exp(2*x),(x,0,5)) # ## Plotting with Sympy # # Finally Sympy can be used to plot functions. Note that this makes use of another Python library called [matplotlib](http://matplotlib.org/). Whilst Sympy allows us to not directly need to make use of matplotlib it could be worth learning to use as it's a very powerful and versatile library. # # Before plotting in Jupyter we need to run a command to tell it to display the plots directly in the notebook: # In[33]: get_ipython().run_line_magic('matplotlib', 'inline') # Let us plot $x^2$: # In[34]: expr = x ** 2 p = plot(expr); # We can directly save that plot to a file if we wish to: # p.save("x_squared.pdf"); # --- # **EXERCISE** Plot the following functions: # # - $y=x + cos(x)$ # - $y=x ^ 2 - e^x$ (you might find `ylim` helpful as an argument) # # Experiment with saving your plots to a file. # # --- # In[35]: expr = cos(x) + x plot(expr); # In[36]: expr = x **2 - exp(x) plot(expr); # In[ ]: