def test_roots_preprocessed(): E, F, J, L = symbols('E,F,J,L') f = -21601054687500000000*E**8*J**8/L**16 + \ 508232812500000000*F*x*E**7*J**7/L**14 - \ 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \ 16194716250000*E**5*F**3*J**5*x**3/L**10 - \ 27633173750*E**4*F**4*J**4*x**4/L**8 + \ 14840215*E**3*F**5*J**3*x**5/L**6 + \ 54794*E**2*F**6*J**2*x**6/(5*L**4) - \ 1153*E*J*F**7*x**7/(80*L**2) + \ 633*F**8*x**8/160000 assert roots(f, x) == {} R1 = roots(f.evalf(strict=False), x, multiple=True) R2 = [ -1304.88375606366, 97.1168816800648, 186.946430171876, 245.526792947065, 503.441004174773, 791.549343830097, 1273.16678129348, 1850.10650616851 ] w = Wild('w') p = w * E * J / (F * L**2) assert len(R1) == len(R2) for r1, r2 in zip(R1, R2): match = r1.match(p) assert match is not None and abs(match[w] - r2) < 1e-10
def test_roots_binomial(): assert roots_binomial((5 * x).as_poly()) == [0] assert roots_binomial((5 * x**4).as_poly()) == [0, 0, 0, 0] assert roots_binomial((5 * x + 2).as_poly()) == [-Rational(2, 5)] A = 10**Rational(3, 4) / 10 assert roots_binomial((5*x**4 + 2).as_poly()) == \ [-A - A*I, -A + A*I, A - A*I, A + A*I] a1 = Symbol('a1', nonnegative=True) b1 = Symbol('b1', nonnegative=True) r0 = roots_quadratic((a1 * x**2 + b1).as_poly(x)) r1 = roots_binomial((a1 * x**2 + b1).as_poly(x)) assert powsimp(r0[0]) == powsimp(r1[0]) assert powsimp(r0[1]) == powsimp(r1[1]) for a, b, s, n in itertools.product((1, 2), (1, 2), (-1, 1), (2, 3, 4, 5)): if a == b and a != 1: # a == b == 1 is sufficient continue p = (a * x**n + s * b).as_poly() ans = roots_binomial(p) assert ans == _nsort(ans) # issue sympy/sympy#8813 assert roots((2 * x**3 - 16 * y**3).as_poly(x)) == { 2 * y * (-Rational(1, 2) - sqrt(3) * I / 2): 1, 2 * y: 1, 2 * y * (-Rational(1, 2) + sqrt(3) * I / 2): 1 } p = (exp(I * x / 3)**4 + exp(I * x / 3)).as_poly(exp(I * x / 3)) assert roots(p) == roots(x**4 + x)
def test_roots_inexact(): R1 = roots(x**2 + x + 1, x, multiple=True) R2 = roots(x**2 + x + 1.0, x, multiple=True) for r1, r2 in zip(R1, R2): assert abs(r1 - r2) < 1e-12 f = x**4 + 3.0*sqrt(2.0)*x**3 - (78.0 + 24.0*sqrt(3.0))*x**2 \ + 144.0*(2*sqrt(3.0) + 9.0) R1 = roots(f, multiple=True) R2 = (-12.7530479110482, -3.85012393732929, 4.89897948556636, 7.46155167569183) for r1, r2 in zip(R1, R2): assert abs(r1 - r2) < 1e-10
def test_roots_mixed(): f = -1936 - 5056 * x - 7592 * x**2 + 2704 * x**3 - 49 * x**4 _re, _im = [], [] p = Poly(f) for r in p.all_roots(): c, (r, ) = r.as_coeff_mul() if r.is_real: r = r.interval _re.append((c * QQ.to_expr(r.a), c * QQ.to_expr(r.b))) else: r = r.interval _im.append((c * QQ.to_expr(r.ax) + c * I * QQ.to_expr(r.ay), c * QQ.to_expr(r.bx) + c * I * QQ.to_expr(r.by))) _nroots = nroots(f) _sroots = roots(f, multiple=True) _re = [Interval(a, b) for (a, b) in _re] _im = [Interval(re(a), re(b)) * Interval(im(a), im(b)) for (a, b) in _im] _intervals = _re + _im _sroots = [r.evalf() for r in _sroots] _nroots = sorted(_nroots, key=lambda x: x.sort_key()) _sroots = sorted(_sroots, key=lambda x: x.sort_key()) for _roots in (_nroots, _sroots): for i, r in zip(_intervals, _roots): if r.is_extended_real: assert r in i else: assert (re(r), im(r)) in i
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic(Poly(x**3 + 2 * a / 27, x)) assert res == [ -root(a + sqrt(a**2), 3) / 3, Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False), Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False) ]
def test_sympyissue_21263(): e = x**3 + 3 * x**2 + 3 * x + y + 1 r = roots(e, x) assert r == { -root(y, 3) - 1: 1, -root(y, 3) * (-Rational(1, 2) - sqrt(3) * I / 2) - 1: 1, -root(y, 3) * (-Rational(1, 2) + sqrt(3) * I / 2) - 1: 1 }
def test_roots1(): assert roots(1) == {} assert roots(1, multiple=True) == [] q = Symbol('q', real=True) assert roots(x**3 - q, x) == { cbrt(q): 1, -cbrt(q) / 2 - sqrt(3) * I * cbrt(q) / 2: 1, -cbrt(q) / 2 + sqrt(3) * I * cbrt(q) / 2: 1 } assert roots_cubic(Poly(x**3 - 1)) == [ 1, Rational(-1, 2) + sqrt(3) * I / 2, Rational(-1, 2) - sqrt(3) * I / 2 ] assert roots([1, x, y]) == { -x / 2 - sqrt(x**2 - 4 * y) / 2: 1, -x / 2 + sqrt(x**2 - 4 * y) / 2: 1 } pytest.raises(ValueError, lambda: roots([1, x, y], z))
def test_roots_slow(): """Just test that calculating these roots does not hang.""" a, b, c, d, x = symbols('a,b,c,d,x') f1 = x**2 * c + (a / b) + x * c * d - a f2 = x**2 * (a + b * (c - d) * a) + x * a * b * c / (b * d - d) + (a * d - c / d) assert list(roots(f1, x).values()) == [1, 1] assert list(roots(f2, x).values()) == [1, 1] zz, yy, xx, zy, zx, yx, k = symbols('zz,yy,xx,zy,zx,yx,k') e1 = (zz - k) * (yy - k) * (xx - k) + zy * yx * zx + zx - zy - yx e2 = (zz - k) * yx * yx + zx * (yy - k) * zx + zy * zy * (xx - k) assert list(roots(e1 - e2, k).values()) == [1, 1, 1] f = x**3 + 2 * x**2 + 8 R = list(roots(f)) assert not any(i for i in [f.subs({x: ri}).evalf(chop=True) for ri in R])
def test_legendre(): pytest.raises(ValueError, lambda: legendre(-1, x)) assert legendre(0, x) == 1 assert legendre(1, x) == x assert legendre(2, x) == ((3 * x**2 - 1) / 2).expand() assert legendre(3, x) == ((5 * x**3 - 3 * x) / 2).expand() assert legendre(4, x) == ((35 * x**4 - 30 * x**2 + 3) / 8).expand() assert legendre(5, x) == ((63 * x**5 - 70 * x**3 + 15 * x) / 8).expand() assert legendre(6, x) == ((231 * x**6 - 315 * x**4 + 105 * x**2 - 5) / 16).expand() assert legendre(10, -1) == 1 assert legendre(11, -1) == -1 assert legendre(10, 1) == 1 assert legendre(11, 1) == 1 assert legendre(10, 0) != 0 assert legendre(11, 0) == 0 assert roots(legendre(4, x), x) == { sqrt(Rational(3, 7) - Rational(2, 35) * sqrt(30)): 1, -sqrt(Rational(3, 7) - Rational(2, 35) * sqrt(30)): 1, sqrt(Rational(3, 7) + Rational(2, 35) * sqrt(30)): 1, -sqrt(Rational(3, 7) + Rational(2, 35) * sqrt(30)): 1, } X = legendre(n, x) assert isinstance(X, legendre) assert legendre(-n, x) == legendre(n - 1, x) assert legendre(n, -x) == (-1)**n * legendre(n, x) assert legendre(n, 0) == sqrt(pi) / (gamma(S.Half - n / 2) * gamma(1 + n / 2)) assert legendre(n, 1) == 1 assert legendre(n, oo) == oo assert conjugate(legendre(n, x)) == legendre(n, conjugate(x)) assert diff(legendre(n, x), x) == \ n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1) assert diff(legendre(n, x), n) == Derivative(legendre(n, x), n)
def test_legendre(): pytest.raises(ValueError, lambda: legendre(-1, x)) assert legendre(0, x) == 1 assert legendre(1, x) == x assert legendre(2, x) == ((3*x**2 - 1)/2).expand() assert legendre(3, x) == ((5*x**3 - 3*x)/2).expand() assert legendre(4, x) == ((35*x**4 - 30*x**2 + 3)/8).expand() assert legendre(5, x) == ((63*x**5 - 70*x**3 + 15*x)/8).expand() assert legendre(6, x) == ((231*x**6 - 315*x**4 + 105*x**2 - 5)/16).expand() assert legendre(10, -1) == 1 assert legendre(11, -1) == -1 assert legendre(10, 1) == 1 assert legendre(11, 1) == 1 assert legendre(10, 0) != 0 assert legendre(11, 0) == 0 assert roots(legendre(4, x), x) == { sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1, -sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1, sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1, -sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1, } X = legendre(n, x) assert isinstance(X, legendre) assert legendre(-n, x) == legendre(n - 1, x) assert legendre(n, -x) == (-1)**n*legendre(n, x) assert legendre(n, 0) == sqrt(pi)/(gamma(Rational(1, 2) - n/2)*gamma(1 + n/2)) assert legendre(n, 1) == 1 assert legendre(n, oo) == oo assert conjugate(legendre(n, x)) == legendre(n, conjugate(x)) assert diff(legendre(n, x), x) == \ n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1) assert diff(legendre(n, x), n) == Derivative(legendre(n, x), n)
def test_roots_cubic(): assert roots_cubic((2 * x**3).as_poly()) == [0, 0, 0] assert roots_cubic((x**3 - 3 * x**2 + 3 * x - 1).as_poly()) == [1, 1, 1] assert roots_cubic((x**3 + 1).as_poly()) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic((2*x**3 - 3*x**2 - 3*x - 1).as_poly())[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(eq.as_poly(), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic((x**3 + 2 * a / 27).as_poly(x)) assert res == [ -root(2, 3) * root(a, 3) / 3, -root(2, 3) * root(a, 3) * (-Rational(1, 2) + sqrt(3) * I / 2) / 3, -root(2, 3) * root(a, 3) * (-Rational(1, 2) - sqrt(3) * I / 2) / 3 ] res = roots_cubic((x**3 - 2 * a / 27).as_poly(x)) assert res == [ root(2, 3) * root(a, 3) / 3, root(2, 3) * root(a, 3) * (-Rational(1, 2) + sqrt(3) * I / 2) / 3, root(2, 3) * root(a, 3) * (-Rational(1, 2) - sqrt(3) * I / 2) / 3 ] # issue sympy/sympy#8438 p = -3 * x**3 - 2 * x**2 + x * y + 1 croots = roots_cubic(p.as_poly(x), x) z = -Rational(3, 2) - 7 * I / 2 # this will fail in code given in commit msg post = [r.subs({y: z}) for r in croots] assert set(post) == set(roots_cubic(p.subs({y: z}).as_poly(x))) # /!\ if p is not made an expression, this is *very* slow assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic(Poly(x**3 + 2 * a / 27, x)) assert res == [ -root(a + sqrt(a**2), 3) / 3, Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False), Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False) ] # issue sympy/sympy#8438 p = Poly([1, y, -2, -3], x).as_expr() croots = roots_cubic(Poly(p, x), x) z = -Rational(3, 2) - 7 * I / 2 # this will fail in code given in commit msg post = [r.subs({y: z}) for r in croots] assert set(post) == set(roots_cubic(Poly(p.subs({y: z}), x))) # /!\ if p is not made an expression, this is *very* slow assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
def test_roots_mixed(): f = -1936 - 5056 * x - 7592 * x**2 + 2704 * x**3 - 49 * x**4 _re, _im = intervals(f, all=True) _nroots = nroots(f) _sroots = roots(f, multiple=True) _re = [Interval(a, b) for (a, b), _ in _re] _im = [ Interval(re(a), re(b)) * Interval(im(a), im(b)) for (a, b), _ in _im ] _intervals = _re + _im _sroots = [r.evalf() for r in _sroots] _nroots = sorted(_nroots, key=lambda x: x.sort_key()) _sroots = sorted(_sroots, key=lambda x: x.sort_key()) for _roots in (_nroots, _sroots): for i, r in zip(_intervals, _roots): if r.is_extended_real: assert r in i else: assert (re(r), im(r)) in i
def test_Domain__algebraic_field(): alg = QQ.algebraic_field(sqrt(3)) assert alg.minpoly == (x**2 - 3).as_poly() assert alg.domain == QQ assert alg.from_expr(sqrt(3)).denominator == 1 assert alg.from_expr(2*sqrt(3)).denominator == 1 assert alg.from_expr(sqrt(3)/2).denominator == 2 assert alg([QQ(3, 2), QQ(7, 38)]).denominator == 38 alg = QQ.algebraic_field(sqrt(2)) assert alg.minpoly == (x**2 - 2).as_poly() assert alg.domain == QQ alg = QQ.algebraic_field(sqrt(2), sqrt(3)) assert alg.minpoly == (x**4 - 10*x**2 + 1).as_poly() assert alg.domain == QQ assert alg(1).numerator == alg(1) assert alg.from_expr(sqrt(3)/2).numerator == alg.from_expr(2*sqrt(3)) assert alg.from_expr(sqrt(3)/2).denominator == 4 pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2))) assert alg.characteristic == 0 assert alg.inject(x).characteristic == 0 assert alg.inject(x).field.characteristic == 0 assert alg.is_RealAlgebraicField is True assert int(alg(2)) == 2 assert int(alg.from_expr(Rational(3, 2))) == 1 alg = QQ.algebraic_field(I) assert alg.algebraic_field(I) == alg assert alg.is_RealAlgebraicField is False pytest.raises(TypeError, lambda: int(alg([1, 1]))) alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3)) assert alg.minpoly == (x**2 - 3).as_poly(x, domain=QQ.algebraic_field(sqrt(2))) # issue sympy/sympy#14476 assert QQ.algebraic_field(Rational(1, 7)) is QQ alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg.from_expr(2*sqrt(2) + I/3) == alg([alg.domain([0, 2]), alg.domain([1])/3]) alg2 = QQ.algebraic_field(sqrt(2)) assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2))) eq = -x**3 + 2*x**2 + 3*x - 2 rs = roots(eq, multiple=True) alg = QQ.algebraic_field(rs[0]) assert alg.is_RealAlgebraicField alg1 = QQ.algebraic_field(I) alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg1 != alg2 alg3 = QQ.algebraic_field(RootOf(4*x**7 + x - 1, 0)) assert alg3.is_RealAlgebraicField assert int(alg3.unit) == 1 assert 1.386 > alg3.unit > 1.385 assert int(alg3([2, -2, 44, 136, 48])) == 622 assert int(alg3([QQ(2331359268715, 10459004949272), QQ(-33484303756044, 12894796053515), QQ(501307906922072, 44208605852241), -22, 16])) == 18 alg4 = QQ.algebraic_field(sqrt(2) + I) assert alg4.convert(alg2.unit) == alg4.from_expr(I) assert QQ.algebraic_field(im(1/((1 + I)**2)) + re(1/((1 + I)**2))) == QQ assert alg1.from_expr(im(1/((1 + I)**2))) == alg1.from_expr(-Rational(1, 2))
def test_sympyissue_16589(): e = x**4 - 8 * sqrt(2) * x**3 + 4 * x**3 - 64 * sqrt(2) * x**2 + 1024 * x rs = roots(e, x) assert 0 in rs assert all(not e.evalf(chop=True, subs={x: r}) for r in rs)
def test_sympyissue_7724(): e = x**4 * I + x**2 + I r1, r2 = roots(e, x), Poly(e, x).all_roots() assert len(r1) == 4 assert {_.evalf() for _ in r1} == {_.evalf() for _ in r2}
def test_roots_composite(): assert len(roots(Poly(y**3 + y**2 * sqrt(x) + y + x, y, composite=True))) == 3
def log_to_real(h, q, x, t): """ Convert complex logarithms to real functions. Given real field K and polynomials h in K[t,x] and q in K[t], returns real function f such that: ___ df d \ ` -- = -- ) a log(h(a, x)) dx dx /__, a | q(a) = 0 Examples ======== >>> from diofant.integrals.rationaltools import log_to_real >>> from diofant.abc import x, y >>> from diofant import Poly, sqrt, Rational >>> log_to_real(Poly(x + 3*y/2 + Rational(1, 2), x, domain='QQ[y]'), ... Poly(3*y**2 + 1, y, domain='ZZ'), x, y) 2*sqrt(3)*atan(2*sqrt(3)*x/3 + sqrt(3)/3)/3 >>> log_to_real(Poly(x**2 - 1, x, domain='ZZ'), ... Poly(-2*y + 1, y, domain='ZZ'), x, y) log(x**2 - 1)/2 See Also ======== log_to_atan """ from diofant import collect u, v = symbols('u,v', cls=Dummy) H = h.as_expr().subs({t: u + I * v}).expand() Q = q.as_expr().subs({t: u + I * v}).expand() H_map = collect(H, I, evaluate=False) Q_map = collect(Q, I, evaluate=False) a, b = H_map.get(Integer(1), Integer(0)), H_map.get(I, Integer(0)) c, d = Q_map.get(Integer(1), Integer(0)), Q_map.get(I, Integer(0)) R = Poly(resultant(c, d, v), u) R_u = roots(R, filter='R') if len(R_u) != R.count_roots(): return result = Integer(0) for r_u in R_u.keys(): C = Poly(c.subs({u: r_u}), v) R_v = roots(C, filter='R') if len(R_v) != C.count_roots(): return for r_v in R_v: if not r_v.is_positive: continue D = d.subs({u: r_u, v: r_v}) if D.evalf(chop=True) != 0: continue A = Poly(a.subs({u: r_u, v: r_v}), x) B = Poly(b.subs({u: r_u, v: r_v}), x) AB = (A**2 + B**2).as_expr() result += r_u * log(AB) + r_v * log_to_atan(A, B) R_q = roots(q, filter='R') if len(R_q) != q.count_roots(): return for r in R_q.keys(): result += r * log(h.as_expr().subs(t, r)) return result
def test_Domain__algebraic_field(): alg = QQ.algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3) assert alg.domain == QQ assert alg.from_expr(sqrt(3)).denominator == 1 assert alg.from_expr(2 * sqrt(3)).denominator == 1 assert alg.from_expr(sqrt(3) / 2).denominator == 2 assert alg([QQ(3, 2), QQ(7, 38)]).denominator == 38 alg = QQ.algebraic_field(sqrt(2)) assert alg.minpoly == Poly(x**2 - 2) assert alg.domain == QQ alg = QQ.algebraic_field(sqrt(2), sqrt(3)) assert alg.minpoly == Poly(x**4 - 10 * x**2 + 1) assert alg.domain == QQ assert alg(1).numerator == alg(1) assert alg.from_expr(sqrt(3) / 2).numerator == alg.from_expr(2 * sqrt(3)) assert alg.from_expr(sqrt(3) / 2).denominator == 4 pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2))) assert alg.characteristic == 0 assert alg.inject(x).characteristic == 0 assert alg.inject(x).field.characteristic == 0 assert alg.is_RealAlgebraicField is True assert int(alg(2)) == 2 assert int(alg.from_expr(Rational(3, 2))) == 1 alg = QQ.algebraic_field(I) assert alg.algebraic_field(I) == alg assert alg.is_RealAlgebraicField is False pytest.raises(TypeError, lambda: int(alg([1, 1]))) alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3, x, domain=QQ.algebraic_field(sqrt(2))) # issue sympy/sympy#14476 assert QQ.algebraic_field(Rational(1, 7)) is QQ alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg.from_expr(2 * sqrt(2) + I / 3) == alg( [alg.domain([0, 2]), alg.domain([1]) / 3]) alg2 = QQ.algebraic_field(sqrt(2)) assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2))) eq = -x**3 + 2 * x**2 + 3 * x - 2 rs = roots(eq, multiple=True) alg = QQ.algebraic_field(rs[0]) assert alg.is_RealAlgebraicField alg1 = QQ.algebraic_field(I) alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg1 != alg2 alg3 = QQ.algebraic_field(RootOf(4 * x**7 + x - 1, 0)) assert alg3.is_RealAlgebraicField assert int(alg3.unit) == 2 assert 2.772 > alg3.unit > 2.771 assert int(alg3([2, -1, 11, 17, 3])) == 622 assert int( alg3([ QQ(2331359268715, 10459004949272), QQ(-16742151878022, 12894796053515), QQ(125326976730518, 44208605852241), QQ(-11, 4), 1 ])) == 18 alg4 = QQ.algebraic_field(sqrt(2) + I) assert alg4.convert(alg2.unit) == alg4.from_expr(I)
def test_roots0(): assert roots(1, x) == {} assert roots(x, x) == {0: 1} assert roots(x**9, x) == {0: 9} assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x) == { -3: 1, 2: 1, 4: 1 } assert roots(x**2 - 2 * x + 1, x, auto=False) == {1: 2} assert roots(2 * x + 1, x) == {Rational(-1, 2): 1} assert roots((2 * x + 1)**2, x) == {Rational(-1, 2): 2} assert roots((2 * x + 1)**5, x) == {Rational(-1, 2): 5} assert roots((2 * x + 1)**10, x) == {Rational(-1, 2): 10} assert roots(x**4 - 1, x) == {I: 1, 1: 1, -1: 1, -I: 1} assert roots((x**4 - 1)**2, x) == {I: 2, 1: 2, -1: 2, -I: 2} assert roots(((2 * x - 3)**2).expand(), x) == {+Rational(3, 2): 2} assert roots(((2 * x + 3)**2).expand(), x) == {-Rational(3, 2): 2} assert roots(((2 * x - 3)**3).expand(), x) == {+Rational(3, 2): 3} assert roots(((2 * x + 3)**3).expand(), x) == {-Rational(3, 2): 3} assert roots(((2 * x - 3)**5).expand(), x) == {+Rational(3, 2): 5} assert roots(((2 * x + 3)**5).expand(), x) == {-Rational(3, 2): 5} assert roots(((a * x - b)**5).expand(), x) == {+b / a: 5} assert roots(((a * x + b)**5).expand(), x) == {-b / a: 5} assert roots(x**2 + (-a - 1) * x + a, x) == {a: 1, 1: 1} assert roots(x**4 - 2 * x**2 + 1, x) == {1: 2, -1: 2} assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \ {1: 2, -1 - sqrt(2): 1, 0: 2, -1 + sqrt(2): 1} assert roots(x**8 - 1, x) == { sqrt(2) / 2 + I * sqrt(2) / 2: 1, sqrt(2) / 2 - I * sqrt(2) / 2: 1, -sqrt(2) / 2 + I * sqrt(2) / 2: 1, -sqrt(2) / 2 - I * sqrt(2) / 2: 1, 1: 1, -1: 1, I: 1, -I: 1 } f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \ 224*x**7 - 384*x**8 - 64*x**9 assert roots(f) == { 0: 2, -2: 2, 2: 1, -Rational(7, 2): 1, -Rational(3, 2): 1, -Rational(1, 2): 1, Rational(3, 2): 1 } assert roots((a + b + c) * x - (a + b + c + d), x) == { (a + b + c + d) / (a + b + c): 1 } assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {} assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x, cubics=False) == { -3: 1, 2: 1, 4: 1 } assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \ {-3: 1, 2: 1, 4: 1, 5: 1} assert roots(x**3 + 2 * x**2 + 4 * x + 8, x) == { -2: 1, -2 * I: 1, 2 * I: 1 } assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \ {-2*I: 1, 2*I: 1, -2: 1} assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x) == \ {1: 1, 0: 1, -2: 1, -2*I: 1, 2*I: 1} r1_2, r1_3 = Rational(1, 2), Rational(1, 3) x0 = (3 * sqrt(33) + 19)**r1_3 x1 = 4 / x0 / 3 x2 = x0 / 3 x3 = sqrt(3) * I / 2 x4 = x3 - r1_2 x5 = -x3 - r1_2 assert roots(x**3 + x**2 - x + 1, x, cubics=True) == { -x1 - x2 - r1_3: 1, -x1 / x4 - x2 * x4 - r1_3: 1, -x1 / x5 - x2 * x5 - r1_3: 1, } f = (x**2 + 2 * x + 3).subs({x: 2 * x**2 + 3 * x}).subs({x: 5 * x - 4}) r13_20, r1_20 = [Rational(*r) for r in ((13, 20), (1, 20))] s2 = sqrt(2) assert roots(f, x) == { r13_20 + r1_20 * sqrt(1 - 8 * I * s2): 1, r13_20 - r1_20 * sqrt(1 - 8 * I * s2): 1, r13_20 + r1_20 * sqrt(1 + 8 * I * s2): 1, r13_20 - r1_20 * sqrt(1 + 8 * I * s2): 1, } f = x**4 + x**3 + x**2 + x + 1 r1_4, r1_8, r5_8 = [Rational(*r) for r in ((1, 4), (1, 8), (5, 8))] assert roots(f, x) == { -r1_4 + r1_4 * 5**r1_2 + I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1, -r1_4 + r1_4 * 5**r1_2 - I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1, -r1_4 - r1_4 * 5**r1_2 + I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1, -r1_4 - r1_4 * 5**r1_2 - I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1, } f = z**3 + (-2 - y) * z**2 + (1 + 2 * y - 2 * x**2) * z - y + 2 * x**2 assert roots(f, z) == { 1: 1, Rational(1, 2) + y / 2 + sqrt(1 - 2 * y + y**2 + 8 * x**2) / 2: 1, Rational(1, 2) + y / 2 - sqrt(1 - 2 * y + y**2 + 8 * x**2) / 2: 1, } assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x, cubics=False) == {} assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x, cubics=True) != {} assert roots(x**4 - 1, x, filter='Z') == {1: 1, -1: 1} assert roots(x**4 - 1, x, filter='R') == {1: 1, -1: 1} assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1} pytest.raises(ValueError, lambda: roots(x**4 - 1, x, filter='spam')) assert roots((x - 1) * (x + 1), x) == {1: 1, -1: 1} assert roots((x - 1) * (x + 1), x, predicate=lambda r: r.is_positive) == { 1: 1 } assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-1, 1] assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I] assert roots(x**3, x, multiple=True) == [0, 0, 0] assert roots(1234, x, multiple=True) == [] f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1 assert roots(f) == { -I * sin(pi / 7) + cos(pi / 7): 1, -I * sin(2 * pi / 7) - cos(2 * pi / 7): 1, -I * sin(3 * pi / 7) + cos(3 * pi / 7): 1, I * sin(pi / 7) + cos(pi / 7): 1, I * sin(2 * pi / 7) - cos(2 * pi / 7): 1, I * sin(3 * pi / 7) + cos(3 * pi / 7): 1, } g = ((x**2 + 1) * f**2).expand() assert roots(g) == { -I * sin(pi / 7) + cos(pi / 7): 2, -I * sin(2 * pi / 7) - cos(2 * pi / 7): 2, -I * sin(3 * pi / 7) + cos(3 * pi / 7): 2, I * sin(pi / 7) + cos(pi / 7): 2, I * sin(2 * pi / 7) - cos(2 * pi / 7): 2, I * sin(3 * pi / 7) + cos(3 * pi / 7): 2, -I: 1, I: 1, } r = roots(x**3 + 40 * x + 64) real_root = [rx for rx in r if rx.is_extended_real][0] cr = 108 + 6 * sqrt(1074) assert real_root == -2 * root(cr, 3) / 3 + 20 / root(cr, 3) eq = Poly((7 + 5 * sqrt(2)) * x**3 + (-6 - 4 * sqrt(2)) * x**2 + (-sqrt(2) - 1) * x + 2, x, domain='EX') assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2 * sqrt(2): 1, -sqrt(2) + 1: 1} eq = Poly(41 * x**5 + 29 * sqrt(2) * x**5 - 153 * x**4 - 108 * sqrt(2) * x**4 + 175 * x**3 + 125 * sqrt(2) * x**3 - 45 * x**2 - 30 * sqrt(2) * x**2 - 26 * sqrt(2) * x - 26 * x + 24, x, domain='EX') assert roots(eq) == { -sqrt(2) + 1: 1, -2 + 2 * sqrt(2): 1, -1 + sqrt(2): 1, -4 + 4 * sqrt(2): 1, -3 + 3 * sqrt(2): 1 } eq = Poly(x**3 - 2 * x**2 + 6 * sqrt(2) * x**2 - 8 * sqrt(2) * x + 23 * x - 14 + 14 * sqrt(2), x, domain='EX') assert roots(eq) == { -2 * sqrt(2) + 2: 1, -2 * sqrt(2) + 1: 1, -2 * sqrt(2) - 1: 1 } assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \ {-sqrt(2) - root(7, 3)/2 - sqrt(3)*root(7, 3)*I/2: 1, -sqrt(2) - root(7, 3)/2 + sqrt(3)*root(7, 3)*I/2: 1, -sqrt(2) + root(7, 3): 1} pytest.raises(PolynomialError, lambda: roots(x * y, x, y))