How to do it...

Let's now define the expression for the function we want to compute by running the following code in a Jupyter cell:

r, theta = symbols('r, theta')
x = r * cos(theta)
y = r * sin(theta)
x, y

In the first line in this code, we define the symbols r and theta. A symbol in sympy is used to represent the name of a mathematical variable.

Next, with the x = r * cos(theta) and y = r * sin(theta) assignments, we define two sympy expressions, x and y.

Notice that the sin() and cos() functions that appear here are defined in sympy and are not the same as the np.sin() function and np.cos() from NumPy!

The last line in the cell, x,y, will print the x and y expressions in pretty format, according to the usual rules for typesetting mathematics.

The reader may have noticed that the preceding expressions compute the Cartesian coordinates for a point expressed in polar coordinates.

One weakness of symbolic code is that it tends to produce slow code, but sympy has a neat solution to this problem. We can create a NumPy ufunc from a set of sympy expressions, as follows:

polar_to_rectangular = lambdify((r, theta), (x, y), modules='numpy')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset