# Ticket #5338: trac_5338.2.patch

File trac_5338.2.patch, 4.0 KB (added by William Stein, 13 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 -  ``var`` - polynomial variable name (default 'x') -  ``algorithm`` - 'algebraic' or 'numerical' (default both, algebraic first) both, but with numerical first) -  ``bits`` - the number of bits to use in numerical approx correct, a ``NotImplementedError`` will be raised. ALGORITHM: Two distinct algorithms are used, depending on the algorithm parameter. By default, the algebraic algorithm is attempted first, then the numerical one. algorithm parameter. By default, the numerical algorithm is attempted first, then the algebraic one. Algebraic: Attempt to evaluate this expression in QQbar, using cyclotomic fields to resolve exponential and trig functions at sage: minpoly(sqrt(2)-3^(1/3)) x^6 - 6*x^4 + 6*x^3 + 12*x^2 + 36*x + 1 Works with trig and exponential functions too. :: sage: (a+b+a*b).absolute_minpoly() x^4 - 22*x^2 - 48*x - 23 The minpoly function is used implicitly when creating number fields:: sage: x = var('x') sage: eqn =  x^3 + sqrt(2)*x + 5 == 0 sage: a = solve(eqn, x)[0].rhs() sage: QQ[a] Number Field in a with defining polynomial x^6 + 10*x^3 - 2*x^2 + 25 Here we solve a cubic and then recover it from its complicated radical expansion. Traceback (most recent call last): 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 ...) Sometimes it fails. Sometimes it fails, as it must given that some numbers aren't algebraic:: :: sage: sin(1).minpoly() sage: sin(1).minpoly(algorithm='numerical') Traceback (most recent call last): ... ValueError: Could not find minimal polynomial (1000 bits, degree 24). .. note:: Failure to produce a minimal polynomial does not Of course, failure to produce a minimal polynomial does not necessarily indicate that this number is transcendental. AUTHORS: - Robert Bradshaw (2008-10): algebraic algorithm """ if algorithm is None or algorithm == 'algebraic': from sage.rings.all import QQbar try: return QQ[var](QQbar(ex).minpoly()) except (TypeError, ValueError): if algorithm == 'algebraic': raise if algorithm is None or algorithm.startswith('numeric'): bits_list = [bits] if bits else [100,200,500,1000] degree_list = [degree] if degree else [2,4,8,12,24] # Otherwise fall back to numerical guess elif epsilon and error < epsilon: return g else: elif algorithm is not None: raise NotImplementedError, "Could not prove minimal polynomial %s (epsilon %s)" % (g, RR(error).str(no_sci=False)) raise ValueError, "Could not find minimal polynomial (%s bits, degree %s)." % (bits, degree) if algorithm is not None: raise ValueError, "Could not find minimal polynomial (%s bits, degree %s)." % (bits, degree) if algorithm is None or algorithm == 'algebraic': from sage.rings.all import QQbar return QQ[var](QQbar(ex).minpoly()) raise ValueError, "Unknown algorithm: %s" % algorithm