Ticket #5338: trac_5338.2.patch

File trac_5338.2.patch, 4.0 KB (added by was, 12 years ago)
  • sage/calculus/calculus.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1258665497 28800
    # Node ID 33b6d31c76351a80bc43eb478403cf3a0d58ea67
    # Parent  5db805d3bdafb3f79835cff2af6a333f749b500c
    trac 5338 -- Sage 3.2.2: speed regression/infinite loop for "K.<b> = QQ[a]"
    
    diff -r 5db805d3bdaf -r 33b6d31c7635 sage/calculus/calculus.py
    a b  
    843843    -  ``var`` - polynomial variable name (default 'x')
    844844
    845845    -  ``algorithm`` - 'algebraic' or 'numerical' (default
    846        both, algebraic first)
     846       both, but with numerical first)
    847847
    848848    -  ``bits`` - the number of bits to use in numerical
    849849       approx
     
    871871    correct, a ``NotImplementedError`` will be raised.
    872872
    873873    ALGORITHM: Two distinct algorithms are used, depending on the
    874     algorithm parameter. By default, the algebraic algorithm is
    875     attempted first, then the numerical one.
     874    algorithm parameter. By default, the numerical algorithm is
     875    attempted first, then the algebraic one.
    876876
    877877    Algebraic: Attempt to evaluate this expression in QQbar, using
    878878    cyclotomic fields to resolve exponential and trig functions at
     
    905905        sage: minpoly(sqrt(2)-3^(1/3))
    906906        x^6 - 6*x^4 + 6*x^3 + 12*x^2 + 36*x + 1
    907907
     908
    908909    Works with trig and exponential functions too.
    909910
    910911    ::
     
    927928        sage: (a+b+a*b).absolute_minpoly()
    928929        x^4 - 22*x^2 - 48*x - 23
    929930
     931    The minpoly function is used implicitly when creating
     932    number fields::
     933   
     934        sage: x = var('x')
     935        sage: eqn =  x^3 + sqrt(2)*x + 5 == 0
     936        sage: a = solve(eqn, x)[0].rhs()
     937        sage: QQ[a]
     938        Number Field in a with defining polynomial x^6 + 10*x^3 - 2*x^2 + 25
     939
    930940    Here we solve a cubic and then recover it from its complicated
    931941    radical expansion.
    932942
     
    988998        Traceback (most recent call last):
    989999        NotImplementedError: Could not prove minimal polynomial x^10 - 11/4*x^8 + 11/4*x^6 - 77/64*x^4 + 55/256*x^2 - 11/1024 (epsilon ...)
    9901000
    991     Sometimes it fails.
     1001    Sometimes it fails, as it must given that some numbers aren't algebraic::
    9921002
    993     ::
    994 
    995         sage: sin(1).minpoly()
     1003        sage: sin(1).minpoly(algorithm='numerical')
    9961004        Traceback (most recent call last):
    9971005        ...
    9981006        ValueError: Could not find minimal polynomial (1000 bits, degree 24).
    9991007
    10001008    .. note::
    10011009
    1002        Failure to produce a minimal polynomial does not
     1010       Of course, failure to produce a minimal polynomial does not
    10031011       necessarily indicate that this number is transcendental.
    10041012
    10051013    AUTHORS:
     
    10081016
    10091017    - Robert Bradshaw (2008-10): algebraic algorithm
    10101018    """
    1011     if algorithm is None or algorithm == 'algebraic':
    1012         from sage.rings.all import QQbar
    1013         try:
    1014             return QQ[var](QQbar(ex).minpoly())
    1015         except (TypeError, ValueError):
    1016             if algorithm == 'algebraic':
    1017                 raise
    1018 
    10191019    if algorithm is None or algorithm.startswith('numeric'):
    10201020        bits_list = [bits] if bits else [100,200,500,1000]
    10211021        degree_list = [degree] if degree else [2,4,8,12,24]
     
    10501050                            # Otherwise fall back to numerical guess
    10511051                            elif epsilon and error < epsilon:
    10521052                                return g
    1053                             else:
     1053                            elif algorithm is not None:
    10541054                                raise NotImplementedError, "Could not prove minimal polynomial %s (epsilon %s)" % (g, RR(error).str(no_sci=False))
    10551055
    1056         raise ValueError, "Could not find minimal polynomial (%s bits, degree %s)." % (bits, degree)
     1056        if algorithm is not None:
     1057            raise ValueError, "Could not find minimal polynomial (%s bits, degree %s)." % (bits, degree)
    10571058
     1059    if algorithm is None or algorithm == 'algebraic':
     1060        from sage.rings.all import QQbar
     1061        return QQ[var](QQbar(ex).minpoly())
     1062       
    10581063    raise ValueError, "Unknown algorithm: %s" % algorithm
    10591064
    10601065