How it works...

The lambdify() sympy function returns a Python function that can be used to evaluate a set of expressions numerically. The arguments to lambdify() are described as follows:

  • The first argument, (r, theta), is a tuple containing the symbols appearing in the expressions. These will map to the input arguments of the polar_to_rectangular() function.
  • The second argument, (x,y), specifies the expressions used to define the function. These must be sympy expressions that contain only the symbols given in the first argument, namely r and theta in this example.
  • The modules='numpy' argument specifies that we want to use NumPy for numerical computations.

We can now use the polar_to_rectangular() function to convert arrays containing polar coordinates to rectangular coordinates, as follows:

r_values = np.array([2, 1.5, -3])
theta_values = np.array([np.pi/4, np.pi/2, -np.pi])
xvalues, yvalues = polar_to_rectangular(r_values, theta_values)

In this code, we first define two arrays, r_values and theta_values, containing, respectively, the values of r and theta for the points we want to convert. Then, we call the polar_to_rectangular() function to effect the conversion, storing the rectangular coordinates in the xvalues and yvalues arrays. Notice that, since polar_to_rectangular() is a ufunc, it can be applied to arrays with arbitrary shapes. In particular, it supports broadcasting, as illustrated in the following code:

r = 4
theta_values = np.linspace(0, 2*np.pi, 300)
xvalues, yvalues = polar_to_rectangular(r, theta_values)

In this code, the polar_to_rectangular() function is called with a scalar as the first argument, r=4, and an array as the second argument. The result is storing the rectangular coordinates of a circle of radius 4 in the xvalues and yvalues arrays.

..................Content has been hidden....................

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