Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can rely on Sympy instead of Maxima or you can extract the expression at the corresponding point and then compute the limit:

sage: f(x) = piecewise([[(0,1),x],[[1,2],x/2]])
sage: limit(f(x),x=1/2,algorithm="sympy")
1/2
sage: limit(f(x).expression_at(1/2),x=1/2)
1/2

You can rely on Sympy instead of Maxima or you can extract the expression at the corresponding point and then compute the limit:

sage: f(x) = piecewise([[(0,1),x],[[1,2],x/2]])
sage: limit(f(x),x=1/2,algorithm="sympy")
1/2
sage: limit(f(x).expression_at(1/2),x=1/2)
1/2

Edit: It is clear that endpoints of the different pieces and points in the interior of a piece cannot receive the same treatment. For the latter, it suffices to compute a limit, as usual, of the corresponding function piece. For the former, one should compute and compare one sided limits. So, a complete solution could be obtained by defining a function that considers the different cases. As a proof of concept, such a function could be similar to the following one:

def p_limit(f, x0, eps=10^(-10), algorithm="maxima"):
    if x0 in f.end_points():
        funs = [f.expression_at(x0+s*eps) for s in [-1,1]]
        lims = [limit(fun, x=x0, dir=s, algorithm=algorithm) 
                for fun,s in zip(funs,["-","+"])]
        if lims[0]==lims[1]:
            return lims[0]
        else:
            return "undefined"
    else:
        return limit(f.expression_at(x0), x=x0, algorithm=algorithm)

Let us test it:

sage: f = piecewise([[(-infinity,-pi), sin(2*x)],  
....:                [(-pi,0), sin(x)/x],  
....:                [[0,0], 1],  
....:                [(0,pi/2), x/tan(x)], 
....:                [[pi/2,pi/2], 0], 
....:                [(pi/2,+infinity), cos(x)]]) 
....:                                                                           
sage: for x0 in [-9*pi/8, -pi, 0, pi/3, pi/2, 3*pi]: 
....:     print("The limit at ", x0, " is ", p_limit(f,x0)) 
....:                                                                           
The limit at  -9/8*pi  is  -1/2*sqrt(2)
The limit at  -pi  is  0
The limit at  0  is  1
The limit at  1/3*pi  is  1/9*sqrt(3)*pi
The limit at  1/2*pi  is  0
The limit at  3*pi  is  -1

One more test:

sage: f = piecewise([[[-4,-1], 2*x^2],  
....:                [(-1,0), (x^2-1)/(x^2+x)],  
....:                [(0,1), -log(x)], 
....:                [[1,2], x^2]]) 
....:                                                                           
sage: for x0 in [-2, -1, -0.5, 0, 1]: 
....:     print("The limit at ", x0, " is ", p_limit(f,x0)) 
....:                                                                           
The limit at  -2  is  8
The limit at  -1  is  2
The limit at  -0.500000000000000  is  3.0
The limit at  0  is  +Infinity
The limit at  1  is  undefined