Applying Limits with SymPy

Amongst the various features that SymPy offers, it also gives us the ability to evaluate limits. Using limits is actually recommended over subs() whenever the point of evaluation is a singularity.

Another feature you may be interested in, is how we can use limits when integrating expressions.


Using Limits in SymPy

Using limits in SymPy is fairly straightforward. You just need to make use of the limit() function in SymPy, and pass in 3 parameters.

The first parameter is the function on which you want the limit applied. The second is the symbol on which the limit is being used. The third parameter is the value to which the symbol in the second parameter converges to.

from sympy import limit, symbols
x, y = symbols("x y")

print( limit(sin(x)/x, x, 0) )
1

Evaluating a Limit from the side

We can choose to evaluate a limit either from the positive side, or the negative side. In order to do this, just add an extra parameter with either “+” or “-” depending on what you want.

from sympy import limit, symbols
x, y = symbols("x y")

print( limit(1/x, x, 0, '+') )      # Positive Side
print (limit(1/x, x, 0, '-') )       # Negative Side
∞
-∞

Integrating with Limits

A slightly more complex use case of the integral() function, is when we need to apply limits. Here’s an example that was shown in the official SymPy docs.

Integration in SymPy with Limits

So how do we go about making the integral expression for this? Well, all we need to do is add an extra parameter in the integrate() function, which define the upper and lower limit.

integrate(exp(-x), (x, 0, oo))
1

The exp() function represents the “e”, which can be found in the SymPy library. The oo that you see as the upper limit is actually two o’s (the letter o) used to represent infinity in SymPy.

Want to learn more about Integration in Sympy? Check our dedicated tutorial series to learn more!


This marks the end of the Limits with SymPy Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments