Ticket #7377: trac_7377-assumptions-p2.patch

File trac_7377-assumptions-p2.patch, 20.7 KB (added by kcrisman, 8 years ago)

trivial correction to p1 patch

  • sage/calculus/calculus.py

    # HG changeset patch
    # User Karl-Dieter Crisman <kcrisman@gmail.com>
    # Date 1299792266 18000
    # Node ID 23b5ece5f4d2a377d261a8e5d359db7354f1dc58
    # Parent  8e1dc3ae4ccc1b0ab2a5da96e0f484e56265138f
    Trac 7377 - provisional patch fixing error messages for assumptions
    
    Also fixes various other things, including extra documentation in a number of places and additional examples of assumptions in summation, integration, and limits.
    
    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/calculus/calculus.py
    a b  
    483483        sage: symbolic_sum(a*q^k, k, 0, n)
    484484        (a*q^(n + 1) - a)/(q - 1)
    485485
    486     The geometric series::
     486    For the geometric series, we will have to assume
     487    the right values for the sum to converge::
    487488
    488489        sage: assume(abs(q) < 1)
    489490        sage: symbolic_sum(a*q^k, k, 0, oo)
     
    498499        Traceback (most recent call last):
    499500        ...
    500501        ValueError: Sum is divergent.
     502        sage: forget()
     503        sage: assumptions() # check the assumptions were really forgotten
     504        []
    501505
    502506    This summation only Mathematica can perform::
    503507
     
    533537        raise ValueError, "summation limits must not depend on the summation variable"
    534538
    535539    if algorithm == 'maxima':
    536         try:
    537             return maxima.sr_sum(expression,v,a,b)
    538         except TypeError, error:
    539             s = str(error)
    540             if "divergent" in s or 'Pole encountered' in s:
    541                 raise ValueError, "Sum is divergent."
    542             else:
    543                 raise
     540        return maxima.sr_sum(expression,v,a,b)
    544541
    545542    elif algorithm == 'mathematica':
    546543        try:
     
    990987        sage: CDF(f.limit(x = I))
    991988        2.06287223508 + 0.74500706218*I
    992989
     990    Notice that Maxima may ask for more information::
     991
     992        sage: var('a')   
     993        a
     994        sage: limit(x^a,x=0)
     995        Traceback (most recent call last):
     996        ...
     997        ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)
     998        Is  a  positive, negative, or zero?
     999
     1000    With this example, Maxima is looking for a LOT of information::
     1001
     1002        sage: assume(a>0)
     1003        sage: limit(x^a,x=0)
     1004        Traceback (most recent call last):
     1005        ...
     1006        ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)
     1007        Is a an integer?
     1008        sage: assume(a,'integer')
     1009        sage: limit(x^a,x=0)
     1010        Traceback (most recent call last):
     1011        ...
     1012        ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)
     1013        Is a an even number?
     1014        sage: assume(a,'even')
     1015        sage: limit(x^a,x=0)
     1016        0
     1017        sage: forget()
     1018
    9931019    More examples::
    9941020
    9951021        sage: limit(x*log(x), x = 0, dir='+')
  • sage/calculus/functional.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/calculus/functional.py
    a b  
    242242        sage: integral(abs(x)*x, x, 0, a)
    243243        Traceback (most recent call last):
    244244        ...
    245         TypeError: Computation failed since Maxima requested additional
    246         constraints (try the command 'assume(a>0)' before integral or limit
    247         evaluation, for example):
     245        ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details)
    248246        Is  a  positive, negative, or zero?
    249247        sage: assume(a>0)
    250248        sage: integral(abs(x)*x, x, 0, a)
  • sage/interfaces/maxima.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/interfaces/maxima.py
    a b  
    580580
    581581    def _expect_expr(self, expr=None, timeout=None):
    582582        """
    583         EXAMPLES:
    584             sage: a,b=var('a,b')
    585             sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x)
     583        Wait for a given expression expr (which could be a regular
     584        expression or list of regular expressions) to appear in the output
     585        for at most timeout seconds. 
     586
     587        See `sage.interfaces.expect.Expect._expect_expr` for full details
     588        on its use and functionality.
     589
     590        TESTS:
     591
     592        These tests indirectly show that the interface is working
     593        and catching certain errors::
     594
     595            sage: maxima('2+2')
     596            4
     597            sage: maxima('integrate(1/(x^3*(a+b*x)^(1/3)),x)')
    586598            Traceback (most recent call last):
    587599            ...
    588             TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(a>0)' before integral or limit evaluation, for example):
     600            TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('a>0')" before integral or limit evaluation, for example):
    589601            Is  a  positive or negative?
    590             sage: assume(a>0)
    591             sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x)
    592             2/9*sqrt(3)*b^2*arctan(1/3*(2*(b*x + a)^(1/3) + a^(1/3))*sqrt(3)/a^(1/3))/a^(7/3) + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
    593             sage: var('x, n')
    594             (x, n)
    595             sage: integral(x^n,x)
     602            sage: maxima.assume('a>0')
     603            [a>0]
     604            sage: maxima('integrate(1/(x^3*(a+b*x)^(1/3)),x)')
     605            -b^2*log((b*x+a)^(2/3)+a^(1/3)*(b*x+a)^(1/3)+a^(2/3))/(9*a^(7/3))+2*b^2*atan((2*(b*x+a)^(1/3)+a^(1/3))/(sqrt(3)*a^(1/3)))/(3^(3/2)*a^(7/3))+2*b^2*log((b*x+a)^(1/3)-a^(1/3))/(9*a^(7/3))+(4*b^2*(b*x+a)^(5/3)-7*a*b^2*(b*x+a)^(2/3))/(6*a^2*(b*x+a)^2-12*a^3*(b*x+a)+6*a^4)
     606            sage: maxima('integrate(x^n,x)')
    596607            Traceback (most recent call last):
    597608            ...
    598             TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(n+1>0)' before integral or limit evaluation, for example):
     609            TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('n+1>0')" before integral or limit evaluation, for example):
    599610            Is  n+1  zero or nonzero?
    600             sage: assume(n+1>0)
    601             sage: integral(x^n,x)
    602             x^(n + 1)/(n + 1)
    603             sage: forget()
     611            sage: maxima.assume('n+1>0')
     612            [n>-1]
     613            sage: maxima('integrate(x^n,x)')
     614            x^(n+1)/(n+1)
     615            sage: maxima.forget([fact for fact in maxima.facts()])
     616            [[a>0,n>-1]]
     617            sage: maxima.facts()
     618            []
     619            sage: var('a')   
     620            a
     621            sage: maxima('limit(x^a,x,0)')
     622            Traceback (most recent call last):
     623            ...
     624            TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('a>0')" before integral or limit evaluation, for example):
     625            Is  a  positive, negative, or zero?
    604626        """
    605627        if expr is None:
    606628            expr = self._prompt_wait
     
    623645                j = v.find('Is ')
    624646                v = v[j:]
    625647                k = v.find(' ',4)
    626                 msg = "Computation failed since Maxima requested additional constraints (try the command 'assume(" + v[4:k] +">0)' before integral or limit evaluation, for example):\n" + v + self._ask[i-1]
     648                msg = """Computation failed since Maxima requested additional constraints (try the command "maxima.assume('""" + v[4:k] +""">0')" before integral or limit evaluation, for example):\n""" + v + self._ask[i-1]
    627649                self._sendline(";")
    628650                self._expect_expr()
    629651                raise ValueError, msg
     
    949971        """
    950972        return MaximaElementFunction
    951973
    952     ##some helper functions to wrap tha calculus use of the maxima interface.
     974    ##some old helper functions to wrap the calculus use of the maxima interface.
    953975    ##these routines expect arguments living in the symbolic ring and return something
    954976    ##that is hopefully coercible into the symbolic ring again.
    955 
    956     def sr_integral(self,*args):
    957         return args[0]._maxima_().integrate(*args[1:])
    958 
    959     def sr_sum(self,expression,v,a,b):
    960         sum  = "'sum(%s, %s, %s, %s)" % tuple([repr(expr._maxima_()) for expr in (expression, v, a, b)])
    961         result = self.simplify_sum(sum)
    962         result = result.ratsimp()
    963         return expression.parent()(result)
    964 
    965     def sr_limit(self,ex,*args):
    966         return ex._maxima_().limit(*args)
    967 
    968     def sr_tlimit(self,ex,*args):
    969         return ex._maxima_().tlimit(*args)
    970 
     977##
     978##    def sr_integral(self,*args):
     979##        return args[0]._maxima_().integrate(*args[1:])
     980##
     981##    def sr_sum(self,expression,v,a,b):
     982##        sum  = "'sum(%s, %s, %s, %s)" % tuple([repr(expr._maxima_()) for expr in (expression, v, a, b)])
     983##        result = self.simplify_sum(sum)
     984##        result = result.ratsimp()
     985##        return expression.parent()(result)
     986##
     987##    def sr_limit(self,ex,*args):
     988##        return ex._maxima_().limit(*args)
     989##
     990##    def sr_tlimit(self,ex,*args):
     991##        return ex._maxima_().tlimit(*args)
     992##
    971993
    972994def is_MaximaElement(x):
    973995    """
  • sage/interfaces/maxima_lib.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/interfaces/maxima_lib.py
    a b  
    405405    ##that is hopefully coercible into the symbolic ring again.
    406406
    407407    def sr_integral(self,*args):
     408        """
     409        Helper function to wrap calculus use of Maxima's integration.
     410
     411        TESTS::
     412
     413            sage: a,b=var('a,b')
     414            sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x)
     415            Traceback (most recent call last):
     416            ...
     417            ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details)
     418            Is  a  positive or negative?
     419            sage: assume(a>0)
     420            sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x)
     421            2/9*sqrt(3)*b^2*arctan(1/3*(2*(b*x + a)^(1/3) + a^(1/3))*sqrt(3)/a^(1/3))/a^(7/3) + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
     422            sage: var('x, n')
     423            (x, n)
     424            sage: integral(x^n,x)
     425            Traceback (most recent call last):
     426            ...
     427            ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(n+1>0)', see `assume?` for more details)
     428            Is  n+1  zero or nonzero?
     429            sage: assume(n+1>0)
     430            sage: integral(x^n,x)
     431            x^(n + 1)/(n + 1)
     432            sage: forget()
     433            sage: assumptions()  # Check the assumptions really were forgotten
     434            []
     435        """
    408436        try:
    409437            return max_to_sr(maxima_eval(([max_integrate],[sr_to_max(SR(a)) for a in args])))
    410438        except RuntimeError, error:
    411439            s = str(error)
    412440            if "Divergent" in s or "divergent" in s:
     441# in pexpect interface, one looks for this - e.g. integrate(1/x^3,x,-1,3) gives a principal value
     442#            if "divergent" in s or 'Principal Value' in s:
    413443                raise ValueError, "Integral is divergent."
     444            elif "Is" in s: # Maxima asked for a condition
     445                j = s.find('Is ')
     446                s = s[j:]
     447                k = s.find(' ',4)
     448                raise ValueError, "Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(" + s[4:k] +">0)', see `assume?` for more details)\n" + s
    414449            else:
    415450                raise error
    416451
    417452    def sr_sum(self,*args):
     453        """
     454        Helper function to wrap calculus use of Maxima's summation.
     455
     456        TESTS::
     457
     458            sage: x, y, k, n = var('x, y, k, n')
     459            sage: sum(binomial(n,k) * x^k * y^(n-k), k, 0, n)
     460            (x + y)^n
     461            sage: q, a = var('q, a')
     462            sage: sum(a*q^k, k, 0, oo)
     463            Traceback (most recent call last):
     464            ...
     465            ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before summation *may* help (example of legal syntax is 'assume(abs(q)-1>0)', see `assume?` for more details)
     466            Is  abs(q)-1  positive, negative, or zero?
     467            sage: assume(q > 1)
     468            sage: sum(a*q^k, k, 0, oo)
     469            Traceback (most recent call last):
     470            ...
     471            ValueError: Sum is divergent.
     472            sage: forget()
     473            sage: assume(abs(q) < 1)
     474            sage: sum(a*q^k, k, 0, oo)
     475            -a/(q - 1)
     476            sage: forget()
     477            sage: assumptions() # check the assumptions were really forgotten
     478            []
     479        """
    418480        try:
    419481            return max_to_sr(maxima_eval([[max_ratsimp],[[max_simplify_sum],([max_sum],[sr_to_max(SR(a)) for a in args])]]));
    420482        except RuntimeError, error:
    421483            s = str(error)
    422484            if "divergent" in s:
     485# in pexpect interface, one looks for this - could not find an example where 'Pole encountered' occurred, though
     486#            if "divergent" in s or 'Pole encountered' in s:
    423487                raise ValueError, "Sum is divergent."
     488            elif "Is" in s: # Maxima asked for a condition
     489                j = s.find('Is ')
     490                s = s[j:]
     491                k = s.find(' ',4)
     492                raise ValueError, "Computation failed since Maxima requested additional constraints; using the 'assume' command before summation *may* help (example of legal syntax is 'assume(" + s[4:k] +">0)', see `assume?` for more details)\n" + s
    424493            else:
    425494                raise error
    426495
    427496    def sr_limit(self,expr,v,a,dir=None):
    428         L=[sr_to_max(SR(a)) for a in [expr,v,a]]
    429         if dir == "plus":
    430             L.append(max_plus)
    431         elif dir == "minus":
    432             L.append(max_minus)
    433         return max_to_sr(maxima_eval(([max_limit],L)))
     497        """
     498        Helper function to wrap calculus use of Maxima's limits.
     499
     500        TESTS::
     501
     502            sage: f = (1+1/x)^x
     503            sage: limit(f,x = oo)
     504            e
     505            sage: limit(f,x = 5)
     506            7776/3125
     507            sage: limit(f,x = 1.2)
     508            2.06961575467...
     509            sage: var('a')   
     510            a
     511            sage: limit(x^a,x=0)
     512            Traceback (most recent call last):
     513            ...
     514            ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)
     515            Is  a  positive, negative, or zero?
     516            sage: assume(a>0)
     517            sage: limit(x^a,x=0)
     518            Traceback (most recent call last):
     519            ...
     520            ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)
     521            Is a an integer?
     522            sage: assume(a,'integer')
     523            sage: assume(a,'even')  # Yes, Maxima will ask this too
     524            sage: limit(x^a,x=0)
     525            0
     526            sage: forget()
     527            sage: assumptions() # check the assumptions were really forgotten
     528            []
     529        """
     530        try:
     531            L=[sr_to_max(SR(a)) for a in [expr,v,a]]
     532            if dir == "plus":
     533                L.append(max_plus)
     534            elif dir == "minus":
     535                L.append(max_minus)
     536            return max_to_sr(maxima_eval(([max_limit],L)))
     537        except RuntimeError, error:
     538            s = str(error)
     539            if "Is" in s: # Maxima asked for a condition
     540                j = s.find('Is ')
     541                s = s[j:]
     542                raise ValueError, "Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details)\n" + s
     543            else:
     544                raise error
    434545
    435546    def sr_tlimit(self,expr,v,a,dir=None):
     547        """
     548        Helper function to wrap calculus use of Maxima's Taylor series limits.
     549
     550        TESTS::
     551
     552            sage: f = (1+1/x)^x
     553            sage: limit(f, x = I, taylor=True)
     554            (-I + 1)^I
     555        """
    436556        L=[sr_to_max(SR(a)) for a in [expr,v,a]]
    437557        if dir == "plus":
    438558            L.append(max_plus)
  • sage/symbolic/assumptions.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/symbolic/assumptions.py
    a b  
    259259   
    260260    -  ``*args`` - assumptions
    261261   
    262     EXAMPLES::
     262    EXAMPLES:
     263
     264    Assumptions are typically used to ensure certain relations are
     265    evaluated as true that are not true in general.
    263266   
     267    Here, we verify that for `x>0`, `\sqrt(x^2)=x`::
     268
    264269        sage: assume(x > 0)
    265270        sage: bool(sqrt(x^2) == x)
    266271        True
     272
     273    This will be assumed in the current Sage session until forgotten::
     274
    267275        sage: forget()
    268276        sage: bool(sqrt(x^2) == x)
    269277        False
    270278   
     279
     280    Another major use case is in taking certain integrals and limits
     281    where the answers may depend on some sign condition::
     282
     283        sage: var('x, n')
     284        (x, n)
     285        sage: assume(n+1>0)
     286        sage: integral(x^n,x)
     287        x^(n + 1)/(n + 1)
     288        sage: forget()
     289
     290    ::
     291
     292        sage: q, a = var('q, a')
     293        sage: assume(q > 1)
     294        sage: sum(a*q^k, k, 0, oo)
     295        Traceback (most recent call last):
     296        ...
     297        ValueError: Sum is divergent.
     298        sage: forget()
     299        sage: assume(abs(q) < 1)
     300        sage: sum(a*q^k, k, 0, oo)
     301        -a/(q - 1)
     302        sage: forget()
     303
    271304    An integer constraint::
    272305   
    273306        sage: var('n, P, r, r2')
     
    278311        sage: solve(c==d,r2)
    279312        [r2 == e^r - 1]
    280313   
    281     ::
     314    Simplifying certain well-known identities works as well::
    282315   
    283316        sage: sin(n*pi)
    284317        sin(pi*n)
  • sage/symbolic/integration/external.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/symbolic/integration/external.py
    a b  
    1818    if a is None:
    1919        result = maxima.sr_integral(expression,v)
    2020    else:
    21         try:
    22             result = maxima.sr_integral(expression, v, a, b)
    23         except TypeError, error:
    24             s = str(error)
    25             if "divergent" in s or 'Principal Value' in s:
    26                 raise ValueError, "Integral is divergent."
    27             else:
    28                 raise
     21        result = maxima.sr_integral(expression, v, a, b)
    2922    return result._sage_()
    3023
    3124def sympy_integrator(expression, v, a=None, b=None):
  • sage/symbolic/integration/integral.py

    diff -r 8e1dc3ae4ccc -r 23b5ece5f4d2 sage/symbolic/integration/integral.py
    a b  
    332332        sage: integral(x^n,x)
    333333        Traceback (most recent call last):
    334334        ...
    335         TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(n+1>0)' before integral or limit evaluation, for example):
     335        ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(n+1>0)', see `assume?` for more details)
    336336        Is  n+1  zero or nonzero?
    337337        sage: assume(n > 0)
    338338        sage: integral(x^n,x)
     
    348348    Note that an exception is raised when a definite integral is
    349349    divergent::
    350350
    351         sage: forget()
    352 
     351        sage: forget() # always remember to forget assumptions you no longer need
    353352        sage: integrate(1/x^3,(x,0,1))
    354353        Traceback (most recent call last):
    355354        ...
     
    439438
    440439    ALIASES: integral() and integrate() are the same.
    441440
    442     EXAMPLES: Here is example where we have to use assume::
     441    EXAMPLES:
     442
     443    Here is an example where we have to use assume::
    443444
    444445        sage: a,b = var('a,b')
    445446        sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)