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
- integrate(f, (x, a, b)) returns the definite integral
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)
1
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)
--------------
4
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.
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:
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)
y*x**2/2
>>> integrate(log(x), x)
-x + x*log(x)
>>> integrate(log(x), (x, 1, a))
1 - a + a*log(a)
>>> integrate(x)
x**2/2
>>> integrate(x*y)
x**2*y**2/4
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.
Represents unevaluated integral.
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((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()
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((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()
96.8853618335341
>>> e.n()
124.616199194723
Replace the integration variable x in the integrand with the expression given by , 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).
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