m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2435 lines
93 KiB

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