# Solving “real world” problems with SymPy

danielhomola Blog 1 Comment

SymPy is an amazing library for symbolic mathematics in Python. It’s like Mathematica, and its online shell version along with SymPy Gamma  is pretty much like Wolfram Alpha (WA). OK, I know you can ask WA some pretty cool questions, but let’s face it, most of use just want to find the derivative of a function, or simplify an expression, and not compare the GDP of Nigeria with the profit of Google. Here it is if you’re interested btw. Also WA isn’t free anymore, and we all love Python so why not give SymPy a try?

This post definitely won’t be an in-depth tour in SymPy, I’m learning it myself at the moment as well. I’ve barely scratched its surface and I already love it, not to mention the ridiculously powerful stuff you can do with it, once you master it.

In this dummy example we’ll go to a gallery and figure out how far we should stand from a huge painting on the wall so we have the widest possible viewing angle. If you have a look at the image below you’ll see, that the bottom of the painting is 3 meters above our level of eyesight and the top of the painting is 15 meters above it. These numbers are totally made up and the this whole example isn’t very realistic, but we’ll go with it anyway because we need an example.. You can imagine that if you were standing right below the painting you wouldn’t see much of it. Same thing would happen if you stood very far away. But where exactly should you stand to get the best viewing angle? (Btw I took this example from here.)

So we are looking for the maximum $\theta$ as we are moving closer to or further away from the painting, effectively varying $x$. Basic geometry tells us that we can find this angle as the difference of two related angles $\alpha-\beta$, and trigonometry tells us that

$f(x) = \theta = \arctan \left(\frac{15}{x}\right)-\arctan \left(\frac{3}{x}\right)$.

OK, so we want to find the maximum of this function with respect to $x$. So we  need to differentiate it, find its critical points and check at which of those critical points the second derivative is negative (i.e. there we’ll have the max of $f(x)$).

We could do this manually remembering that

$\frac{d}{dx}\arctan x=\frac{1}{1+x^2},$

but what’s the fun in that? :) So let’s type a few lines of code into SymPy’s online shell:

In the first line we define $f(x)$. The cool thing here is that this is done symbolically, as we would do it on a piece of paper.  Then in the 2nd line we differentiate this function. Again, this is done as it would be done on paper. If you now type $df$, you’ll get the derivative:

$f'(x) = -\frac{15}{x^2 \left( 1+\frac{255}{x^2}\right)}+\frac{3}{x^2 \left( 1+\frac{9}{x^2}\right)}$

Even if I had looked up $\frac{d}{dx} arctan x$, it would have taken a bit of time for me to get here to be honest.. But we aren’t nearly there.. In the third line we get the second derivative, in the 4th we find the critical points of $f(x)$. Again everything is symbolic, nothing would make sense in pure Python, but it works beautifully in SymPy.

In the 5th line we print the critical points. We get $-3\sqrt{5}$ and $3\sqrt{5}$. Remember, we are looking for $x$, which is a distance, so we should be suspicious(to say the least) about the negative value, but we still need to check that $f(x)$ actually has a local maximum at $3\sqrt{5}$.

That’s exactly what the 6th line does. We substitute in the second critical point into the second derivative, and indeed we get a negative value, confirming that $f(x)$ has a local maximum at $3\sqrt{5}$.

In the 7th line we use the $N()$ function to print out the numerical value of this expression, which is $\approx$ 6.7 meters. So we should stand about 6.7 meters away from the painting to get the maximum possible viewing angle.

There you go, in less than 10 lines we did some optimization symbolically in Python. Actually we could have done this in about three lines, sacrificing a bit of clarity, but that’s never a good trade-off..

As I said, this is barely scratching the surface of what SymPy can do for you, but if you were after an alternative of Wolfram Alpha for symbolic mathematics, I think you’ll find it extremely helpful.

Solving “real world” problems with SymPy