In [1]:
from sympy import *
init_printing(use_latex='mathjax')
x, y, z = symbols('x,y,z')
n, m = symbols('n,m', integer=True)

In [2]:
%matplotlib inline

Numeric Evaluation

In this section we'll learn how to use our symbolic equations to drive numeric computations

.subs and .evalf

The simplest (and slowest) ways to evaluate an expression numerically is with the .subs and .evalf methods


In [3]:
sin(x)


Out[3]:
$$\sin{\left (x \right )}$$

In [4]:
sin(x).subs({x: 0})


Out[4]:
$$0$$

In [5]:
acos(x).subs({x: -1})


Out[5]:
$$\pi$$

In [6]:
acos(x).subs({x: -1}).evalf()


Out[6]:
$$3.14159265358979$$

In [7]:
acos(x).subs({x: -1}).evalf(n=100)


Out[7]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$

Exercise

In a previous section we computed the following symbolic integral

$$ \int_y^z x^n dx $$

In [8]:
result = integrate(x**n, (x, y, z))
result


Out[8]:
$$\begin{cases} - \log{\left (y \right )} + \log{\left (z \right )} & \text{for}\: n = -1 \\- \frac{y^{n + 1}}{n + 1} + \frac{z^{n + 1}}{n + 1} & \text{otherwise} \end{cases}$$

Use .subs and a dictionary with keys n, y, z to evaluate this result when

n == 2
y == 0
z == 3

In [9]:
# Evaluate the resulting integral on the above values

Exercise

This integral takes on a special form when $n = -1$. Use subs to find the expression when

n == -1
y == 5
z == 100

Then use .evalf to evaluate this subs-ed expression as a float.


In [10]:
# Evaluate the resulting integral for the values {n: -1, y: 5, z: 100}
# Then use evalf to get a numeric result

lambdify

The .subs and .evalf methods are great for when you want to evaluate an expression at a single point. When you want to evaluate your expression on lots of points they quickly become slow.

To resolve this problem SymPy can rewrite its expressions as normal Python functions using the math library, vectorized computations using the NumPy library, C or Fortran Code using code printers, or even more sophisticated systems.

We'll talk about some of the more advanced topics later. For now, lambdify...


In [11]:
# function = lambdify(input, output)

f = lambdify(x, x**2)
f(3)


Out[11]:
$$9$$

In [12]:
import numpy as np
f = lambdify(x, x**2, 'numpy')  # Use numpy backend
data = np.array([1, 2, 3, 4, 5])
f(data)


Out[12]:
array([ 1,  4,  9, 16, 25])

Exercise

Here is a radial wave function for the Carbon atom at $n=3$, $l=1$


In [13]:
from sympy.physics.hydrogen import R_nl
n = 3
l = 1
r = 6 # Carbon
expr = R_nl(n, l, x, r)
expr


Out[13]:
$$\frac{8 x}{3} \left(- 4 x + 4\right) e^{- 2 x}$$

Create a function, f, that evaluate this expression using the 'numpy' backend


In [14]:
# Create Numpy function mapping x to expr with the numpy backend

We can plot your function from $x \in [0, 5]$ with the following numpy/matplotlib code


In [73]:
from matplotlib.pyplot import plot
nx = np.linspace(0, 5, 1000)
plot(nx, f(nx))

Exercise

Create a numpy function that computes the derivative of our expression. Plot the result alongside the original.


In [15]:
# Compute derivative of expr with respect to x

In [16]:
# Create new fprime function using lambdify

In [17]:
# Plot results alongside f(nx)