1 from sympy
.core
import S
, Add
, sympify
, Basic
2 from sympy
.core
.methods
import RelMeths
, ArithMeths
3 from gruntz
import gruntz
5 def limit(e
, z
, z0
, dir="+"):
7 Compute the limit of e(z) at the point z0.
9 z0 can be any expression, including oo and -oo.
11 For dir="+" (default) it calculates the limit from the right
12 (z->z0+) and for dir="-" the limit from the left (z->z0-). For infinite z0
13 (oo or -oo), the dir argument doesn't matter.
17 >>> limit(sin(x)/x, x, 0)
19 >>> limit(1/x, x, 0, dir="+")
21 >>> limit(1/x, x, 0, dir="-")
28 First we try some heuristics for easy and frequent cases like "x", "1/x",
29 "x**2" and similar, so that it's fast. For all other cases, we use the
30 Gruntz algorithm (see the gruntz() function).
44 if e
.args
[1].is_Rational
:
55 if e
.args
[1].is_number
:
56 if e
.args
[1].evalf() > 0:
66 return Add(*[limit(term
, z
, z0
, dir) for term
in e
.args
])
68 # this is a case like limit(x*y+x*z, z, 2) == x*y+2*x
69 # but we need to make sure, that the general gruntz() algorithm is
70 # executed for a case like "limit(sqrt(x+1)-sqrt(x),x,oo)==0"
75 return gruntz(e
, z
, z0
, dir)
77 class Limit(Basic
, RelMeths
, ArithMeths
):
78 """Represents unevaluated limit.
82 >>> Limit(sin(x)/x, x, 0)
83 Limit(1/x*sin(x), x, 0, dir='+')
84 >>> Limit(1/x, x, 0, dir="-")
85 Limit(1/x, x, 0, dir='-')
88 def __new__(cls
, e
, z
, z0
, dir="+"):
92 obj
= Basic
.__new
__(cls
)
93 obj
._args
= (e
, z
, z0
, dir)
97 e
, z
, z0
, dir = self
.args
98 return limit(e
, z
, z0
, dir)