Exemplo n.º 1
0
def test_garbage_input():
    raises(ValueError, lambda: solveset_real([x], x))
    raises(ValueError, lambda: solveset_real(x, pi))
    raises(ValueError, lambda: solveset_real(x, x**2))

    raises(ValueError, lambda: solveset_complex([x], x))
    raises(ValueError, lambda: solveset_complex(x, pi))
Exemplo n.º 2
0
def test_solve_complex_sqrt():
    assert solveset_complex(sqrt(5*x + 6) - 2 - x, x) == \
        FiniteSet(-S(1), S(2))
    assert solveset_complex(sqrt(5*x + 6) - (2 + 2*I) - x, x) == \
        FiniteSet(-S(2), 3 - 4*I)
    assert solveset_complex(4*x*(1 - a * sqrt(x)), x) == \
        FiniteSet(S(0), 1 / a ** 2)
Exemplo n.º 3
0
def test_garbage_input():
    raises(ValueError, lambda: solveset_real([x], x))
    raises(ValueError, lambda: solveset_real(x, pi))
    raises(ValueError, lambda: solveset_real(x, x ** 2))

    raises(ValueError, lambda: solveset_complex([x], x))
    raises(ValueError, lambda: solveset_complex(x, pi))
Exemplo n.º 4
0
def test_solve_complex_sqrt():
    assert solveset_complex(sqrt(5*x + 6) - 2 - x, x) == \
        FiniteSet(-S(1), S(2))
    assert solveset_complex(sqrt(5*x + 6) - (2 + 2*I) - x, x) == \
        FiniteSet(-S(2), 3 - 4*I)
    assert solveset_complex(4*x*(1 - a * sqrt(x)), x) == \
        FiniteSet(S(0), 1 / a ** 2)
Exemplo n.º 5
0
def test_solveset_complex_exp():
    from sympy.abc import x, n

    assert solveset_complex(exp(x) - 1, x) == imageset(Lambda(n, I * 2 * n * pi), S.Integers)
    assert solveset_complex(exp(x) - I, x) == imageset(Lambda(n, I * (2 * n * pi + pi / 2)), S.Integers)
    assert solveset_complex(1 / exp(x), x) == S.EmptySet
    assert solveset_complex(sinh(x).rewrite(exp), x) == imageset(Lambda(n, n * pi * I), S.Integers)
Exemplo n.º 6
0
def test_solveset_complex_rational():
    assert solveset_complex((x - 1)*(x - I)/(x - 3), x) == \
        FiniteSet(1, I)

    assert solveset_complex((x - y**3)/((y**2)*sqrt(1 - y**2)), x) == \
        FiniteSet(y**3)
    assert solveset_complex(-x**2 - I, x) == \
        FiniteSet(-sqrt(2)/2 + sqrt(2)*I/2, sqrt(2)/2 - sqrt(2)*I/2)
Exemplo n.º 7
0
def test_solveset_complex_rational():
    assert solveset_complex((x - 1)*(x - I)/(x - 3), x) == \
        FiniteSet(1, I)

    assert solveset_complex((x - y**3)/((y**2)*sqrt(1 - y**2)), x) == \
        FiniteSet(y**3)
    assert solveset_complex(-x**2 - I, x) == \
        FiniteSet(-sqrt(2)/2 + sqrt(2)*I/2, sqrt(2)/2 - sqrt(2)*I/2)
Exemplo n.º 8
0
def test_solveset_complex_exp():
    from sympy.abc import x, n
    assert solveset_complex(exp(x) - 1, x) == \
        imageset(Lambda(n, I*2*n*pi), S.Integers)
    assert solveset_complex(exp(x) - I, x) == \
        imageset(Lambda(n, I*(2*n*pi + pi/2)), S.Integers)
    assert solveset_complex(1 / exp(x), x) == S.EmptySet
    assert solveset_complex(sinh(x).rewrite(exp), x) == \
        imageset(Lambda(n, n*pi*I), S.Integers)
Exemplo n.º 9
0
def test_solve_polynomial_symbolic_param():
    assert solveset_complex((x**2 - 1)**2 - a, x) == \
        FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
                  sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a)))

    # issue 4507
    assert solveset_complex(y - b/(1 + a*x), x) == \
        FiniteSet((b/y - 1)/a) - FiniteSet(-1/a)

    # issue 4508
    assert solveset_complex(y - b*x/(a + x), x) == \
        FiniteSet(-a*y/(y - b)) - FiniteSet(-a)
