Ticket #11643: trac_11643-preliminary.patch

File trac_11643-preliminary.patch, 2.5 KB (added by nbruin, 11 years ago)
  • sage/interfaces/maxima_lib.py

    # HG changeset patch
    # User Nils Bruin <nbruin@sfu.ca>
    # Date 1312401197 25200
    # Node ID f2bb957f257e7e007b5d5bcb7846416fd4d27dc9
    # Parent  4fc30672ecde8c0e99ff23ba962c6e54b0b3381a
    #11643: make SR<->maxima interface aware of bfloats
    
    diff --git a/sage/interfaces/maxima_lib.py b/sage/interfaces/maxima_lib.py
    a b def reduce_load_MaximaLib(): 
    956956#############################################
    957957
    958958import sage.rings.real_double
     959import sage.rings.real_mpfr
    959960import sage.symbolic.expression
    960961import sage.functions.trig
    961962import sage.functions.log
    cddr=EclObject("cddr") 
    971972caddr=EclObject("caddr")
    972973caaadr=EclObject("caaadr")
    973974cadadr=EclObject("cadadr")
     975caddar=EclObject("caddar")
    974976meval=EclObject("meval")
    975977NIL=EclObject("NIL")
    976978
    def sage_rat(x,y): 
    10791081
    10801082mplus=EclObject("MPLUS")
    10811083mtimes=EclObject("MTIMES")
     1084bigfloat=EclObject("BIGFLOAT")
     1085simp=EclObject("SIMP")
    10821086rat=EclObject("RAT")
    10831087max_op_dict[mplus]=add_vararg
    10841088max_op_dict[mtimes]=mul_vararg
    def mdiff_to_sage(expr): 
    11801184    """
    11811185    return max_to_sr(expr.cadr()).diff(*[max_to_sr(e) for e in expr.cddr()])
    11821186
     1187def bigfloat_to_sage(expr):
     1188    r"""
     1189    Special conversion rule for BIGFLOAT expressions.
     1190   
     1191    INPUT:
     1192   
     1193    - ``expr`` - ECL object; a Maxima BIGFLOAT expression
     1194    """
     1195    p=caddar(expr).python()
     1196    m=cadr(expr).python()
     1197    e=caddr(expr).python()
     1198    return sage.rings.real_mpfr.RealField(p)(m*2**(e-p))
     1199
    11831200def dummy_integrate(expr):
    11841201    r"""
    11851202    We would like to simply tie Maxima's integrate to
    special_max_to_sage={ 
    12201237    mrat : mrat_to_sage,
    12211238    mqapply : mqapply_to_sage,
    12221239    mdiff : mdiff_to_sage,
     1240    bigfloat: bigfloat_to_sage,
    12231241    EclObject("%INTEGRATE") : dummy_integrate
    12241242}
    12251243
    def pyobject_to_max(obj): 
    12671285    """
    12681286    if isinstance(obj,sage.rings.rational.Rational):
    12691287        return EclObject(obj) if (obj.denom().is_one()) else EclObject([[rat], obj.numer(),obj.denom()])
     1288    elif isinstance(obj,sage.rings.real_mpfr.RealNumber) and obj.prec() != 53:
     1289        s,m,e = obj.sign_mantissa_exponent()
     1290        p = obj.prec()
     1291        return EclObject([[bigfloat,simp,obj.prec()],m,e+p])
    12701292    elif isinstance(obj,sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]:
    12711293        re, im = obj.list()
    12721294        return EclObject([[mplus], pyobject_to_max(re), [[mtimes], pyobject_to_max(im), max_i]])