In [4]:
from sympy import *
init_printing()
x, y, z = symbols('x,y,z')

Solve

Equation solving is both a common need also a common building block for more complicated symbolic algorithms.

Here we introduce the solve function


In [7]:
solve(x**2 - 4, x)


Out[7]:
$$\begin{bmatrix}-2, & 2\end{bmatrix}$$

Solve takes two arguments, an equation like $x^2 - 4$ and a variable on which we want to solve, like $x$.

Solve returns the values of the variable, $x$, for which the equation, $x^2 - 4$ equals 0.

Exercise

What would the following code produce? Are you sure?


In [ ]:
solve(x**2 - 9 == 0, x)

Symbolic use of solve

Results of solve don't need to be numeric, like [-2, 2]. We can use solve to perform algebraic manipulations. For example if we know a simple equation for the area of a square

area = height * width

we can solve this equation for any of the variables. For example how would we solve this system for the height, given the area and width?


In [10]:
height, width, area = symbols('height,width,area')
solve(area - height*width, height)


Out[10]:
$$\begin{bmatrix}\frac{area}{width}\end{bmatrix}$$

Note that we would have liked to have written

solve(area == height * width, height)

But the == gotcha bites us. Instead we remember that solve expects an expression that is equal to zero, so we rewrite the equation

area = height * width

into the equation

0 = height * width - area

and that is what we give to solve.

Exercise

Compute the radius of a sphere, given the volume. Reminder, the volume of a sphere of radius r is given by

$$ V = \frac{4}{3}\pi r^3 $$

In [11]:
# Solve for the radius of a sphere, given the volume

You will probably get several solutions, this is fine. The first one is probably the one that you want.

Substitution

We often want to substitute in one expression for another. For this we use the subs method


In [14]:
x**2


Out[14]:
$$x^{2}$$

In [13]:
# Replace x with y
(x**2).subs({x: y})


Out[13]:
$$y^{2}$$

Exercise

Subsitute $x$ for $sin(x)$ in the equation $x^2 + 2\cdot x + 1$


In [21]:
# Replace x with sin(x)

Subs + Solve

We can use subs and solve together to plug the solution of one equation into another


In [25]:
# Solve for the height of a rectangle given area and width

soln = solve(area - height*width, height)[0]
soln


Out[25]:
$$\frac{area}{width}$$

In [26]:
# Define perimeter of rectangle in terms of height and width

perimeter = 2*(height + width)

In [27]:
# Substitute the solution for height into the expression for perimeter

perimeter.subs({height: soln})


Out[27]:
$$\frac{2 area}{width} + 2 width$$

Exercise

In the last section you solved for the radius of a sphere given its volume


In [34]:
V, r = symbols('V,r', real=True)
4*pi/3 * r**3


Out[34]:
$$\frac{4 \pi}{3} r^{3}$$

In [36]:
solve(V - 4*pi/3 * r**3, r)[0]


Out[36]:
$$\frac{\sqrt[3]{6} \sqrt[3]{V}}{2 \sqrt[3]{\pi}}$$

Now lets compute the surface area of a sphere in terms of the volume. Recall that the surface area of a sphere is given by

$$ 4 \pi r^2 $$

In [49]:
(?).subs(?)


  File "<ipython-input-49-df38c236f23a>", line 1
    (?).subs(?)
     ^
SyntaxError: invalid syntax

Does the expression look right? How would you expect the surface area to scale with respect to the volume? What is the exponent on $V$?

Plotting

SymPy can plot expressions easily using the plot function. By default this links against matplotlib.


In [41]:
%matplotlib inline

In [42]:
plot(x**2)


Out[42]:
<sympy.plotting.plot.Plot at 0x7f58e1de6710>

Exercise

In the last exercise you derived a relationship between the volume of a sphere and the surface area. Plot this relationship using plot.


In [ ]:
plot(?)

Low dependencies

You may know that SymPy tries to be a very low-dependency project. Our user base is very broad. Some entertaining aspects result. For example, textplot.


In [50]:
textplot(x**2, -3, 3)


      9 |                                                        
        |  .                                                    /
        |   \                                                  / 
        |    \                                                /  
        |     \                                              /   
        |      \                                            .    
        |       \                                                
        |        \                                        ..     
4.50149 | --------\--------------------------------------/-------
        |          \                                    /        
        |           \                                  /         
        |            \                                /          
        |             ..                            ..           
        |               \                          /             
        |                ..                      ..              
        |                  ..                  ..                
        |                    ..              ..                  
0.00297 |                      ..............                    
          -3                     0                          3

Exercise

Play with textplot and enjoy :)