If we are dealing with a DiracDelta expression, i.e. DiracDelta(g(x)),
we try to simplify it.
If we could simplify it, then we integrate the resulting expression.
We already know we can integrate a simplified expression, because only
simple DiracDelta expressions are involved.
If we couldn’t simplify it, there are two cases:
The expression is a simple expression: we return the integral,
taking care if we are dealing with a Derivative or with a proper
DiracDelta.
The expression is not simple (i.e. DiracDelta(cos(x))): we can do
nothing at all.
If the node is a multiplication node having a DiracDelta term:
First we expand it.
If the expansion did work, then we try to integrate the expansion.
If not, we try to extract a simple DiracDelta term, then we have two
cases:
We have a simple DiracDelta term, so we return the integral.
We didn’t have a simple term, but we do have an expression with
simplified DiracDelta terms, so we integrate this expression.
The standard form of the derivative of a Bessel function of order n
contains two Bessel functions of orders n-1 and n+1, respectively.
Such forms cannot be used in parallel Risch algorithm, because
there is a linear recurrence relation between the three functions
while the algorithm expects that functions and derivatives are
represented in terms of algebraically independent transcendentals.
The solution is to take two of the functions, e.g., those of orders
n and n-1, and to express the derivatives in terms of the pair.
To guarantee that the proper form is used the two derivatives are
cached as soon as one is encountered.
Derivatives of other functions are also cached at no extra cost.
All derivatives are with respect to the same variable x.
Returns a set of all functional components of the given expression
which includes symbols, function applications and compositions and
non-integer powers. Fractional powers are collected with
minimal, positive exponents.
Compute indefinite integral using heuristic Risch algorithm.
This is a heuristic approach to indefinite integration in finite
terms using the extended heuristic (parallel) Risch algorithm, based
on Manuel Bronstein’s “Poor Man’s Integrator”.
The algorithm supports various classes of functions including
transcendental elementary or special functions like Airy,
Bessel, Whittaker and Lambert.
Note that this algorithm is not a decision procedure. If it isn’t
able to compute the antiderivative for a given function, then this is
not a proof that such a functions does not exist. One should use
recursive Risch algorithm in such case. It’s an open question if
this algorithm can be made a full decision procedure.
This is an internal integrator procedure. You should use toplevel
‘integrate’ function in most cases, as this procedure needs some
preprocessing steps and otherwise may fail.
A wrapper around the heurisch integration algorithm.
This method takes the result from heurisch and checks for poles in the
denominator. For each of these poles, the integral is reevaluated, and
the final integration result is given in terms of a Piecewise.
For demonstration purposes, this interval will only be split into 2
regions, bounded by [3, 5] and [5, 7].
The left-hand rule uses function evaluations at the left of each
interval:
>>> e.as_sum(2,'left')2*sin(5) + 2*sin(3)
The midpoint rule uses evaluations at the center of each interval:
>>> e.as_sum(2,'midpoint')2*sin(4) + 2*sin(6)
The right-hand rule uses function evaluations at the right of each
interval:
>>> e.as_sum(2,'right')2*sin(5) + 2*sin(7)
The trapezoid rule uses function evaluations on both sides of the
intervals. This is equivalent to taking the average of the left and
right hand rule results:
All but the trapexoid method may be used when dealing with a function
with a discontinuity. Here, the discontinuity at x = 0 can be avoided
by using the midpoint or right-hand method:
>>> e=Integral(1/sqrt(x),(x,0,1))>>> e.as_sum(5).n(4)1.730>>> e.as_sum(10).n(4)1.809>>> e.doit().n(4)# the actual value is 22.000
The left- or trapezoid method will encounter the discontinuity and
return oo:
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 depends on a certain
symbol or not.
Performs a change of variables from x to u using the relationship
given by x and u which will define the transformations f and F
(which are inverses of each other) as follows:
If x is a Symbol (which is a variable of integration) then u
will be interpreted as some function, f(u), with inverse F(u).
This, in effect, just makes the substitution of x with f(x).
If u is a Symbol then x will be interpreted as some function,
F(x), with inverse f(u). This is commonly referred to as
u-substitution.
Once f and F have been identified, the transformation is made as
follows:
\[\int_a^b x \mathrm{d}x \rightarrow \int_{F(a)}^{F(b)} f(x)
\frac{\mathrm{d}}{\mathrm{d}x}\]
where F(x) is the inverse of f(x) and the limits and integrand have
been corrected so as to retain the same value after integration.
Notes
The mappings, F(x) or f(u), must lead to a unique integral. Linear
or rational linear expression, 2*x, 1/x and sqrt(x), will
always work; quadratic expressions like x**2 - 1 are acceptable
as long as the resulting integrand does not depend on the sign of
the solutions (see examples).
The integral will be returned unchanged if x is not a variable of
integration.
x must be (or contain) only one of of the integration variables. If
u has more than one free symbol then it should be sent as a tuple
(u, uvar) where uvar identifies which variable is replacing
the integration variable.
XXX can it contain another integration variable?
This attempt fails because x = +/-sqrt(u + 1) and the
sign does not cancel out of the integrand:
>>> Integral(cos(x**2-1),(x,0,1)).transform(x**2-1,u)Traceback (most recent call last):...ValueError:The mapping between F(x) and f(u) did not give a unique integrand.
transform can do a substitution. Here, the previous
result is transformed back into the original expression
using “u-substitution”:
>>> ui=_>>> _.transform(sqrt(u+1),x)==iTrue
We can accomplish the same with a regular substitution:
>>> ui.transform(u,x**2-1)==iTrue
If the x does not contain a symbol of integration then
the integral will be returned unchanged. Integral i does
not have an integration variable a so no change is made:
>>> i.transform(a,x)==iTrue
When u has more than one free symbol the symbol that is
replacing x must be identified by passing u as a tuple:
>>> Integral(x,(x,0,1)).transform(x,(u+a,u))Integral(a + u, (u, -a, -a + 1))>>> Integral(x,(x,0,1)).transform(x,(u+a,a))Integral(a + u, (a, -u, -u + 1))
See also
variables
Lists the integration variables
as_dummy
Replace integration variables with dummy ones
modelparameters.sympy.integrals.integrals.integrate(f, var, ...)[source]¶
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) – indefinite integration with result
given with a replacing symbol
a tuple (symbol, a, b) – definite integration
Several variables can be specified, in which case the result is
multiple integration. (If var is omitted and the integrand is
univariate, the indefinite integral in that variable will be performed.)
Indefinite integrals are returned without terms that are independent
of the integration variables. (see examples)
Definite improper integrals often entail delicate convergence
conditions. Pass conds=’piecewise’, ‘separate’ or ‘none’ to have
these returned, respectively, as a Piecewise function, as a separate
result (i.e. result will be a tuple), or not at all (default is
‘piecewise’).
Strategy
SymPy uses various approaches to definite integration. One method is to
find an antiderivative for the integrand, and then use the fundamental
theorem of calculus. Various functions are implemented to integrate
polynomial, rational and trigonometric functions, and integrands
containing DiracDelta terms.
SymPy also implements the part of the Risch algorithm, which is a decision
procedure for integrating elementary functions, i.e., the algorithm can
either find an elementary antiderivative, or prove that one does not
exist. There is also a (very successful, albeit somewhat slow) general
implementation of the heuristic Risch algorithm. This algorithm will
eventually be phased out as more of the full Risch algorithm is
implemented. See the docstring of Integral._eval_integral() for more
details on computing the antiderivative using algebraic methods.
The option risch=True can be used to use only the (full) Risch algorithm.
This is useful if you want to know if an elementary function has an
elementary antiderivative. If the indefinite Integral returned by this
function is an instance of NonElementaryIntegral, that means that the
Risch algorithm has proven that integral to be non-elementary. Note that
by default, additional methods (such as the Meijer G method outlined
below) are tried on these integrals, as they may be expressible in terms
of special functions, so if you only care about elementary answers, use
risch=True. Also note that an unevaluated Integral returned by this
function is not necessarily a NonElementaryIntegral, even with risch=True,
as it may just be an indication that the particular part of the Risch
algorithm needed to integrate that function is not yet implemented.
Another family of strategies comes from re-writing the integrand in
terms of so-called Meijer G-functions. Indefinite integrals of a
single G-function can always be computed, and the definite integral
of a product of two G-functions can be computed from zero to
infinity. Various strategies are implemented to rewrite integrands
as G-functions, and use this information to compute integrals (see
the meijerint module).
The option manual=True can be used to use only an algorithm that tries
to mimic integration by hand. This algorithm does not handle as many
integrands as the other algorithms implemented but may return results in
a more familiar form. The manualintegrate module has functions that
return the steps used (see the module docstring for more information).
In general, the algebraic methods work best for computing
antiderivatives of (possibly complicated) combinations of elementary
functions. The G-function methods work best for computing definite
integrals from zero to infinity of moderately complicated
combinations of special functions, or indefinite integrals of very
simple combinations of special functions.
The strategy employed by the integration code is as follows:
If computing a definite integral, and both limits are real,
and at least one limit is +- oo, try the G-function method of
definite integration first.
Try to find an antiderivative, using all available methods, ordered
by performance (that is try fastest method first, slowest last; in
particular polynomial integration is tried first, Meijer
G-functions second to last, and heuristic Risch last).
If still not successful, try G-functions irrespective of the
limits.
The option meijerg=True, False, None can be used to, respectively:
always use G-function methods and no others, never use G-function
methods, or use all available methods (in order as described above).
It defaults to None.
Integration method that emulates by-hand techniques.
This module also provides functionality to get the steps used to evaluate a
particular integral, in the integral_steps function. This will return
nested namedtuples representing the integration rules used. The
manualintegrate function computes the integral using those steps given
an integrand; given the steps, _manualintegrate will evaluate them.
The integrator can be extended with new heuristics and evaluation
techniques. To do so, write a function that accepts an IntegralInfo
object and returns either a namedtuple representing a rule or
None. Then, write another function that accepts the namedtuple’s fields
and returns the antiderivative, and decorate it with
@evaluates(namedtuple_type). If the new technique requires a new
match, add the key and call to the antiderivative function to integral_steps.
To enable simple substitutions, add the match to find_substitutions.
rule – The first step; most rules have substeps that must also be
considered. These substeps can be evaluated using manualintegrate
to obtain a result.
Derivative of f in form expected by find_substitutions
SymPy’s derivatives for some trig functions (like cot) aren’t in a form
that works well with finding substitutions; this replaces the
derivatives for those particular forms with something that works better.
Integrate functions by rewriting them as Meijer G-functions.
There are three user-visible functions that can be used by other parts of the
sympy library to solve various integration problems:
meijerint_indefinite
meijerint_definite
meijerint_inversion
They can be used to compute, respectively, indefinite integrals, definite
integrals over intervals of the real line, and inverse laplace-type integrals
(from c-I*oo to c+I*oo). See the respective docstrings for details.
The main references for this are:
[L] Luke, Y. L. (1969), The Special Functions and Their Approximations,
Volume 1
[R] Kelly B. Roach. Meijer G Function Representations.
In: Proceedings of the 1997 International Symposium on Symbolic and
Algebraic Computation, pages 205-211, New York, 1997. ACM.
[P] A. P. Prudnikov, Yu. A. Brychkov and O. I. Marichev (1990).
Integrals and Series: More Special Functions, Vol. 3,.
Gordon and Breach Science Publisher
modelparameters.sympy.integrals.meijerint.meijerint_definite(f, x, a, b)[source]¶
Integrate f over the interval [a, b], by rewriting it as a product
of two G functions, or as a single G function.
Return res, cond, where cond are convergence conditions.
This function is implemented as a succession of functions
meijerint_definite, _meijerint_definite_2, _meijerint_definite_3,
_meijerint_definite_4. Each function in the list calls the next one
(presumably) several times. This means that calling meijerint_definite
can be very costly.
Compute the inverse laplace transform
:math:int_{c+iinfty}^{c-iinfty} f(x) e^{tx) dx,
for real c larger than the real part of all singularities of f.
Note that t is always assumed real and positive.
Return None if the integral does not exist or could not be evaluated.
Performs indefinite integration of rational functions.
Given a field \(K\) and a rational function \(f = p/q\),
where \(p\) and \(q\) are polynomials in \(K[x]\),
returns a function \(g\) such that \(f = g'\).
Given a field K and polynomials f and g in K[x], such that f and g
are coprime, deg(f) < deg(g) and g is square-free, returns a list
of tuples (s_i, q_i) of polynomials, for i = 1..n, such that s_i
in K[t, x] and q_i in K[t], and:
___ ___
d f d ` `
– - = – ) ) a log(s_i(a, x))
dx g dx /__, /__,
Given a field K and polynomials f and g in K[x], such that f and g
are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
such that f/g = A’ + B and B has square-free denominator.
This function handles the indefinite integrations of Singularity functions.
The integrate function calls this function internally whenever an
instance of SingularityFunction is passed as argument.
The idea for integration is the following:
If we are dealing with a SingularityFunction expression,
i.e. SingularityFunction(x,a,n), we just return
SingularityFunction(x,a,n+1)/(n+1) if n>=0 and
SingularityFunction(x,a,n+1) if n<0.
If the node is a multiplication or power node having a
SingularityFunction term we rewrite the whole expression in terms of
Heaviside and DiracDelta and then integrate the output. Lastly, we
rewrite the output of integration back in terms of SingularityFunction.
If none of the above case arises, we return None.
Examples
>>> from.singularityfunctionsimportsingularityintegrate>>> from..importSingularityFunction,symbols,Function>>> x,a,n,y=symbols('x a n y')>>> f=Function('f')>>> singularityintegrate(SingularityFunction(x,a,3),x)SingularityFunction(x, a, 4)/4>>> singularityintegrate(5*SingularityFunction(x,5,-2),x)5*SingularityFunction(x, 5, -1)>>> singularityintegrate(6*SingularityFunction(x,5,-1),x)6*SingularityFunction(x, 5, 0)>>> singularityintegrate(x*SingularityFunction(x,0,-1),x)0>>> singularityintegrate(SingularityFunction(x,1,-1)*f(x),x)f(1)*SingularityFunction(x, 1, 0)
To implement a concrete transform, derive from this class and implement
the _compute_transform(f, x, s, **hints) and _as_integral(f, x, s)
functions. If the transform cannot be computed, raise IntegralTransformError.
Also set cls._name.
Implement self._collapse_extra if your function returns more than just a
number and possibly a convergence condition.
Compute the unitary, ordinary-frequency cosine transform of f, defined
as
\[F(k) = \sqrt{\frac{2}{\pi}} \int_{0}^\infty f(x) \cos(2\pi x k) \mathrm{d} x.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated CosineTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
Compute the unitary, ordinary-frequency Fourier transform of f, defined
as
\[F(k) = \int_{-\infty}^\infty f(x) e^{-2\pi i x k} \mathrm{d} x.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated FourierTransform object.
For other Fourier transform conventions, see the function
sympy.integrals.transforms._fourier_transform().
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
\[F_\nu(k) = \int_{0}^\infty f(r) J_\nu(k r) r \mathrm{d} r.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated HankelTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
Compute the unitary, ordinary-frequency inverse cosine transform of F,
defined as
\[f(x) = \sqrt{\frac{2}{\pi}} \int_{0}^\infty F(k) \cos(2\pi x k) \mathrm{d} k.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated InverseCosineTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
Compute the unitary, ordinary-frequency inverse Fourier transform of F,
defined as
\[f(x) = \int_{-\infty}^\infty F(k) e^{2\pi i x k} \mathrm{d} k.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated InverseFourierTransform object.
For other Fourier transform conventions, see the function
sympy.integrals.transforms._fourier_transform().
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
Compute the inverse Hankel transform of F defined as
\[f(r) = \int_{0}^\infty F_\nu(k) J_\nu(k r) k \mathrm{d} k.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated InverseHankelTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
for any c in the fundamental strip. Under certain regularity
conditions on F and/or f,
this recovers f from its Mellin transform F
(and vice versa), for positive real x.
One of a or b may be passed as None; a suitable c will be
inferred.
If the integral cannot be computed in closed form, this function returns
an unevaluated InverseMellinTransform object.
Note that this function will assume x to be positive and real, regardless
of the sympy assumptions!
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Compute the unitary, ordinary-frequency inverse sine transform of F,
defined as
\[f(x) = \sqrt{\frac{2}{\pi}} \int_{0}^\infty F(k) \sin(2\pi x k) \mathrm{d} k.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated InverseSineTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.
For all “sensible” functions, this converges absolutely in a
half plane a < operatorname{Re}(s).
This function returns (F,a,cond)
where F is the Laplace transform of f, operatorname{Re}(s) > a is the half-plane
of convergence, and cond are auxiliary convergence conditions.
If the integral cannot be computed in closed form, this function returns
an unevaluated LaplaceTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit(). If noconds=True,
only F will be returned (i.e. not cond, and also not the plane a).
For all “sensible” functions, this converges absolutely in a strip
a < operatorname{Re}(s) < b.
The Mellin transform is related via change of variables to the Fourier
transform, and also to the (bilateral) Laplace transform.
This function returns (F,(a,b),cond)
where F is the Mellin transform of f, (a,b) is the fundamental strip
(as above), and cond are auxiliary convergence conditions.
If the integral cannot be computed in closed form, this function returns
an unevaluated MellinTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit(). If noconds=False,
then only F will be returned (i.e. not cond, and also not the strip
(a,b)).
Compute the unitary, ordinary-frequency sine transform of f, defined
as
\[F(k) = \sqrt{\frac{2}{\pi}} \int_{0}^\infty f(x) \sin(2\pi x k) \mathrm{d} x.\]
If the transform cannot be computed in closed form, this
function returns an unevaluated SineTransform object.
For a description of possible hints, refer to the docstring of
sympy.integrals.transforms.IntegralTransform.doit().
Note that for this transform, by default noconds=True.