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
In [3]:
sin(x)
Out[3]:
In [4]:
sin(x).subs({x: 0})
Out[4]:
In [5]:
acos(x).subs({x: -1})
Out[5]:
In [6]:
acos(x).subs({x: -1}).evalf()
Out[6]:
In [7]:
acos(x).subs({x: -1}).evalf(n=100)
Out[7]:
In [8]:
result = integrate(x**n, (x, y, z))
result
Out[8]:
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
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]:
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]:
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]:
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))
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)