Exemplo n.º 10
0
def test_solve_polynomial_symbolic_param():
    assert solveset_complex((x**2 - 1)**2 - a, x) == \
        FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
                  sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a)))

    # issue 4507
    assert solveset_complex(y - b/(1 + a*x), x) == \
        FiniteSet((b/y - 1)/a) - FiniteSet(-1/a)

    # issue 4508
    assert solveset_complex(y - b*x/(a + x), x) == \
        FiniteSet(-a*y/(y - b)) - FiniteSet(-a)
Exemplo n.º 11
0
def test_solve_quintics():
    skip("This test is too slow")
    f = x**5 - 110 * x**3 - 55 * x**2 + 2310 * x + 979
    s = solveset_complex(f, x)
    for root in s:
        res = f.subs(x, root.n()).n()
        assert tn(res, 0)

    f = x**5 + 15 * x + 12
    s = solveset_complex(f, x)
    for root in s:
        res = f.subs(x, root.n()).n()
        assert tn(res, 0)
Exemplo n.º 12
0
def test_solve_quintics():
    skip("This test is too slow")
    f = x**5 - 110*x**3 - 55*x**2 + 2310*x + 979
    s = solveset_complex(f, x)
    for root in s:
        res = f.subs(x, root.n()).n()
        assert tn(res, 0)

    f = x**5 + 15*x + 12
    s = solveset_complex(f, x)
    for root in s:
        res = f.subs(x, root.n()).n()
        assert tn(res, 0)
Exemplo n.º 13
0
def test_solveset_complex_polynomial():
    from sympy.abc import x, a, b, c
    assert solveset_complex(a*x**2 + b*x + c, x) == \
        FiniteSet(-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a),
                  -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a))

    assert solveset_complex(x - y**3, y) == FiniteSet(
        (-x**Rational(1, 3)) / 2 + I * sqrt(3) * x**Rational(1, 3) / 2,
        x**Rational(1, 3),
        (-x**Rational(1, 3)) / 2 - I * sqrt(3) * x**Rational(1, 3) / 2)

    assert solveset_complex(x + 1/x - 1, x) == \
        FiniteSet(Rational(1, 2) + I*sqrt(3)/2, Rational(1, 2) - I*sqrt(3)/2)
Exemplo n.º 14
0
def test_solveset_complex_polynomial():
    from sympy.abc import x, a, b, c
    assert solveset_complex(a*x**2 + b*x + c, x) == \
        FiniteSet(-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a),
                  -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a))

    assert solveset_complex(x - y**3, y) == FiniteSet(
        (-x**Rational(1, 3))/2 + I*sqrt(3)*x**Rational(1, 3)/2,
        x**Rational(1, 3),
        (-x**Rational(1, 3))/2 - I*sqrt(3)*x**Rational(1, 3)/2)

    assert solveset_complex(x + 1/x - 1, x) == \
        FiniteSet(Rational(1, 2) + I*sqrt(3)/2, Rational(1, 2) - I*sqrt(3)/2)
Exemplo n.º 15
0
def test_solve_sqrt_3():
    R = Symbol('R')
    eq = sqrt(2) * R * sqrt(1 /
                            (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 /
                                                                 (R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(*[
        S(5) / 3 + 4 * sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) /
        3, -sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 +
        40 * re(1 / ((-S(1) / 2 - sqrt(3) * I / 2) *
                     (S(251) / 27 + sqrt(111) * I / 9)**(S(1) / 3))) / 9 +
        sqrt(30) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + S(5) / 3 + I *
        (-sqrt(30) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 -
         sqrt(10) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + 40 * im(1 / (
             (-S(1) / 2 - sqrt(3) * I / 2) *
             (S(251) / 27 + sqrt(111) * I / 9)**(S(1) / 3))) / 9)
    ])

    # the number of real roots will depend on the value of m: for m=1 there are 4
    # and for m=-1 there are none.
    eq = -sqrt((m - q)**2 + (-m / (2 * q) + S(1) / 2)**2) + sqrt(
        (-m**2 / 2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4)**2 +
        (m**2 / 2 - m - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 -
         S(1) / 4)**2)
    unsolved_object = ConditionSet(
        q,
        Eq((-2 * sqrt(4 * q**2 * (m - q)**2 + (-m + q)**2) + sqrt(
            (-2 * m**2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) - 1)**2 +
            (2 * m**2 - 4 * m - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) - 1)**2)
            * Abs(q)) / Abs(q), 0), S.Reals)
    assert solveset_real(eq, q) == unsolved_object
