May 18, 2017

Python Maths Prettifier

Ever run into a problem like this?

tl;dr I made a web app that translates Python maths expressions into more human-readable maths script so you can see what you’re putting into matplotlib. Try it out by clicking here:

If you happen to be an engineering or physics student, plotting functions in MatLab or Python might have become second nature by now. However, when you’re just starting out, the Python maths syntax can seem quite messy compared to its traditional counterpart. For instance, consider this actual physics question from a second-year course on electromagnetism:

Plot the following electric field along the z-axis of a charged disc against the value of z for $q = 100 \times 2\pi \epsilon_{0} \ \text{C}$ and $R = 10\ \text{m}$

using matplotlib in Python:

$$E(z) = \frac{q}{2\pi \epsilon_{0} R^2}\left (1-\frac{z}{\sqrt{R^2 + z^2}} \right) $$

Well crap. There are a lot of variables in there to transcribe into your python equation, and once you’re finished it’s tough to spot any mistakes without a visual representation of the maths in a form that you’re used to. Or at least that was the problem I had.

Here’s what you’d have to write in Python in order to get the correct equation:

import matplotlib.pyplot as plt
import numpy as np
import math as math
z = np.arange(0.,100.)
q = 2 * math.pi * 8.85 * 10 ** (-12) * 100
fieldStrength = (q / (2 * math.pi * 8.85 * 10 ** (-12) * 10 ** 2)) * (1 - (z / np.sqrt(10**2 + z**2)))
plt.xlabel('z distance')
plt.ylabel('Electric Field')
plt.plot(z,fieldStrength,'r')
plt.show()

which yields:

Python Graph

This is cool, but what if you get an ugly error in the terminal like:

Traceback (most recent call last):
  File "matplot_test.py", line 6, in <module>
    fieldStrength = (q / (2 * math.pi * 8.85 * 10 ^ (-12) * 10 ** 2)) * (1 - (z / np.sqrt(10**2 + z**2)))
TypeError: unsupported operand type(s) for ^: 'float' and 'int'

because you forgot that in Python the exponent operator isn’t ‘^’, it’s two asterisks: ‘**‘? Well, then that’s somewhat easy to fix actually. BUT what if your Python syntax is correct, your code is interpreted successfully, but your result isn’t what you intended because you couldn’t check your equation easily? Well then you have a bigger problem. And that’s where this app comes in handy. Python Maths Prettifier takes python code as input and renders it into typographic maths using Latex in MathJax. In other words, you get a preview of what you’re actually inputting into your graph, in human maths language instead of Python’s stupid double asterisks. Inputting the value assigned to fieldStrength above, you’d get the following output from the app:

$$ \frac{q}{2\pi \times 8.85 \times 10^{-12}\times 10^2} \times \left(1-\frac{z}{\sqrt{10^2 + z^2}} \right) $$

which is what you want.

You can use the following commands from the sympy Python library for maths:

  • ‘pi’ is the constant pi
  • ‘Derivative(f(x),x)’ (second item is variable of differentiation)
  • ‘Integral(f(x),x)’ (“)
  • ‘sqrt’ is square root
  • anything else you can find on sympy

Feel free to try it out by clicking here:

Let me know if you encounter any problems in the comments below.

© Angus Lowe 2018

Powered by Hugo & Kiss.