In [1]:
# This short notebook will show how to take symbolic integrals in Python.
# We will require the 'SymPy' module.
import sympy as sym
In [2]:
# First, let's define a symbol x.
x = sym.Symbol('x')
In [3]:
# Then we can define f in terms of x.
f = 2*x**2+3
In [4]:
# To evaluate f at a particular value of x, we can use 'subs()'.
z = f.subs(x, 2)
print('2(2)^2 + 3 =', z)
2(2)^2 + 3 = 11
In [5]:
# To integrate f with respect to x, we use 'integrate()'.
intf = f.integrate(x)
intf
Out[5]:
$\displaystyle \frac{2 x^{3}}{3} + 3 x$
In [6]:
# We can get the same result using 'integrate()' in the following way:
intf = sym.integrate(f, x)
intf
Out[6]:
$\displaystyle \frac{2 x^{3}}{3} + 3 x$
In [7]:
# The above examples are indefinite integrals of f.  We can evaluate
# definite integrals using the same 'integrate()' function.  We just
# need to pass the limits of integration...
intf = sym.integrate(f, (x, 0, 3))
intf
Out[7]:
$\displaystyle 27$
In [8]:
# or:
intf = f.integrate((x, 0, 3))
intf
Out[8]:
$\displaystyle 27$
In [10]:
# You can also used symbols for the integration limits.
a = sym.Symbol('a')
b = sym.Symbol('b')
intf = f.integrate((x, a, b))
intf
Out[10]:
$\displaystyle - \frac{2 a^{3}}{3} - 3 a + \frac{2 b^{3}}{3} + 3 b$
In [11]:
# We can make our functions more complicated.  Here's a function of two variables.
y = sym.Symbol('y')
g = x*sym.sin(x*y)
g
Out[11]:
$\displaystyle x \sin{\left(x y \right)}$
In [12]:
# Here's the x-integral of g...
g.integrate(x)
Out[12]:
$\displaystyle \begin{cases} - \frac{x \cos{\left(x y \right)}}{y} + \frac{\sin{\left(x y \right)}}{y^{2}} & \text{for}\: y \neq 0 \\0 & \text{otherwise} \end{cases}$
In [13]:
# and here's the y-integral.
g.integrate(y)
Out[13]:
$\displaystyle x \left(\begin{cases} - \frac{\cos{\left(x y \right)}}{x} & \text{for}\: x \neq 0 \\0 & \text{otherwise} \end{cases}\right)$
In [14]:
# Notice that the results of both of the g integrals depend on whether or not
# x and y are zero.  We can specify that our variables are non-zero when they
# are first declared.
x = sym.Symbol('x', nonzero = True)
y = sym.Symbol('y', nonzero = True)
g = x*sym.sin(x*y)
In [15]:
# Now the solutions of our integrals have no conditions.
g.integrate(x), g.integrate(y)
Out[15]:
(-x*cos(x*y)/y + sin(x*y)/y**2, -cos(x*y))
In [16]:
# Alternatively, you can define Python functions.
def fcn(x):
    return 2*x**2 + 3
In [17]:
# These functions can be integrated in the same way.  Here's the indefinite
# integral...
fcn(x).integrate(x)
Out[17]:
$\displaystyle \frac{2 x^{3}}{3} + 3 x$
In [18]:
# ... and here's the indefinite integral.
sym.integrate(fcn(x), (x, a, b))
Out[18]:
$\displaystyle - \frac{2 a^{3}}{3} - 3 a + \frac{2 b^{3}}{3} + 3 b$
In [19]:
# Of course, you can have functions of multiple variables.
def gcn(x, y):
    return x*sym.sin(x*y)
In [20]:
# Here's the indefinite y-integral followed by the indefinite x-integral.
z = sym.integrate(sym.integrate(gcn(x, y), y), x)
z
Out[20]:
$\displaystyle - \frac{\sin{\left(x y \right)}}{y}$
In [21]:
# Here's the definite y-integral followed by the definite x-integral using
# the other method of calling the 'integrate()' function.
z = gcn(x, y).integrate((y, 1, 3)).integrate((x, -2, -1))
print(z)
print(sym.N(z))
-sin(1) + sin(3)/3 - sin(6)/3 + sin(2)
0.208004944104050