def test_roots2(): """Just test that calculating these roots does not hang (final result is not checked) """ 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 roots(f1, x).values() == [1, 1] assert roots(f2, x).values() == [1, 1]
def test_roots(): assert roots(1, x) == {} assert roots(x, x) == {S.Zero: 1} assert roots(x**9, x) == {S.Zero: 9} assert roots(2*x+1, x) == {-S.Half: 1} assert roots((2*x+1)**2, x) == {-S.Half: 2} assert roots((2*x+1)**5, x) == {-S.Half: 5} assert roots((2*x+1)**10, x) == {-S.Half: 10} assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1} assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 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**4-2*x**2+1, x) == {S.One: 2, -S.One: 2} assert roots(x**6-4*x**4+4*x**3-x**2, x) == \ {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1} assert roots(x**8-1, x) == { 2**S.Half/2 + I*2**S.Half/2: 1, 2**S.Half/2 - I*2**S.Half/2: 1, -2**S.Half/2 + I*2**S.Half/2: 1, -2**S.Half/2 - I*2**S.Half/2: 1, S.One: 1, -S.One: 1, I: 1, -I: 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) == {} r1_2, r1_3, r1_9, r4_9, r19_27 = [ Rational(*r) \ for r in ((1,2), (1,3), (1,9), (4,9), (19,27)) ] assert roots(x**3+x**2-x+1, x, cubics=True) in [ { -r1_3 - (r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3): 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 + r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) + \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) - \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, }, { -r1_3 - (r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3): 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9/(-r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) + \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) - \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, }, ] f = (x**2+2*x+3).subs(x, 2*x**2 + 3*x).subs(x, 5*x-4) r1_2, r2_25, r13_20, r1_100 = [ Rational(*r) \ for r in ((1,2), (2,25), (13,20), (1,100)) ] assert roots(f, x) == { r13_20 + r1_2*(r1_100 - r2_25*I*2**r1_2)**r1_2: 1, r13_20 - r1_2*(r1_100 - r2_25*I*2**r1_2)**r1_2: 1, r13_20 + r1_2*(r1_100 + r2_25*I*2**r1_2)**r1_2: 1, r13_20 - r1_2*(r1_100 + r2_25*I*2**r1_2)**r1_2: 1, } p = Poly(z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2, z) assert roots(p) == { S.One: 1, S.Half + S.Half*y + S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 1, S.Half + S.Half*y - S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 1, } assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {} assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \ {-2*I: 1, 2*I: 1, -Integer(2): 1} assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x) == {} assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=True) != {} assert roots(x**4-1, x, domain='Z') == {S.One: 1, -S.One: 1} assert roots(x**4-1, x, domain='I') == {I: 1, -I: 1} assert roots((x-1)*(x+1), x) == {S.One: 1, -S.One: 1} assert roots((x-1)*(x+1), x, predicate=lambda r: r.is_positive) == {S.One: 1} assert roots(x**4-1, x, domain='Z', multiple=True) == [S.One, -S.One] assert roots(x**4-1, x, domain='I', multiple=True) in ([I, -I], [-I, I]) assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero] assert roots(1234, x, multiple=True) == []
def test_roots(): assert roots(1, x) == {} assert roots([1]) == {} assert roots(x) == {S.Zero: 1} assert roots(x, x) == {S.Zero: 1} uv = Poly(x, x) mv = Poly(x + y, x, y) assert roots(uv) == {S.Zero: 1} assert roots(uv, x) == {S.Zero: 1} assert roots(mv, x) == {-y: 1} raises(SymbolsError,"roots([1],x)") raises(SymbolsError,"roots(x+y, x, y)") raises(SymbolsError,"roots(uv, x, y)") raises(SymbolsError,"roots(mv, x, y)") raises(MultivariatePolyError,"roots(mv)") assert roots(x**9, x) == {S.Zero: 9} assert roots(((x-2)*(x+3)*(x-4)).expand(), x) == {-S(3): 1, S(2): 1, S(4): 1} assert roots(2*x+1, x) == roots([2, 1]) assert roots([2, 1]) == {-S.Half: 1} assert roots((2*x+1)**2, x) == {-S.Half: 2} assert roots((2*x+1)**5, x) == {-S.Half: 5} assert roots((2*x+1)**10, x) == {-S.Half: 10} assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1} assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 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**4-2*x**2+1, x) == {S.One: 2, -S.One: 2} assert roots(x**6-4*x**4+4*x**3-x**2, x) == \ {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1} assert roots(x**8-1, x) == { 2**S.Half/2 + I*2**S.Half/2: 1, 2**S.Half/2 - I*2**S.Half/2: 1, -2**S.Half/2 + I*2**S.Half/2: 1, -2**S.Half/2 - I*2**S.Half/2: 1, S.One: 1, -S.One: 1, I: 1, -I: 1 } assert roots(-2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 \ - 224*x**7 - 384*x**8 - 64*x**9, x) == {S(0): 2, -S(2): 2, S(2): 1, -S(7)/2: 1,\ -S(3)/2: 1, -S(1)/2: 1, S(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) == {-S(3): 1, S(2): 1, S(4): 1} assert roots(((x-2)*(x+3)*(x-4)*(x-5)).expand(), x, cubics=False) == \ {-S(3): 1, S(2): 1, S(4): 1, S(5): 1} assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {-S(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, -S(2): 1} assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \ {S(1): 1, S(0): 1, -S(2): 1, -2*I: 1, 2*I: 1} r1_2, r1_3, r1_9, r4_9, r19_27 = [ Rational(*r) \ for r in ((1,2), (1,3), (1,9), (4,9), (19,27)) ] assert set(tmp.evalf() for tmp in roots(x**3+x**2-x+1, x, cubics=True)) == \ set(tmp.evalf() for tmp in [ -r1_3 - (r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3), -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 + r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) + \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) - \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 ]) f = (x**2+2*x+3).subs(x, 2*x**2 + 3*x).subs(x, 5*x-4) r1_2, r13_20, r1_100 = [ Rational(*r) \ for r in ((1,2), (13,20), (1,100)) ] ans = { r13_20 + r1_100*(25 - 200*I*2**r1_2)**r1_2: 1, r13_20 - r1_100*(25 - 200*I*2**r1_2)**r1_2: 1, r13_20 + r1_100*(25 + 200*I*2**r1_2)**r1_2: 1, r13_20 - r1_100*(25 + 200*I*2**r1_2)**r1_2: 1, } assert set(tmp.evalf() for tmp in roots(f, x).keys()) == \ set(tmp.evalf() for tmp in ans.keys()) p = Poly(z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2, z) ans = { S.One: 1, S.Half + S.Half*y + S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 1, S.Half + S.Half*y - S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 1, }.keys() assert all(tmp.expand() in ans for tmp in roots(p).keys()) 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, domain='Z') == {S.One: 1, -S.One: 1} assert roots(x**4-1, x, domain='I') == {I: 1, -I: 1} assert roots((x-1)*(x+1), x) == {S.One: 1, -S.One: 1} assert roots((x-1)*(x+1), x, predicate=lambda r: r.is_positive) == {S.One: 1} assert roots(x**4-1, x, domain='Z', multiple=True) == [S.One, -S.One] assert roots(x**4-1, x, domain='I', multiple=True) in ([I, -I], [-I, I]) assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero] assert roots(1234, x, multiple=True) == []
def test_roots(): assert roots(1, x) == {} assert roots(x, x) == {S.Zero: 1} assert roots(x**9, x) == {S.Zero: 9} assert roots(((x-2)*(x+3)*(x-4)).expand(), x) == {-S(3): 1, S(2): 1, S(4): 1} assert roots(2*x+1, x) == {-S.Half: 1} assert roots((2*x+1)**2, x) == {-S.Half: 2} assert roots((2*x+1)**5, x) == {-S.Half: 5} assert roots((2*x+1)**10, x) == {-S.Half: 10} assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1} assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 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**4-2*x**2+1, x) == {S.One: 2, -S.One: 2} assert roots(x**6-4*x**4+4*x**3-x**2, x) == \ {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1} assert roots(x**8-1, x) == { 2**S.Half/2 + I*2**S.Half/2: 1, 2**S.Half/2 - I*2**S.Half/2: 1, -2**S.Half/2 + I*2**S.Half/2: 1, -2**S.Half/2 - I*2**S.Half/2: 1, S.One: 1, -S.One: 1, I: 1, -I: 1 } assert roots(-2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 \ - 224*x**7 - 384*x**8 - 64*x**9, x) == {S(0): 2, -S(2): 2, S(2): 1, -S(7)/2: 1,\ -S(3)/2: 1, -S(1)/2: 1, S(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) == {-S(3): 1, S(2): 1, S(4): 1} assert roots(((x-2)*(x+3)*(x-4)*(x-5)).expand(), x, cubics=False) == \ {-S(3): 1, S(2): 1, S(4): 1, S(5): 1} assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {-S(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, -S(2): 1} assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \ {S(1): 1, S(0): 1, -S(2): 1, -2*I: 1, 2*I: 1} r1_2, r1_3, r1_9, r4_9, r19_27 = [ Rational(*r) \ for r in ((1,2), (1,3), (1,9), (4,9), (19,27)) ] assert roots(x**3+x**2-x+1, x, cubics=True) in [ { -r1_3 - (r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3): 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 + r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) + \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) - \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, }, { -r1_3 - (r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3): 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 - \ r4_9/(-r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) + \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, -r1_3 + r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3 + \ r4_9/(r1_2 - r1_2*I*3**r1_2)*(r19_27 + r1_9*3**r1_2*11**r1_2)**(-r1_3) - \ r1_2*I*3**r1_2*(r19_27 + r1_9*3**r1_2*11**r1_2)**r1_3: 1, }, ] f = (x**2+2*x+3).subs(x, 2*x**2 + 3*x).subs(x, 5*x-4) r1_2, r13_20, r1_100 = [ Rational(*r) \ for r in ((1,2), (13,20), (1,100)) ] assert roots(f, x) == { r13_20 + r1_100*(25 - 200*I*2**r1_2)**r1_2: 1, r13_20 - r1_100*(25 - 200*I*2**r1_2)**r1_2: 1, r13_20 + r1_100*(25 + 200*I*2**r1_2)**r1_2: 1, r13_20 - r1_100*(25 + 200*I*2**r1_2)**r1_2: 1, } p = Poly(z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2, z) assert roots(p) == { S.One: 1, S.Half + S.Half*y + S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 1, S.Half + S.Half*y - S.Half*(1 - 2*y + y**2 + 8*x**2)**S.Half: 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, domain='Z') == {S.One: 1, -S.One: 1} assert roots(x**4-1, x, domain='I') == {I: 1, -I: 1} assert roots((x-1)*(x+1), x) == {S.One: 1, -S.One: 1} assert roots((x-1)*(x+1), x, predicate=lambda r: r.is_positive) == {S.One: 1} assert roots(x**4-1, x, domain='Z', multiple=True) == [S.One, -S.One] assert roots(x**4-1, x, domain='I', multiple=True) in ([I, -I], [-I, I]) assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero] assert roots(1234, x, multiple=True) == []