def _muly(p, x, y): """ Returns ``_mexpand(y**deg*p.subs({x:x / y}))`` """ p1 = poly_from_expr(p, x)[0] n = degree(p1) a = [c * x**i * y**(n - i) for (i, ), c in p1.terms()] return Add(*a)
def _invertx(p, x): """ Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))`` """ p1 = poly_from_expr(p, x)[0] n = degree(p1) a = [c * x**(n - i) for (i, ), c in p1.terms()] return Add(*a)
def test_minpoly_compose(): # issue 6868 eq = (-1 / (800 * sqrt( Rational(-1, 240) + 1 / (18000 * (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3)) + 2 * (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3)))) mp = minimal_polynomial(eq + 3, x) assert mp == 8000 * x**2 - 48000 * x + 71999 # issue 5888 assert minimal_polynomial(exp(I * pi / 8), x) == x**8 + 1 mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(exp(2 * I * pi / 7), x) assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1 mp = minimal_polynomial(exp(2 * I * pi / 15), x) assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1 mp = minimal_polynomial(cos(2 * pi / 7), x) assert mp == 8 * x**3 + 4 * x**2 - 4 * x - 1 mp = minimal_polynomial(sin(2 * pi / 7), x) ex = (5 * cos(2 * pi / 7) - 7) / (9 * cos(pi / 7) - 5 * cos(3 * pi / 7)) mp = minimal_polynomial(ex, x) assert mp == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(-1 / (2 * cos(pi / 7)), x) == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(sin(2*pi/15), x) == \ 256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1 assert minimal_polynomial(sin(5 * pi / 14), x) == 8 * x**3 - 4 * x**2 - 4 * x + 1 assert minimal_polynomial(cos( pi / 15), x) == 16 * x**4 + 8 * x**3 - 16 * x**2 - 8 * x + 1 ex = RootOf(x**3 + x * 4 + 1, 0) mp = minimal_polynomial(ex, x) assert mp == x**3 + 4 * x + 1 mp = minimal_polynomial(ex + 1, x) assert mp == x**3 - 3 * x**2 + 7 * x - 4 assert minimal_polynomial(exp(I * pi / 3), x) == x**2 - x + 1 assert minimal_polynomial(exp(I * pi / 4), x) == x**4 + 1 assert minimal_polynomial(exp(I * pi / 6), x) == x**4 - x**2 + 1 assert minimal_polynomial(exp(I * pi / 9), x) == x**6 - x**3 + 1 assert minimal_polynomial(exp(I * pi / 10), x) == x**8 - x**6 + x**4 - x**2 + 1 assert minimal_polynomial(sin(pi / 9), x) == 64 * x**6 - 96 * x**4 + 36 * x**2 - 3 assert minimal_polynomial(sin(pi/11), x) == 1024*x**10 - 2816*x**8 + \ 2816*x**6 - 1232*x**4 + 220*x**2 - 11 ex = 2**Rational(1, 3) * exp(Rational(2, 3) * I * pi) assert minimal_polynomial(ex, x) == x**3 - 2 pytest.raises(NotAlgebraic, lambda: minimal_polynomial(cos(pi * sqrt(2)), x)) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(pi * sqrt(2)), x)) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(exp(I * pi * sqrt(2)), x)) # issue 5934 ex = 1 / (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) + 24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2) + 1 pytest.raises(ZeroDivisionError, lambda: minimal_polynomial(ex, x)) ex = sqrt(1 + 2**Rational(1, 3)) + sqrt(1 + 2**Rational(1, 4)) + sqrt(2) mp = minimal_polynomial(ex, x) assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576
def test_minpoly_compose(): # issue sympy/sympy#6868 eq = (-1 / (800 * sqrt( Rational(-1, 240) + 1 / (18000 * cbrt(Rational(-1, 17280000) + sqrt(15) * I / 28800000)) + 2 * cbrt(Rational(-1, 17280000) + sqrt(15) * I / 28800000)))) mp = minimal_polynomial(eq + 3)(x) assert mp == 8000 * x**2 - 48000 * x + 71999 # issue sympy/sympy#5888 assert minimal_polynomial(exp(I * pi / 8))(x) == x**8 + 1 mp = minimal_polynomial(sin(pi / 7) + sqrt(2))(x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2))(x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2))(x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(cos(pi / 9))(x) assert mp == 8 * x**3 - 6 * x - 1 mp = minimal_polynomial(sin(pi / 7) + sqrt(2))(x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2))(x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2))(x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(exp(2 * I * pi / 7))(x) assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1 mp = minimal_polynomial(exp(2 * I * pi / 15))(x) assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1 mp = minimal_polynomial(cos(2 * pi / 7))(x) assert mp == 8 * x**3 + 4 * x**2 - 4 * x - 1 mp = minimal_polynomial(sin(2 * pi / 7))(x) ex = (5 * cos(2 * pi / 7) - 7) / (9 * cos(pi / 7) - 5 * cos(3 * pi / 7)) mp = minimal_polynomial(ex)(x) assert mp == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(-1 / (2 * cos(pi / 7)))(x) == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(sin(2*pi/15))(x) == \ 256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1 assert minimal_polynomial(sin(5 * pi / 14))(x) == 8 * x**3 - 4 * x**2 - 4 * x + 1 assert minimal_polynomial(cos( pi / 15))(x) == 16 * x**4 + 8 * x**3 - 16 * x**2 - 8 * x + 1 assert minimal_polynomial(cos( pi / 17))(x) == (256 * x**8 - 128 * x**7 - 448 * x**6 + 192 * x**5 + 240 * x**4 - 80 * x**3 - 40 * x**2 + 8 * x + 1) assert minimal_polynomial(cos( 2 * pi / 21))(x) == (64 * x**6 - 32 * x**5 - 96 * x**4 + 48 * x**3 + 32 * x**2 - 16 * x + 1) ex = RootOf(x**3 + x * 4 + 1, 0) mp = minimal_polynomial(ex)(x) assert mp == x**3 + 4 * x + 1 mp = minimal_polynomial(ex + 1)(x) assert mp == x**3 - 3 * x**2 + 7 * x - 4 assert minimal_polynomial(exp(I * pi / 3))(x) == x**2 - x + 1 assert minimal_polynomial(exp(I * pi / 4))(x) == x**4 + 1 assert minimal_polynomial(exp(I * pi / 6))(x) == x**4 - x**2 + 1 assert minimal_polynomial(exp(I * pi / 9))(x) == x**6 - x**3 + 1 assert minimal_polynomial(exp(I * pi / 10))(x) == x**8 - x**6 + x**4 - x**2 + 1 assert minimal_polynomial(exp(I * pi / 18))(x) == x**12 - x**6 + 1 assert minimal_polynomial(sin( pi / 9))(x) == 64 * x**6 - 96 * x**4 + 36 * x**2 - 3 assert minimal_polynomial(sin(pi/11))(x) == 1024*x**10 - 2816*x**8 + \ 2816*x**6 - 1232*x**4 + 220*x**2 - 11 ex = cbrt(2) * exp(Rational(2, 3) * I * pi) assert minimal_polynomial(ex)(x) == x**3 - 2 pytest.raises(NotAlgebraic, lambda: minimal_polynomial(cos(pi * sqrt(2)))) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(pi * sqrt(2)))) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(exp(I * pi * sqrt(2)))) # issue sympy/sympy#5934 ex = 1 / (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) + 24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2) + 1 pytest.raises(ZeroDivisionError, lambda: minimal_polynomial(ex)) ex = sqrt(1 + cbrt(2)) + sqrt(1 + root(2, 4)) + sqrt(2) mp = minimal_polynomial(ex)(x) assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576 mp = minimal_polynomial(sin(pi / 27))(x) assert mp == (262144 * x**18 - 1179648 * x**16 + 2211840 * x**14 - 2236416 * x**12 + 1317888 * x**10 - 456192 * x**8 + 88704 * x**6 - 8640 * x**4 + 324 * x**2 - 3) ex = sqrt(2) - RootOf(x**2 - 2, 0, radicals=False) for meth in ('compose', 'groebner'): assert minimal_polynomial(ex, method=meth)(x) == x**2 - 8
def _minpoly_groebner(ex, x, cls): """ Computes the minimal polynomial of an algebraic number using Groebner bases Examples ======== >>> from diofant import minimal_polynomial, sqrt, Rational >>> from diofant.abc import x >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=False) x**2 - 2*x - 1 """ from diofant.polys.polytools import degree from diofant.core.function import expand_multinomial generator = numbered_symbols('a', cls=Dummy) mapping, symbols, replace = {}, {}, [] def update_mapping(ex, exp, base=None): a = next(generator) symbols[ex] = a if base is not None: mapping[ex] = a**exp + base else: mapping[ex] = exp.as_expr(a) return a def bottom_up_scan(ex): if ex.is_Atom: if ex is S.ImaginaryUnit: if ex not in mapping: return update_mapping(ex, 2, 1) else: return symbols[ex] elif ex.is_Rational: return ex elif ex.is_Add: return Add(*[bottom_up_scan(g) for g in ex.args]) elif ex.is_Mul: return Mul(*[bottom_up_scan(g) for g in ex.args]) elif ex.is_Pow: if ex.exp.is_Rational: if ex.exp < 0 and ex.base.is_Add: coeff, terms = ex.base.as_coeff_add() elt, _ = primitive_element(terms, polys=True) alg = ex.base - coeff # XXX: turn this into eval() inverse = invert(elt.gen + coeff, elt).as_expr() base = inverse.subs(elt.gen, alg).expand() if ex.exp == -1: return bottom_up_scan(base) else: ex = base**(-ex.exp) if not ex.exp.is_Integer: base, exp = (ex.base**ex.exp.p).expand(), Rational( 1, ex.exp.q) else: base, exp = ex.base, ex.exp base = bottom_up_scan(base) expr = base**exp if expr not in mapping: return update_mapping(expr, 1 / exp, -base) else: return symbols[expr] elif ex.is_AlgebraicNumber: if ex.root not in mapping: return update_mapping(ex.root, ex.minpoly) else: return symbols[ex.root] raise NotAlgebraic("%s doesn't seem to be an algebraic number" % ex) def simpler_inverse(ex): """ Returns True if it is more likely that the minimal polynomial algorithm works better with the inverse """ if ex.is_Pow: if (1 / ex.exp).is_integer and ex.exp < 0: if ex.base.is_Add: return True if ex.is_Mul: hit = True a = [] for p in ex.args: if p.is_Add: return False if p.is_Pow: if p.base.is_Add and p.exp > 0: return False if hit: return True return False inverted = False ex = expand_multinomial(ex) if ex.is_AlgebraicNumber: return ex.minpoly.as_expr(x) elif ex.is_Rational: result = ex.q * x - ex.p else: inverted = simpler_inverse(ex) if inverted: ex = ex**-1 res = None if ex.is_Pow and (1 / ex.exp).is_Integer: n = 1 / ex.exp res = _minimal_polynomial_sq(ex.base, n, x) elif _is_sum_surds(ex): res = _minimal_polynomial_sq(ex, S.One, x) if res is not None: result = res if res is None: bus = bottom_up_scan(ex) F = [x - bus] + list(mapping.values()) G = groebner(F, list(symbols.values()) + [x], order='lex') _, factors = factor_list(G[-1]) # by construction G[-1] has root `ex` result = _choose_factor(factors, x, ex) if inverted: result = _invertx(result, x) if result.coeff(x**degree(result, x)) < 0: result = expand_mul(-result) return result
def minimal_polynomial(ex, x=None, **args): """ Computes the minimal polynomial of an algebraic element. Parameters ========== ex : algebraic element expression x : independent variable of the minimal polynomial compose : boolean, optional If ``True`` (default), the minimal polynomial of the subexpressions of ``ex`` are computed, then the arithmetic operations on them are performed using the resultant and factorization. Else a bottom-up algorithm is used with ``groebner``. The default algorithm stalls less frequently. polys : boolean, optional if ``True`` returns a ``Poly`` object (the default is ``False``). domain : Domain, optional If no ground domain is given, it will be generated automatically from the expression. Examples ======== >>> from diofant import minimal_polynomial, sqrt, solve, QQ >>> from diofant.abc import x, y >>> minimal_polynomial(sqrt(2), x) x**2 - 2 >>> minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) x - sqrt(2) >>> minimal_polynomial(sqrt(2) + sqrt(3), x) x**4 - 10*x**2 + 1 >>> minimal_polynomial(solve(x**3 + x + 3)[0], x) x**3 + x + 3 >>> minimal_polynomial(sqrt(y), x) x**2 - y """ from diofant.polys.polytools import degree from diofant.polys.domains import FractionField from diofant.core.basic import preorder_traversal compose = args.get('compose', True) polys = args.get('polys', False) dom = args.get('domain', None) ex = sympify(ex) if ex.is_number: # not sure if it's always needed but try it for numbers (issue 8354) ex = _mexpand(ex, recursive=True) for expr in preorder_traversal(ex): if expr.is_AlgebraicNumber: compose = False break if x is not None: x, cls = sympify(x), Poly else: x, cls = Dummy('x'), PurePoly if not dom: dom = FractionField(QQ, list( ex.free_symbols)) if ex.free_symbols else QQ if hasattr(dom, 'symbols') and x in dom.symbols: raise GeneratorsError( "the variable %s is an element of the ground domain %s" % (x, dom)) if compose: result = _minpoly_compose(ex, x, dom) result = result.primitive()[1] c = result.coeff(x**degree(result, x)) if c.is_negative: result = expand_mul(-result) return cls(result, x, field=True) if polys else result.collect(x) if not dom.is_QQ: raise NotImplementedError("groebner method only works for QQ") result = _minpoly_groebner(ex, x, cls) return cls(result, x, field=True) if polys else result.collect(x)
def _minpoly_op_algebraic_element(op, ex1, ex2, x, dom, mp1=None, mp2=None): """ return the minimal polynomial for ``op(ex1, ex2)`` Parameters ========== op : operation ``Add`` or ``Mul`` ex1, ex2 : expressions for the algebraic elements x : indeterminate of the polynomials dom: ground domain mp1, mp2 : minimal polynomials for ``ex1`` and ``ex2`` or None Examples ======== >>> from diofant import sqrt, Add, Mul, QQ >>> from diofant.abc import x, y >>> p1 = sqrt(sqrt(2) + 1) >>> p2 = sqrt(sqrt(2) - 1) >>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ) x - 1 >>> q1 = sqrt(y) >>> q2 = 1 / y >>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y)) x**2*y**2 - 2*x*y - y**3 + 1 References ========== .. [1] http://en.wikipedia.org/wiki/Resultant .. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638 "Degrees of sums in a separable field extension". """ y = Dummy(str(x)) if mp1 is None: mp1 = _minpoly_compose(ex1, x, dom) if mp2 is None: mp2 = _minpoly_compose(ex2, y, dom) else: mp2 = mp2.subs({x: y}) if op is Add: # mp1a = mp1.subs({x: x - y}) if dom == QQ: R, X = ring('X', QQ) p1 = R(dict_from_expr(mp1)[0]) p2 = R(dict_from_expr(mp2)[0]) else: (p1, p2), _ = parallel_poly_from_expr((mp1, x - y), x, y) r = p1.compose(p2) mp1a = r.as_expr() elif op is Mul: mp1a = _muly(mp1, x, y) else: raise NotImplementedError('option not available') if op is Mul or dom != QQ: r = resultant(mp1a, mp2, gens=[y, x]) else: r = rs_compose_add(p1, p2) r = expr_from_dict(r.as_expr_dict(), x) deg1 = degree(mp1, x) deg2 = degree(mp2, y) if op is Mul and deg1 == 1 or deg2 == 1: # if deg1 = 1, then mp1 = x - a; mp1a = x - y - a; # r = mp2(x - a), so that `r` is irreducible return r r = Poly(r, x, domain=dom) _, factors = r.factor_list() res = _choose_factor(factors, x, op(ex1, ex2), dom) return res.as_expr()
def test_minpoly_compose(): # issue sympy/sympy#6868 eq = (-1/(800*sqrt(Rational(-1, 240) + 1/(18000*cbrt(Rational(-1, 17280000) + sqrt(15)*I/28800000)) + 2*cbrt(Rational(-1, 17280000) + sqrt(15)*I/28800000)))) mp = minimal_polynomial(eq + 3)(x) assert mp == 8000*x**2 - 48000*x + 71999 # issue sympy/sympy#5888 assert minimal_polynomial(exp(I*pi/8))(x) == x**8 + 1 mp = minimal_polynomial(sin(pi/7) + sqrt(2))(x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi/7) + sqrt(2))(x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I*pi/7) + sqrt(2))(x) assert mp == x**12 - 2*x**11 - 9*x**10 + 16*x**9 + 43*x**8 - 70*x**7 - 97*x**6 + 126*x**5 + 211*x**4 - 212*x**3 - 37*x**2 + 142*x + 127 mp = minimal_polynomial(cos(pi/9))(x) assert mp == 8*x**3 - 6*x - 1 mp = minimal_polynomial(sin(pi/7) + sqrt(2))(x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi/7) + sqrt(2))(x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I*pi/7) + sqrt(2))(x) assert mp == x**12 - 2*x**11 - 9*x**10 + 16*x**9 + 43*x**8 - 70*x**7 - 97*x**6 + 126*x**5 + 211*x**4 - 212*x**3 - 37*x**2 + 142*x + 127 mp = minimal_polynomial(exp(2*I*pi/7))(x) assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1 mp = minimal_polynomial(exp(2*I*pi/15))(x) assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1 mp = minimal_polynomial(cos(2*pi/7))(x) assert mp == 8*x**3 + 4*x**2 - 4*x - 1 mp = minimal_polynomial(sin(2*pi/7))(x) ex = (5*cos(2*pi/7) - 7)/(9*cos(pi/7) - 5*cos(3*pi/7)) mp = minimal_polynomial(ex)(x) assert mp == x**3 + 2*x**2 - x - 1 assert minimal_polynomial(-1/(2*cos(pi/7)))(x) == x**3 + 2*x**2 - x - 1 assert minimal_polynomial(sin(2*pi/15))(x) == \ 256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1 assert minimal_polynomial(sin(5*pi/14))(x) == 8*x**3 - 4*x**2 - 4*x + 1 assert minimal_polynomial(cos(pi/15))(x) == 16*x**4 + 8*x**3 - 16*x**2 - 8*x + 1 assert minimal_polynomial(cos(pi/17))(x) == (256*x**8 - 128*x**7 - 448*x**6 + 192*x**5 + 240*x**4 - 80*x**3 - 40*x**2 + 8*x + 1) assert minimal_polynomial(cos(2*pi/21))(x) == (64*x**6 - 32*x**5 - 96*x**4 + 48*x**3 + 32*x**2 - 16*x + 1) ex = RootOf(x**3 + x*4 + 1, 0) mp = minimal_polynomial(ex)(x) assert mp == x**3 + 4*x + 1 mp = minimal_polynomial(ex + 1)(x) assert mp == x**3 - 3*x**2 + 7*x - 4 assert minimal_polynomial(exp(I*pi/3))(x) == x**2 - x + 1 assert minimal_polynomial(exp(I*pi/4))(x) == x**4 + 1 assert minimal_polynomial(exp(I*pi/6))(x) == x**4 - x**2 + 1 assert minimal_polynomial(exp(I*pi/9))(x) == x**6 - x**3 + 1 assert minimal_polynomial(exp(I*pi/10))(x) == x**8 - x**6 + x**4 - x**2 + 1 assert minimal_polynomial(exp(I*pi/18))(x) == x**12 - x**6 + 1 assert minimal_polynomial(sin(pi/9))(x) == 64*x**6 - 96*x**4 + 36*x**2 - 3 assert minimal_polynomial(sin(pi/11))(x) == 1024*x**10 - 2816*x**8 + \ 2816*x**6 - 1232*x**4 + 220*x**2 - 11 ex = cbrt(2)*exp(Rational(2, 3)*I*pi) assert minimal_polynomial(ex)(x) == x**3 - 2 pytest.raises(NotAlgebraic, lambda: minimal_polynomial(cos(pi*sqrt(2)))) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(pi*sqrt(2)))) pytest.raises(NotAlgebraic, lambda: minimal_polynomial(exp(I*pi*sqrt(2)))) # issue sympy/sympy#5934 ex = 1/(-36000 - 7200*sqrt(5) + (12*sqrt(10)*sqrt(sqrt(5) + 5) + 24*sqrt(10)*sqrt(-sqrt(5) + 5))**2) + 1 pytest.raises(ZeroDivisionError, lambda: minimal_polynomial(ex)) ex = sqrt(1 + cbrt(2)) + sqrt(1 + root(2, 4)) + sqrt(2) mp = minimal_polynomial(ex)(x) assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576 mp = minimal_polynomial(sin(pi/27))(x) assert mp == (262144*x**18 - 1179648*x**16 + 2211840*x**14 - 2236416*x**12 + 1317888*x**10 - 456192*x**8 + 88704*x**6 - 8640*x**4 + 324*x**2 - 3) ex = sqrt(2) - RootOf(x**2 - 2, 0, radicals=False) for meth in ('compose', 'groebner'): assert minimal_polynomial(ex, method=meth)(x) == x**2 - 8