The integrals module in SymPy implements methdos calculating definite un undefinite integrals of expressions.

Principal method in this module is integrate()

  • integrate(f, x) returns the indefinite integral \int f\,dx
  • integrate(f, (x, a, b)) returns the definite integral \int_{a}^{b} f\,dx


SymPy can integrate a vast array of functions. It can integrate polynomial functions:

    >>> from sympy import *
>>> import sys
>>> sys.displayhook = pprint
    >>> x = Symbol('x')
    >>> integrate(x**2 + x + 1, x)
         2    3
        x    x
    x + -- + --
        2    3

Rational functions:

>>> integrate(x/(x**2+2*x+1), x)
log(1 + x) + -----
             1 + x

Exponential-polynomial functions. Multiplicative combinations of polynomials and the functions exp, cos and sin can be integrated by hand using repeated integration by parts, which is an extremely tedious process. Happily, SymPy will deal with these integrals.

>>> integrate(x**2 * exp(x) * cos(x), x)
 x           2  x                                x    2         x
e *sin(x)   x *e *sin(x)      x          cos(x)*e    x *cos(x)*e
--------- + ------------ - x*e *sin(x) - --------- + ------------
    2            2                           2            2

even a few nonelementary integrals (in particular, some integrals involving the error function) can be evaluated:

>>> integrate(exp(-x**2)*erf(x), x)
  ____    2
\/ pi *erf (x)


There is a general method for calculating antiderivatives of elementary functions, called the Risch algorithm. The Risch algorithm is a decision procedure that can determine whether an elementary solution exists, and in that case calculate it. It can be extended to handle many nonelementary functions in addition to the elementary ones.

SymPy currently uses a simplified version of the Risch algorithm, called the Risch-Norman algorithm. This algorithm is much faster, but may fail to find an antiderivative, although it is still very powerful. SymPy also uses pattern matching and heuristics to speed up evaluation of some types of integrals, e.g. polynomials.

API reference

static integrals.integrate(expr, *args, **kwargs)

integrate(f, var, ...)

Compute definite or indefinite integral of one or more variables using Risch-Norman algorithm and table lookup. This procedure is able to handle elementary algebraic and transcendental functions and also a huge class of special functions, including Airy, Bessel, Whittaker and Lambert.

var can be:

  • a symbol – indefinite integration
  • a tuple (symbol, a, b) – definite integration

Several variables can be specified, in which case the result is multiple integration.

Also, if no var is specified at all, then the full anti-derivative of f is returned. This is equivalent to integrating f over all its variables.


>>> from sympy import integrate, log
>>> from import a, x, y
>>> integrate(x*y, x)
>>> integrate(log(x), x)
-x + x*log(x)
>>> integrate(log(x), (x, 1, a))
1 - a + a*log(a)
>>> integrate(x)
>>> integrate(x*y)

See also the doctest of Integral._eval_integral(), which explains thoroughly the strategy that SymPy uses for integration.

Class Integral represents an unevaluated integral and has some methods that help in the integration of an expression.

class sympy.integrals.Integral

Represents unevaluated integral.

as_sum(n, method='midpoint')

Approximates the integral by a sum.

method ... one of: left, right, midpoint

This is basically just the rectangle method [1], the only difference is where the function value is taken in each interval.


method = midpoint:

Uses the n-order midpoint rule to evaluate the integral.

Midpoint rule uses rectangles approximation for the given area (e.g. definite integral) of the function with heights equal to the point on the curve exactly in the middle of each interval (thus midpoint method). See [1] for more information.


>>> from sympy import sqrt
>>> from import x
>>> from sympy.integrals import Integral
>>> e = Integral(sqrt(x**3+1), (x, 2, 10))
>>> e
Integral((1 + x**3)**(1/2), (x, 2, 10))
>>> e.as_sum(4, method="midpoint")
2*730**(1/2) + 4*7**(1/2) + 4*86**(1/2) + 6*14**(1/2)
>>> e.as_sum(4, method="midpoint").n()
>>> e.n()


Uses the n-order rectangle rule to evaluate the integral, at each interval the function value is taken at the left hand side of the interval.


>>> from sympy import sqrt
>>> from import x
>>> e = Integral(sqrt(x**3+1), (x, 2, 10))
>>> e
Integral((1 + x**3)**(1/2), (x, 2, 10))
>>> e.as_sum(4, method="left")
6 + 2*65**(1/2) + 2*217**(1/2) + 6*57**(1/2)
>>> e.as_sum(4, method="left").n()
>>> e.n()
transform(x, mapping, inverse=False)

Replace the integration variable x in the integrand with the expression given by mapping, e.g. 2*x or 1/x. The integrand and endpoints are rescaled to preserve the value of the original integral.

In effect, this performs a variable substitution (although the symbol remains unchanged; follow up with subs to obtain a new symbol.)

With inverse=True, the inverse transformation is performed.

The mapping must be uniquely invertible (e.g. a linear or linear fractional transformation).

TODO and Bugs

There are still lots of functions that sympy does not know how to integrate. For bugs related to this module, see

Table Of Contents

Previous topic

Extended LaTeXModule for Sympy

Next topic

Logic Module

This Page