Exemplo n.º 16
0
def test_solve_sqrt_3():
    R = Symbol("R")
    eq = sqrt(2) * R * sqrt(1 / (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 / (R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(
        *[
            S(5) / 3 + 4 * sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3,
            -sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3
            + 40 * re(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9
            + sqrt(30) * sin(atan(3 * sqrt(111) / 251) / 3) / 3
            + S(5) / 3
            + I
            * (
                -sqrt(30) * cos(atan(3 * sqrt(111) / 251) / 3) / 3
                - sqrt(10) * sin(atan(3 * sqrt(111) / 251) / 3) / 3
                + 40 * im(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9
            ),
        ]
    )

    # the number of real roots will depend on the value of m: for m=1 there are 4
    # and for m=-1 there are none.
    eq = -sqrt((m - q) ** 2 + (-m / (2 * q) + S(1) / 2) ** 2) + sqrt(
        (-m ** 2 / 2 - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2
        + (m ** 2 / 2 - m - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2
    )
    raises(NotImplementedError, lambda: solveset_real(eq, q))
Exemplo n.º 17
0
def test_solve_sqrt_3():
    R = Symbol('R')
    eq = sqrt(2) * R * sqrt(1 /
                            (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 /
                                                                 (R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(*[
        S(5) / 3 + 4 * sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) /
        3, -sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 +
        40 * re(1 / ((-S(1) / 2 - sqrt(3) * I / 2) *
                     (S(251) / 27 + sqrt(111) * I / 9)**(S(1) / 3))) / 9 +
        sqrt(30) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + S(5) / 3 + I *
        (-sqrt(30) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 -
         sqrt(10) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + 40 * im(1 / (
             (-S(1) / 2 - sqrt(3) * I / 2) *
             (S(251) / 27 + sqrt(111) * I / 9)**(S(1) / 3))) / 9)
    ])

    # the number of real roots will depend on the value of m: for m=1 there are 4
    # and for m=-1 there are none.
    eq = -sqrt((m - q)**2 + (-m / (2 * q) + S(1) / 2)**2) + sqrt(
        (-m**2 / 2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4)**2 +
        (m**2 / 2 - m - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 -
         S(1) / 4)**2)
    raises(NotImplementedError, lambda: solveset_real(eq, q))
Exemplo n.º 18
0
def test_solve_trig():
    from sympy.abc import n
    assert solveset_real(sin(x), x) == \
        Union(imageset(Lambda(n, 2*pi*n), S.Integers),
              imageset(Lambda(n, 2*pi*n + pi), S.Integers))

    assert solveset_real(sin(x) - 1, x) == \
        imageset(Lambda(n, 2*pi*n + pi/2), S.Integers)

    assert solveset_real(cos(x), x) == \
        Union(imageset(Lambda(n, 2*pi*n - pi/2), S.Integers),
              imageset(Lambda(n, 2*pi*n + pi/2), S.Integers))

    assert solveset_real(sin(x) + cos(x), x) == \
        Union(imageset(Lambda(n, 2*n*pi - pi/4), S.Integers),
              imageset(Lambda(n, 2*n*pi + 3*pi/4), S.Integers))

    assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet

    assert solveset_complex(cos(x) - S.Half, x) == \
        Union(imageset(Lambda(n, 2*n*pi + pi/3), S.Integers),
              imageset(Lambda(n, 2*n*pi - pi/3), S.Integers))

    y, a = symbols('y,a')
    assert solveset(sin(y + a) - sin(y), a, domain=S.Reals) == \
        Union(imageset(Lambda(n, 2*n*pi), S.Integers),
        imageset(Lambda(n,
        -I*(I*(2*n*pi +arg(-exp(-2*I*y))) + 2*im(y))), S.Integers))
Exemplo n.º 19
0
def test_solve_trig():
    from sympy.abc import n

    assert solveset_real(sin(x), x) == Union(
        imageset(Lambda(n, 2 * pi * n), S.Integers), imageset(Lambda(n, 2 * pi * n + pi), S.Integers)
    )

    assert solveset_real(sin(x) - 1, x) == imageset(Lambda(n, 2 * pi * n + pi / 2), S.Integers)

    assert solveset_real(cos(x), x) == Union(
        imageset(Lambda(n, 2 * pi * n - pi / 2), S.Integers), imageset(Lambda(n, 2 * pi * n + pi / 2), S.Integers)
    )

    assert solveset_real(sin(x) + cos(x), x) == Union(
        imageset(Lambda(n, 2 * n * pi - pi / 4), S.Integers), imageset(Lambda(n, 2 * n * pi + 3 * pi / 4), S.Integers)
    )

    assert solveset_real(sin(x) ** 2 + cos(x) ** 2, x) == S.EmptySet

    assert solveset_complex(cos(x) - S.Half, x) == Union(
        imageset(Lambda(n, 2 * n * pi + pi / 3), S.Integers), imageset(Lambda(n, 2 * n * pi - pi / 3), S.Integers)
    )

    y, a = symbols("y,a")
    assert solveset(sin(y + a) - sin(y), a, domain=S.Reals) == Union(
        imageset(Lambda(n, 2 * n * pi), S.Integers),
        imageset(Lambda(n, -I * (I * (2 * n * pi + arg(-exp(-2 * I * y))) + 2 * im(y))), S.Integers),
    )
Exemplo n.º 20
0
def test_solve_sqrt_3():
    R = Symbol("R")
    eq = sqrt(2) * R * sqrt(1 / (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 / (R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(
        *[
            S(5) / 3 + 4 * sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3,
            -sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3
            + 40 * re(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9
            + sqrt(30) * sin(atan(3 * sqrt(111) / 251) / 3) / 3
            + S(5) / 3
            + I
            * (
                -sqrt(30) * cos(atan(3 * sqrt(111) / 251) / 3) / 3
                - sqrt(10) * sin(atan(3 * sqrt(111) / 251) / 3) / 3
                + 40 * im(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9
            ),
        ]
    )

    # the number of real roots will depend on the value of m: for m=1 there are 4
    # and for m=-1 there are none.
    eq = -sqrt((m - q) ** 2 + (-m / (2 * q) + S(1) / 2) ** 2) + sqrt(
        (-m ** 2 / 2 - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2
        + (m ** 2 / 2 - m - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2
    )
    unsolved_object = ConditionSet(
        q,
        Eq(
            (
                -2 * sqrt(4 * q ** 2 * (m - q) ** 2 + (-m + q) ** 2)
                + sqrt(
                    (-2 * m ** 2 - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) - 1) ** 2
                    + (2 * m ** 2 - 4 * m - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) - 1) ** 2
                )
                * Abs(q)
            )
            / Abs(q),
            0,
        ),
        S.Reals,
    )
    assert solveset_real(eq, q) == unsolved_object
Exemplo n.º 21
0
def test_solve_sqrt_3():
    R = Symbol('R')
    eq = sqrt(2)*R*sqrt(1/(R + 1)) + (R + 1)*(sqrt(2)*sqrt(1/(R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(*[(S(5)/3 + 40/(3*(251 + 3*sqrt(111)*I)**(S(1)/3)) +
                       (251 + 3*sqrt(111)*I)**(S(1)/3)/3,), ((-160 + (1 +
                       sqrt(3)*I)*(10 - (1 + sqrt(3)*I)*(251 +
                       3*sqrt(111)*I)**(S(1)/3))*(251 +
                       3*sqrt(111)*I)**(S(1)/3))/Mul(6, (1 +
                       sqrt(3)*I), (251 + 3*sqrt(111)*I)**(S(1)/3),
                       evaluate=False),)])

    eq = -sqrt((m - q)**2 + (-m/(2*q) + S(1)/2)**2) + sqrt((-m**2/2 - sqrt(
        4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)**2 + (m**2/2 - m - sqrt(
            4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)**2)
    assert solveset_real(eq, q) == FiniteSet(
        m**2/2 - sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4,
        m**2/2 + sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)
Exemplo n.º 22
0
def test_solve_sqrt_3():
    R = Symbol('R')
    eq = sqrt(2) * R * sqrt(1 /
                            (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 /
                                                                 (R + 1)) - 1)
    sol = solveset_complex(eq, R)

    assert sol == FiniteSet(
        *[(S(5) / 3 + 40 / (3 * (251 + 3 * sqrt(111) * I)**(S(1) / 3)) +
           (251 + 3 * sqrt(111) * I)**(S(1) / 3) / 3, ),
          ((-160 + (1 + sqrt(3) * I) *
            (10 - (1 + sqrt(3) * I) * (251 + 3 * sqrt(111) * I)**(S(1) / 3)) *
            (251 + 3 * sqrt(111) * I)**(S(1) / 3)) /
           Mul(6, (1 + sqrt(3) * I), (251 + 3 * sqrt(111) * I)**(S(1) / 3),
               evaluate=False), )])

    eq = -sqrt((m - q)**2 + (-m / (2 * q) + S(1) / 2)**2) + sqrt(
        (-m**2 / 2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4)**2 +
        (m**2 / 2 - m - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 -
         S(1) / 4)**2)
    assert solveset_real(eq, q) == FiniteSet(
        m**2 / 2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4,
        m**2 / 2 + sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4)
Exemplo n.º 23
0
def test_return_root_of():
    f = x ** 5 - 15 * x ** 3 - 5 * x ** 2 + 10 * x + 20
    s = list(solveset_complex(f, x))
    for root in s:
        assert root.func == RootOf

    # if one uses solve to get the roots of a polynomial that has a RootOf
    # solution, make sure that the use of nfloat during the solve process
    # doesn't fail. Note: if you want numerical solutions to a polynomial
    # it is *much* faster to use nroots to get them than to solve the
    # equation only to get RootOf solutions which are then numerically
    # evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather
    # than [i.n() for i in solve(eq)] to get the numerical roots of eq.
    assert (
        nfloat(list(solveset_complex(x ** 5 + 3 * x ** 3 + 7, x))[0], exponent=False)
        == RootOf(x ** 5 + 3 * x ** 3 + 7, 0).n()
    )

    sol = list(solveset_complex(x ** 6 - 2 * x + 2, x))
    assert all(isinstance(i, RootOf) for i in sol) and len(sol) == 6

    f = x ** 5 - 15 * x ** 3 - 5 * x ** 2 + 10 * x + 20
    s = list(solveset_complex(f, x))
    for root in s:
        assert root.func == RootOf

    s = x ** 5 + 4 * x ** 3 + 3 * x ** 2 + S(7) / 4
    assert solveset_complex(s, x) == FiniteSet(*Poly(s * 4, domain="ZZ").all_roots())

    # XXX: this comparison should work without converting the FiniteSet to list
    # See #7876
    eq = x * (x - 1) ** 2 * (x + 1) * (x ** 6 - x + 1)
    assert list(solveset_complex(eq, x)) == list(
        FiniteSet(
            -1,
            0,
            1,
            RootOf(x ** 6 - x + 1, 0),
            RootOf(x ** 6 - x + 1, 1),
            RootOf(x ** 6 - x + 1, 2),
            RootOf(x ** 6 - x + 1, 3),
            RootOf(x ** 6 - x + 1, 4),
            RootOf(x ** 6 - x + 1, 5),
        )
    )
Exemplo n.º 24
0
def test_return_root_of():
    f = x**5 - 15 * x**3 - 5 * x**2 + 10 * x + 20
    s = list(solveset_complex(f, x))
    for root in s:
        assert root.func == CRootOf

    # if one uses solve to get the roots of a polynomial that has a CRootOf
    # solution, make sure that the use of nfloat during the solve process
    # doesn't fail. Note: if you want numerical solutions to a polynomial
    # it is *much* faster to use nroots to get them than to solve the
    # equation only to get CRootOf solutions which are then numerically
    # evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather
    # than [i.n() for i in solve(eq)] to get the numerical roots of eq.
    assert nfloat(list(solveset_complex(x**5 + 3 * x**3 + 7, x))[0],
                  exponent=False) == CRootOf(x**5 + 3 * x**3 + 7, 0).n()

    sol = list(solveset_complex(x**6 - 2 * x + 2, x))
    assert all(isinstance(i, CRootOf) for i in sol) and len(sol) == 6

    f = x**5 - 15 * x**3 - 5 * x**2 + 10 * x + 20
    s = list(solveset_complex(f, x))
    for root in s:
        assert root.func == CRootOf

    s = x**5 + 4 * x**3 + 3 * x**2 + S(7) / 4
    assert solveset_complex(s, x) == \
        FiniteSet(*Poly(s*4, domain='ZZ').all_roots())

    # XXX: this comparison should work without converting the FiniteSet to list
    # See #7876
    eq = x * (x - 1)**2 * (x + 1) * (x**6 - x + 1)
    assert list(solveset_complex(eq, x)) == \
        list(FiniteSet(-1, 0, 1, CRootOf(x**6 - x + 1, 0),
                       CRootOf(x**6 - x + 1, 1),
                       CRootOf(x**6 - x + 1, 2),
                       CRootOf(x**6 - x + 1, 3),
                       CRootOf(x**6 - x + 1, 4),
                       CRootOf(x**6 - x + 1, 5)))
Exemplo n.º 25
0
def test_sol_zero_complex():
    assert solveset_complex(0, x) == S.Complexes
Exemplo n.º 26
0
def test_solve_complex_unsolvable():
    raises(NotImplementedError, lambda: solveset_complex(cos(x) - S.Half, x))
Exemplo n.º 27
0
def test_solve_polynomial_symbolic_param():
    assert solveset_complex((x**2 - 1)**2 - a, x) == \
        FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
                  sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a)))
Exemplo n.º 28
0
def test_solve_lambert():
    assert solveset_real(x * exp(x) - 1, x) == FiniteSet(LambertW(1))
    assert solveset_real(x + 2**x, x) == \
        FiniteSet(-LambertW(log(2))/log(2))

    # issue 4739
    assert solveset_real(exp(log(5) * x) - 2**x, x) == FiniteSet(0)
    ans = solveset_real(3 * x + 5 + 2**(-5 * x + 3), x)
    assert ans == FiniteSet(-Rational(5, 3) +
                            LambertW(-10240 * 2**(S(1) / 3) * log(2) / 3) /
                            (5 * log(2)))

    eq = 2 * (3 * x + 4)**5 - 6 * 7**(3 * x + 9)
    result = solveset_real(eq, x)
    ans = FiniteSet(
        (log(2401) + 5 * LambertW(-log(7**(7 * 3**Rational(1, 5) / 5)))) /
        (3 * log(7)) / -1)
    assert result == ans
    assert solveset_real(eq.expand(), x) == result

    assert solveset_real(5*x - 1 + 3*exp(2 - 7*x), x) == \
        FiniteSet(Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7)

    assert solveset_real(2*x + 5 + log(3*x - 2), x) == \
        FiniteSet(Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2)

    assert solveset_real(3*x + log(4*x), x) == \
        FiniteSet(LambertW(Rational(3, 4))/3)

    assert solveset_complex(x**z*y**z - 2, z) == \
        FiniteSet(log(2)/(log(x) + log(y)))

    assert solveset_real(x**x - 2) == FiniteSet(exp(LambertW(log(2))))

    a = Symbol('a')
    assert solveset_real(-a * x + 2 * x * log(x), x) == FiniteSet(exp(a / 2))
    a = Symbol('a', real=True)
    assert solveset_real(a/x + exp(x/2), x) == \
        FiniteSet(2*LambertW(-a/2))
    assert solveset_real((a/x + exp(x/2)).diff(x), x) == \
        FiniteSet(4*LambertW(sqrt(2)*sqrt(a)/4))

    assert solveset_real(1 / (1 / x - y + exp(y)), x) == EmptySet()
    # coverage test
    p = Symbol('p', positive=True)
    w = Symbol('w')
    assert solveset_real((1 / p + 1)**(p + 1), p) == EmptySet()
    assert solveset_real(tanh(x + 3) * tanh(x - 3) - 1, x) == EmptySet()
    assert solveset_real(2*x**w - 4*y**w, w) == \
        solveset_real((x/y)**w - 2, w)

    assert solveset_real((x**2 - 2*x + 1).subs(x, log(x) + 3*x), x) == \
        FiniteSet(LambertW(3*S.Exp1)/3)
    assert solveset_real((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) == \
        FiniteSet(LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3)
    assert solveset_real((x**2 - 2*x - 2).subs(x, log(x) + 3*x), x) == \
        FiniteSet(LambertW(3*exp(1 + sqrt(3)))/3, LambertW(3*exp(-sqrt(3) + 1))/3)
    assert solveset_real(x*log(x) + 3*x + 1, x) == \
        FiniteSet(exp(-3 + LambertW(-exp(3))))
    eq = (x * exp(x) - 3).subs(x, x * exp(x))
    assert solveset_real(eq, x) == \
        FiniteSet(LambertW(3*exp(-LambertW(3))))

    assert solveset_real(3*log(a**(3*x + 5)) + a**(3*x + 5), x) == \
        FiniteSet(-((log(a**5) + LambertW(S(1)/3))/(3*log(a))))
    p = symbols('p', positive=True)
    assert solveset_real(3*log(p**(3*x + 5)) + p**(3*x + 5), x) == \
        FiniteSet(
        log((-3**(S(1)/3) - 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p),
        log((-3**(S(1)/3) + 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p),
        log((3*LambertW(S(1)/3)/p**5)**(1/(3*log(p)))),)  # checked numerically
    # check collection
    b = Symbol('b')
    eq = 3 * log(a**(3 * x + 5)) + b * log(a**(3 * x + 5)) + a**(3 * x + 5)
    assert solveset_real(
        eq,
        x) == FiniteSet(-((log(a**5) + LambertW(1 / (b + 3))) / (3 * log(a))))

    # issue 4271
    assert solveset_real((a / x + exp(x / 2)).diff(x, 2),
                         x) == FiniteSet(6 * LambertW(
                             (-1)**(S(1) / 3) * a**(S(1) / 3) / 3))

    assert solveset_real(x**3 - 3**x, x) == \
        FiniteSet(-3/log(3)*LambertW(-log(3)/3))
    assert solveset_real(x**2 - 2**x, x) == FiniteSet(2)
    assert solveset_real(-x**2 + 2**x, x) == FiniteSet(2)
    assert solveset_real(3**cos(x) - cos(x)**3) == FiniteSet(
        acos(-3 * LambertW(-log(3) / 3) / log(3)))

    assert solveset_real(4**(x / 2) - 2**(x / 3), x) == FiniteSet(0)
    assert solveset_real(5**(x / 2) - 2**(x / 3), x) == FiniteSet(0)
    b = sqrt(6) * sqrt(log(2)) / sqrt(log(5))
    assert solveset_real(5**(x / 2) - 2**(3 / x), x) == FiniteSet(-b, b)
Exemplo n.º 29
0
def my_func(y, t):
    eqn = -(4*z**3 - 9*t*z + 27)*(z**2 - 3*t) - y**2
    return solveset_complex(eqn, z)
Exemplo n.º 30
0
def test_solveset_complex_tan():
    s = solveset_complex(tan(x).rewrite(exp), x)
    assert s == imageset(Lambda(n, pi*n), S.Integers) - \
        imageset(Lambda(n, pi*n + pi/2), S.Integers)
Exemplo n.º 31
0
def test_solve_complex_log():
    assert solveset_complex(log(x), x) == FiniteSet(1)
    assert solveset_complex(1 - log(a + 4*x**2), x) == \
        FiniteSet(-sqrt(-a/4 + E/4), sqrt(-a/4 + E/4))
Exemplo n.º 32
0
def test_solve_complex_log():
    assert solveset_complex(log(x), x) == FiniteSet(1)
    assert solveset_complex(1 - log(a + 4*x**2), x) == \
        FiniteSet(-sqrt(-a/4 + E/4), sqrt(-a/4 + E/4))
Exemplo n.º 33
0
def test_sol_zero_complex():
    # This should return the complex set after it is implemented
    raises(NotImplementedError, lambda: solveset_complex(0, x))
Exemplo n.º 34
0
def test_solveset_complex_exp():
    from sympy.abc import x, n

    assert solveset_complex(exp(x) - 1, x) == imageset(Lambda(n, I * 2 * n * pi), S.Integers)
    assert solveset_complex(exp(x) - I, x) == imageset(Lambda(n, I * (2 * n * pi + pi / 2)), S.Integers)
Exemplo n.º 35
0
def test_solve_complex_unsolvable():
    raises(NotImplementedError, lambda: solveset_complex(cos(x) - S.Half, x))
Exemplo n.º 36
0
def test_sol_zero_complex():
    assert solveset_complex(0, x) == S.Complexes
Exemplo n.º 37
0
def test_solveset_complex_exp():
    from sympy.abc import x, n
    assert solveset_complex(exp(x) - 1, x) == \
        imageset(Lambda(n, I*2*n*pi), S.Integers)
    assert solveset_complex(exp(x) - I, x) == \
        imageset(Lambda(n, I*(2*n*pi + pi/2)), S.Integers)
Exemplo n.º 38
0
def test_sol_zero_complex():
    # This should return the complex set after it is implemented
    raises(NotImplementedError, lambda: solveset_complex(0, x))
Exemplo n.º 39
0
def test_solveset_complex_tan():
    s = solveset_complex(tan(x).rewrite(exp), x)
    assert s == imageset(Lambda(n, pi*n), S.Integers) - \
        imageset(Lambda(n, pi*n + pi/2), S.Integers)
Exemplo n.º 40
0
def test_solve_complex_unsolvable():
    unsolved_object = ConditionSet(x, Eq(2 * cos(x) - 1, 0), S.Complexes)
    solution = solveset_complex(cos(x) - S.Half, x)
    assert solution == unsolved_object
Exemplo n.º 41
0
def test_solve_invalid_sol():
    assert 0 not in solveset_real(sin(x) / x, x)
    assert 0 not in solveset_complex((exp(x) - 1) / x, x)
Exemplo n.º 42
0
def test_solve_invalid_sol():
    assert 0 not in solveset_real(sin(x)/x, x)
    assert 0 not in solveset_complex((exp(x) - 1)/x, x)
Exemplo n.º 43
0
def test_solve_complex_unsolvable():
    unsolved_object = ConditionSet(x, Eq(2*cos(x) - 1, 0), S.Complexes)
    solution = solveset_complex(cos(x) - S.Half, x)
    assert solution == unsolved_object
Exemplo n.º 44
0
def test_solve_lambert():
    assert solveset_real(x*exp(x) - 1, x) == FiniteSet(LambertW(1))
    assert solveset_real(x + 2**x, x) == \
        FiniteSet(-LambertW(log(2))/log(2))

    # issue 4739
    assert solveset_real(exp(log(5)*x) - 2**x, x) == FiniteSet(0)
    ans = solveset_real(3*x + 5 + 2**(-5*x + 3), x)
    assert ans == FiniteSet(-Rational(5, 3) +
                            LambertW(-10240*2**(S(1)/3)*log(2)/3)/(5*log(2)))

    eq = 2*(3*x + 4)**5 - 6*7**(3*x + 9)
    result = solveset_real(eq, x)
    ans = FiniteSet((log(2401) +
                     5*LambertW(-log(7**(7*3**Rational(1, 5)/5))))/(3*log(7))/-1)
    assert result == ans
    assert solveset_real(eq.expand(), x) == result

    assert solveset_real(5*x - 1 + 3*exp(2 - 7*x), x) == \
        FiniteSet(Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7)

    assert solveset_real(2*x + 5 + log(3*x - 2), x) == \
        FiniteSet(Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2)

    assert solveset_real(3*x + log(4*x), x) == \
        FiniteSet(LambertW(Rational(3, 4))/3)

    assert solveset_complex(x**z*y**z - 2, z) == \
        FiniteSet(log(2)/(log(x) + log(y)))

    assert solveset_real(x**x - 2) == FiniteSet(exp(LambertW(log(2))))

    a = Symbol('a')
    assert solveset_real(-a*x + 2*x*log(x), x) == FiniteSet(exp(a/2))
    a = Symbol('a', real=True)
    assert solveset_real(a/x + exp(x/2), x) == \
        FiniteSet(2*LambertW(-a/2))
    assert solveset_real((a/x + exp(x/2)).diff(x), x) == \
        FiniteSet(4*LambertW(sqrt(2)*sqrt(a)/4))

    assert solveset_real(1/(1/x - y + exp(y)), x) == EmptySet()
    # coverage test
    p = Symbol('p', positive=True)
    w = Symbol('w')
    assert solveset_real((1/p + 1)**(p + 1), p) == EmptySet()
    assert solveset_real(tanh(x + 3)*tanh(x - 3) - 1, x) == EmptySet()
    assert solveset_real(2*x**w - 4*y**w, w) == \
        solveset_real((x/y)**w - 2, w)

    assert solveset_real((x**2 - 2*x + 1).subs(x, log(x) + 3*x), x) == \
        FiniteSet(LambertW(3*S.Exp1)/3)
    assert solveset_real((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) == \
        FiniteSet(LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3)
    assert solveset_real((x**2 - 2*x - 2).subs(x, log(x) + 3*x), x) == \
        FiniteSet(LambertW(3*exp(1 + sqrt(3)))/3, LambertW(3*exp(-sqrt(3) + 1))/3)
    assert solveset_real(x*log(x) + 3*x + 1, x) == \
        FiniteSet(exp(-3 + LambertW(-exp(3))))
    eq = (x*exp(x) - 3).subs(x, x*exp(x))
    assert solveset_real(eq, x) == \
        FiniteSet(LambertW(3*exp(-LambertW(3))))

    assert solveset_real(3*log(a**(3*x + 5)) + a**(3*x + 5), x) == \
        FiniteSet(-((log(a**5) + LambertW(S(1)/3))/(3*log(a))))
    p = symbols('p', positive=True)
    assert solveset_real(3*log(p**(3*x + 5)) + p**(3*x + 5), x) == \
        FiniteSet(
        log((-3**(S(1)/3) - 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p),
        log((-3**(S(1)/3) + 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p),
        log((3*LambertW(S(1)/3)/p**5)**(1/(3*log(p)))),)  # checked numerically
    # check collection
    b = Symbol('b')
    eq = 3*log(a**(3*x + 5)) + b*log(a**(3*x + 5)) + a**(3*x + 5)
    assert solveset_real(eq, x) == FiniteSet(
        -((log(a**5) + LambertW(1/(b + 3)))/(3*log(a))))

    # issue 4271
    assert solveset_real((a/x + exp(x/2)).diff(x, 2), x) == FiniteSet(
        6*LambertW((-1)**(S(1)/3)*a**(S(1)/3)/3))

    assert solveset_real(x**3 - 3**x, x) == \
        FiniteSet(-3/log(3)*LambertW(-log(3)/3))
    assert solveset_real(x**2 - 2**x, x) == FiniteSet(2)
    assert solveset_real(-x**2 + 2**x, x) == FiniteSet(2)
    assert solveset_real(3**cos(x) - cos(x)**3) == FiniteSet(
        acos(-3*LambertW(-log(3)/3)/log(3)))

    assert solveset_real(4**(x/2) - 2**(x/3), x) == FiniteSet(0)
    assert solveset_real(5**(x/2) - 2**(x/3), x) == FiniteSet(0)
    b = sqrt(6)*sqrt(log(2))/sqrt(log(5))
    assert solveset_real(5**(x/2) - 2**(3/x), x) == FiniteSet(-b, b)
Exemplo n.º 45
0
def test_solve_polynomial_symbolic_param():
    assert solveset_complex((x**2 - 1)**2 - a, x) == \
        FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
                  sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a)))