def test_issue_7638(): f = pi / log(sqrt(2)) assert ((1 + I)**(I * f / 2))**0.3 == (1 + I)**(0.15 * I * f) # if 1/3 -> 1.0/3 this should fail since it cannot be shown that the # sign will be +/-1; for the previous "small arg" case, it didn't matter # that this could not be proved assert (1 + I)**(4 * I * f) == ((1 + I)**(12 * I * f))**Rational(1, 3) assert (((1 + I)**(I * (1 + 7 * f)))**Rational(1, 3)).exp == Rational(1, 3) r = symbols('r', real=True) assert sqrt(r**2) == abs(r) assert cbrt(r**3) != r assert sqrt(Pow(2 * I, 5 * S.Half)) != (2 * I)**Rational(5, 4) p = symbols('p', positive=True) assert cbrt(p**2) == p**Rational(2, 3) assert NS(((0.2 + 0.7 * I)**(0.7 + 1.0 * I))**(0.5 - 0.1 * I), 1) == '0.4 + 0.2*I' assert sqrt(1 / (1 + I)) == sqrt(1 - I) / sqrt(2) # or 1/sqrt(1 + I) e = 1 / (1 - sqrt(2)) assert sqrt(e) == I / sqrt(-1 + sqrt(2)) assert e**Rational(-1, 2) == -I * sqrt(-1 + sqrt(2)) assert sqrt((cos(1)**2 + sin(1)**2 - 1)**(3 + I)).exp in [S.Half, Rational(3, 2) + I / 2] assert sqrt(r**Rational(4, 3)) != r**Rational(2, 3) assert sqrt((p + I)**Rational(4, 3)) == (p + I)**Rational(2, 3) assert sqrt((p - p**2 * I)**2) == p - p**2 * I assert sqrt((p + r * I)**2) != p + r * I e = (1 + I / 5) assert sqrt(e**5) == e**(5 * S.Half) assert sqrt(e**6) == e**3 assert sqrt((1 + I * r)**6) != (1 + I * r)**3
def test_root(): from sympy.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_issue_7638(): f = pi/log(sqrt(2)) assert ((1 + I)**(I*f/2))**0.3 == (1 + I)**(0.15*I*f) # if 1/3 -> 1.0/3 this should fail since it cannot be shown that the # sign will be +/-1; for the previous "small arg" case, it didn't matter # that this could not be proved assert (1 + I)**(4*I*f) == ((1 + I)**(12*I*f))**(S(1)/3) assert (((1 + I)**(I*(1 + 7*f)))**(S(1)/3)).exp == S(1)/3 r = symbols('r', real=True) assert sqrt(r**2) == abs(r) assert cbrt(r**3) != r assert sqrt(Pow(2*I, 5*S.Half)) != (2*I)**(5/S(4)) p = symbols('p', positive=True) assert cbrt(p**2) == p**(2/S(3)) assert NS(((0.2 + 0.7*I)**(0.7 + 1.0*I))**(0.5 - 0.1*I), 1) == '0.4 + 0.2*I' assert sqrt(1/(1 + I)) == sqrt(1 - I)/sqrt(2) # or 1/sqrt(1 + I) e = 1/(1 - sqrt(2)) assert sqrt(e) == I/sqrt(-1 + sqrt(2)) assert e**-S.Half == -I*sqrt(-1 + sqrt(2)) assert sqrt((cos(1)**2 + sin(1)**2 - 1)**(3 + I)).exp == S.Half assert sqrt(r**(4/S(3))) != r**(2/S(3)) assert sqrt((p + I)**(4/S(3))) == (p + I)**(2/S(3)) assert sqrt((p - p**2*I)**2) == p - p**2*I assert sqrt((p + r*I)**2) != p + r*I e = (1 + I/5) assert sqrt(e**5) == e**(5*S.Half) assert sqrt(e**6) == e**3 assert sqrt((1 + I*r)**6) != (1 + I*r)**3
def test_root(): from sympy.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5)/2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2)*I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1/n) assert root(x, -n) == x**(-1/n) assert root(x, n, k) == x**(1/n)*(-1)**(2*k/n)
def test_nseries(): x = Symbol('x') assert sqrt(I*x - 1)._eval_nseries(x, 4, None, 1) == I + x/2 + I*x**2/8 - x**3/16 + O(x**4) assert sqrt(I*x - 1)._eval_nseries(x, 4, None, -1) == -I - x/2 - I*x**2/8 + x**3/16 + O(x**4) assert cbrt(I*x - 1)._eval_nseries(x, 4, None, 1) == (-1)**(S(1)/3) - (-1)**(S(5)/6)*x/3 + \ (-1)**(S(1)/3)*x**2/9 + 5*(-1)**(S(5)/6)*x**3/81 + O(x**4) assert cbrt(I*x - 1)._eval_nseries(x, 4, None, -1) == (-1)**(S(1)/3)*exp(-2*I*pi/3) - \ (-1)**(S(5)/6)*x*exp(-2*I*pi/3)/3 + (-1)**(S(1)/3)*x**2*exp(-2*I*pi/3)/9 + \ 5*(-1)**(S(5)/6)*x**3*exp(-2*I*pi/3)/81 + O(x**4) assert (1 / (exp(-1/x) + 1/x))._eval_nseries(x, 2, None) == -x**2*exp(-1/x) + x
def test_issue_14000(): assert isinstance(sqrt(4, evaluate=False), Pow) == True assert isinstance(cbrt(3.5, evaluate=False), Pow) == True assert isinstance(root(16, 4, evaluate=False), Pow) == True assert sqrt(4, evaluate=False) == Pow(4, S.Half, evaluate=False) assert cbrt(3.5, evaluate=False) == Pow(3.5, Rational(1, 3), evaluate=False) assert root(4, 2, evaluate=False) == Pow(4, S.Half, evaluate=False) assert root(16, 4, 2, evaluate=False).has(Pow) == True assert real_root(-8, 3, evaluate=False).has(Pow) == True
def test_issue_14000(): assert isinstance(sqrt(4, evaluate=False), Pow) == True assert isinstance(cbrt(3.5, evaluate=False), Pow) == True assert isinstance(root(16, 4, evaluate=False), Pow) == True assert sqrt(4, evaluate=False) == Pow(4, S.Half, evaluate=False) assert cbrt(3.5, evaluate=False) == Pow(3.5, Rational(1, 3), evaluate=False) assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(16, 4, 2, evaluate=False).has(Pow) == True assert real_root(-8, 3, evaluate=False).has(Pow) == True
def test_pow_1(): assert ((1 + x)**2).nseries(x, n=5) == x**2 + 2 * x + 1 # https://github.com/sympy/sympy/issues/21075 assert ((sqrt(x) + 1)**2).nseries(x) == 2 * sqrt(x) + x + 1 assert ((sqrt(x) + cbrt(x))**2).nseries(x) == 2*x**Rational(5, 6)\ + x**Rational(2, 3) + x
def test_branch_cuts(): assert limit(asin(I * x + 2), x, 0) == pi - asin(2) assert limit(asin(I * x + 2), x, 0, '-') == asin(2) assert limit(asin(I * x - 2), x, 0) == -asin(2) assert limit(asin(I * x - 2), x, 0, '-') == -pi + asin(2) assert limit(acos(I * x + 2), x, 0) == -acos(2) assert limit(acos(I * x + 2), x, 0, '-') == acos(2) assert limit(acos(I * x - 2), x, 0) == acos(-2) assert limit(acos(I * x - 2), x, 0, '-') == 2 * pi - acos(-2) assert limit(atan(x + 2 * I), x, 0) == I * atanh(2) assert limit(atan(x + 2 * I), x, 0, '-') == -pi + I * atanh(2) assert limit(atan(x - 2 * I), x, 0) == pi - I * atanh(2) assert limit(atan(x - 2 * I), x, 0, '-') == -I * atanh(2) assert limit(atan(1 / x), x, 0) == pi / 2 assert limit(atan(1 / x), x, 0, '-') == -pi / 2 assert limit(atan(x), x, oo) == pi / 2 assert limit(atan(x), x, -oo) == -pi / 2 assert limit(acot(x + S(1) / 2 * I), x, 0) == pi - I * acoth(S(1) / 2) assert limit(acot(x + S(1) / 2 * I), x, 0, '-') == -I * acoth(S(1) / 2) assert limit(acot(x - S(1) / 2 * I), x, 0) == I * acoth(S(1) / 2) assert limit(acot(x - S(1) / 2 * I), x, 0, '-') == -pi + I * acoth(S(1) / 2) assert limit(acot(x), x, 0) == pi / 2 assert limit(acot(x), x, 0, '-') == -pi / 2 assert limit(asec(I * x + S(1) / 2), x, 0) == asec(S(1) / 2) assert limit(asec(I * x + S(1) / 2), x, 0, '-') == -asec(S(1) / 2) assert limit(asec(I * x - S(1) / 2), x, 0) == 2 * pi - asec(-S(1) / 2) assert limit(asec(I * x - S(1) / 2), x, 0, '-') == asec(-S(1) / 2) assert limit(acsc(I * x + S(1) / 2), x, 0) == acsc(S(1) / 2) assert limit(acsc(I * x + S(1) / 2), x, 0, '-') == pi - acsc(S(1) / 2) assert limit(acsc(I * x - S(1) / 2), x, 0) == -pi + acsc(S(1) / 2) assert limit(acsc(I * x - S(1) / 2), x, 0, '-') == -acsc(S(1) / 2) assert limit(log(I * x - 1), x, 0) == I * pi assert limit(log(I * x - 1), x, 0, '-') == -I * pi assert limit(log(-I * x - 1), x, 0) == -I * pi assert limit(log(-I * x - 1), x, 0, '-') == I * pi assert limit(sqrt(I * x - 1), x, 0) == I assert limit(sqrt(I * x - 1), x, 0, '-') == -I assert limit(sqrt(-I * x - 1), x, 0) == -I assert limit(sqrt(-I * x - 1), x, 0, '-') == I assert limit(cbrt(I * x - 1), x, 0) == (-1)**(S(1) / 3) assert limit(cbrt(I * x - 1), x, 0, '-') == -(-1)**(S(2) / 3) assert limit(cbrt(-I * x - 1), x, 0) == -(-1)**(S(2) / 3) assert limit(cbrt(-I * x - 1), x, 0, '-') == (-1)**(S(1) / 3)
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs(dict(x=-8, n=3)) == -2 assert g.subs(dict(x=8, n=3)) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs(dict(x=I, n=3)) == cbrt(I) assert g.subs(dict(x=-8, n=2)) == sqrt(-8) assert g.subs(dict(x=I, n=2)) == sqrt(I)
def _minpoly_compose(ex, x, dom): """ Computes the minimal polynomial of an algebraic element using operations on minimal polynomials Examples ======== >>> from sympy import minimal_polynomial, sqrt, Rational >>> from sympy.abc import x, y >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=True) x**2 - 2*x - 1 >>> minimal_polynomial(sqrt(y) + 1/y, x, compose=True) x**2*y**2 - 2*x*y - y**3 + 1 """ if ex.is_Rational: return ex.q * x - ex.p if ex is I: _, factors = factor_list(x**2 + 1, x, domain=dom) return x**2 + 1 if len(factors) == 1 else x - I if ex is S.GoldenRatio: _, factors = factor_list(x**2 - x - 1, x, domain=dom) if len(factors) == 1: return x**2 - x - 1 else: return _choose_factor(factors, x, (1 + sqrt(5)) / 2, dom=dom) if ex is S.TribonacciConstant: _, factors = factor_list(x**3 - x**2 - x - 1, x, domain=dom) if len(factors) == 1: return x**3 - x**2 - x - 1 else: fac = (1 + cbrt(19 - 3 * sqrt(33)) + cbrt(19 + 3 * sqrt(33))) / 3 return _choose_factor(factors, x, fac, dom=dom) if hasattr(dom, 'symbols') and ex in dom.symbols: return x - ex if dom.is_QQ and _is_sum_surds(ex): # eliminate the square roots ex -= x while 1: ex1 = _separate_sq(ex) if ex1 is ex: return ex else: ex = ex1 if ex.is_Add: res = _minpoly_add(x, dom, *ex.args) elif ex.is_Mul: f = Factors(ex).factors r = sift(f.items(), lambda itx: itx[0].is_Rational and itx[1].is_Rational) if r[True] and dom == QQ: ex1 = Mul(*[bx**ex for bx, ex in r[False] + r[None]]) r1 = dict(r[True]) dens = [y.q for y in r1.values()] lcmdens = reduce(lcm, dens, 1) neg1 = S.NegativeOne expn1 = r1.pop(neg1, S.Zero) nums = [base**(y.p * lcmdens // y.q) for base, y in r1.items()] ex2 = Mul(*nums) mp1 = minimal_polynomial(ex1, x) # use the fact that in SymPy canonicalization products of integers # raised to rational powers are organized in relatively prime # bases, and that in ``base**(n/d)`` a perfect power is # simplified with the root # Powers of -1 have to be treated separately to preserve sign. mp2 = ex2.q * x**lcmdens - ex2.p * neg1**(expn1 * lcmdens) ex2 = neg1**expn1 * ex2**Rational(1, lcmdens) res = _minpoly_op_algebraic_element(Mul, ex1, ex2, x, dom, mp1=mp1, mp2=mp2) else: res = _minpoly_mul(x, dom, *ex.args) elif ex.is_Pow: res = _minpoly_pow(ex.base, ex.exp, x, dom) elif ex.__class__ is sin: res = _minpoly_sin(ex, x) elif ex.__class__ is cos: res = _minpoly_cos(ex, x) elif ex.__class__ is tan: res = _minpoly_tan(ex, x) elif ex.__class__ is exp: res = _minpoly_exp(ex, x) elif ex.__class__ is CRootOf: res = _minpoly_rootof(ex, x) else: raise NotAlgebraic("%s does not seem to be an algebraic element" % ex) return res
while remainder: lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder) x, lastx = lastx - quotient * x, x y, lasty = lasty - quotient * y, y return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) def modinv(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise ValueError return x % m n1 = 924506488821656685683910901697171383575761384058997452768161613244316449994435541406042874502024337501621283644549497446327156438552952982774526792356194523541927862677535193330297876054850415513120023262998063090052673978470859715791539316871 n2 = 88950937117255391223977435698486265468789676087383749025900580476857958577458361251855358598960638495873663408330100969812759959637583297211068274793121379054729169786199319454344007481804946263873110263761707375758247409 n3 = 46120424124283407631877739918717497745499448442081604908717069311339764302716539899549382470988469546914660420190473379187397425725302899111432304753418508501904277711772373006543099077921097373552317823052570252978144835744949941108416471431004677 c1 = 388825822870813587493154615238012547494666151428446904627095554917874019374474234421038941934804209410745453928513883448152675699305596595130706561989245940306390625802518940063853046813376063232724848204735684760377804361178651844505881089386 c2 = 4132099145786478580573701281040504422332184017792293421890701268012883566853254627860193724809808999005233349057847375798626123207766954266507411969802654226242300965967704040276250440511648395550180630597000941240639594 c3 = 43690392479478733802175619151519523453201200942800536494806512990350504964044289998495399805335942227586694852363272883331080188161308470522306485983861114557449204887644890409995598852299488628159224012730372865280540944897915435604154376354144428 e = 3 N = n1 * n2 * n3 pt_cubed = crt([n3, n1, n2], [c3, c1, c2])[0] pt = cbrt(pt_cubed) flag = str(long_to_bytes(pt)[::-1]) flag = flag[2:-1] print(flag)
def test_minimal_polynomial(): assert minimal_polynomial(-7, x) == x + 7 assert minimal_polynomial(-1, x) == x + 1 assert minimal_polynomial(0, x) == x assert minimal_polynomial(1, x) == x - 1 assert minimal_polynomial(7, x) == x - 7 assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(5), x) == x**2 - 5 assert minimal_polynomial(sqrt(6), x) == x**2 - 6 assert minimal_polynomial(2 * sqrt(2), x) == x**2 - 8 assert minimal_polynomial(3 * sqrt(5), x) == x**2 - 45 assert minimal_polynomial(4 * sqrt(6), x) == x**2 - 96 assert minimal_polynomial(2 * sqrt(2) + 3, x) == x**2 - 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) + 6, x) == x**2 - 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) + 7, x) == x**2 - 14 * x - 47 assert minimal_polynomial(2 * sqrt(2) - 3, x) == x**2 + 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) - 6, x) == x**2 + 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) - 7, x) == x**2 + 14 * x - 47 assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2 * x**2 - 5 assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10 * x**4 + 49 assert minimal_polynomial(2 * I + sqrt(2 + I), x) == x**4 + 4 * x**2 + 8 * x + 37 assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10 * x**2 + 1 assert minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6), x) == x**4 - 22 * x**2 - 48 * x - 23 a = 1 - 9 * sqrt(2) + 7 * sqrt(3) assert minimal_polynomial( 1 / a, x) == 392 * x**4 - 1232 * x**3 + 612 * x**2 + 4 * x - 1 assert minimal_polynomial( 1 / sqrt(a), x) == 392 * x**8 - 1232 * x**6 + 612 * x**4 + 4 * x**2 - 1 raises(NotAlgebraic, lambda: minimal_polynomial(oo, x)) raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x)) raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x)) assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2) assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2) assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2, domain='QQ') assert minimal_polynomial(sqrt(2), x, polys=True, compose=False) == Poly(x**2 - 2, domain='QQ') a = AlgebraicNumber(sqrt(2)) b = AlgebraicNumber(sqrt(3)) assert minimal_polynomial(a, x) == x**2 - 2 assert minimal_polynomial(b, x) == x**2 - 3 assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2, domain='QQ') assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3, domain='QQ') assert minimal_polynomial(sqrt(a / 2 + 17), x) == 2 * x**4 - 68 * x**2 + 577 assert minimal_polynomial(sqrt(b / 2 + 17), x) == 4 * x**4 - 136 * x**2 + 1153 a, b = sqrt(2) / 3 + 7, AlgebraicNumber(sqrt(2) / 3 + 7) f = 81*x**8 - 2268*x**6 - 4536*x**5 + 22644*x**4 + 63216*x**3 - \ 31608*x**2 - 189648*x + 141358 assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f assert minimal_polynomial(a**Q(3, 2), x) == 729 * x**4 - 506898 * x**2 + 84604519 # issue 5994 eq = S(''' -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)))''') assert minimal_polynomial(eq, x) == 8000 * x**2 - 1 ex = (sqrt(5) * sqrt(I) / (5 * sqrt(1 + 125 * I)) + 25 * sqrt(5) / (I**Q(5, 2) * (1 + 125 * I)**Q(3, 2)) + 3125 * sqrt(5) / (I**Q(11, 2) * (1 + 125 * I)**Q(3, 2)) + 5 * I * sqrt(1 - I / 125)) mp = minimal_polynomial(ex, x) assert mp == 25 * x**4 + 5000 * x**2 + 250016 ex = 1 + sqrt(2) + sqrt(3) mp = minimal_polynomial(ex, x) assert mp == x**4 - 4 * x**3 - 4 * x**2 + 16 * x - 8 ex = 1 / (1 + sqrt(2) + sqrt(3)) mp = minimal_polynomial(ex, x) assert mp == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1 p = (expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3))**Rational(1, 3) mp = minimal_polynomial(p, x) assert mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008 p = expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(p, x) assert mp == x**8 - 512 * x**7 - 118208 * x**6 + 31131136 * x**5 + 647362560 * x**4 - 56026611712 * x**3 + 116994310144 * x**2 + 404854931456 * x - 27216576512 assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"), x) == x - 1 a = 1 + sqrt(2) assert minimal_polynomial((a * sqrt(2) + a)**3, x) == x**2 - 198 * x + 1 p = 1 / (1 + sqrt(2) + sqrt(3)) assert minimal_polynomial( p, x, compose=False) == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1 p = 2 / (1 + sqrt(2) + sqrt(3)) assert minimal_polynomial( p, x, compose=False) == x**4 - 4 * x**3 + 2 * x**2 + 4 * x - 2 assert minimal_polynomial(1 + sqrt(2) * I, x, compose=False) == x**2 - 2 * x + 3 assert minimal_polynomial(1 / (1 + sqrt(2)) + 1, x, compose=False) == x**2 - 2 assert minimal_polynomial(sqrt(2) * I + I * (1 + sqrt(2)), x, compose=False) == x**4 + 18 * x**2 + 49 # minimal polynomial of I assert minimal_polynomial(I, x, domain=QQ.algebraic_field(I)) == x - I K = QQ.algebraic_field(I * (sqrt(2) + 1)) assert minimal_polynomial(I, x, domain=K) == x - I assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1 assert minimal_polynomial(I, x, domain='QQ(y)') == x**2 + 1 #issue 11553 assert minimal_polynomial(GoldenRatio, x) == x**2 - x - 1 assert minimal_polynomial(TribonacciConstant + 3, x) == x**3 - 10 * x**2 + 32 * x - 34 assert minimal_polynomial(GoldenRatio, x, domain=QQ.algebraic_field(sqrt(5))) == \ 2*x - sqrt(5) - 1 assert minimal_polynomial(TribonacciConstant, x, domain=QQ.algebraic_field(cbrt(19 - 3*sqrt(33)))) == \ 48*x - 19*(19 - 3*sqrt(33))**Rational(2, 3) - 3*sqrt(33)*(19 - 3*sqrt(33))**Rational(2, 3) \ - 16*(19 - 3*sqrt(33))**Rational(1, 3) - 16 # AlgebraicNumber with an alias. # Wester H24 phi = AlgebraicNumber(S.GoldenRatio.expand(func=True), alias='phi') assert minimal_polynomial(phi, x) == x**2 - x - 1