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 as we are moving closer to or further away from the painting, effectively varying . Basic geometry tells us that we can find this angle as the difference of two related angles , and trigonometry tells us that

.

OK, so we want to find the maximum of this function with respect to . 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 ).

We could do this manually remembering that

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

1 2 3 4 5 6 7 |
f = atan(15/x) - atan(3/x) df1 = diff(f) df2 = diff(diff(f)) critical = solve(Eq(df1, 0)) critical df2.subs(x, critical[1]) N(critical[1]) |

In the first line we define . 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 , you’ll get the derivative:

Even if I had looked up , 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 . 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 and . Remember, we are looking for , which is a distance, so we should be suspicious(to say the least) about the negative value, but we still need to check that actually has a local maximum at .

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 has a local maximum at .

In the 7th line we use the function to print out the numerical value of this expression, which is 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.

Pingback: Newton’s method with 10 lines of Python | Daniel Homola()