Integrals

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

Examples

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)
 3    2
x    x
-- + -- + x
3    2

Rational functions:

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

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)
 2  x           2  x                         x           x
x *e *sin(x)   x *e *cos(x)      x          e *sin(x)   e *cos(x)
------------ + ------------ - 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)
--------------
      4

Internals

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.

Examples

>>> from sympy import integrate, log
>>> from sympy.abc import a, x, y
>>> integrate(x*y, x)
x**2*y/2
>>> integrate(log(x), x)
x*log(x) - x
>>> integrate(log(x), (x, 1, a))
a*log(a) - a + 1
>>> integrate(x)
x**2/2
>>> integrate(x*y)
Traceback (most recent call last):
...
ValueError: specify integration variables to integrate x*y

Note that integrate(x) syntax is meant only for convenience in interactive sessions and should be avoided in library code.

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_dummy()

Replace instances of the integration variables with their dummy counterparts to make clear what are dummy variables and what are real-world symbols in an Integral. The “integral at” limit that has a length of 1 will be explicated with its length-2 equivalent.

>>> from sympy import Integral
>>> from sympy.abc import x, y
>>> Integral(x).as_dummy()
Integral(_x, (_x, x))
>>> Integral(x, (x, x, y), (y, x, y)).as_dummy()
Integral(_x, (_x, x, _y), (_y, x, y))

If there were no dummies in the original expression, then the output of this function will show which symbols cannot be changed by subs(), those with an underscore prefix.

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.

[1] http://en.wikipedia.org/wiki/Rectangle_method

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.

Examples:

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

method=left:

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.

Examples:

>>> from sympy import sqrt
>>> from sympy.abc import x
>>> e = Integral(sqrt(x**3+1), (x, 2, 10))
>>> e
Integral((x**3 + 1)**(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()
96.8853618335341
>>> e.n()
124.616199194723
free_symbols

This method returns the symbols that will exist when the integral is evaluated. This is useful if one is trying to determine whether an integral is dependent on a certain symbol or not.

>>> from sympy import Integral
>>> from sympy.abc import x, y
>>> Integral(x, (x, y, 1)).free_symbols
set([y])
is_number

Return True if the Integral will result in a number, else False.

sympy considers anything that will result in a number to have is_number == True.

>>> from sympy import log
>>> log(2).is_number
True

Integrals are a special case since they contain symbols that can be replaced with numbers. Whether the integral can be done or not is another issue. But answering whether the final result is a number is not difficult.

>>> from sympy import Integral
>>> from sympy.abc import x, y
>>> Integral(x).is_number
False
>>> Integral(x, y).is_number
False
>>> Integral(x, (y, 1, x)).is_number
False
>>> Integral(x, (y, 1, 2)).is_number
False
>>> Integral(x, (y, 1, 1)).is_number
True
>>> Integral(x, (x, 1, 2)).is_number
True
>>> Integral(x*y, (x, 1, 2), (y, 1, 3)).is_number
True
>>> Integral(1, x, (x, 1, 2)).is_number
True
is_zero

Since Integral doesn’t autosimplify it it useful to see if it would simplify to zero or not in a trivial manner, i.e. when the function is 0 or two limits of a definite integral are the same.

This is a very naive and quick test, not intended to check for special patterns like Integral(sin(m*x)*cos(n*x), (x, 0, 2*pi)) == 0.

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).

variables

Return a list of the integration variables.

>>> from sympy import Integral
>>> from sympy.abc import x, i
>>> Integral(x**i, (i, 1, 3)).variables
[i]

TODO and Bugs

There are still lots of functions that sympy does not know how to integrate. For bugs related to this module, see http://code.google.com/p/sympy/issues/list?q=label:Integration

Table Of Contents

Previous topic

Extended LaTeXModule for Sympy

Next topic

Logic Module

This Page