1from sympy import (
2    Abs, And, Derivative, Dummy, Eq, Float, Function, Gt, I, Integral,
3    LambertW, Lt, Matrix, Or, Poly, Q, Rational, S, Symbol, Ne,
4    Wild, acos, asin, atan, atanh, binomial, cos, cosh, diff, erf, erfinv, erfc,
5    erfcinv, exp, im, log, pi, re, sec, sin,
6    sinh, solve, solve_linear, sqrt, sstr, symbols, sympify, tan, tanh,
7    root, atan2, arg, Mul, SparseMatrix, ask, Tuple, nsolve, oo,
8    E, cbrt, denom, Add, Piecewise, GoldenRatio, TribonacciConstant)
9
10from sympy.core.function import nfloat
11from sympy.solvers import solve_linear_system, solve_linear_system_LU, \
12    solve_undetermined_coeffs
13from sympy.solvers.bivariate import _filtered_gens, _solve_lambert, _lambert
14from sympy.solvers.solvers import _invert, unrad, checksol, posify, _ispow, \
15    det_quick, det_perm, det_minor, _simple_dens, denoms
16
17from sympy.physics.units import cm
18from sympy.polys.rootoftools import CRootOf
19
20from sympy.testing.pytest import slow, XFAIL, SKIP, raises
21from sympy.testing.randtest import verify_numerically as tn
22
23from sympy.abc import a, b, c, d, e, k, h, p, x, y, z, t, q, m, R
24
25
26def NS(e, n=15, **options):
27    return sstr(sympify(e).evalf(n, **options), full_prec=True)
28
29
30def test_swap_back():
31    f, g = map(Function, 'fg')
32    fx, gx = f(x), g(x)
33    assert solve([fx + y - 2, fx - gx - 5], fx, y, gx) == \
34        {fx: gx + 5, y: -gx - 3}
35    assert solve(fx + gx*x - 2, [fx, gx], dict=True)[0] == {fx: 2, gx: 0}
36    assert solve(fx + gx**2*x - y, [fx, gx], dict=True) == [{fx: y - gx**2*x}]
37    assert solve([f(1) - 2, x + 2], dict=True) == [{x: -2, f(1): 2}]
38
39
40def guess_solve_strategy(eq, symbol):
41    try:
42        solve(eq, symbol)
43        return True
44    except (TypeError, NotImplementedError):
45        return False
46
47
48def test_guess_poly():
49    # polynomial equations
50    assert guess_solve_strategy( S(4), x )  # == GS_POLY
51    assert guess_solve_strategy( x, x )  # == GS_POLY
52    assert guess_solve_strategy( x + a, x )  # == GS_POLY
53    assert guess_solve_strategy( 2*x, x )  # == GS_POLY
54    assert guess_solve_strategy( x + sqrt(2), x)  # == GS_POLY
55    assert guess_solve_strategy( x + 2**Rational(1, 4), x)  # == GS_POLY
56    assert guess_solve_strategy( x**2 + 1, x )  # == GS_POLY
57    assert guess_solve_strategy( x**2 - 1, x )  # == GS_POLY
58    assert guess_solve_strategy( x*y + y, x )  # == GS_POLY
59    assert guess_solve_strategy( x*exp(y) + y, x)  # == GS_POLY
60    assert guess_solve_strategy(
61        (x - y**3)/(y**2*sqrt(1 - y**2)), x)  # == GS_POLY
62
63
64def test_guess_poly_cv():
65    # polynomial equations via a change of variable
66    assert guess_solve_strategy( sqrt(x) + 1, x )  # == GS_POLY_CV_1
67    assert guess_solve_strategy(
68        x**Rational(1, 3) + sqrt(x) + 1, x )  # == GS_POLY_CV_1
69    assert guess_solve_strategy( 4*x*(1 - sqrt(x)), x )  # == GS_POLY_CV_1
70
71    # polynomial equation multiplying both sides by x**n
72    assert guess_solve_strategy( x + 1/x + y, x )  # == GS_POLY_CV_2
73
74
75def test_guess_rational_cv():
76    # rational functions
77    assert guess_solve_strategy( (x + 1)/(x**2 + 2), x)  # == GS_RATIONAL
78    assert guess_solve_strategy(
79        (x - y**3)/(y**2*sqrt(1 - y**2)), y)  # == GS_RATIONAL_CV_1
80
81    # rational functions via the change of variable y -> x**n
82    assert guess_solve_strategy( (sqrt(x) + 1)/(x**Rational(1, 3) + sqrt(x) + 1), x ) \
83        #== GS_RATIONAL_CV_1
84
85
86def test_guess_transcendental():
87    #transcendental functions
88    assert guess_solve_strategy( exp(x) + 1, x )  # == GS_TRANSCENDENTAL
89    assert guess_solve_strategy( 2*cos(x) - y, x )  # == GS_TRANSCENDENTAL
90    assert guess_solve_strategy(
91        exp(x) + exp(-x) - y, x )  # == GS_TRANSCENDENTAL
92    assert guess_solve_strategy(3**x - 10, x)  # == GS_TRANSCENDENTAL
93    assert guess_solve_strategy(-3**x + 10, x)  # == GS_TRANSCENDENTAL
94
95    assert guess_solve_strategy(a*x**b - y, x)  # == GS_TRANSCENDENTAL
96
97
98def test_solve_args():
99    # equation container, issue 5113
100    ans = {x: -3, y: 1}
101    eqs = (x + 5*y - 2, -3*x + 6*y - 15)
102    assert all(solve(container(eqs), x, y) == ans for container in
103        (tuple, list, set, frozenset))
104    assert solve(Tuple(*eqs), x, y) == ans
105    # implicit symbol to solve for
106    assert set(solve(x**2 - 4)) == {S(2), -S(2)}
107    assert solve([x + y - 3, x - y - 5]) == {x: 4, y: -1}
108    assert solve(x - exp(x), x, implicit=True) == [exp(x)]
109    # no symbol to solve for
110    assert solve(42) == solve(42, x) == []
111    assert solve([1, 2]) == []
112    # duplicate symbols removed
113    assert solve((x - 3, y + 2), x, y, x) == {x: 3, y: -2}
114    # unordered symbols
115    # only 1
116    assert solve(y - 3, {y}) == [3]
117    # more than 1
118    assert solve(y - 3, {x, y}) == [{y: 3}]
119    # multiple symbols: take the first linear solution+
120    # - return as tuple with values for all requested symbols
121    assert solve(x + y - 3, [x, y]) == [(3 - y, y)]
122    # - unless dict is True
123    assert solve(x + y - 3, [x, y], dict=True) == [{x: 3 - y}]
124    # - or no symbols are given
125    assert solve(x + y - 3) == [{x: 3 - y}]
126    # multiple symbols might represent an undetermined coefficients system
127    assert solve(a + b*x - 2, [a, b]) == {a: 2, b: 0}
128    args = (a + b)*x - b**2 + 2, a, b
129    assert solve(*args) == \
130        [(-sqrt(2), sqrt(2)), (sqrt(2), -sqrt(2))]
131    assert solve(*args, set=True) == \
132        ([a, b], {(-sqrt(2), sqrt(2)), (sqrt(2), -sqrt(2))})
133    assert solve(*args, dict=True) == \
134        [{b: sqrt(2), a: -sqrt(2)}, {b: -sqrt(2), a: sqrt(2)}]
135    eq = a*x**2 + b*x + c - ((x - h)**2 + 4*p*k)/4/p
136    flags = dict(dict=True)
137    assert solve(eq, [h, p, k], exclude=[a, b, c], **flags) == \
138        [{k: c - b**2/(4*a), h: -b/(2*a), p: 1/(4*a)}]
139    flags.update(dict(simplify=False))
140    assert solve(eq, [h, p, k], exclude=[a, b, c], **flags) == \
141        [{k: (4*a*c - b**2)/(4*a), h: -b/(2*a), p: 1/(4*a)}]
142    # failing undetermined system
143    assert solve(a*x + b**2/(x + 4) - 3*x - 4/x, a, b, dict=True) == \
144        [{a: (-b**2*x + 3*x**3 + 12*x**2 + 4*x + 16)/(x**2*(x + 4))}]
145    # failed single equation
146    assert solve(1/(1/x - y + exp(y))) == []
147    raises(
148        NotImplementedError, lambda: solve(exp(x) + sin(x) + exp(y) + sin(y)))
149    # failed system
150    # --  when no symbols given, 1 fails
151    assert solve([y, exp(x) + x]) == {x: -LambertW(1), y: 0}
152    #     both fail
153    assert solve(
154        (exp(x) - x, exp(y) - y)) == {x: -LambertW(-1), y: -LambertW(-1)}
155    # --  when symbols given
156    solve([y, exp(x) + x], x, y) == [(-LambertW(1), 0)]
157    # symbol is a number
158    assert solve(x**2 - pi, pi) == [x**2]
159    # no equations
160    assert solve([], [x]) == []
161    # overdetermined system
162    # - nonlinear
163    assert solve([(x + y)**2 - 4, x + y - 2]) == [{x: -y + 2}]
164    # - linear
165    assert solve((x + y - 2, 2*x + 2*y - 4)) == {x: -y + 2}
166    # When one or more args are Boolean
167    assert solve(Eq(x**2, 0.0)) == [0]  # issue 19048
168    assert solve([True, Eq(x, 0)], [x], dict=True) == [{x: 0}]
169    assert solve([Eq(x, x), Eq(x, 0), Eq(x, x+1)], [x], dict=True) == []
170    assert not solve([Eq(x, x+1), x < 2], x)
171    assert solve([Eq(x, 0), x+1<2]) == Eq(x, 0)
172    assert solve([Eq(x, x), Eq(x, x+1)], x) == []
173    assert solve(True, x) == []
174    assert solve([x - 1, False], [x], set=True) == ([], set())
175
176
177def test_solve_polynomial1():
178    assert solve(3*x - 2, x) == [Rational(2, 3)]
179    assert solve(Eq(3*x, 2), x) == [Rational(2, 3)]
180
181    assert set(solve(x**2 - 1, x)) == {-S.One, S.One}
182    assert set(solve(Eq(x**2, 1), x)) == {-S.One, S.One}
183
184    assert solve(x - y**3, x) == [y**3]
185    rx = root(x, 3)
186    assert solve(x - y**3, y) == [
187        rx, -rx/2 - sqrt(3)*I*rx/2, -rx/2 +  sqrt(3)*I*rx/2]
188    a11, a12, a21, a22, b1, b2 = symbols('a11,a12,a21,a22,b1,b2')
189
190    assert solve([a11*x + a12*y - b1, a21*x + a22*y - b2], x, y) == \
191        {
192            x: (a22*b1 - a12*b2)/(a11*a22 - a12*a21),
193            y: (a11*b2 - a21*b1)/(a11*a22 - a12*a21),
194        }
195
196    solution = {y: S.Zero, x: S.Zero}
197
198    assert solve((x - y, x + y), x, y ) == solution
199    assert solve((x - y, x + y), (x, y)) == solution
200    assert solve((x - y, x + y), [x, y]) == solution
201
202    assert set(solve(x**3 - 15*x - 4, x)) == {
203        -2 + 3**S.Half,
204        S(4),
205        -2 - 3**S.Half
206    }
207
208    assert set(solve((x**2 - 1)**2 - a, x)) == \
209        {sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
210             sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a))}
211
212
213def test_solve_polynomial2():
214    assert solve(4, x) == []
215
216
217def test_solve_polynomial_cv_1a():
218    """
219    Test for solving on equations that can be converted to a polynomial equation
220    using the change of variable y -> x**Rational(p, q)
221    """
222    assert solve( sqrt(x) - 1, x) == [1]
223    assert solve( sqrt(x) - 2, x) == [4]
224    assert solve( x**Rational(1, 4) - 2, x) == [16]
225    assert solve( x**Rational(1, 3) - 3, x) == [27]
226    assert solve(sqrt(x) + x**Rational(1, 3) + x**Rational(1, 4), x) == [0]
227
228
229def test_solve_polynomial_cv_1b():
230    assert set(solve(4*x*(1 - a*sqrt(x)), x)) == {S.Zero, 1/a**2}
231    assert set(solve(x*(root(x, 3) - 3), x)) == {S.Zero, S(27)}
232
233
234def test_solve_polynomial_cv_2():
235    """
236    Test for solving on equations that can be converted to a polynomial equation
237    multiplying both sides of the equation by x**m
238    """
239    assert solve(x + 1/x - 1, x) in \
240        [[ S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2],
241         [ S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]]
242
243
244def test_quintics_1():
245    f = x**5 - 110*x**3 - 55*x**2 + 2310*x + 979
246    s = solve(f, check=False)
247    for r in s:
248        res = f.subs(x, r.n()).n()
249        assert tn(res, 0)
250
251    f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20
252    s = solve(f)
253    for r in s:
254        assert r.func == CRootOf
255
256    # if one uses solve to get the roots of a polynomial that has a CRootOf
257    # solution, make sure that the use of nfloat during the solve process
258    # doesn't fail. Note: if you want numerical solutions to a polynomial
259    # it is *much* faster to use nroots to get them than to solve the
260    # equation only to get RootOf solutions which are then numerically
261    # evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather
262    # than [i.n() for i in solve(eq)] to get the numerical roots of eq.
263    assert nfloat(solve(x**5 + 3*x**3 + 7)[0], exponent=False) == \
264        CRootOf(x**5 + 3*x**3 + 7, 0).n()
265
266
267def test_quintics_2():
268    f = x**5 + 15*x + 12
269    s = solve(f, check=False)
270    for r in s:
271        res = f.subs(x, r.n()).n()
272        assert tn(res, 0)
273
274    f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20
275    s = solve(f)
276    for r in s:
277        assert r.func == CRootOf
278
279    assert solve(x**5 - 6*x**3 - 6*x**2 + x - 6) == [
280        CRootOf(x**5 - 6*x**3 - 6*x**2 + x - 6, 0),
281        CRootOf(x**5 - 6*x**3 - 6*x**2 + x - 6, 1),
282        CRootOf(x**5 - 6*x**3 - 6*x**2 + x - 6, 2),
283        CRootOf(x**5 - 6*x**3 - 6*x**2 + x - 6, 3),
284        CRootOf(x**5 - 6*x**3 - 6*x**2 + x - 6, 4)]
285
286
287def test_quintics_3():
288    y = x**5 + x**3 - 2**Rational(1, 3)
289    assert solve(y) == solve(-y) == []
290
291
292def test_highorder_poly():
293    # just testing that the uniq generator is unpacked
294    sol = solve(x**6 - 2*x + 2)
295    assert all(isinstance(i, CRootOf) for i in sol) and len(sol) == 6
296
297
298def test_solve_rational():
299    """Test solve for rational functions"""
300    assert solve( ( x - y**3 )/( (y**2)*sqrt(1 - y**2) ), x) == [y**3]
301
302
303def test_solve_nonlinear():
304    assert solve(x**2 - y**2, x, y, dict=True) == [{x: -y}, {x: y}]
305    assert solve(x**2 - y**2/exp(x), y, x, dict=True) == [{y: -x*sqrt(exp(x))},
306                                                          {y: x*sqrt(exp(x))}]
307
308
309def test_issue_8666():
310    x = symbols('x')
311    assert solve(Eq(x**2 - 1/(x**2 - 4), 4 - 1/(x**2 - 4)), x) == []
312    assert solve(Eq(x + 1/x, 1/x), x) == []
313
314
315def test_issue_7228():
316    assert solve(4**(2*(x**2) + 2*x) - 8, x) == [Rational(-3, 2), S.Half]
317
318
319def test_issue_7190():
320    assert solve(log(x-3) + log(x+3), x) == [sqrt(10)]
321
322
323def test_issue_21004():
324    x = symbols('x')
325    f = x/sqrt(x**2+1)
326    f_diff = f.diff(x)
327    assert solve(f_diff, x) == []
328
329
330def test_linear_system():
331    x, y, z, t, n = symbols('x, y, z, t, n')
332
333    assert solve([x - 1, x - y, x - 2*y, y - 1], [x, y]) == []
334
335    assert solve([x - 1, x - y, x - 2*y, x - 1], [x, y]) == []
336    assert solve([x - 1, x - 1, x - y, x - 2*y], [x, y]) == []
337
338    assert solve([x + 5*y - 2, -3*x + 6*y - 15], x, y) == {x: -3, y: 1}
339
340    M = Matrix([[0, 0, n*(n + 1), (n + 1)**2, 0],
341                [n + 1, n + 1, -2*n - 1, -(n + 1), 0],
342                [-1, 0, 1, 0, 0]])
343
344    assert solve_linear_system(M, x, y, z, t) == \
345        {x: t*(-n-1)/n, z: t*(-n-1)/n, y: 0}
346
347    assert solve([x + y + z + t, -z - t], x, y, z, t) == {x: -y, z: -t}
348
349
350@XFAIL
351def test_linear_system_xfail():
352    # https://github.com/sympy/sympy/issues/6420
353    M = Matrix([[0,    15.0, 10.0, 700.0],
354                [1,    1,    1,    100.0],
355                [0,    10.0, 5.0,  200.0],
356                [-5.0, 0,    0,    0    ]])
357
358    assert solve_linear_system(M, x, y, z) == {x: 0, y: -60.0, z: 160.0}
359
360
361def test_linear_system_function():
362    a = Function('a')
363    assert solve([a(0, 0) + a(0, 1) + a(1, 0) + a(1, 1), -a(1, 0) - a(1, 1)],
364        a(0, 0), a(0, 1), a(1, 0), a(1, 1)) == {a(1, 0): -a(1, 1), a(0, 0): -a(0, 1)}
365
366
367def test_linear_system_symbols_doesnt_hang_1():
368
369    def _mk_eqs(wy):
370        # Equations for fitting a wy*2 - 1 degree polynomial between two points,
371        # at end points derivatives are known up to order: wy - 1
372        order = 2*wy - 1
373        x, x0, x1 = symbols('x, x0, x1', real=True)
374        y0s = symbols('y0_:{}'.format(wy), real=True)
375        y1s = symbols('y1_:{}'.format(wy), real=True)
376        c = symbols('c_:{}'.format(order+1), real=True)
377
378        expr = sum([coeff*x**o for o, coeff in enumerate(c)])
379        eqs = []
380        for i in range(wy):
381            eqs.append(expr.diff(x, i).subs({x: x0}) - y0s[i])
382            eqs.append(expr.diff(x, i).subs({x: x1}) - y1s[i])
383        return eqs, c
384
385    #
386    # The purpose of this test is just to see that these calls don't hang. The
387    # expressions returned are complicated so are not included here. Testing
388    # their correctness takes longer than solving the system.
389    #
390
391    for n in range(1, 7+1):
392        eqs, c = _mk_eqs(n)
393        solve(eqs, c)
394
395
396def test_linear_system_symbols_doesnt_hang_2():
397
398    M = Matrix([
399        [66, 24, 39, 50, 88, 40, 37, 96, 16, 65, 31, 11, 37, 72, 16, 19, 55, 37, 28, 76],
400        [10, 93, 34, 98, 59, 44, 67, 74, 74, 94, 71, 61, 60, 23,  6,  2, 57,  8, 29, 78],
401        [19, 91, 57, 13, 64, 65, 24, 53, 77, 34, 85, 58, 87, 39, 39,  7, 36, 67, 91,  3],
402        [74, 70, 15, 53, 68, 43, 86, 83, 81, 72, 25, 46, 67, 17, 59, 25, 78, 39, 63,  6],
403        [69, 40, 67, 21, 67, 40, 17, 13, 93, 44, 46, 89, 62, 31, 30, 38, 18, 20, 12, 81],
404        [50, 22, 74, 76, 34, 45, 19, 76, 28, 28, 11, 99, 97, 82,  8, 46, 99, 57, 68, 35],
405        [58, 18, 45, 88, 10, 64,  9, 34, 90, 82, 17, 41, 43, 81, 45, 83, 22, 88, 24, 39],
406        [42, 21, 70, 68,  6, 33, 64, 81, 83, 15, 86, 75, 86, 17, 77, 34, 62, 72, 20, 24],
407        [ 7,  8,  2, 72, 71, 52, 96,  5, 32, 51, 31, 36, 79, 88, 25, 77, 29, 26, 33, 13],
408        [19, 31, 30, 85, 81, 39, 63, 28, 19, 12, 16, 49, 37, 66, 38, 13,  3, 71, 61, 51],
409        [29, 82, 80, 49, 26, 85,  1, 37,  2, 74, 54, 82, 26, 47, 54,  9, 35,  0, 99, 40],
410        [15, 49, 82, 91, 93, 57, 45, 25, 45, 97, 15, 98, 48, 52, 66, 24, 62, 54, 97, 37],
411        [62, 23, 73, 53, 52, 86, 28, 38,  0, 74, 92, 38, 97, 70, 71, 29, 26, 90, 67, 45],
412        [ 2, 32, 23, 24, 71, 37, 25, 71,  5, 41, 97, 65, 93, 13, 65, 45, 25, 88, 69, 50],
413        [40, 56,  1, 29, 79, 98, 79, 62, 37, 28, 45, 47,  3,  1, 32, 74, 98, 35, 84, 32],
414        [33, 15, 87, 79, 65,  9, 14, 63, 24, 19, 46, 28, 74, 20, 29, 96, 84, 91, 93,  1],
415        [97, 18, 12, 52,  1,  2, 50, 14, 52, 76, 19, 82, 41, 73, 51, 79, 13,  3, 82, 96],
416        [40, 28, 52, 10, 10, 71, 56, 78, 82,  5, 29, 48,  1, 26, 16, 18, 50, 76, 86, 52],
417        [38, 89, 83, 43, 29, 52, 90, 77, 57,  0, 67, 20, 81, 88, 48, 96, 88, 58, 14,  3]])
418
419    syms = x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18 = symbols('x:19')
420
421    sol = {
422        x0:  -S(1967374186044955317099186851240896179)/3166636564687820453598895768302256588,
423        x1:  -S(84268280268757263347292368432053826)/791659141171955113399723942075564147,
424        x2:  -S(229962957341664730974463872411844965)/1583318282343910226799447884151128294,
425        x3:   S(990156781744251750886760432229180537)/6333273129375640907197791536604513176,
426        x4:  -S(2169830351210066092046760299593096265)/18999819388126922721593374609813539528,
427        x5:   S(4680868883477577389628494526618745355)/9499909694063461360796687304906769764,
428        x6:  -S(1590820774344371990683178396480879213)/3166636564687820453598895768302256588,
429        x7:  -S(54104723404825537735226491634383072)/339282489073695048599881689460956063,
430        x8:   S(3182076494196560075964847771774733847)/6333273129375640907197791536604513176,
431        x9:  -S(10870817431029210431989147852497539675)/18999819388126922721593374609813539528,
432        x10: -S(13118019242576506476316318268573312603)/18999819388126922721593374609813539528,
433        x11: -S(5173852969886775824855781403820641259)/4749954847031730680398343652453384882,
434        x12:  S(4261112042731942783763341580651820563)/4749954847031730680398343652453384882,
435        x13: -S(821833082694661608993818117038209051)/6333273129375640907197791536604513176,
436        x14:  S(906881575107250690508618713632090559)/904753304196520129599684505229216168,
437        x15: -S(732162528717458388995329317371283987)/6333273129375640907197791536604513176,
438        x16:  S(4524215476705983545537087360959896817)/9499909694063461360796687304906769764,
439        x17: -S(3898571347562055611881270844646055217)/6333273129375640907197791536604513176,
440        x18:  S(7513502486176995632751685137907442269)/18999819388126922721593374609813539528
441    }
442
443    eqs = list(M * Matrix(syms + (1,)))
444    assert solve(eqs, syms) == sol
445
446    y = Symbol('y')
447    eqs = list(y * M * Matrix(syms + (1,)))
448    assert solve(eqs, syms) == sol
449
450
451def test_linear_systemLU():
452    n = Symbol('n')
453
454    M = Matrix([[1, 2, 0, 1], [1, 3, 2*n, 1], [4, -1, n**2, 1]])
455
456    assert solve_linear_system_LU(M, [x, y, z]) == {z: -3/(n**2 + 18*n),
457                                                  x: 1 - 12*n/(n**2 + 18*n),
458                                                  y: 6*n/(n**2 + 18*n)}
459
460# Note: multiple solutions exist for some of these equations, so the tests
461# should be expected to break if the implementation of the solver changes
462# in such a way that a different branch is chosen
463
464@slow
465def test_solve_transcendental():
466    from sympy.abc import a, b
467
468    assert solve(exp(x) - 3, x) == [log(3)]
469    assert set(solve((a*x + b)*(exp(x) - 3), x)) == {-b/a, log(3)}
470    assert solve(cos(x) - y, x) == [-acos(y) + 2*pi, acos(y)]
471    assert solve(2*cos(x) - y, x) == [-acos(y/2) + 2*pi, acos(y/2)]
472    assert solve(Eq(cos(x), sin(x)), x) == [pi/4]
473
474    assert set(solve(exp(x) + exp(-x) - y, x)) in [{
475        log(y/2 - sqrt(y**2 - 4)/2),
476        log(y/2 + sqrt(y**2 - 4)/2),
477    }, {
478        log(y - sqrt(y**2 - 4)) - log(2),
479        log(y + sqrt(y**2 - 4)) - log(2)},
480    {
481        log(y/2 - sqrt((y - 2)*(y + 2))/2),
482        log(y/2 + sqrt((y - 2)*(y + 2))/2)}]
483    assert solve(exp(x) - 3, x) == [log(3)]
484    assert solve(Eq(exp(x), 3), x) == [log(3)]
485    assert solve(log(x) - 3, x) == [exp(3)]
486    assert solve(sqrt(3*x) - 4, x) == [Rational(16, 3)]
487    assert solve(3**(x + 2), x) == []
488    assert solve(3**(2 - x), x) == []
489    assert solve(x + 2**x, x) == [-LambertW(log(2))/log(2)]
490    assert solve(2*x + 5 + log(3*x - 2), x) == \
491        [Rational(2, 3) + LambertW(2*exp(Rational(-19, 3))/3)/2]
492    assert solve(3*x + log(4*x), x) == [LambertW(Rational(3, 4))/3]
493    assert set(solve((2*x + 8)*(8 + exp(x)), x)) == {S(-4), log(8) + pi*I}
494    eq = 2*exp(3*x + 4) - 3
495    ans = solve(eq, x)  # this generated a failure in flatten
496    assert len(ans) == 3 and all(eq.subs(x, a).n(chop=True) == 0 for a in ans)
497    assert solve(2*log(3*x + 4) - 3, x) == [(exp(Rational(3, 2)) - 4)/3]
498    assert solve(exp(x) + 1, x) == [pi*I]
499
500    eq = 2*(3*x + 4)**5 - 6*7**(3*x + 9)
501    result = solve(eq, x)
502    ans = [(log(2401) + 5*LambertW((-1 + sqrt(5) + sqrt(2)*I*sqrt(sqrt(5) + \
503        5))*log(7**(7*3**Rational(1, 5)/20))* -1))/(-3*log(7)), \
504        (log(2401) + 5*LambertW((1 + sqrt(5) - sqrt(2)*I*sqrt(5 - \
505        sqrt(5)))*log(7**(7*3**Rational(1, 5)/20))))/(-3*log(7)), \
506        (log(2401) + 5*LambertW((1 + sqrt(5) + sqrt(2)*I*sqrt(5 - \
507        sqrt(5)))*log(7**(7*3**Rational(1, 5)/20))))/(-3*log(7)), \
508        (log(2401) + 5*LambertW((-sqrt(5) + 1 + sqrt(2)*I*sqrt(sqrt(5) + \
509        5))*log(7**(7*3**Rational(1, 5)/20))))/(-3*log(7)), \
510        (log(2401) + 5*LambertW(-log(7**(7*3**Rational(1, 5)/5))))/(-3*log(7))]
511    assert result == ans
512    # it works if expanded, too
513    assert solve(eq.expand(), x) == result
514
515    assert solve(z*cos(x) - y, x) == [-acos(y/z) + 2*pi, acos(y/z)]
516    assert solve(z*cos(2*x) - y, x) == [-acos(y/z)/2 + pi, acos(y/z)/2]
517    assert solve(z*cos(sin(x)) - y, x) == [
518        pi - asin(acos(y/z)), asin(acos(y/z) - 2*pi) + pi,
519        -asin(acos(y/z) - 2*pi), asin(acos(y/z))]
520
521    assert solve(z*cos(x), x) == [pi/2, pi*Rational(3, 2)]
522
523    # issue 4508
524    assert solve(y - b*x/(a + x), x) in [[-a*y/(y - b)], [a*y/(b - y)]]
525    assert solve(y - b*exp(a/x), x) == [a/log(y/b)]
526    # issue 4507
527    assert solve(y - b/(1 + a*x), x) in [[(b - y)/(a*y)], [-((y - b)/(a*y))]]
528    # issue 4506
529    assert solve(y - a*x**b, x) == [(y/a)**(1/b)]
530    # issue 4505
531    assert solve(z**x - y, x) == [log(y)/log(z)]
532    # issue 4504
533    assert solve(2**x - 10, x) == [1 + log(5)/log(2)]
534    # issue 6744
535    assert solve(x*y) == [{x: 0}, {y: 0}]
536    assert solve([x*y]) == [{x: 0}, {y: 0}]
537    assert solve(x**y - 1) == [{x: 1}, {y: 0}]
538    assert solve([x**y - 1]) == [{x: 1}, {y: 0}]
539    assert solve(x*y*(x**2 - y**2)) == [{x: 0}, {x: -y}, {x: y}, {y: 0}]
540    assert solve([x*y*(x**2 - y**2)]) == [{x: 0}, {x: -y}, {x: y}, {y: 0}]
541    # issue 4739
542    assert solve(exp(log(5)*x) - 2**x, x) == [0]
543    # issue 14791
544    assert solve(exp(log(5)*x) - exp(log(2)*x), x) == [0]
545    f = Function('f')
546    assert solve(y*f(log(5)*x) - y*f(log(2)*x), x) == [0]
547    assert solve(f(x) - f(0), x) == [0]
548    assert solve(f(x) - f(2 - x), x) == [1]
549    raises(NotImplementedError, lambda: solve(f(x, y) - f(1, 2), x))
550    raises(NotImplementedError, lambda: solve(f(x, y) - f(2 - x, 2), x))
551    raises(ValueError, lambda: solve(f(x, y) - f(1 - x), x))
552    raises(ValueError, lambda: solve(f(x, y) - f(1), x))
553
554    # misc
555    # make sure that the right variables is picked up in tsolve
556    # shouldn't generate a GeneratorsNeeded error in _tsolve when the NaN is generated
557    # for eq_down. Actual answers, as determined numerically are approx. +/- 0.83
558    raises(NotImplementedError, lambda:
559        solve(sinh(x)*sinh(sinh(x)) + cosh(x)*cosh(sinh(x)) - 3))
560
561    # watch out for recursive loop in tsolve
562    raises(NotImplementedError, lambda: solve((x + 2)**y*x - 3, x))
563
564    # issue 7245
565    assert solve(sin(sqrt(x))) == [0, pi**2]
566
567    # issue 7602
568    a, b = symbols('a, b', real=True, negative=False)
569    assert str(solve(Eq(a, 0.5 - cos(pi*b)/2), b)) == \
570        '[2.0 - 0.318309886183791*acos(1.0 - 2.0*a), 0.318309886183791*acos(1.0 - 2.0*a)]'
571
572    # issue 15325
573    assert solve(y**(1/x) - z, x) == [log(y)/log(z)]
574
575
576def test_solve_for_functions_derivatives():
577    t = Symbol('t')
578    x = Function('x')(t)
579    y = Function('y')(t)
580    a11, a12, a21, a22, b1, b2 = symbols('a11,a12,a21,a22,b1,b2')
581
582    soln = solve([a11*x + a12*y - b1, a21*x + a22*y - b2], x, y)
583    assert soln == {
584        x: (a22*b1 - a12*b2)/(a11*a22 - a12*a21),
585        y: (a11*b2 - a21*b1)/(a11*a22 - a12*a21),
586    }
587
588    assert solve(x - 1, x) == [1]
589    assert solve(3*x - 2, x) == [Rational(2, 3)]
590
591    soln = solve([a11*x.diff(t) + a12*y.diff(t) - b1, a21*x.diff(t) +
592            a22*y.diff(t) - b2], x.diff(t), y.diff(t))
593    assert soln == { y.diff(t): (a11*b2 - a21*b1)/(a11*a22 - a12*a21),
594            x.diff(t): (a22*b1 - a12*b2)/(a11*a22 - a12*a21) }
595
596    assert solve(x.diff(t) - 1, x.diff(t)) == [1]
597    assert solve(3*x.diff(t) - 2, x.diff(t)) == [Rational(2, 3)]
598
599    eqns = {3*x - 1, 2*y - 4}
600    assert solve(eqns, {x, y}) == { x: Rational(1, 3), y: 2 }
601    x = Symbol('x')
602    f = Function('f')
603    F = x**2 + f(x)**2 - 4*x - 1
604    assert solve(F.diff(x), diff(f(x), x)) == [(-x + 2)/f(x)]
605
606    # Mixed cased with a Symbol and a Function
607    x = Symbol('x')
608    y = Function('y')(t)
609
610    soln = solve([a11*x + a12*y.diff(t) - b1, a21*x +
611            a22*y.diff(t) - b2], x, y.diff(t))
612    assert soln == { y.diff(t): (a11*b2 - a21*b1)/(a11*a22 - a12*a21),
613            x: (a22*b1 - a12*b2)/(a11*a22 - a12*a21) }
614
615    # issue 13263
616    x = Symbol('x')
617    f = Function('f')
618    soln = solve([f(x).diff(x) + f(x).diff(x, 2) - 1, f(x).diff(x) - f(x).diff(x, 2)],
619            f(x).diff(x), f(x).diff(x, 2))
620    assert soln == { f(x).diff(x, 2): 1/2, f(x).diff(x): 1/2 }
621
622    soln = solve([f(x).diff(x, 2) + f(x).diff(x, 3) - 1, 1 - f(x).diff(x, 2) -
623            f(x).diff(x, 3), 1 - f(x).diff(x,3)], f(x).diff(x, 2), f(x).diff(x, 3))
624    assert soln == { f(x).diff(x, 2): 0, f(x).diff(x, 3): 1 }
625
626
627def test_issue_3725():
628    f = Function('f')
629    F = x**2 + f(x)**2 - 4*x - 1
630    e = F.diff(x)
631    assert solve(e, f(x).diff(x)) in [[(2 - x)/f(x)], [-((x - 2)/f(x))]]
632
633
634def test_issue_3870():
635    a, b, c, d = symbols('a b c d')
636    A = Matrix(2, 2, [a, b, c, d])
637    B = Matrix(2, 2, [0, 2, -3, 0])
638    C = Matrix(2, 2, [1, 2, 3, 4])
639
640    assert solve(A*B - C, [a, b, c, d]) == {a: 1, b: Rational(-1, 3), c: 2, d: -1}
641    assert solve([A*B - C], [a, b, c, d]) == {a: 1, b: Rational(-1, 3), c: 2, d: -1}
642    assert solve(Eq(A*B, C), [a, b, c, d]) == {a: 1, b: Rational(-1, 3), c: 2, d: -1}
643
644    assert solve([A*B - B*A], [a, b, c, d]) == {a: d, b: Rational(-2, 3)*c}
645    assert solve([A*C - C*A], [a, b, c, d]) == {a: d - c, b: Rational(2, 3)*c}
646    assert solve([A*B - B*A, A*C - C*A], [a, b, c, d]) == {a: d, b: 0, c: 0}
647
648    assert solve([Eq(A*B, B*A)], [a, b, c, d]) == {a: d, b: Rational(-2, 3)*c}
649    assert solve([Eq(A*C, C*A)], [a, b, c, d]) == {a: d - c, b: Rational(2, 3)*c}
650    assert solve([Eq(A*B, B*A), Eq(A*C, C*A)], [a, b, c, d]) == {a: d, b: 0, c: 0}
651
652
653def test_solve_linear():
654    w = Wild('w')
655    assert solve_linear(x, x) == (0, 1)
656    assert solve_linear(x, exclude=[x]) == (0, 1)
657    assert solve_linear(x, symbols=[w]) == (0, 1)
658    assert solve_linear(x, y - 2*x) in [(x, y/3), (y, 3*x)]
659    assert solve_linear(x, y - 2*x, exclude=[x]) == (y, 3*x)
660    assert solve_linear(3*x - y, 0) in [(x, y/3), (y, 3*x)]
661    assert solve_linear(3*x - y, 0, [x]) == (x, y/3)
662    assert solve_linear(3*x - y, 0, [y]) == (y, 3*x)
663    assert solve_linear(x**2/y, 1) == (y, x**2)
664    assert solve_linear(w, x) in [(w, x), (x, w)]
665    assert solve_linear(cos(x)**2 + sin(x)**2 + 2 + y) == \
666        (y, -2 - cos(x)**2 - sin(x)**2)
667    assert solve_linear(cos(x)**2 + sin(x)**2 + 2 + y, symbols=[x]) == (0, 1)
668    assert solve_linear(Eq(x, 3)) == (x, 3)
669    assert solve_linear(1/(1/x - 2)) == (0, 0)
670    assert solve_linear((x + 1)*exp(-x), symbols=[x]) == (x, -1)
671    assert solve_linear((x + 1)*exp(x), symbols=[x]) == ((x + 1)*exp(x), 1)
672    assert solve_linear(x*exp(-x**2), symbols=[x]) == (x, 0)
673    assert solve_linear(0**x - 1) == (0**x - 1, 1)
674    assert solve_linear(1 + 1/(x - 1)) == (x, 0)
675    eq = y*cos(x)**2 + y*sin(x)**2 - y  # = y*(1 - 1) = 0
676    assert solve_linear(eq) == (0, 1)
677    eq = cos(x)**2 + sin(x)**2  # = 1
678    assert solve_linear(eq) == (0, 1)
679    raises(ValueError, lambda: solve_linear(Eq(x, 3), 3))
680
681
682def test_solve_undetermined_coeffs():
683    assert solve_undetermined_coeffs(a*x**2 + b*x**2 + b*x + 2*c*x + c + 1, [a, b, c], x) == \
684        {a: -2, b: 2, c: -1}
685    # Test that rational functions work
686    assert solve_undetermined_coeffs(a/x + b/(x + 1) - (2*x + 1)/(x**2 + x), [a, b], x) == \
687        {a: 1, b: 1}
688    # Test cancellation in rational functions
689    assert solve_undetermined_coeffs(((c + 1)*a*x**2 + (c + 1)*b*x**2 +
690    (c + 1)*b*x + (c + 1)*2*c*x + (c + 1)**2)/(c + 1), [a, b, c], x) == \
691        {a: -2, b: 2, c: -1}
692
693
694def test_solve_inequalities():
695    x = Symbol('x')
696    sol = And(S.Zero < x, x < oo)
697    assert solve(x + 1 > 1) == sol
698    assert solve([x + 1 > 1]) == sol
699    assert solve([x + 1 > 1], x) == sol
700    assert solve([x + 1 > 1], [x]) == sol
701
702    system = [Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]
703    assert solve(system) == \
704        And(Or(And(Lt(-sqrt(2), x), Lt(x, -1)),
705               And(Lt(1, x), Lt(x, sqrt(2)))), Eq(0, 0))
706
707    x = Symbol('x', real=True)
708    system = [Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]
709    assert solve(system) == \
710        Or(And(Lt(-sqrt(2), x), Lt(x, -1)), And(Lt(1, x), Lt(x, sqrt(2))))
711
712    # issues 6627, 3448
713    assert solve((x - 3)/(x - 2) < 0, x) == And(Lt(2, x), Lt(x, 3))
714    assert solve(x/(x + 1) > 1, x) == And(Lt(-oo, x), Lt(x, -1))
715
716    assert solve(sin(x) > S.Half) == And(pi/6 < x, x < pi*Rational(5, 6))
717
718    assert solve(Eq(False, x < 1)) == (S.One <= x) & (x < oo)
719    assert solve(Eq(True, x < 1)) == (-oo < x) & (x < 1)
720    assert solve(Eq(x < 1, False)) == (S.One <= x) & (x < oo)
721    assert solve(Eq(x < 1, True)) == (-oo < x) & (x < 1)
722
723    assert solve(Eq(False, x)) == False
724    assert solve(Eq(0, x)) == [0]
725    assert solve(Eq(True, x)) == True
726    assert solve(Eq(1, x)) == [1]
727    assert solve(Eq(False, ~x)) == True
728    assert solve(Eq(True, ~x)) == False
729    assert solve(Ne(True, x)) == False
730    assert solve(Ne(1, x)) == (x > -oo) & (x < oo) & Ne(x, 1)
731
732
733def test_issue_4793():
734    assert solve(1/x) == []
735    assert solve(x*(1 - 5/x)) == [5]
736    assert solve(x + sqrt(x) - 2) == [1]
737    assert solve(-(1 + x)/(2 + x)**2 + 1/(2 + x)) == []
738    assert solve(-x**2 - 2*x + (x + 1)**2 - 1) == []
739    assert solve((x/(x + 1) + 3)**(-2)) == []
740    assert solve(x/sqrt(x**2 + 1), x) == [0]
741    assert solve(exp(x) - y, x) == [log(y)]
742    assert solve(exp(x)) == []
743    assert solve(x**2 + x + sin(y)**2 + cos(y)**2 - 1, x) in [[0, -1], [-1, 0]]
744    eq = 4*3**(5*x + 2) - 7
745    ans = solve(eq, x)
746    assert len(ans) == 5 and all(eq.subs(x, a).n(chop=True) == 0 for a in ans)
747    assert solve(log(x**2) - y**2/exp(x), x, y, set=True) == (
748        [x, y],
749        {(x, sqrt(exp(x) * log(x ** 2))), (x, -sqrt(exp(x) * log(x ** 2)))})
750    assert solve(x**2*z**2 - z**2*y**2) == [{x: -y}, {x: y}, {z: 0}]
751    assert solve((x - 1)/(1 + 1/(x - 1))) == []
752    assert solve(x**(y*z) - x, x) == [1]
753    raises(NotImplementedError, lambda: solve(log(x) - exp(x), x))
754    raises(NotImplementedError, lambda: solve(2**x - exp(x) - 3))
755
756
757def test_PR1964():
758    # issue 5171
759    assert solve(sqrt(x)) == solve(sqrt(x**3)) == [0]
760    assert solve(sqrt(x - 1)) == [1]
761    # issue 4462
762    a = Symbol('a')
763    assert solve(-3*a/sqrt(x), x) == []
764    # issue 4486
765    assert solve(2*x/(x + 2) - 1, x) == [2]
766    # issue 4496
767    assert set(solve((x**2/(7 - x)).diff(x))) == {S.Zero, S(14)}
768    # issue 4695
769    f = Function('f')
770    assert solve((3 - 5*x/f(x))*f(x), f(x)) == [x*Rational(5, 3)]
771    # issue 4497
772    assert solve(1/root(5 + x, 5) - 9, x) == [Rational(-295244, 59049)]
773
774    assert solve(sqrt(x) + sqrt(sqrt(x)) - 4) == [(Rational(-1, 2) + sqrt(17)/2)**4]
775    assert set(solve(Poly(sqrt(exp(x)) + sqrt(exp(-x)) - 4))) in \
776        [
777            {log((-sqrt(3) + 2)**2), log((sqrt(3) + 2)**2)},
778            {2*log(-sqrt(3) + 2), 2*log(sqrt(3) + 2)},
779            {log(-4*sqrt(3) + 7), log(4*sqrt(3) + 7)},
780        ]
781    assert set(solve(Poly(exp(x) + exp(-x) - 4))) == \
782        {log(-sqrt(3) + 2), log(sqrt(3) + 2)}
783    assert set(solve(x**y + x**(2*y) - 1, x)) == \
784        {(Rational(-1, 2) + sqrt(5)/2)**(1/y), (Rational(-1, 2) - sqrt(5)/2)**(1/y)}
785
786    assert solve(exp(x/y)*exp(-z/y) - 2, y) == [(x - z)/log(2)]
787    assert solve(
788        x**z*y**z - 2, z) in [[log(2)/(log(x) + log(y))], [log(2)/(log(x*y))]]
789    # if you do inversion too soon then multiple roots (as for the following)
790    # will be missed, e.g. if exp(3*x) = exp(3) -> 3*x = 3
791    E = S.Exp1
792    assert solve(exp(3*x) - exp(3), x) in [
793        [1, log(E*(Rational(-1, 2) - sqrt(3)*I/2)), log(E*(Rational(-1, 2) + sqrt(3)*I/2))],
794        [1, log(-E/2 - sqrt(3)*E*I/2), log(-E/2 + sqrt(3)*E*I/2)],
795        ]
796
797    # coverage test
798    p = Symbol('p', positive=True)
799    assert solve((1/p + 1)**(p + 1)) == []
800
801
802def test_issue_5197():
803    x = Symbol('x', real=True)
804    assert solve(x**2 + 1, x) == []
805    n = Symbol('n', integer=True, positive=True)
806    assert solve((n - 1)*(n + 2)*(2*n - 1), n) == [1]
807    x = Symbol('x', positive=True)
808    y = Symbol('y')
809    assert solve([x + 5*y - 2, -3*x + 6*y - 15], x, y) == []
810                 # not {x: -3, y: 1} b/c x is positive
811    # The solution following should not contain (-sqrt(2), sqrt(2))
812    assert solve((x + y)*n - y**2 + 2, x, y) == [(sqrt(2), -sqrt(2))]
813    y = Symbol('y', positive=True)
814    # The solution following should not contain {y: -x*exp(x/2)}
815    assert solve(x**2 - y**2/exp(x), y, x, dict=True) == [{y: x*exp(x/2)}]
816    x, y, z = symbols('x y z', positive=True)
817    assert solve(z**2*x**2 - z**2*y**2/exp(x), y, x, z, dict=True) == [{y: x*exp(x/2)}]
818
819
820def test_checking():
821    assert set(
822        solve(x*(x - y/x), x, check=False)) == {sqrt(y), S.Zero, -sqrt(y)}
823    assert set(solve(x*(x - y/x), x, check=True)) == {sqrt(y), -sqrt(y)}
824    # {x: 0, y: 4} sets denominator to 0 in the following so system should return None
825    assert solve((1/(1/x + 2), 1/(y - 3) - 1)) == []
826    # 0 sets denominator of 1/x to zero so None is returned
827    assert solve(1/(1/x + 2)) == []
828
829
830def test_issue_4671_4463_4467():
831    assert solve(sqrt(x**2 - 1) - 2) in ([sqrt(5), -sqrt(5)],
832                                           [-sqrt(5), sqrt(5)])
833    assert solve((2**exp(y**2/x) + 2)/(x**2 + 15), y) == [
834        -sqrt(x*log(1 + I*pi/log(2))), sqrt(x*log(1 + I*pi/log(2)))]
835
836    C1, C2 = symbols('C1 C2')
837    f = Function('f')
838    assert solve(C1 + C2/x**2 - exp(-f(x)), f(x)) == [log(x**2/(C1*x**2 + C2))]
839    a = Symbol('a')
840    E = S.Exp1
841    assert solve(1 - log(a + 4*x**2), x) in (
842        [-sqrt(-a + E)/2, sqrt(-a + E)/2],
843        [sqrt(-a + E)/2, -sqrt(-a + E)/2]
844    )
845    assert solve(log(a**(-3) - x**2)/a, x) in (
846        [-sqrt(-1 + a**(-3)), sqrt(-1 + a**(-3))],
847        [sqrt(-1 + a**(-3)), -sqrt(-1 + a**(-3))],)
848    assert solve(1 - log(a + 4*x**2), x) in (
849        [-sqrt(-a + E)/2, sqrt(-a + E)/2],
850        [sqrt(-a + E)/2, -sqrt(-a + E)/2],)
851    assert solve((a**2 + 1)*(sin(a*x) + cos(a*x)), x) == [-pi/(4*a)]
852    assert solve(3 - (sinh(a*x) + cosh(a*x)), x) == [log(3)/a]
853    assert set(solve(3 - (sinh(a*x) + cosh(a*x)**2), x)) == \
854        {log(-2 + sqrt(5))/a, log(-sqrt(2) + 1)/a,
855        log(-sqrt(5) - 2)/a, log(1 + sqrt(2))/a}
856    assert solve(atan(x) - 1) == [tan(1)]
857
858
859def test_issue_5132():
860    r, t = symbols('r,t')
861    assert set(solve([r - x**2 - y**2, tan(t) - y/x], [x, y])) == \
862        {(
863            -sqrt(r*cos(t)**2), -1*sqrt(r*cos(t)**2)*tan(t)),
864            (sqrt(r*cos(t)**2), sqrt(r*cos(t)**2)*tan(t))}
865    assert solve([exp(x) - sin(y), 1/y - 3], [x, y]) == \
866        [(log(sin(Rational(1, 3))), Rational(1, 3))]
867    assert solve([exp(x) - sin(y), 1/exp(y) - 3], [x, y]) == \
868        [(log(-sin(log(3))), -log(3))]
869    assert set(solve([exp(x) - sin(y), y**2 - 4], [x, y])) == \
870        {(log(-sin(2)), -S(2)), (log(sin(2)), S(2))}
871    eqs = [exp(x)**2 - sin(y) + z**2, 1/exp(y) - 3]
872    assert solve(eqs, set=True) == \
873        ([y, z], {
874        (-log(3), sqrt(-exp(2*x) - sin(log(3)))),
875        (-log(3), -sqrt(-exp(2*x) - sin(log(3))))})
876    assert solve(eqs, x, z, set=True) == (
877        [x, z],
878        {(x, sqrt(-exp(2*x) + sin(y))), (x, -sqrt(-exp(2*x) + sin(y)))})
879    assert set(solve(eqs, x, y)) == \
880        {
881            (log(-sqrt(-z**2 - sin(log(3)))), -log(3)),
882        (log(-z**2 - sin(log(3)))/2, -log(3))}
883    assert set(solve(eqs, y, z)) == \
884        {
885            (-log(3), -sqrt(-exp(2*x) - sin(log(3)))),
886        (-log(3), sqrt(-exp(2*x) - sin(log(3))))}
887    eqs = [exp(x)**2 - sin(y) + z, 1/exp(y) - 3]
888    assert solve(eqs, set=True) == ([y, z], {
889        (-log(3), -exp(2*x) - sin(log(3)))})
890    assert solve(eqs, x, z, set=True) == (
891        [x, z], {(x, -exp(2*x) + sin(y))})
892    assert set(solve(eqs, x, y)) == {
893            (log(-sqrt(-z - sin(log(3)))), -log(3)),
894            (log(-z - sin(log(3)))/2, -log(3))}
895    assert solve(eqs, z, y) == \
896        [(-exp(2*x) - sin(log(3)), -log(3))]
897    assert solve((sqrt(x**2 + y**2) - sqrt(10), x + y - 4), set=True) == (
898        [x, y], {(S.One, S(3)), (S(3), S.One)})
899    assert set(solve((sqrt(x**2 + y**2) - sqrt(10), x + y - 4), x, y)) == \
900        {(S.One, S(3)), (S(3), S.One)}
901
902
903def test_issue_5335():
904    lam, a0, conc = symbols('lam a0 conc')
905    a = 0.005
906    b = 0.743436700916726
907    eqs = [lam + 2*y - a0*(1 - x/2)*x - a*x/2*x,
908           a0*(1 - x/2)*x - 1*y - b*y,
909           x + y - conc]
910    sym = [x, y, a0]
911    # there are 4 solutions obtained manually but only two are valid
912    assert len(solve(eqs, sym, manual=True, minimal=True)) == 2
913    assert len(solve(eqs, sym)) == 2  # cf below with rational=False
914
915
916@SKIP("Hangs")
917def _test_issue_5335_float():
918    # gives ZeroDivisionError: polynomial division
919    lam, a0, conc = symbols('lam a0 conc')
920    a = 0.005
921    b = 0.743436700916726
922    eqs = [lam + 2*y - a0*(1 - x/2)*x - a*x/2*x,
923           a0*(1 - x/2)*x - 1*y - b*y,
924           x + y - conc]
925    sym = [x, y, a0]
926    assert len(solve(eqs, sym, rational=False)) == 2
927
928
929def test_issue_5767():
930    assert set(solve([x**2 + y + 4], [x])) == \
931        {(-sqrt(-y - 4),), (sqrt(-y - 4),)}
932
933
934def test_polysys():
935    assert set(solve([x**2 + 2/y - 2, x + y - 3], [x, y])) == \
936        {(S.One, S(2)), (1 + sqrt(5), 2 - sqrt(5)),
937        (1 - sqrt(5), 2 + sqrt(5))}
938    assert solve([x**2 + y - 2, x**2 + y]) == []
939    # the ordering should be whatever the user requested
940    assert solve([x**2 + y - 3, x - y - 4], (x, y)) != solve([x**2 +
941                 y - 3, x - y - 4], (y, x))
942
943
944@slow
945def test_unrad1():
946    raises(NotImplementedError, lambda:
947        unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x)) + 3))
948    raises(NotImplementedError, lambda:
949        unrad(sqrt(x) + (x + 1)**Rational(1, 3) + 2*sqrt(y)))
950
951    s = symbols('s', cls=Dummy)
952
953    # checkers to deal with possibility of answer coming
954    # back with a sign change (cf issue 5203)
955    def check(rv, ans):
956        assert bool(rv[1]) == bool(ans[1])
957        if ans[1]:
958            return s_check(rv, ans)
959        e = rv[0].expand()
960        a = ans[0].expand()
961        return e in [a, -a] and rv[1] == ans[1]
962
963    def s_check(rv, ans):
964        # get the dummy
965        rv = list(rv)
966        d = rv[0].atoms(Dummy)
967        reps = list(zip(d, [s]*len(d)))
968        # replace s with this dummy
969        rv = (rv[0].subs(reps).expand(), [rv[1][0].subs(reps), rv[1][1].subs(reps)])
970        ans = (ans[0].subs(reps).expand(), [ans[1][0].subs(reps), ans[1][1].subs(reps)])
971        return str(rv[0]) in [str(ans[0]), str(-ans[0])] and \
972            str(rv[1]) == str(ans[1])
973
974    assert unrad(1) is None
975    assert check(unrad(sqrt(x)),
976        (x, []))
977    assert check(unrad(sqrt(x) + 1),
978        (x - 1, []))
979    assert check(unrad(sqrt(x) + root(x, 3) + 2),
980        (s**3 + s**2 + 2, [s, s**6 - x]))
981    assert check(unrad(sqrt(x)*root(x, 3) + 2),
982        (x**5 - 64, []))
983    assert check(unrad(sqrt(x) + (x + 1)**Rational(1, 3)),
984        (x**3 - (x + 1)**2, []))
985    assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(2*x)),
986        (-2*sqrt(2)*x - 2*x + 1, []))
987    assert check(unrad(sqrt(x) + sqrt(x + 1) + 2),
988        (16*x - 9, []))
989    assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - x)),
990        (5*x**2 - 4*x, []))
991    assert check(unrad(a*sqrt(x) + b*sqrt(x) + c*sqrt(y) + d*sqrt(y)),
992        ((a*sqrt(x) + b*sqrt(x))**2 - (c*sqrt(y) + d*sqrt(y))**2, []))
993    assert check(unrad(sqrt(x) + sqrt(1 - x)),
994        (2*x - 1, []))
995    assert check(unrad(sqrt(x) + sqrt(1 - x) - 3),
996        (x**2 - x + 16, []))
997    assert check(unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x)),
998        (5*x**2 - 2*x + 1, []))
999    assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - 3) in [
1000        (25*x**4 + 376*x**3 + 1256*x**2 - 2272*x + 784, []),
1001        (25*x**8 - 476*x**6 + 2534*x**4 - 1468*x**2 + 169, [])]
1002    assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - sqrt(1 - 2*x)) == \
1003        (41*x**4 + 40*x**3 + 232*x**2 - 160*x + 16, [])  # orig root at 0.487
1004    assert check(unrad(sqrt(x) + sqrt(x + 1)), (S.One, []))
1005
1006    eq = sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x))
1007    assert check(unrad(eq),
1008        (16*x**2 - 9*x, []))
1009    assert set(solve(eq, check=False)) == {S.Zero, Rational(9, 16)}
1010    assert solve(eq) == []
1011    # but this one really does have those solutions
1012    assert set(solve(sqrt(x) - sqrt(x + 1) + sqrt(1 - sqrt(x)))) == \
1013        {S.Zero, Rational(9, 16)}
1014
1015    assert check(unrad(sqrt(x) + root(x + 1, 3) + 2*sqrt(y), y),
1016        (S('2*sqrt(x)*(x + 1)**(1/3) + x - 4*y + (x + 1)**(2/3)'), []))
1017    assert check(unrad(sqrt(x/(1 - x)) + (x + 1)**Rational(1, 3)),
1018        (x**5 - x**4 - x**3 + 2*x**2 + x - 1, []))
1019    assert check(unrad(sqrt(x/(1 - x)) + 2*sqrt(y), y),
1020        (4*x*y + x - 4*y, []))
1021    assert check(unrad(sqrt(x)*sqrt(1 - x) + 2, x),
1022        (x**2 - x + 4, []))
1023
1024    # http://tutorial.math.lamar.edu/
1025    #        Classes/Alg/SolveRadicalEqns.aspx#Solve_Rad_Ex2_a
1026    assert solve(Eq(x, sqrt(x + 6))) == [3]
1027    assert solve(Eq(x + sqrt(x - 4), 4)) == [4]
1028    assert solve(Eq(1, x + sqrt(2*x - 3))) == []
1029    assert set(solve(Eq(sqrt(5*x + 6) - 2, x))) == {-S.One, S(2)}
1030    assert set(solve(Eq(sqrt(2*x - 1) - sqrt(x - 4), 2))) == {S(5), S(13)}
1031    assert solve(Eq(sqrt(x + 7) + 2, sqrt(3 - x))) == [-6]
1032    # http://www.purplemath.com/modules/solverad.htm
1033    assert solve((2*x - 5)**Rational(1, 3) - 3) == [16]
1034    assert set(solve(x + 1 - root(x**4 + 4*x**3 - x, 4))) == \
1035        {Rational(-1, 2), Rational(-1, 3)}
1036    assert set(solve(sqrt(2*x**2 - 7) - (3 - x))) == {-S(8), S(2)}
1037    assert solve(sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4)) == [0]
1038    assert solve(sqrt(x + 4) + sqrt(2*x - 1) - 3*sqrt(x - 1)) == [5]
1039    assert solve(sqrt(x)*sqrt(x - 7) - 12) == [16]
1040    assert solve(sqrt(x - 3) + sqrt(x) - 3) == [4]
1041    assert solve(sqrt(9*x**2 + 4) - (3*x + 2)) == [0]
1042    assert solve(sqrt(x) - 2 - 5) == [49]
1043    assert solve(sqrt(x - 3) - sqrt(x) - 3) == []
1044    assert solve(sqrt(x - 1) - x + 7) == [10]
1045    assert solve(sqrt(x - 2) - 5) == [27]
1046    assert solve(sqrt(17*x - sqrt(x**2 - 5)) - 7) == [3]
1047    assert solve(sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x))) == []
1048
1049    # don't posify the expression in unrad and do use _mexpand
1050    z = sqrt(2*x + 1)/sqrt(x) - sqrt(2 + 1/x)
1051    p = posify(z)[0]
1052    assert solve(p) == []
1053    assert solve(z) == []
1054    assert solve(z + 6*I) == [Rational(-1, 11)]
1055    assert solve(p + 6*I) == []
1056    # issue 8622
1057    assert unrad(root(x + 1, 5) - root(x, 3)) == (
1058        -(x**5 - x**3 - 3*x**2 - 3*x - 1), [])
1059    # issue #8679
1060    assert check(unrad(x + root(x, 3) + root(x, 3)**2 + sqrt(y), x),
1061        (s**3 + s**2 + s + sqrt(y), [s, s**3 - x]))
1062
1063    # for coverage
1064    assert check(unrad(sqrt(x) + root(x, 3) + y),
1065        (s**3 + s**2 + y, [s, s**6 - x]))
1066    assert solve(sqrt(x) + root(x, 3) - 2) == [1]
1067    raises(NotImplementedError, lambda:
1068        solve(sqrt(x) + root(x, 3) + root(x + 1, 5) - 2))
1069    # fails through a different code path
1070    raises(NotImplementedError, lambda: solve(-sqrt(2) + cosh(x)/x))
1071    # unrad some
1072    assert solve(sqrt(x + root(x, 3))+root(x - y, 5), y) == [
1073        x + (x**Rational(1, 3) + x)**Rational(5, 2)]
1074    assert check(unrad(sqrt(x) - root(x + 1, 3)*sqrt(x + 2) + 2),
1075        (s**10 + 8*s**8 + 24*s**6 - 12*s**5 - 22*s**4 - 160*s**3 - 212*s**2 -
1076        192*s - 56, [s, s**2 - x]))
1077    e = root(x + 1, 3) + root(x, 3)
1078    assert unrad(e) == (2*x + 1, [])
1079    eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5)
1080    assert check(unrad(eq),
1081        (15625*x**4 + 173000*x**3 + 355600*x**2 - 817920*x + 331776, []))
1082    assert check(unrad(root(x, 4) + root(x, 4)**3 - 1),
1083        (s**3 + s - 1, [s, s**4 - x]))
1084    assert check(unrad(root(x, 2) + root(x, 2)**3 - 1),
1085        (x**3 + 2*x**2 + x - 1, []))
1086    assert unrad(x**0.5) is None
1087    assert check(unrad(t + root(x + y, 5) + root(x + y, 5)**3),
1088        (s**3 + s + t, [s, s**5 - x - y]))
1089    assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, y),
1090        (s**3 + s + x, [s, s**5 - x - y]))
1091    assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, x),
1092        (s**5 + s**3 + s - y, [s, s**5 - x - y]))
1093    assert check(unrad(root(x - 1, 3) + root(x + 1, 5) + root(2, 5)),
1094        (s**5 + 5*2**Rational(1, 5)*s**4 + s**3 + 10*2**Rational(2, 5)*s**3 +
1095        10*2**Rational(3, 5)*s**2 + 5*2**Rational(4, 5)*s + 4, [s, s**3 - x + 1]))
1096    raises(NotImplementedError, lambda:
1097        unrad((root(x, 2) + root(x, 3) + root(x, 4)).subs(x, x**5 - x + 1)))
1098
1099    # the simplify flag should be reset to False for unrad results;
1100    # if it's not then this next test will take a long time
1101    assert solve(root(x, 3) + root(x, 5) - 2) == [1]
1102    eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5)
1103    assert check(unrad(eq),
1104        ((5*x - 4)*(3125*x**3 + 37100*x**2 + 100800*x - 82944), []))
1105    ans = S('''
1106        [4/5, -1484/375 + 172564/(140625*(114*sqrt(12657)/78125 +
1107        12459439/52734375)**(1/3)) +
1108        4*(114*sqrt(12657)/78125 + 12459439/52734375)**(1/3)]''')
1109    assert solve(eq) == ans
1110    # duplicate radical handling
1111    assert check(unrad(sqrt(x + root(x + 1, 3)) - root(x + 1, 3) - 2),
1112        (s**3 - s**2 - 3*s - 5, [s, s**3 - x - 1]))
1113    # cov post-processing
1114    e = root(x**2 + 1, 3) - root(x**2 - 1, 5) - 2
1115    assert check(unrad(e),
1116        (s**5 - 10*s**4 + 39*s**3 - 80*s**2 + 80*s - 30,
1117        [s, s**3 - x**2 - 1]))
1118
1119    e = sqrt(x + root(x + 1, 2)) - root(x + 1, 3) - 2
1120    assert check(unrad(e),
1121        (s**6 - 2*s**5 - 7*s**4 - 3*s**3 + 26*s**2 + 40*s + 25,
1122        [s, s**3 - x - 1]))
1123    assert check(unrad(e, _reverse=True),
1124        (s**6 - 14*s**5 + 73*s**4 - 187*s**3 + 276*s**2 - 228*s + 89,
1125        [s, s**2 - x - sqrt(x + 1)]))
1126    # this one needs r0, r1 reversal to work
1127    assert check(unrad(sqrt(x + sqrt(root(x, 3) - 1)) - root(x, 6) - 2),
1128        (s**12 - 2*s**8 - 8*s**7 - 8*s**6 + s**4 + 8*s**3 + 23*s**2 +
1129        32*s + 17, [s, s**6 - x]))
1130
1131    # why does this pass
1132    assert unrad(root(cosh(x), 3)/x*root(x + 1, 5) - 1) == (
1133        -(x**15 - x**3*cosh(x)**5 - 3*x**2*cosh(x)**5 - 3*x*cosh(x)**5
1134        - cosh(x)**5), [])
1135    # and this fail?
1136    #assert unrad(sqrt(cosh(x)/x) + root(x + 1, 3)*sqrt(x) - 1) == (
1137    #    -s**6 + 6*s**5 - 15*s**4 + 20*s**3 - 15*s**2 + 6*s + x**5 +
1138    #    2*x**4 + x**3 - 1, [s, s**2 - cosh(x)/x])
1139
1140    # watch for symbols in exponents
1141    assert unrad(S('(x+y)**(2*y/3) + (x+y)**(1/3) + 1')) is None
1142    assert check(unrad(S('(x+y)**(2*y/3) + (x+y)**(1/3) + 1'), x),
1143        (s**(2*y) + s + 1, [s, s**3 - x - y]))
1144    # should _Q be so lenient?
1145    assert unrad(x**(S.Half/y) + y, x) == (x**(1/y) - y**2, [])
1146
1147    # This tests two things: that if full unrad is attempted and fails
1148    # the solution should still be found; also it tests that the use of
1149    # composite
1150    assert len(solve(sqrt(y)*x + x**3 - 1, x)) == 3
1151    assert len(solve(-512*y**3 + 1344*(x + 2)**Rational(1, 3)*y**2 -
1152        1176*(x + 2)**Rational(2, 3)*y - 169*x + 686, y, _unrad=False)) == 3
1153
1154    # watch out for when the cov doesn't involve the symbol of interest
1155    eq = S('-x + (7*y/8 - (27*x/2 + 27*sqrt(x**2)/2)**(1/3)/3)**3 - 1')
1156    assert solve(eq, y) == [
1157        2**(S(2)/3)*(27*x + 27*sqrt(x**2))**(S(1)/3)*S(4)/21 + (512*x/343 +
1158        S(512)/343)**(S(1)/3)*(-S(1)/2 - sqrt(3)*I/2), 2**(S(2)/3)*(27*x +
1159        27*sqrt(x**2))**(S(1)/3)*S(4)/21 + (512*x/343 +
1160        S(512)/343)**(S(1)/3)*(-S(1)/2 + sqrt(3)*I/2), 2**(S(2)/3)*(27*x +
1161        27*sqrt(x**2))**(S(1)/3)*S(4)/21 + (512*x/343 + S(512)/343)**(S(1)/3)]
1162
1163    eq = root(x + 1, 3) - (root(x, 3) + root(x, 5))
1164    assert check(unrad(eq),
1165        (3*s**13 + 3*s**11 + s**9 - 1, [s, s**15 - x]))
1166    assert check(unrad(eq - 2),
1167        (3*s**13 + 3*s**11 + 6*s**10 + s**9 + 12*s**8 + 6*s**6 + 12*s**5 +
1168        12*s**3 + 7, [s, s**15 - x]))
1169    assert check(unrad(root(x, 3) - root(x + 1, 4)/2 + root(x + 2, 3)),
1170        (s*(4096*s**9 + 960*s**8 + 48*s**7 - s**6 - 1728),
1171        [s, s**4 - x - 1]))  # orig expr has two real roots: -1, -.389
1172    assert check(unrad(root(x, 3) + root(x + 1, 4) - root(x + 2, 3)/2),
1173        (343*s**13 + 2904*s**12 + 1344*s**11 + 512*s**10 - 1323*s**9 -
1174        3024*s**8 - 1728*s**7 + 1701*s**5 + 216*s**4 - 729*s, [s, s**4 - x -
1175        1]))  # orig expr has one real root: -0.048
1176    assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3)),
1177        (729*s**13 - 216*s**12 + 1728*s**11 - 512*s**10 + 1701*s**9 -
1178        3024*s**8 + 1344*s**7 + 1323*s**5 - 2904*s**4 + 343*s, [s, s**4 - x -
1179        1]))  # orig expr has 2 real roots: -0.91, -0.15
1180    assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3) - 2),
1181        (729*s**13 + 1242*s**12 + 18496*s**10 + 129701*s**9 + 388602*s**8 +
1182        453312*s**7 - 612864*s**6 - 3337173*s**5 - 6332418*s**4 - 7134912*s**3
1183        - 5064768*s**2 - 2111913*s - 398034, [s, s**4 - x - 1]))
1184        # orig expr has 1 real root: 19.53
1185
1186    ans = solve(sqrt(x) + sqrt(x + 1) -
1187                sqrt(1 - x) - sqrt(2 + x))
1188    assert len(ans) == 1 and NS(ans[0])[:4] == '0.73'
1189    # the fence optimization problem
1190    # https://github.com/sympy/sympy/issues/4793#issuecomment-36994519
1191    F = Symbol('F')
1192    eq = F - (2*x + 2*y + sqrt(x**2 + y**2))
1193    ans = F*Rational(2, 7) - sqrt(2)*F/14
1194    X = solve(eq, x, check=False)
1195    for xi in reversed(X):  # reverse since currently, ans is the 2nd one
1196        Y = solve((x*y).subs(x, xi).diff(y), y, simplify=False, check=False)
1197        if any((a - ans).expand().is_zero for a in Y):
1198            break
1199    else:
1200        assert None  # no answer was found
1201    assert solve(sqrt(x + 1) + root(x, 3) - 2) == S('''
1202        [(-11/(9*(47/54 + sqrt(93)/6)**(1/3)) + 1/3 + (47/54 +
1203        sqrt(93)/6)**(1/3))**3]''')
1204    assert solve(sqrt(sqrt(x + 1)) + x**Rational(1, 3) - 2) == S('''
1205        [(-sqrt(-2*(-1/16 + sqrt(6913)/16)**(1/3) + 6/(-1/16 +
1206        sqrt(6913)/16)**(1/3) + 17/2 + 121/(4*sqrt(-6/(-1/16 +
1207        sqrt(6913)/16)**(1/3) + 2*(-1/16 + sqrt(6913)/16)**(1/3) + 17/4)))/2 +
1208        sqrt(-6/(-1/16 + sqrt(6913)/16)**(1/3) + 2*(-1/16 +
1209        sqrt(6913)/16)**(1/3) + 17/4)/2 + 9/4)**3]''')
1210    assert solve(sqrt(x) + root(sqrt(x) + 1, 3) - 2) == S('''
1211        [(-(81/2 + 3*sqrt(741)/2)**(1/3)/3 + (81/2 + 3*sqrt(741)/2)**(-1/3) +
1212        2)**2]''')
1213    eq = S('''
1214        -x + (1/2 - sqrt(3)*I/2)*(3*x**3/2 - x*(3*x**2 - 34)/2 + sqrt((-3*x**3
1215        + x*(3*x**2 - 34) + 90)**2/4 - 39304/27) - 45)**(1/3) + 34/(3*(1/2 -
1216        sqrt(3)*I/2)*(3*x**3/2 - x*(3*x**2 - 34)/2 + sqrt((-3*x**3 + x*(3*x**2
1217        - 34) + 90)**2/4 - 39304/27) - 45)**(1/3))''')
1218    assert check(unrad(eq),
1219        (s*-(-s**6 + sqrt(3)*s**6*I - 153*2**Rational(2, 3)*3**Rational(1, 3)*s**4 +
1220        51*12**Rational(1, 3)*s**4 - 102*2**Rational(2, 3)*3**Rational(5, 6)*s**4*I - 1620*s**3 +
1221        1620*sqrt(3)*s**3*I + 13872*18**Rational(1, 3)*s**2 - 471648 +
1222        471648*sqrt(3)*I), [s, s**3 - 306*x - sqrt(3)*sqrt(31212*x**2 -
1223        165240*x + 61484) + 810]))
1224
1225    assert solve(eq) == [] # not other code errors
1226    eq = root(x, 3) - root(y, 3) + root(x, 5)
1227    assert check(unrad(eq),
1228           (s**15 + 3*s**13 + 3*s**11 + s**9 - y, [s, s**15 - x]))
1229    eq = root(x, 3) + root(y, 3) + root(x*y, 4)
1230    assert check(unrad(eq),
1231                 (s*y*(-s**12 - 3*s**11*y - 3*s**10*y**2 - s**9*y**3 -
1232                       3*s**8*y**2 + 21*s**7*y**3 - 3*s**6*y**4 - 3*s**4*y**4 -
1233                       3*s**3*y**5 - y**6), [s, s**4 - x*y]))
1234    raises(NotImplementedError,
1235           lambda: unrad(root(x, 3) + root(y, 3) + root(x*y, 5)))
1236
1237    # Test unrad with an Equality
1238    eq = Eq(-x**(S(1)/5) + x**(S(1)/3), -3**(S(1)/3) - (-1)**(S(3)/5)*3**(S(1)/5))
1239    assert check(unrad(eq),
1240        (-s**5 + s**3 - 3**(S(1)/3) - (-1)**(S(3)/5)*3**(S(1)/5), [s, s**15 - x]))
1241
1242    # make sure buried radicals are exposed
1243    s = sqrt(x) - 1
1244    assert unrad(s**2 - s**3) == (x**3 - 6*x**2 + 9*x - 4, [])
1245    # make sure numerators which are already polynomial are rejected
1246    assert unrad((x/(x + 1) + 3)**(-2), x) is None
1247
1248
1249@slow
1250def test_unrad_slow():
1251    # this has roots with multiplicity > 1; there should be no
1252    # repeats in roots obtained, however
1253    eq = (sqrt(1 + sqrt(1 - 4*x**2)) - x*(1 + sqrt(1 + 2*sqrt(1 - 4*x**2))))
1254    assert solve(eq) == [S.Half]
1255
1256
1257@XFAIL
1258def test_unrad_fail():
1259    # this only works if we check real_root(eq.subs(x, Rational(1, 3)))
1260    # but checksol doesn't work like that
1261    assert solve(root(x**3 - 3*x**2, 3) + 1 - x) == [Rational(1, 3)]
1262    assert solve(root(x + 1, 3) + root(x**2 - 2, 5) + 1) == [
1263        -1, -1 + CRootOf(x**5 + x**4 + 5*x**3 + 8*x**2 + 10*x + 5, 0)**3]
1264
1265
1266def test_checksol():
1267    x, y, r, t = symbols('x, y, r, t')
1268    eq = r - x**2 - y**2
1269    dict_var_soln = {y: - sqrt(r) / sqrt(tan(t)**2 + 1),
1270        x: -sqrt(r)*tan(t)/sqrt(tan(t)**2 + 1)}
1271    assert checksol(eq, dict_var_soln) == True
1272    assert checksol(Eq(x, False), {x: False}) is True
1273    assert checksol(Ne(x, False), {x: False}) is False
1274    assert checksol(Eq(x < 1, True), {x: 0}) is True
1275    assert checksol(Eq(x < 1, True), {x: 1}) is False
1276    assert checksol(Eq(x < 1, False), {x: 1}) is True
1277    assert checksol(Eq(x < 1, False), {x: 0}) is False
1278    assert checksol(Eq(x + 1, x**2 + 1), {x: 1}) is True
1279    assert checksol([x - 1, x**2 - 1], x, 1) is True
1280    assert checksol([x - 1, x**2 - 2], x, 1) is False
1281    assert checksol(Poly(x**2 - 1), x, 1) is True
1282    raises(ValueError, lambda: checksol(x, 1))
1283    raises(ValueError, lambda: checksol([], x, 1))
1284
1285
1286def test__invert():
1287    assert _invert(x - 2) == (2, x)
1288    assert _invert(2) == (2, 0)
1289    assert _invert(exp(1/x) - 3, x) == (1/log(3), x)
1290    assert _invert(exp(1/x + a/x) - 3, x) == ((a + 1)/log(3), x)
1291    assert _invert(a, x) == (a, 0)
1292
1293
1294def test_issue_4463():
1295    assert solve(-a*x + 2*x*log(x), x) == [exp(a/2)]
1296    assert solve(x**x) == []
1297    assert solve(x**x - 2) == [exp(LambertW(log(2)))]
1298    assert solve(((x - 3)*(x - 2))**((x - 3)*(x - 4))) == [2]
1299
1300@slow
1301def test_issue_5114_solvers():
1302    a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r = symbols('a:r')
1303
1304    # there is no 'a' in the equation set but this is how the
1305    # problem was originally posed
1306    syms = a, b, c, f, h, k, n
1307    eqs = [b + r/d - c/d,
1308    c*(1/d + 1/e + 1/g) - f/g - r/d,
1309        f*(1/g + 1/i + 1/j) - c/g - h/i,
1310        h*(1/i + 1/l + 1/m) - f/i - k/m,
1311        k*(1/m + 1/o + 1/p) - h/m - n/p,
1312        n*(1/p + 1/q) - k/p]
1313    assert len(solve(eqs, syms, manual=True, check=False, simplify=False)) == 1
1314
1315
1316def test_issue_5849():
1317    #
1318    # XXX: This system does not have a solution for most values of the
1319    # parameters. Generally solve returns the empty set for systems that are
1320    # generically inconsistent.
1321    #
1322    I1, I2, I3, I4, I5, I6 = symbols('I1:7')
1323    dI1, dI4, dQ2, dQ4, Q2, Q4 = symbols('dI1,dI4,dQ2,dQ4,Q2,Q4')
1324
1325    e = (
1326        I1 - I2 - I3,
1327        I3 - I4 - I5,
1328        I4 + I5 - I6,
1329        -I1 + I2 + I6,
1330        -2*I1 - 2*I3 - 2*I5 - 3*I6 - dI1/2 + 12,
1331        -I4 + dQ4,
1332        -I2 + dQ2,
1333        2*I3 + 2*I5 + 3*I6 - Q2,
1334        I4 - 2*I5 + 2*Q4 + dI4
1335    )
1336
1337    ans = [{
1338    I1: I2 + I3,
1339    dI1: -4*I2 - 8*I3 - 4*I5 - 6*I6 + 24,
1340    I4: I3 - I5,
1341    dQ4: I3 - I5,
1342    Q4: -I3/2 + 3*I5/2 - dI4/2,
1343    dQ2: I2,
1344    Q2: 2*I3 + 2*I5 + 3*I6}]
1345
1346    v = I1, I4, Q2, Q4, dI1, dI4, dQ2, dQ4
1347    assert solve(e, *v, manual=True, check=False, dict=True) == ans
1348    assert solve(e, *v, manual=True, check=False) == ans[0]
1349    assert solve(e, *v, manual=True) == []
1350    assert solve(e, *v) == []
1351
1352    # the matrix solver (tested below) doesn't like this because it produces
1353    # a zero row in the matrix. Is this related to issue 4551?
1354    assert [ei.subs(
1355        ans[0]) for ei in e] == [0, 0, I3 - I6, -I3 + I6, 0, 0, 0, 0, 0]
1356
1357
1358def test_issue_5849_matrix():
1359    '''Same as test_issue_5849 but solved with the matrix solver.
1360
1361    A solution only exists if I3 == I6 which is not generically true,
1362    but `solve` does not return conditions under which the solution is
1363    valid, only a solution that is canonical and consistent with the input.
1364    '''
1365    # a simple example with the same issue
1366    # assert solve([x+y+z, x+y], [x, y]) == {x: y}
1367    # the longer example
1368    I1, I2, I3, I4, I5, I6 = symbols('I1:7')
1369    dI1, dI4, dQ2, dQ4, Q2, Q4 = symbols('dI1,dI4,dQ2,dQ4,Q2,Q4')
1370
1371    e = (
1372        I1 - I2 - I3,
1373        I3 - I4 - I5,
1374        I4 + I5 - I6,
1375        -I1 + I2 + I6,
1376        -2*I1 - 2*I3 - 2*I5 - 3*I6 - dI1/2 + 12,
1377        -I4 + dQ4,
1378        -I2 + dQ2,
1379        2*I3 + 2*I5 + 3*I6 - Q2,
1380        I4 - 2*I5 + 2*Q4 + dI4
1381    )
1382    assert solve(e, I1, I4, Q2, Q4, dI1, dI4, dQ2, dQ4) == []
1383
1384
1385def test_issue_21882():
1386
1387    a, b, c, d, f, g, k = unknowns = symbols('a, b, c, d, f, g, k')
1388
1389    equations = [
1390        -k*a + b + 5*f/6 + 2*c/9 + 5*d/6 + 4*a/3,
1391        -k*f + 4*f/3 + d/2,
1392        -k*d + f/6 + d,
1393        13*b/18 + 13*c/18 + 13*a/18,
1394        -k*c + b/2 + 20*c/9 + a,
1395        -k*b + b + c/18 + a/6,
1396        5*b/3 + c/3 + a,
1397        2*b/3 + 2*c + 4*a/3,
1398        -g,
1399    ]
1400
1401    answer = [
1402        {a: 0, f: 0, b: 0, d: 0, c: 0, g: 0},
1403        {a: 0, f: -d, b: 0, k: S(5)/6, c: 0, g: 0},
1404        {a: -2*c, f: 0, b: c, d: 0, k: S(13)/18, g: 0},
1405    ]
1406
1407    assert solve(equations, unknowns, dict=True) == answer
1408
1409
1410def test_issue_5901():
1411    f, g, h = map(Function, 'fgh')
1412    a = Symbol('a')
1413    D = Derivative(f(x), x)
1414    G = Derivative(g(a), a)
1415    assert solve(f(x) + f(x).diff(x), f(x)) == \
1416        [-D]
1417    assert solve(f(x) - 3, f(x)) == \
1418        [3]
1419    assert solve(f(x) - 3*f(x).diff(x), f(x)) == \
1420        [3*D]
1421    assert solve([f(x) - 3*f(x).diff(x)], f(x)) == \
1422        {f(x): 3*D}
1423    assert solve([f(x) - 3*f(x).diff(x), f(x)**2 - y + 4], f(x), y) == \
1424        [{f(x): 3*D, y: 9*D**2 + 4}]
1425    assert solve(-f(a)**2*g(a)**2 + f(a)**2*h(a)**2 + g(a).diff(a),
1426                h(a), g(a), set=True) == \
1427        ([g(a)], {
1428        (-sqrt(h(a)**2*f(a)**2 + G)/f(a),),
1429        (sqrt(h(a)**2*f(a)**2+ G)/f(a),)})
1430    args = [f(x).diff(x, 2)*(f(x) + g(x)) - g(x)**2 + 2, f(x), g(x)]
1431    assert set(solve(*args)) == \
1432        {(-sqrt(2), sqrt(2)), (sqrt(2), -sqrt(2))}
1433    eqs = [f(x)**2 + g(x) - 2*f(x).diff(x), g(x)**2 - 4]
1434    assert solve(eqs, f(x), g(x), set=True) == \
1435        ([f(x), g(x)], {
1436        (-sqrt(2*D - 2), S(2)),
1437        (sqrt(2*D - 2), S(2)),
1438        (-sqrt(2*D + 2), -S(2)),
1439        (sqrt(2*D + 2), -S(2))})
1440
1441    # the underlying problem was in solve_linear that was not masking off
1442    # anything but a Mul or Add; it now raises an error if it gets anything
1443    # but a symbol and solve handles the substitutions necessary so solve_linear
1444    # won't make this error
1445    raises(
1446        ValueError, lambda: solve_linear(f(x) + f(x).diff(x), symbols=[f(x)]))
1447    assert solve_linear(f(x) + f(x).diff(x), symbols=[x]) == \
1448        (f(x) + Derivative(f(x), x), 1)
1449    assert solve_linear(f(x) + Integral(x, (x, y)), symbols=[x]) == \
1450        (f(x) + Integral(x, (x, y)), 1)
1451    assert solve_linear(f(x) + Integral(x, (x, y)) + x, symbols=[x]) == \
1452        (x + f(x) + Integral(x, (x, y)), 1)
1453    assert solve_linear(f(y) + Integral(x, (x, y)) + x, symbols=[x]) == \
1454        (x, -f(y) - Integral(x, (x, y)))
1455    assert solve_linear(x - f(x)/a + (f(x) - 1)/a, symbols=[x]) == \
1456        (x, 1/a)
1457    assert solve_linear(x + Derivative(2*x, x)) == \
1458        (x, -2)
1459    assert solve_linear(x + Integral(x, y), symbols=[x]) == \
1460        (x, 0)
1461    assert solve_linear(x + Integral(x, y) - 2, symbols=[x]) == \
1462        (x, 2/(y + 1))
1463
1464    assert set(solve(x + exp(x)**2, exp(x))) == \
1465        {-sqrt(-x), sqrt(-x)}
1466    assert solve(x + exp(x), x, implicit=True) == \
1467        [-exp(x)]
1468    assert solve(cos(x) - sin(x), x, implicit=True) == []
1469    assert solve(x - sin(x), x, implicit=True) == \
1470        [sin(x)]
1471    assert solve(x**2 + x - 3, x, implicit=True) == \
1472        [-x**2 + 3]
1473    assert solve(x**2 + x - 3, x**2, implicit=True) == \
1474        [-x + 3]
1475
1476
1477def test_issue_5912():
1478    assert set(solve(x**2 - x - 0.1, rational=True)) == \
1479        {S.Half + sqrt(35)/10, -sqrt(35)/10 + S.Half}
1480    ans = solve(x**2 - x - 0.1, rational=False)
1481    assert len(ans) == 2 and all(a.is_Number for a in ans)
1482    ans = solve(x**2 - x - 0.1)
1483    assert len(ans) == 2 and all(a.is_Number for a in ans)
1484
1485
1486def test_float_handling():
1487    def test(e1, e2):
1488        return len(e1.atoms(Float)) == len(e2.atoms(Float))
1489    assert solve(x - 0.5, rational=True)[0].is_Rational
1490    assert solve(x - 0.5, rational=False)[0].is_Float
1491    assert solve(x - S.Half, rational=False)[0].is_Rational
1492    assert solve(x - 0.5, rational=None)[0].is_Float
1493    assert solve(x - S.Half, rational=None)[0].is_Rational
1494    assert test(nfloat(1 + 2*x), 1.0 + 2.0*x)
1495    for contain in [list, tuple, set]:
1496        ans = nfloat(contain([1 + 2*x]))
1497        assert type(ans) is contain and test(list(ans)[0], 1.0 + 2.0*x)
1498    k, v = list(nfloat({2*x: [1 + 2*x]}).items())[0]
1499    assert test(k, 2*x) and test(v[0], 1.0 + 2.0*x)
1500    assert test(nfloat(cos(2*x)), cos(2.0*x))
1501    assert test(nfloat(3*x**2), 3.0*x**2)
1502    assert test(nfloat(3*x**2, exponent=True), 3.0*x**2.0)
1503    assert test(nfloat(exp(2*x)), exp(2.0*x))
1504    assert test(nfloat(x/3), x/3.0)
1505    assert test(nfloat(x**4 + 2*x + cos(Rational(1, 3)) + 1),
1506            x**4 + 2.0*x + 1.94495694631474)
1507    # don't call nfloat if there is no solution
1508    tot = 100 + c + z + t
1509    assert solve(((.7 + c)/tot - .6, (.2 + z)/tot - .3, t/tot - .1)) == []
1510
1511
1512def test_check_assumptions():
1513    x = symbols('x', positive=True)
1514    assert solve(x**2 - 1) == [1]
1515
1516
1517def test_issue_6056():
1518    assert solve(tanh(x + 3)*tanh(x - 3) - 1) == []
1519    assert solve(tanh(x - 1)*tanh(x + 1) + 1) == \
1520            [I*pi*Rational(-3, 4), -I*pi/4, I*pi/4, I*pi*Rational(3, 4)]
1521    assert solve((tanh(x + 3)*tanh(x - 3) + 1)**2) == \
1522            [I*pi*Rational(-3, 4), -I*pi/4, I*pi/4, I*pi*Rational(3, 4)]
1523
1524
1525def test_issue_5673():
1526    eq = -x + exp(exp(LambertW(log(x)))*LambertW(log(x)))
1527    assert checksol(eq, x, 2) is True
1528    assert checksol(eq, x, 2, numerical=False) is None
1529
1530
1531def test_exclude():
1532    R, C, Ri, Vout, V1, Vminus, Vplus, s = \
1533        symbols('R, C, Ri, Vout, V1, Vminus, Vplus, s')
1534    Rf = symbols('Rf', positive=True)  # to eliminate Rf = 0 soln
1535    eqs = [C*V1*s + Vplus*(-2*C*s - 1/R),
1536           Vminus*(-1/Ri - 1/Rf) + Vout/Rf,
1537           C*Vplus*s + V1*(-C*s - 1/R) + Vout/R,
1538           -Vminus + Vplus]
1539    assert solve(eqs, exclude=s*C*R) == [
1540        {
1541            Rf: Ri*(C*R*s + 1)**2/(C*R*s),
1542            Vminus: Vplus,
1543            V1: 2*Vplus + Vplus/(C*R*s),
1544            Vout: C*R*Vplus*s + 3*Vplus + Vplus/(C*R*s)},
1545        {
1546            Vplus: 0,
1547            Vminus: 0,
1548            V1: 0,
1549            Vout: 0},
1550    ]
1551
1552    # TODO: Investigate why currently solution [0] is preferred over [1].
1553    assert solve(eqs, exclude=[Vplus, s, C]) in [[{
1554        Vminus: Vplus,
1555        V1: Vout/2 + Vplus/2 + sqrt((Vout - 5*Vplus)*(Vout - Vplus))/2,
1556        R: (Vout - 3*Vplus - sqrt(Vout**2 - 6*Vout*Vplus + 5*Vplus**2))/(2*C*Vplus*s),
1557        Rf: Ri*(Vout - Vplus)/Vplus,
1558    }, {
1559        Vminus: Vplus,
1560        V1: Vout/2 + Vplus/2 - sqrt((Vout - 5*Vplus)*(Vout - Vplus))/2,
1561        R: (Vout - 3*Vplus + sqrt(Vout**2 - 6*Vout*Vplus + 5*Vplus**2))/(2*C*Vplus*s),
1562        Rf: Ri*(Vout - Vplus)/Vplus,
1563    }], [{
1564        Vminus: Vplus,
1565        Vout: (V1**2 - V1*Vplus - Vplus**2)/(V1 - 2*Vplus),
1566        Rf: Ri*(V1 - Vplus)**2/(Vplus*(V1 - 2*Vplus)),
1567        R: Vplus/(C*s*(V1 - 2*Vplus)),
1568    }]]
1569
1570
1571def test_high_order_roots():
1572    s = x**5 + 4*x**3 + 3*x**2 + Rational(7, 4)
1573    assert set(solve(s)) == set(Poly(s*4, domain='ZZ').all_roots())
1574
1575
1576def test_minsolve_linear_system():
1577    def count(dic):
1578        return len([x for x in dic.values() if x == 0])
1579    assert count(solve([x + y + z, y + z + a + t], particular=True, quick=True)) \
1580        == 3
1581    assert count(solve([x + y + z, y + z + a + t], particular=True, quick=False)) \
1582        == 3
1583    assert count(solve([x + y + z, y + z + a], particular=True, quick=True)) == 1
1584    assert count(solve([x + y + z, y + z + a], particular=True, quick=False)) == 2
1585
1586
1587def test_real_roots():
1588    # cf. issue 6650
1589    x = Symbol('x', real=True)
1590    assert len(solve(x**5 + x**3 + 1)) == 1
1591
1592
1593def test_issue_6528():
1594    eqs = [
1595        327600995*x**2 - 37869137*x + 1809975124*y**2 - 9998905626,
1596        895613949*x**2 - 273830224*x*y + 530506983*y**2 - 10000000000]
1597    # two expressions encountered are > 1400 ops long so if this hangs
1598    # it is likely because simplification is being done
1599    assert len(solve(eqs, y, x, check=False)) == 4
1600
1601
1602def test_overdetermined():
1603    x = symbols('x', real=True)
1604    eqs = [Abs(4*x - 7) - 5, Abs(3 - 8*x) - 1]
1605    assert solve(eqs, x) == [(S.Half,)]
1606    assert solve(eqs, x, manual=True) == [(S.Half,)]
1607    assert solve(eqs, x, manual=True, check=False) == [(S.Half,), (S(3),)]
1608
1609
1610def test_issue_6605():
1611    x = symbols('x')
1612    assert solve(4**(x/2) - 2**(x/3)) == [0, 3*I*pi/log(2)]
1613    # while the first one passed, this one failed
1614    x = symbols('x', real=True)
1615    assert solve(5**(x/2) - 2**(x/3)) == [0]
1616    b = sqrt(6)*sqrt(log(2))/sqrt(log(5))
1617    assert solve(5**(x/2) - 2**(3/x)) == [-b, b]
1618
1619
1620def test__ispow():
1621    assert _ispow(x**2)
1622    assert not _ispow(x)
1623    assert not _ispow(True)
1624
1625
1626def test_issue_6644():
1627    eq = -sqrt((m - q)**2 + (-m/(2*q) + S.Half)**2) + sqrt((-m**2/2 - sqrt(
1628    4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2 + (m**2/2 - m - sqrt(
1629    4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2)
1630    sol = solve(eq, q, simplify=False, check=False)
1631    assert len(sol) == 5
1632
1633
1634def test_issue_6752():
1635    assert solve([a**2 + a, a - b], [a, b]) == [(-1, -1), (0, 0)]
1636    assert solve([a**2 + a*c, a - b], [a, b]) == [(0, 0), (-c, -c)]
1637
1638
1639def test_issue_6792():
1640    assert solve(x*(x - 1)**2*(x + 1)*(x**6 - x + 1)) == [
1641        -1, 0, 1, CRootOf(x**6 - x + 1, 0), CRootOf(x**6 - x + 1, 1),
1642         CRootOf(x**6 - x + 1, 2), CRootOf(x**6 - x + 1, 3),
1643         CRootOf(x**6 - x + 1, 4), CRootOf(x**6 - x + 1, 5)]
1644
1645
1646def test_issues_6819_6820_6821_6248_8692():
1647    # issue 6821
1648    x, y = symbols('x y', real=True)
1649    assert solve(abs(x + 3) - 2*abs(x - 3)) == [1, 9]
1650    assert solve([abs(x) - 2, arg(x) - pi], x) == [(-2,)]
1651    assert set(solve(abs(x - 7) - 8)) == {-S.One, S(15)}
1652
1653    # issue 8692
1654    assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [
1655        Rational(-1, 2) + sqrt(61)/2, -sqrt(69)/2 + S.Half]
1656
1657    # issue 7145
1658    assert solve(2*abs(x) - abs(x - 1)) == [-1, Rational(1, 3)]
1659
1660    x = symbols('x')
1661    assert solve([re(x) - 1, im(x) - 2], x) == [
1662        {re(x): 1, x: 1 + 2*I, im(x): 2}]
1663
1664    # check for 'dict' handling of solution
1665    eq = sqrt(re(x)**2 + im(x)**2) - 3
1666    assert solve(eq) == solve(eq, x)
1667
1668    i = symbols('i', imaginary=True)
1669    assert solve(abs(i) - 3) == [-3*I, 3*I]
1670    raises(NotImplementedError, lambda: solve(abs(x) - 3))
1671
1672    w = symbols('w', integer=True)
1673    assert solve(2*x**w - 4*y**w, w) == solve((x/y)**w - 2, w)
1674
1675    x, y = symbols('x y', real=True)
1676    assert solve(x + y*I + 3) == {y: 0, x: -3}
1677    # issue 2642
1678    assert solve(x*(1 + I)) == [0]
1679
1680    x, y = symbols('x y', imaginary=True)
1681    assert solve(x + y*I + 3 + 2*I) == {x: -2*I, y: 3*I}
1682
1683    x = symbols('x', real=True)
1684    assert solve(x + y + 3 + 2*I) == {x: -3, y: -2*I}
1685
1686    # issue 6248
1687    f = Function('f')
1688    assert solve(f(x + 1) - f(2*x - 1)) == [2]
1689    assert solve(log(x + 1) - log(2*x - 1)) == [2]
1690
1691    x = symbols('x')
1692    assert solve(2**x + 4**x) == [I*pi/log(2)]
1693
1694
1695def test_issue_14607():
1696    # issue 14607
1697    s, tau_c, tau_1, tau_2, phi, K = symbols(
1698        's, tau_c, tau_1, tau_2, phi, K')
1699
1700    target = (s**2*tau_1*tau_2 + s*tau_1 + s*tau_2 + 1)/(K*s*(-phi + tau_c))
1701
1702    K_C, tau_I, tau_D = symbols('K_C, tau_I, tau_D',
1703                                positive=True, nonzero=True)
1704    PID = K_C*(1 + 1/(tau_I*s) + tau_D*s)
1705
1706    eq = (target - PID).together()
1707    eq *= denom(eq).simplify()
1708    eq = Poly(eq, s)
1709    c = eq.coeffs()
1710
1711    vars = [K_C, tau_I, tau_D]
1712    s = solve(c, vars, dict=True)
1713
1714    assert len(s) == 1
1715
1716    knownsolution = {K_C: -(tau_1 + tau_2)/(K*(phi - tau_c)),
1717                     tau_I: tau_1 + tau_2,
1718                     tau_D: tau_1*tau_2/(tau_1 + tau_2)}
1719
1720    for var in vars:
1721        assert s[0][var].simplify() == knownsolution[var].simplify()
1722
1723
1724def test_lambert_multivariate():
1725    from sympy.abc import x, y
1726    assert _filtered_gens(Poly(x + 1/x + exp(x) + y), x) == {x, exp(x)}
1727    assert _lambert(x, x) == []
1728    assert solve((x**2 - 2*x + 1).subs(x, log(x) + 3*x)) == [LambertW(3*S.Exp1)/3]
1729    assert solve((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1)) == \
1730          [LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3]
1731    assert solve((x**2 - 2*x - 2).subs(x, log(x) + 3*x)) == \
1732          [LambertW(3*exp(1 - sqrt(3)))/3, LambertW(3*exp(1 + sqrt(3)))/3]
1733    eq = (x*exp(x) - 3).subs(x, x*exp(x))
1734    assert solve(eq) == [LambertW(3*exp(-LambertW(3)))]
1735    # coverage test
1736    raises(NotImplementedError, lambda: solve(x - sin(x)*log(y - x), x))
1737    ans = [3, -3*LambertW(-log(3)/3)/log(3)]  # 3 and 2.478...
1738    assert solve(x**3 - 3**x, x) == ans
1739    assert set(solve(3*log(x) - x*log(3))) == set(ans)
1740    assert solve(LambertW(2*x) - y, x) == [y*exp(y)/2]
1741
1742
1743@XFAIL
1744def test_other_lambert():
1745    assert solve(3*sin(x) - x*sin(3), x) == [3]
1746    assert set(solve(x**a - a**x), x) == {
1747        a, -a*LambertW(-log(a)/a)/log(a)}
1748
1749
1750@slow
1751def test_lambert_bivariate():
1752    # tests passing current implementation
1753    assert solve((x**2 + x)*exp(x**2 + x) - 1) == [
1754        Rational(-1, 2) + sqrt(1 + 4*LambertW(1))/2,
1755        Rational(-1, 2) - sqrt(1 + 4*LambertW(1))/2]
1756    assert solve((x**2 + x)*exp((x**2 + x)*2) - 1) == [
1757        Rational(-1, 2) + sqrt(1 + 2*LambertW(2))/2,
1758        Rational(-1, 2) - sqrt(1 + 2*LambertW(2))/2]
1759    assert solve(a/x + exp(x/2), x) == [2*LambertW(-a/2)]
1760    assert solve((a/x + exp(x/2)).diff(x), x) == \
1761            [4*LambertW(-sqrt(2)*sqrt(a)/4), 4*LambertW(sqrt(2)*sqrt(a)/4)]
1762    assert solve((1/x + exp(x/2)).diff(x), x) == \
1763        [4*LambertW(-sqrt(2)/4),
1764        4*LambertW(sqrt(2)/4),  # nsimplifies as 2*2**(141/299)*3**(206/299)*5**(205/299)*7**(37/299)/21
1765        4*LambertW(-sqrt(2)/4, -1)]
1766    assert solve(x*log(x) + 3*x + 1, x) == \
1767            [exp(-3 + LambertW(-exp(3)))]
1768    assert solve(-x**2 + 2**x, x) == [2, 4, -2*LambertW(log(2)/2)/log(2)]
1769    assert solve(x**2 - 2**x, x) == [2, 4, -2*LambertW(log(2)/2)/log(2)]
1770    ans = solve(3*x + 5 + 2**(-5*x + 3), x)
1771    assert len(ans) == 1 and ans[0].expand() == \
1772        Rational(-5, 3) + LambertW(-10240*root(2, 3)*log(2)/3)/(5*log(2))
1773    assert solve(5*x - 1 + 3*exp(2 - 7*x), x) == \
1774        [Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7]
1775    assert solve((log(x) + x).subs(x, x**2 + 1)) == [
1776        -I*sqrt(-LambertW(1) + 1), sqrt(-1 + LambertW(1))]
1777    # check collection
1778    ax = a**(3*x + 5)
1779    ans = solve(3*log(ax) + b*log(ax) + ax, x)
1780    x0 = 1/log(a)
1781    x1 = sqrt(3)*I
1782    x2 = b + 3
1783    x3 = x2*LambertW(1/x2)/a**5
1784    x4 = x3**Rational(1, 3)/2
1785    assert ans == [
1786        x0*log(x4*(x1 - 1)),
1787        x0*log(-x4*(x1 + 1)),
1788        x0*log(x3)/3]
1789    x1 = LambertW(Rational(1, 3))
1790    x2 = a**(-5)
1791    x3 = 3**Rational(1, 3)
1792    x4 = 3**Rational(5, 6)*I
1793    x5 = x1**Rational(1, 3)*x2**Rational(1, 3)/2
1794    ans = solve(3*log(ax) + ax, x)
1795    assert ans == [
1796        x0*log(3*x1*x2)/3,
1797        x0*log(x5*(-x3 + x4)),
1798        x0*log(-x5*(x3 + x4))]
1799    # coverage
1800    p = symbols('p', positive=True)
1801    eq = 4*2**(2*p + 3) - 2*p - 3
1802    assert _solve_lambert(eq, p, _filtered_gens(Poly(eq), p)) == [
1803        Rational(-3, 2) - LambertW(-4*log(2))/(2*log(2))]
1804    assert set(solve(3**cos(x) - cos(x)**3)) == {
1805        acos(3), acos(-3*LambertW(-log(3)/3)/log(3))}
1806    # should give only one solution after using `uniq`
1807    assert solve(2*log(x) - 2*log(z) + log(z + log(x) + log(z)), x) == [
1808        exp(-z + LambertW(2*z**4*exp(2*z))/2)/z]
1809    # cases when p != S.One
1810    # issue 4271
1811    ans = solve((a/x + exp(x/2)).diff(x, 2), x)
1812    x0 = (-a)**Rational(1, 3)
1813    x1 = sqrt(3)*I
1814    x2 = x0/6
1815    assert ans == [
1816        6*LambertW(x0/3),
1817        6*LambertW(x2*(x1 - 1)),
1818        6*LambertW(-x2*(x1 + 1))]
1819    assert solve((1/x + exp(x/2)).diff(x, 2), x) == \
1820                [6*LambertW(Rational(-1, 3)), 6*LambertW(Rational(1, 6) - sqrt(3)*I/6), \
1821                6*LambertW(Rational(1, 6) + sqrt(3)*I/6), 6*LambertW(Rational(-1, 3), -1)]
1822    assert solve(x**2 - y**2/exp(x), x, y, dict=True) == \
1823                [{x: 2*LambertW(-y/2)}, {x: 2*LambertW(y/2)}]
1824    # this is slow but not exceedingly slow
1825    assert solve((x**3)**(x/2) + pi/2, x) == [
1826        exp(LambertW(-2*log(2)/3 + 2*log(pi)/3 + I*pi*Rational(2, 3)))]
1827
1828
1829def test_rewrite_trig():
1830    assert solve(sin(x) + tan(x)) == [0, -pi, pi, 2*pi]
1831    assert solve(sin(x) + sec(x)) == [
1832        -2*atan(Rational(-1, 2) + sqrt(2)*sqrt(1 - sqrt(3)*I)/2 + sqrt(3)*I/2),
1833        2*atan(S.Half - sqrt(2)*sqrt(1 + sqrt(3)*I)/2 + sqrt(3)*I/2), 2*atan(S.Half
1834        + sqrt(2)*sqrt(1 + sqrt(3)*I)/2 + sqrt(3)*I/2), 2*atan(S.Half -
1835        sqrt(3)*I/2 + sqrt(2)*sqrt(1 - sqrt(3)*I)/2)]
1836    assert solve(sinh(x) + tanh(x)) == [0, I*pi]
1837
1838    # issue 6157
1839    assert solve(2*sin(x) - cos(x), x) == [atan(S.Half)]
1840
1841
1842@XFAIL
1843def test_rewrite_trigh():
1844    # if this import passes then the test below should also pass
1845    from sympy import sech
1846    assert solve(sinh(x) + sech(x)) == [
1847        2*atanh(Rational(-1, 2) + sqrt(5)/2 - sqrt(-2*sqrt(5) + 2)/2),
1848        2*atanh(Rational(-1, 2) + sqrt(5)/2 + sqrt(-2*sqrt(5) + 2)/2),
1849        2*atanh(-sqrt(5)/2 - S.Half + sqrt(2 + 2*sqrt(5))/2),
1850        2*atanh(-sqrt(2 + 2*sqrt(5))/2 - sqrt(5)/2 - S.Half)]
1851
1852
1853def test_uselogcombine():
1854    eq = z - log(x) + log(y/(x*(-1 + y**2/x**2)))
1855    assert solve(eq, x, force=True) == [-sqrt(y*(y - exp(z))), sqrt(y*(y - exp(z)))]
1856    assert solve(log(x + 3) + log(1 + 3/x) - 3) in [
1857        [-3 + sqrt(-12 + exp(3))*exp(Rational(3, 2))/2 + exp(3)/2,
1858        -sqrt(-12 + exp(3))*exp(Rational(3, 2))/2 - 3 + exp(3)/2],
1859        [-3 + sqrt(-36 + (-exp(3) + 6)**2)/2 + exp(3)/2,
1860        -3 - sqrt(-36 + (-exp(3) + 6)**2)/2 + exp(3)/2],
1861        ]
1862    assert solve(log(exp(2*x) + 1) + log(-tanh(x) + 1) - log(2)) == []
1863
1864
1865def test_atan2():
1866    assert solve(atan2(x, 2) - pi/3, x) == [2*sqrt(3)]
1867
1868
1869def test_errorinverses():
1870    assert solve(erf(x) - y, x) == [erfinv(y)]
1871    assert solve(erfinv(x) - y, x) == [erf(y)]
1872    assert solve(erfc(x) - y, x) == [erfcinv(y)]
1873    assert solve(erfcinv(x) - y, x) == [erfc(y)]
1874
1875
1876def test_issue_2725():
1877    R = Symbol('R')
1878    eq = sqrt(2)*R*sqrt(1/(R + 1)) + (R + 1)*(sqrt(2)*sqrt(1/(R + 1)) - 1)
1879    sol = solve(eq, R, set=True)[1]
1880    assert sol == {(Rational(5, 3) + (Rational(-1, 2) - sqrt(3)*I/2)*(Rational(251, 27) +
1881        sqrt(111)*I/9)**Rational(1, 3) + 40/(9*((Rational(-1, 2) - sqrt(3)*I/2)*(Rational(251, 27) +
1882        sqrt(111)*I/9)**Rational(1, 3))),), (Rational(5, 3) + 40/(9*(Rational(251, 27) +
1883        sqrt(111)*I/9)**Rational(1, 3)) + (Rational(251, 27) + sqrt(111)*I/9)**Rational(1, 3),)}
1884
1885
1886def test_issue_5114_6611():
1887    # See that it doesn't hang; this solves in about 2 seconds.
1888    # Also check that the solution is relatively small.
1889    # Note: the system in issue 6611 solves in about 5 seconds and has
1890    # an op-count of 138336 (with simplify=False).
1891    b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r = symbols('b:r')
1892    eqs = Matrix([
1893        [b - c/d + r/d], [c*(1/g + 1/e + 1/d) - f/g - r/d],
1894        [-c/g + f*(1/j + 1/i + 1/g) - h/i], [-f/i + h*(1/m + 1/l + 1/i) - k/m],
1895        [-h/m + k*(1/p + 1/o + 1/m) - n/p], [-k/p + n*(1/q + 1/p)]])
1896    v = Matrix([f, h, k, n, b, c])
1897    ans = solve(list(eqs), list(v), simplify=False)
1898    # If time is taken to simplify then then 2617 below becomes
1899    # 1168 and the time is about 50 seconds instead of 2.
1900    assert sum([s.count_ops() for s in ans.values()]) <= 3270
1901
1902
1903def test_det_quick():
1904    m = Matrix(3, 3, symbols('a:9'))
1905    assert m.det() == det_quick(m)  # calls det_perm
1906    m[0, 0] = 1
1907    assert m.det() == det_quick(m)  # calls det_minor
1908    m = Matrix(3, 3, list(range(9)))
1909    assert m.det() == det_quick(m)  # defaults to .det()
1910    # make sure they work with Sparse
1911    s = SparseMatrix(2, 2, (1, 2, 1, 4))
1912    assert det_perm(s) == det_minor(s) == s.det()
1913
1914
1915def test_real_imag_splitting():
1916    a, b = symbols('a b', real=True)
1917    assert solve(sqrt(a**2 + b**2) - 3, a) == \
1918        [-sqrt(-b**2 + 9), sqrt(-b**2 + 9)]
1919    a, b = symbols('a b', imaginary=True)
1920    assert solve(sqrt(a**2 + b**2) - 3, a) == []
1921
1922
1923def test_issue_7110():
1924    y = -2*x**3 + 4*x**2 - 2*x + 5
1925    assert any(ask(Q.real(i)) for i in solve(y))
1926
1927
1928def test_units():
1929    assert solve(1/x - 1/(2*cm)) == [2*cm]
1930
1931
1932def test_issue_7547():
1933    A, B, V = symbols('A,B,V')
1934    eq1 = Eq(630.26*(V - 39.0)*V*(V + 39) - A + B, 0)
1935    eq2 = Eq(B, 1.36*10**8*(V - 39))
1936    eq3 = Eq(A, 5.75*10**5*V*(V + 39.0))
1937    sol = Matrix(nsolve(Tuple(eq1, eq2, eq3), [A, B, V], (0, 0, 0)))
1938    assert str(sol) == str(Matrix(
1939        [['4442890172.68209'],
1940         ['4289299466.1432'],
1941         ['70.5389666628177']]))
1942
1943
1944def test_issue_7895():
1945    r = symbols('r', real=True)
1946    assert solve(sqrt(r) - 2) == [4]
1947
1948
1949def test_issue_2777():
1950    # the equations represent two circles
1951    x, y = symbols('x y', real=True)
1952    e1, e2 = sqrt(x**2 + y**2) - 10, sqrt(y**2 + (-x + 10)**2) - 3
1953    a, b = Rational(191, 20), 3*sqrt(391)/20
1954    ans = [(a, -b), (a, b)]
1955    assert solve((e1, e2), (x, y)) == ans
1956    assert solve((e1, e2/(x - a)), (x, y)) == []
1957    # make the 2nd circle's radius be -3
1958    e2 += 6
1959    assert solve((e1, e2), (x, y)) == []
1960    assert solve((e1, e2), (x, y), check=False) == ans
1961
1962
1963def test_issue_7322():
1964    number = 5.62527e-35
1965    assert solve(x - number, x)[0] == number
1966
1967
1968def test_nsolve():
1969    raises(ValueError, lambda: nsolve(x, (-1, 1), method='bisect'))
1970    raises(TypeError, lambda: nsolve((x - y + 3,x + y,z - y),(x,y,z),(-50,50)))
1971    raises(TypeError, lambda: nsolve((x + y, x - y), (0, 1)))
1972
1973
1974@slow
1975def test_high_order_multivariate():
1976    assert len(solve(a*x**3 - x + 1, x)) == 3
1977    assert len(solve(a*x**4 - x + 1, x)) == 4
1978    assert solve(a*x**5 - x + 1, x) == []  # incomplete solution allowed
1979    raises(NotImplementedError, lambda:
1980        solve(a*x**5 - x + 1, x, incomplete=False))
1981
1982    # result checking must always consider the denominator and CRootOf
1983    # must be checked, too
1984    d = x**5 - x + 1
1985    assert solve(d*(1 + 1/d)) == [CRootOf(d + 1, i) for i in range(5)]
1986    d = x - 1
1987    assert solve(d*(2 + 1/d)) == [S.Half]
1988
1989
1990def test_base_0_exp_0():
1991    assert solve(0**x - 1) == [0]
1992    assert solve(0**(x - 2) - 1) == [2]
1993    assert solve(S('x*(1/x**0 - x)', evaluate=False)) == \
1994        [0, 1]
1995
1996
1997def test__simple_dens():
1998    assert _simple_dens(1/x**0, [x]) == set()
1999    assert _simple_dens(1/x**y, [x]) == {x**y}
2000    assert _simple_dens(1/root(x, 3), [x]) == {x}
2001
2002
2003def test_issue_8755():
2004    # This tests two things: that if full unrad is attempted and fails
2005    # the solution should still be found; also it tests the use of
2006    # keyword `composite`.
2007    assert len(solve(sqrt(y)*x + x**3 - 1, x)) == 3
2008    assert len(solve(-512*y**3 + 1344*(x + 2)**Rational(1, 3)*y**2 -
2009        1176*(x + 2)**Rational(2, 3)*y - 169*x + 686, y, _unrad=False)) == 3
2010
2011
2012@slow
2013def test_issue_8828():
2014    x1 = 0
2015    y1 = -620
2016    r1 = 920
2017    x2 = 126
2018    y2 = 276
2019    x3 = 51
2020    y3 = 205
2021    r3 = 104
2022    v = x, y, z
2023
2024    f1 = (x - x1)**2 + (y - y1)**2 - (r1 - z)**2
2025    f2 = (x2 - x)**2 + (y2 - y)**2 - z**2
2026    f3 = (x - x3)**2 + (y - y3)**2 - (r3 - z)**2
2027    F = f1,f2,f3
2028
2029    g1 = sqrt((x - x1)**2 + (y - y1)**2) + z - r1
2030    g2 = f2
2031    g3 = sqrt((x - x3)**2 + (y - y3)**2) + z - r3
2032    G = g1,g2,g3
2033
2034    A = solve(F, v)
2035    B = solve(G, v)
2036    C = solve(G, v, manual=True)
2037
2038    p, q, r = [{tuple(i.evalf(2) for i in j) for j in R} for R in [A, B, C]]
2039    assert p == q == r
2040
2041
2042@slow
2043def test_issue_2840_8155():
2044    assert solve(sin(3*x) + sin(6*x)) == [
2045        0, pi*Rational(-5, 3), pi*Rational(-4, 3), -pi, pi*Rational(-2, 3),
2046        pi*Rational(-4, 9), -pi/3, pi*Rational(-2, 9), pi*Rational(2, 9),
2047        pi/3, pi*Rational(4, 9), pi*Rational(2, 3), pi, pi*Rational(4, 3),
2048        pi*Rational(14, 9), pi*Rational(5, 3), pi*Rational(16, 9), 2*pi,
2049        -2*I*log(-(-1)**Rational(1, 9)), -2*I*log(-(-1)**Rational(2, 9)),
2050        -2*I*log(-sin(pi/18) - I*cos(pi/18)),
2051        -2*I*log(-sin(pi/18) + I*cos(pi/18)),
2052        -2*I*log(sin(pi/18) - I*cos(pi/18)),
2053        -2*I*log(sin(pi/18) + I*cos(pi/18))]
2054    assert solve(2*sin(x) - 2*sin(2*x)) == [
2055        0, pi*Rational(-5, 3), -pi, -pi/3, pi/3, pi, pi*Rational(5, 3)]
2056
2057
2058def test_issue_9567():
2059    assert solve(1 + 1/(x - 1)) == [0]
2060
2061
2062def test_issue_11538():
2063    assert solve(x + E) == [-E]
2064    assert solve(x**2 + E) == [-I*sqrt(E), I*sqrt(E)]
2065    assert solve(x**3 + 2*E) == [
2066        -cbrt(2 * E),
2067        cbrt(2)*cbrt(E)/2 - cbrt(2)*sqrt(3)*I*cbrt(E)/2,
2068        cbrt(2)*cbrt(E)/2 + cbrt(2)*sqrt(3)*I*cbrt(E)/2]
2069    assert solve([x + 4, y + E], x, y) == {x: -4, y: -E}
2070    assert solve([x**2 + 4, y + E], x, y) == [
2071        (-2*I, -E), (2*I, -E)]
2072
2073    e1 = x - y**3 + 4
2074    e2 = x + y + 4 + 4 * E
2075    assert len(solve([e1, e2], x, y)) == 3
2076
2077
2078@slow
2079def test_issue_12114():
2080    a, b, c, d, e, f, g = symbols('a,b,c,d,e,f,g')
2081    terms = [1 + a*b + d*e, 1 + a*c + d*f, 1 + b*c + e*f,
2082             g - a**2 - d**2, g - b**2 - e**2, g - c**2 - f**2]
2083    s = solve(terms, [a, b, c, d, e, f, g], dict=True)
2084    assert s == [{a: -sqrt(-f**2 - 1), b: -sqrt(-f**2 - 1),
2085                  c: -sqrt(-f**2 - 1), d: f, e: f, g: -1},
2086                 {a: sqrt(-f**2 - 1), b: sqrt(-f**2 - 1),
2087                  c: sqrt(-f**2 - 1), d: f, e: f, g: -1},
2088                 {a: -sqrt(3)*f/2 - sqrt(-f**2 + 2)/2,
2089                  b: sqrt(3)*f/2 - sqrt(-f**2 + 2)/2, c: sqrt(-f**2 + 2),
2090                  d: -f/2 + sqrt(-3*f**2 + 6)/2,
2091                  e: -f/2 - sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
2092                 {a: -sqrt(3)*f/2 + sqrt(-f**2 + 2)/2,
2093                  b: sqrt(3)*f/2 + sqrt(-f**2 + 2)/2, c: -sqrt(-f**2 + 2),
2094                  d: -f/2 - sqrt(-3*f**2 + 6)/2,
2095                  e: -f/2 + sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
2096                 {a: sqrt(3)*f/2 - sqrt(-f**2 + 2)/2,
2097                  b: -sqrt(3)*f/2 - sqrt(-f**2 + 2)/2, c: sqrt(-f**2 + 2),
2098                  d: -f/2 - sqrt(-3*f**2 + 6)/2,
2099                  e: -f/2 + sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
2100                 {a: sqrt(3)*f/2 + sqrt(-f**2 + 2)/2,
2101                  b: -sqrt(3)*f/2 + sqrt(-f**2 + 2)/2, c: -sqrt(-f**2 + 2),
2102                  d: -f/2 + sqrt(-3*f**2 + 6)/2,
2103                  e: -f/2 - sqrt(3)*sqrt(-f**2 + 2)/2, g: 2}]
2104
2105
2106def test_inf():
2107    assert solve(1 - oo*x) == []
2108    assert solve(oo*x, x) == []
2109    assert solve(oo*x - oo, x) == []
2110
2111
2112def test_issue_12448():
2113    f = Function('f')
2114    fun = [f(i) for i in range(15)]
2115    sym = symbols('x:15')
2116    reps = dict(zip(fun, sym))
2117
2118    (x, y, z), c = sym[:3], sym[3:]
2119    ssym = solve([c[4*i]*x + c[4*i + 1]*y + c[4*i + 2]*z + c[4*i + 3]
2120        for i in range(3)], (x, y, z))
2121
2122    (x, y, z), c = fun[:3], fun[3:]
2123    sfun = solve([c[4*i]*x + c[4*i + 1]*y + c[4*i + 2]*z + c[4*i + 3]
2124        for i in range(3)], (x, y, z))
2125
2126    assert sfun[fun[0]].xreplace(reps).count_ops() == \
2127        ssym[sym[0]].count_ops()
2128
2129
2130def test_denoms():
2131    assert denoms(x/2 + 1/y) == {2, y}
2132    assert denoms(x/2 + 1/y, y) == {y}
2133    assert denoms(x/2 + 1/y, [y]) == {y}
2134    assert denoms(1/x + 1/y + 1/z, [x, y]) == {x, y}
2135    assert denoms(1/x + 1/y + 1/z, x, y) == {x, y}
2136    assert denoms(1/x + 1/y + 1/z, {x, y}) == {x, y}
2137
2138
2139def test_issue_12476():
2140    x0, x1, x2, x3, x4, x5 = symbols('x0 x1 x2 x3 x4 x5')
2141    eqns = [x0**2 - x0, x0*x1 - x1, x0*x2 - x2, x0*x3 - x3, x0*x4 - x4, x0*x5 - x5,
2142            x0*x1 - x1, -x0/3 + x1**2 - 2*x2/3, x1*x2 - x1/3 - x2/3 - x3/3,
2143            x1*x3 - x2/3 - x3/3 - x4/3, x1*x4 - 2*x3/3 - x5/3, x1*x5 - x4, x0*x2 - x2,
2144            x1*x2 - x1/3 - x2/3 - x3/3, -x0/6 - x1/6 + x2**2 - x2/6 - x3/3 - x4/6,
2145            -x1/6 + x2*x3 - x2/3 - x3/6 - x4/6 - x5/6, x2*x4 - x2/3 - x3/3 - x4/3,
2146            x2*x5 - x3, x0*x3 - x3, x1*x3 - x2/3 - x3/3 - x4/3,
2147            -x1/6 + x2*x3 - x2/3 - x3/6 - x4/6 - x5/6,
2148            -x0/6 - x1/6 - x2/6 + x3**2 - x3/3 - x4/6, -x1/3 - x2/3 + x3*x4 - x3/3,
2149            -x2 + x3*x5, x0*x4 - x4, x1*x4 - 2*x3/3 - x5/3, x2*x4 - x2/3 - x3/3 - x4/3,
2150            -x1/3 - x2/3 + x3*x4 - x3/3, -x0/3 - 2*x2/3 + x4**2, -x1 + x4*x5, x0*x5 - x5,
2151            x1*x5 - x4, x2*x5 - x3, -x2 + x3*x5, -x1 + x4*x5, -x0 + x5**2, x0 - 1]
2152    sols = [{x0: 1, x3: Rational(1, 6), x2: Rational(1, 6), x4: Rational(-2, 3), x1: Rational(-2, 3), x5: 1},
2153            {x0: 1, x3: S.Half, x2: Rational(-1, 2), x4: 0, x1: 0, x5: -1},
2154            {x0: 1, x3: Rational(-1, 3), x2: Rational(-1, 3), x4: Rational(1, 3), x1: Rational(1, 3), x5: 1},
2155            {x0: 1, x3: 1, x2: 1, x4: 1, x1: 1, x5: 1},
2156            {x0: 1, x3: Rational(-1, 3), x2: Rational(1, 3), x4: sqrt(5)/3, x1: -sqrt(5)/3, x5: -1},
2157            {x0: 1, x3: Rational(-1, 3), x2: Rational(1, 3), x4: -sqrt(5)/3, x1: sqrt(5)/3, x5: -1}]
2158
2159    assert solve(eqns) == sols
2160
2161
2162def test_issue_13849():
2163    t = symbols('t')
2164    assert solve((t*(sqrt(5) + sqrt(2)) - sqrt(2), t), t) == []
2165
2166
2167def test_issue_14860():
2168    from sympy.physics.units import newton, kilo
2169    assert solve(8*kilo*newton + x + y, x) == [-8000*newton - y]
2170
2171
2172def test_issue_14721():
2173    k, h, a, b = symbols(':4')
2174    assert solve([
2175        -1 + (-k + 1)**2/b**2 + (-h - 1)**2/a**2,
2176        -1 + (-k + 1)**2/b**2 + (-h + 1)**2/a**2,
2177        h, k + 2], h, k, a, b) == [
2178        (0, -2, -b*sqrt(1/(b**2 - 9)), b),
2179        (0, -2, b*sqrt(1/(b**2 - 9)), b)]
2180    assert solve([
2181        h, h/a + 1/b**2 - 2, -h/2 + 1/b**2 - 2], a, h, b) == [
2182        (a, 0, -sqrt(2)/2), (a, 0, sqrt(2)/2)]
2183    assert solve((a + b**2 - 1, a + b**2 - 2)) == []
2184
2185
2186def test_issue_14779():
2187    x = symbols('x', real=True)
2188    assert solve(sqrt(x**4 - 130*x**2 + 1089) + sqrt(x**4 - 130*x**2
2189                 + 3969) - 96*Abs(x)/x,x) == [sqrt(130)]
2190
2191
2192def test_issue_15307():
2193    assert solve((y - 2, Mul(x + 3,x - 2, evaluate=False))) == \
2194        [{x: -3, y: 2}, {x: 2, y: 2}]
2195    assert solve((y - 2, Mul(3, x - 2, evaluate=False))) == \
2196        {x: 2, y: 2}
2197    assert solve((y - 2, Add(x + 4, x - 2, evaluate=False))) == \
2198        {x: -1, y: 2}
2199    eq1 = Eq(12513*x + 2*y - 219093, -5726*x - y)
2200    eq2 = Eq(-2*x + 8, 2*x - 40)
2201    assert solve([eq1, eq2]) == {x:12, y:75}
2202
2203
2204def test_issue_15415():
2205    assert solve(x - 3, x) == [3]
2206    assert solve([x - 3], x) == {x:3}
2207    assert solve(Eq(y + 3*x**2/2, y + 3*x), y) == []
2208    assert solve([Eq(y + 3*x**2/2, y + 3*x)], y) == []
2209    assert solve([Eq(y + 3*x**2/2, y + 3*x), Eq(x, 1)], y) == []
2210
2211
2212@slow
2213def test_issue_15731():
2214    # f(x)**g(x)=c
2215    assert solve(Eq((x**2 - 7*x + 11)**(x**2 - 13*x + 42), 1)) == [2, 3, 4, 5, 6, 7]
2216    assert solve((x)**(x + 4) - 4) == [-2]
2217    assert solve((-x)**(-x + 4) - 4) == [2]
2218    assert solve((x**2 - 6)**(x**2 - 2) - 4) == [-2, 2]
2219    assert solve((x**2 - 2*x - 1)**(x**2 - 3) - 1/(1 - 2*sqrt(2))) == [sqrt(2)]
2220    assert solve(x**(x + S.Half) - 4*sqrt(2)) == [S(2)]
2221    assert solve((x**2 + 1)**x - 25) == [2]
2222    assert solve(x**(2/x) - 2) == [2, 4]
2223    assert solve((x/2)**(2/x) - sqrt(2)) == [4, 8]
2224    assert solve(x**(x + S.Half) - Rational(9, 4)) == [Rational(3, 2)]
2225    # a**g(x)=c
2226    assert solve((-sqrt(sqrt(2)))**x - 2) == [4, log(2)/(log(2**Rational(1, 4)) + I*pi)]
2227    assert solve((sqrt(2))**x - sqrt(sqrt(2))) == [S.Half]
2228    assert solve((-sqrt(2))**x + 2*(sqrt(2))) == [3,
2229            (3*log(2)**2 + 4*pi**2 - 4*I*pi*log(2))/(log(2)**2 + 4*pi**2)]
2230    assert solve((sqrt(2))**x - 2*(sqrt(2))) == [3]
2231    assert solve(I**x + 1) == [2]
2232    assert solve((1 + I)**x - 2*I) == [2]
2233    assert solve((sqrt(2) + sqrt(3))**x - (2*sqrt(6) + 5)**Rational(1, 3)) == [Rational(2, 3)]
2234    # bases of both sides are equal
2235    b = Symbol('b')
2236    assert solve(b**x - b**2, x) == [2]
2237    assert solve(b**x - 1/b, x) == [-1]
2238    assert solve(b**x - b, x) == [1]
2239    b = Symbol('b', positive=True)
2240    assert solve(b**x - b**2, x) == [2]
2241    assert solve(b**x - 1/b, x) == [-1]
2242
2243
2244def test_issue_10933():
2245    assert solve(x**4 + y*(x + 0.1), x)  # doesn't fail
2246    assert solve(I*x**4 + x**3 + x**2 + 1.)  # doesn't fail
2247
2248
2249def test_Abs_handling():
2250    x = symbols('x', real=True)
2251    assert solve(abs(x/y), x) == [0]
2252
2253
2254def test_issue_7982():
2255    x = Symbol('x')
2256    # Test that no exception happens
2257    assert solve([2*x**2 + 5*x + 20 <= 0, x >= 1.5], x) is S.false
2258    # From #8040
2259    assert solve([x**3 - 8.08*x**2 - 56.48*x/5 - 106 >= 0, x - 1 <= 0], [x]) is S.false
2260
2261
2262def test_issue_14645():
2263    x, y = symbols('x y')
2264    assert solve([x*y - x - y, x*y - x - y], [x, y]) == [(y/(y - 1), y)]
2265
2266
2267def test_issue_12024():
2268    x, y = symbols('x y')
2269    assert solve(Piecewise((0.0, x < 0.1), (x, x >= 0.1)) - y) == \
2270        [{y: Piecewise((0.0, x < 0.1), (x, True))}]
2271
2272
2273def test_issue_17452():
2274    assert solve((7**x)**x + pi, x) == [-sqrt(log(pi) + I*pi)/sqrt(log(7)),
2275                                        sqrt(log(pi) + I*pi)/sqrt(log(7))]
2276    assert solve(x**(x/11) + pi/11, x) == [exp(LambertW(-11*log(11) + 11*log(pi) + 11*I*pi))]
2277
2278
2279def test_issue_17799():
2280    assert solve(-erf(x**(S(1)/3))**pi + I, x) == []
2281
2282
2283def test_issue_17650():
2284    x = Symbol('x', real=True)
2285    assert solve(abs(abs(x**2 - 1) - x) - x) == [1, -1 + sqrt(2), 1 + sqrt(2)]
2286
2287
2288def test_issue_17882():
2289    eq = -8*x**2/(9*(x**2 - 1)**(S(4)/3)) + 4/(3*(x**2 - 1)**(S(1)/3))
2290    assert unrad(eq) is None
2291
2292
2293def test_issue_17949():
2294    assert solve(exp(+x+x**2), x) == []
2295    assert solve(exp(-x+x**2), x) == []
2296    assert solve(exp(+x-x**2), x) == []
2297    assert solve(exp(-x-x**2), x) == []
2298
2299
2300def test_issue_10993():
2301    assert solve(Eq(binomial(x, 2), 3)) == [-2, 3]
2302    assert solve(Eq(pow(x, 2) + binomial(x, 3), x)) == [-4, 0, 1]
2303    assert solve(Eq(binomial(x, 2), 0)) == [0, 1]
2304    assert solve(a+binomial(x, 3), a) == [-binomial(x, 3)]
2305    assert solve(x-binomial(a, 3) + binomial(y, 2) + sin(a), x) == [-sin(a) + binomial(a, 3) - binomial(y, 2)]
2306    assert solve((x+1)-binomial(x+1, 3), x) == [-2, -1, 3]
2307
2308
2309def test_issue_11553():
2310    eq1 = x + y + 1
2311    eq2 = x + GoldenRatio
2312    assert solve([eq1, eq2], x, y) == {x: -GoldenRatio, y: -1 + GoldenRatio}
2313    eq3 = x + 2 + TribonacciConstant
2314    assert solve([eq1, eq3], x, y) == {x: -2 - TribonacciConstant, y: 1 + TribonacciConstant}
2315
2316
2317def test_issue_19113_19102():
2318    t = S(1)/3
2319    solve(cos(x)**5-sin(x)**5)
2320    assert solve(4*cos(x)**3 - 2*sin(x)**3) == [
2321        atan(2**(t)), -atan(2**(t)*(1 - sqrt(3)*I)/2),
2322        -atan(2**(t)*(1 + sqrt(3)*I)/2)]
2323    h = S.Half
2324    assert solve(cos(x)**2 + sin(x)) == [
2325        2*atan(-h + sqrt(5)/2 + sqrt(2)*sqrt(1 - sqrt(5))/2),
2326        -2*atan(h + sqrt(5)/2 + sqrt(2)*sqrt(1 + sqrt(5))/2),
2327        -2*atan(-sqrt(5)/2 + h + sqrt(2)*sqrt(1 - sqrt(5))/2),
2328        -2*atan(-sqrt(2)*sqrt(1 + sqrt(5))/2 + h + sqrt(5)/2)]
2329    assert solve(3*cos(x) - sin(x)) == [atan(3)]
2330
2331
2332def test_issue_19509():
2333    a = S(3)/4
2334    b = S(5)/8
2335    c = sqrt(5)/8
2336    d = sqrt(5)/4
2337    assert solve(1/(x -1)**5 - 1) == [2,
2338        -d + a - sqrt(-b + c),
2339        -d + a + sqrt(-b + c),
2340        d + a - sqrt(-b - c),
2341        d + a + sqrt(-b - c)]
2342
2343def test_issue_20747():
2344    THT, HT, DBH, dib, c0, c1, c2, c3, c4  = symbols('THT HT DBH dib c0 c1 c2 c3 c4')
2345    f = DBH*c3 + THT*c4 + c2
2346    rhs = 1 - ((HT - 1)/(THT - 1))**c1*(1 - exp(c0/f))
2347    eq = dib - DBH*(c0 - f*log(rhs))
2348    term = ((1 - exp((DBH*c0 - dib)/(DBH*(DBH*c3 + THT*c4 + c2))))
2349            / (1 - exp(c0/(DBH*c3 + THT*c4 + c2))))
2350    sol = [THT*term**(1/c1) - term**(1/c1) + 1]
2351    assert solve(eq, HT) == sol
2352
2353def test_issue_20902():
2354    f = (t / ((1 + t) ** 2))
2355    assert solve(f.subs({t: 3 * x + 2}).diff(x) > 0, x) == (S(-1) < x) & (x < S(-1)/3)
2356    assert solve(f.subs({t: 3 * x + 3}).diff(x) > 0, x) == (S(-4)/3 < x) & (x < S(-2)/3)
2357    assert solve(f.subs({t: 3 * x + 4}).diff(x) > 0, x) == (S(-5)/3 < x) & (x < S(-1))
2358    assert solve(f.subs({t: 3 * x + 2}).diff(x) > 0, x) == (S(-1) < x) & (x < S(-1)/3)
2359
2360
2361def test_issue_21034():
2362    a = symbols('a', real=True)
2363    system = [x - cosh(cos(4)), y - sinh(cos(a)), z - tanh(x)]
2364    assert solve(system, x, y, z) == {x: cosh(cos(4)), z: tanh(cosh(cos(4))),
2365        y: sinh(cos(a))}
2366    #Constants inside hyperbolic functions should not be rewritten in terms of exp
2367    newsystem = [(exp(x) - exp(-x)) - tanh(x)*(exp(x) + exp(-x)) + x - 5]
2368    assert solve(newsystem, x) == {x: 5}
2369    #If the variable of interest is present in hyperbolic function, only then
2370    # it shouuld be rewritten in terms of exp and solved further
2371
2372
2373def test_issue_4886():
2374    z = a*sqrt(R**2*a**2 + R**2*b**2 - c**2)/(a**2 + b**2)
2375    t = b*c/(a**2 + b**2)
2376    sol = [((b*(t - z) - c)/(-a), t - z), ((b*(t + z) - c)/(-a), t + z)]
2377    assert solve([x**2 + y**2 - R**2, a*x + b*y - c], x, y) == sol
2378
2379
2380def test_issue_6819():
2381    a, b, c, d = symbols('a b c d', positive=True)
2382    assert solve(a*b**x - c*d**x, x) == [log(c/a)/log(b/d)]
2383
2384
2385def test_issue_21852():
2386    solution = [21 - 21*sqrt(2)/2]
2387    assert solve(2*x + sqrt(2*x**2) - 21) == solution
2388
2389
2390def test_issue_21942():
2391    eq = -d + (a*c**(1 - e) + b**(1 - e)*(1 - a))**(1/(1 - e))
2392    sol = solve(eq, c, simplify=False, check=False)
2393    assert sol == [(b/b**e - b/(a*b**e) + d**(1 - e)/a)**(1/(1 - e))]
2394