Ticket #10251: 10251_rebase_bessel_scipy.2.patch

File 10251_rebase_bessel_scipy.2.patch, 3.2 KB (added by jvkersch, 2 years ago)
  • sage/functions/special.py

    # HG changeset patch
    # User Joris Vankerschaver <joris.vankerschaver@gmail.com>
    # Date 1296089613 28800
    # Node ID f5675304d61546d1de1ea0a0f51e71a64d74e303
    # Parent  063ffd2a1aa7c0aaf2f7548513dc8ee546a0a108
    Rebase of 10251: Bessel functions have small imaginary component
    
    diff -r 063ffd2a1aa7 -r f5675304d615 sage/functions/special.py
    a b  
    719719        1.2660658777520... 
    720720        sage: bessel_I(1,1,"scipy") 
    721721        0.565159103992... 
     722 
     723    Check whether the return value is real whenever the argument is real (#10251):: 
     724     
     725        sage: bessel_I(5, 1.5, algorithm='scipy') in RR 
     726        True 
     727         
    722728    """ 
    723729    if algorithm=="pari": 
    724730        from sage.libs.pari.all import pari 
     
    741747        ans = ans.replace("(","") 
    742748        ans = ans.replace(")","") 
    743749        ans = ans.replace("j","*I") 
    744         return sage_eval(ans) 
     750        ans = sage_eval(ans) 
     751        return real(ans) if z in RR else ans # Return real value when arg is real 
    745752    elif algorithm == "maxima": 
    746753        if prec != 53: 
    747754            raise ValueError, "for the maxima algorithm the precision must be 53" 
     
    804811        0.0583793793051868 
    805812        sage: bessel_J(3,10,"scipy") 
    806813        0.0583793793052... 
     814 
     815    Check whether the return value is real whenever the argument is real (#10251)::                                                                                                                                                            
     816        sage: bessel_J(5, 1.5, algorithm='scipy') in RR                                                                       
     817        True 
    807818    """ 
    808819     
    809820    if algorithm=="pari": 
     
    829840        ans = ans.replace("(","") 
    830841        ans = ans.replace(")","") 
    831842        ans = ans.replace("j","*I") 
    832         return sage_eval(ans) 
     843        ans = sage_eval(ans) 
     844        return real(ans) if z in RR else ans 
    833845    elif algorithm == "maxima": 
    834846        if prec != 53: 
    835847            raise ValueError, "for the maxima algorithm the precision must be 53" 
     
    871883        Traceback (most recent call last): 
    872884        ... 
    873885        NotImplementedError: The K-Bessel function is only implemented for the pari and scipy algorithms 
     886 
     887        Check whether the return value is real whenever the argument is real (#10251):: 
     888 
     889        sage: bessel_K(5, 1.5, algorithm='scipy') in RR 
     890        True 
     891 
    874892    """ 
    875893    if algorithm=="scipy": 
    876894        if prec != 53: 
     
    880898        ans = ans.replace("(","") 
    881899        ans = ans.replace(")","") 
    882900        ans = ans.replace("j","*I") 
    883         return sage_eval(ans) 
     901        ans = sage_eval(ans) 
     902        return real(ans) if z in RR else ans 
    884903    elif algorithm == 'pari': 
    885904        from sage.libs.pari.all import pari 
    886905        try: 
     
    945964        ans = ans.replace("(","") 
    946965        ans = ans.replace(")","") 
    947966        ans = ans.replace("j","*I") 
    948         return sage_eval(ans) 
     967        ans = sage_eval(ans) 
     968        return real(ans) if z in RR else ans 
    949969    elif algorithm == "maxima": 
    950970        if prec != 53: 
    951971            raise ValueError, "for the maxima algorithm the precision must be 53"