图片解析应用
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.

3106 lines
126 KiB

  1. from sympy.core.containers import Tuple
  2. from sympy.core.function import (Function, Lambda, nfloat, diff)
  3. from sympy.core.mod import Mod
  4. from sympy.core.numbers import (E, I, Rational, oo, pi, Integer)
  5. from sympy.core.relational import (Eq, Gt, Ne, Ge)
  6. from sympy.core.singleton import S
  7. from sympy.core.sorting import ordered
  8. from sympy.core.symbol import (Dummy, Symbol, symbols)
  9. from sympy.functions.elementary.complexes import (Abs, arg, im, re, sign, conjugate)
  10. from sympy.functions.elementary.exponential import (LambertW, exp, log)
  11. from sympy.functions.elementary.hyperbolic import (HyperbolicFunction,
  12. sinh, tanh, cosh, sech, coth)
  13. from sympy.functions.elementary.miscellaneous import sqrt, Min, Max
  14. from sympy.functions.elementary.piecewise import Piecewise
  15. from sympy.functions.elementary.trigonometric import (
  16. TrigonometricFunction, acos, acot, acsc, asec, asin, atan, atan2,
  17. cos, cot, csc, sec, sin, tan)
  18. from sympy.functions.special.error_functions import (erf, erfc,
  19. erfcinv, erfinv)
  20. from sympy.logic.boolalg import And
  21. from sympy.matrices.dense import MutableDenseMatrix as Matrix
  22. from sympy.matrices.immutable import ImmutableDenseMatrix
  23. from sympy.polys.polytools import Poly
  24. from sympy.polys.rootoftools import CRootOf
  25. from sympy.sets.contains import Contains
  26. from sympy.sets.conditionset import ConditionSet
  27. from sympy.sets.fancysets import ImageSet, Range
  28. from sympy.sets.sets import (Complement, FiniteSet,
  29. Intersection, Interval, Union, imageset, ProductSet)
  30. from sympy.simplify import simplify
  31. from sympy.tensor.indexed import Indexed
  32. from sympy.utilities.iterables import numbered_symbols
  33. from sympy.testing.pytest import (XFAIL, raises, skip, slow, SKIP, _both_exp_pow)
  34. from sympy.core.random import verify_numerically as tn
  35. from sympy.physics.units import cm
  36. from sympy.solvers import solve
  37. from sympy.solvers.solveset import (
  38. solveset_real, domain_check, solveset_complex, linear_eq_to_matrix,
  39. linsolve, _is_function_class_equation, invert_real, invert_complex,
  40. solveset, solve_decomposition, substitution, nonlinsolve, solvify,
  41. _is_finite_with_finite_vars, _transolve, _is_exponential,
  42. _solve_exponential, _is_logarithmic, _is_lambert,
  43. _solve_logarithm, _term_factors, _is_modular, NonlinearError)
  44. from sympy.abc import (a, b, c, d, e, f, g, h, i, j, k, l, m, n, q, r,
  45. t, w, x, y, z)
  46. def dumeq(i, j):
  47. if type(i) in (list, tuple):
  48. return all(dumeq(i, j) for i, j in zip(i, j))
  49. return i == j or i.dummy_eq(j)
  50. @_both_exp_pow
  51. def test_invert_real():
  52. x = Symbol('x', real=True)
  53. def ireal(x, s=S.Reals):
  54. return Intersection(s, x)
  55. assert invert_real(exp(x), z, x) == (x, ireal(FiniteSet(log(z))))
  56. y = Symbol('y', positive=True)
  57. n = Symbol('n', real=True)
  58. assert invert_real(x + 3, y, x) == (x, FiniteSet(y - 3))
  59. assert invert_real(x*3, y, x) == (x, FiniteSet(y / 3))
  60. assert invert_real(exp(x), y, x) == (x, FiniteSet(log(y)))
  61. assert invert_real(exp(3*x), y, x) == (x, FiniteSet(log(y) / 3))
  62. assert invert_real(exp(x + 3), y, x) == (x, FiniteSet(log(y) - 3))
  63. assert invert_real(exp(x) + 3, y, x) == (x, ireal(FiniteSet(log(y - 3))))
  64. assert invert_real(exp(x)*3, y, x) == (x, FiniteSet(log(y / 3)))
  65. assert invert_real(log(x), y, x) == (x, FiniteSet(exp(y)))
  66. assert invert_real(log(3*x), y, x) == (x, FiniteSet(exp(y) / 3))
  67. assert invert_real(log(x + 3), y, x) == (x, FiniteSet(exp(y) - 3))
  68. assert invert_real(Abs(x), y, x) == (x, FiniteSet(y, -y))
  69. assert invert_real(2**x, y, x) == (x, FiniteSet(log(y)/log(2)))
  70. assert invert_real(2**exp(x), y, x) == (x, ireal(FiniteSet(log(log(y)/log(2)))))
  71. assert invert_real(x**2, y, x) == (x, FiniteSet(sqrt(y), -sqrt(y)))
  72. assert invert_real(x**S.Half, y, x) == (x, FiniteSet(y**2))
  73. raises(ValueError, lambda: invert_real(x, x, x))
  74. # issue 21236
  75. assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1/pi)))
  76. assert invert_real(x**pi, -E, x) == (x, S.EmptySet)
  77. assert invert_real(x**Rational(3/2), 1000, x) == (x, FiniteSet(100))
  78. assert invert_real(x**1.0, 1, x) == (x**1.0, FiniteSet(1))
  79. raises(ValueError, lambda: invert_real(S.One, y, x))
  80. assert invert_real(x**31 + x, y, x) == (x**31 + x, FiniteSet(y))
  81. lhs = x**31 + x
  82. base_values = FiniteSet(y - 1, -y - 1)
  83. assert invert_real(Abs(x**31 + x + 1), y, x) == (lhs, base_values)
  84. assert dumeq(invert_real(sin(x), y, x),
  85. (x, imageset(Lambda(n, n*pi + (-1)**n*asin(y)), S.Integers)))
  86. assert dumeq(invert_real(sin(exp(x)), y, x),
  87. (x, imageset(Lambda(n, log((-1)**n*asin(y) + n*pi)), S.Integers)))
  88. assert dumeq(invert_real(csc(x), y, x),
  89. (x, imageset(Lambda(n, n*pi + (-1)**n*acsc(y)), S.Integers)))
  90. assert dumeq(invert_real(csc(exp(x)), y, x),
  91. (x, imageset(Lambda(n, log((-1)**n*acsc(y) + n*pi)), S.Integers)))
  92. assert dumeq(invert_real(cos(x), y, x),
  93. (x, Union(imageset(Lambda(n, 2*n*pi + acos(y)), S.Integers), \
  94. imageset(Lambda(n, 2*n*pi - acos(y)), S.Integers))))
  95. assert dumeq(invert_real(cos(exp(x)), y, x),
  96. (x, Union(imageset(Lambda(n, log(2*n*pi + acos(y))), S.Integers), \
  97. imageset(Lambda(n, log(2*n*pi - acos(y))), S.Integers))))
  98. assert dumeq(invert_real(sec(x), y, x),
  99. (x, Union(imageset(Lambda(n, 2*n*pi + asec(y)), S.Integers), \
  100. imageset(Lambda(n, 2*n*pi - asec(y)), S.Integers))))
  101. assert dumeq(invert_real(sec(exp(x)), y, x),
  102. (x, Union(imageset(Lambda(n, log(2*n*pi + asec(y))), S.Integers), \
  103. imageset(Lambda(n, log(2*n*pi - asec(y))), S.Integers))))
  104. assert dumeq(invert_real(tan(x), y, x),
  105. (x, imageset(Lambda(n, n*pi + atan(y)), S.Integers)))
  106. assert dumeq(invert_real(tan(exp(x)), y, x),
  107. (x, imageset(Lambda(n, log(n*pi + atan(y))), S.Integers)))
  108. assert dumeq(invert_real(cot(x), y, x),
  109. (x, imageset(Lambda(n, n*pi + acot(y)), S.Integers)))
  110. assert dumeq(invert_real(cot(exp(x)), y, x),
  111. (x, imageset(Lambda(n, log(n*pi + acot(y))), S.Integers)))
  112. assert dumeq(invert_real(tan(tan(x)), y, x),
  113. (tan(x), imageset(Lambda(n, n*pi + atan(y)), S.Integers)))
  114. x = Symbol('x', positive=True)
  115. assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1/pi)))
  116. def test_invert_complex():
  117. assert invert_complex(x + 3, y, x) == (x, FiniteSet(y - 3))
  118. assert invert_complex(x*3, y, x) == (x, FiniteSet(y / 3))
  119. assert invert_complex((x - 1)**3, 0, x) == (x, FiniteSet(1))
  120. assert dumeq(invert_complex(exp(x), y, x),
  121. (x, imageset(Lambda(n, I*(2*pi*n + arg(y)) + log(Abs(y))), S.Integers)))
  122. assert invert_complex(log(x), y, x) == (x, FiniteSet(exp(y)))
  123. raises(ValueError, lambda: invert_real(1, y, x))
  124. raises(ValueError, lambda: invert_complex(x, x, x))
  125. raises(ValueError, lambda: invert_complex(x, x, 1))
  126. # https://github.com/skirpichev/omg/issues/16
  127. assert invert_complex(sinh(x), 0, x) != (x, FiniteSet(0))
  128. def test_domain_check():
  129. assert domain_check(1/(1 + (1/(x+1))**2), x, -1) is False
  130. assert domain_check(x**2, x, 0) is True
  131. assert domain_check(x, x, oo) is False
  132. assert domain_check(0, x, oo) is False
  133. def test_issue_11536():
  134. assert solveset(0**x - 100, x, S.Reals) == S.EmptySet
  135. assert solveset(0**x - 1, x, S.Reals) == FiniteSet(0)
  136. def test_issue_17479():
  137. f = (x**2 + y**2)**2 + (x**2 + z**2)**2 - 2*(2*x**2 + y**2 + z**2)
  138. fx = f.diff(x)
  139. fy = f.diff(y)
  140. fz = f.diff(z)
  141. sol = nonlinsolve([fx, fy, fz], [x, y, z])
  142. assert len(sol) >= 4 and len(sol) <= 20
  143. # nonlinsolve has been giving a varying number of solutions
  144. # (originally 18, then 20, now 19) due to various internal changes.
  145. # Unfortunately not all the solutions are actually valid and some are
  146. # redundant. Since the original issue was that an exception was raised,
  147. # this first test only checks that nonlinsolve returns a "plausible"
  148. # solution set. The next test checks the result for correctness.
  149. @XFAIL
  150. def test_issue_18449():
  151. x, y, z = symbols("x, y, z")
  152. f = (x**2 + y**2)**2 + (x**2 + z**2)**2 - 2*(2*x**2 + y**2 + z**2)
  153. fx = diff(f, x)
  154. fy = diff(f, y)
  155. fz = diff(f, z)
  156. sol = nonlinsolve([fx, fy, fz], [x, y, z])
  157. for (xs, ys, zs) in sol:
  158. d = {x: xs, y: ys, z: zs}
  159. assert tuple(_.subs(d).simplify() for _ in (fx, fy, fz)) == (0, 0, 0)
  160. # After simplification and removal of duplicate elements, there should
  161. # only be 4 parametric solutions left:
  162. # simplifiedsolutions = FiniteSet((sqrt(1 - z**2), z, z),
  163. # (-sqrt(1 - z**2), z, z),
  164. # (sqrt(1 - z**2), -z, z),
  165. # (-sqrt(1 - z**2), -z, z))
  166. # TODO: Is the above solution set definitely complete?
  167. def test_issue_21047():
  168. f = (2 - x)**2 + (sqrt(x - 1) - 1)**6
  169. assert solveset(f, x, S.Reals) == FiniteSet(2)
  170. f = (sqrt(x)-1)**2 + (sqrt(x)+1)**2 -2*x**2 + sqrt(2)
  171. assert solveset(f, x, S.Reals) == FiniteSet(
  172. S.Half - sqrt(2*sqrt(2) + 5)/2, S.Half + sqrt(2*sqrt(2) + 5)/2)
  173. def test_is_function_class_equation():
  174. assert _is_function_class_equation(TrigonometricFunction,
  175. tan(x), x) is True
  176. assert _is_function_class_equation(TrigonometricFunction,
  177. tan(x) - 1, x) is True
  178. assert _is_function_class_equation(TrigonometricFunction,
  179. tan(x) + sin(x), x) is True
  180. assert _is_function_class_equation(TrigonometricFunction,
  181. tan(x) + sin(x) - a, x) is True
  182. assert _is_function_class_equation(TrigonometricFunction,
  183. sin(x)*tan(x) + sin(x), x) is True
  184. assert _is_function_class_equation(TrigonometricFunction,
  185. sin(x)*tan(x + a) + sin(x), x) is True
  186. assert _is_function_class_equation(TrigonometricFunction,
  187. sin(x)*tan(x*a) + sin(x), x) is True
  188. assert _is_function_class_equation(TrigonometricFunction,
  189. a*tan(x) - 1, x) is True
  190. assert _is_function_class_equation(TrigonometricFunction,
  191. tan(x)**2 + sin(x) - 1, x) is True
  192. assert _is_function_class_equation(TrigonometricFunction,
  193. tan(x) + x, x) is False
  194. assert _is_function_class_equation(TrigonometricFunction,
  195. tan(x**2), x) is False
  196. assert _is_function_class_equation(TrigonometricFunction,
  197. tan(x**2) + sin(x), x) is False
  198. assert _is_function_class_equation(TrigonometricFunction,
  199. tan(x)**sin(x), x) is False
  200. assert _is_function_class_equation(TrigonometricFunction,
  201. tan(sin(x)) + sin(x), x) is False
  202. assert _is_function_class_equation(HyperbolicFunction,
  203. tanh(x), x) is True
  204. assert _is_function_class_equation(HyperbolicFunction,
  205. tanh(x) - 1, x) is True
  206. assert _is_function_class_equation(HyperbolicFunction,
  207. tanh(x) + sinh(x), x) is True
  208. assert _is_function_class_equation(HyperbolicFunction,
  209. tanh(x) + sinh(x) - a, x) is True
  210. assert _is_function_class_equation(HyperbolicFunction,
  211. sinh(x)*tanh(x) + sinh(x), x) is True
  212. assert _is_function_class_equation(HyperbolicFunction,
  213. sinh(x)*tanh(x + a) + sinh(x), x) is True
  214. assert _is_function_class_equation(HyperbolicFunction,
  215. sinh(x)*tanh(x*a) + sinh(x), x) is True
  216. assert _is_function_class_equation(HyperbolicFunction,
  217. a*tanh(x) - 1, x) is True
  218. assert _is_function_class_equation(HyperbolicFunction,
  219. tanh(x)**2 + sinh(x) - 1, x) is True
  220. assert _is_function_class_equation(HyperbolicFunction,
  221. tanh(x) + x, x) is False
  222. assert _is_function_class_equation(HyperbolicFunction,
  223. tanh(x**2), x) is False
  224. assert _is_function_class_equation(HyperbolicFunction,
  225. tanh(x**2) + sinh(x), x) is False
  226. assert _is_function_class_equation(HyperbolicFunction,
  227. tanh(x)**sinh(x), x) is False
  228. assert _is_function_class_equation(HyperbolicFunction,
  229. tanh(sinh(x)) + sinh(x), x) is False
  230. def test_garbage_input():
  231. raises(ValueError, lambda: solveset_real([y], y))
  232. x = Symbol('x', real=True)
  233. assert solveset_real(x, 1) == S.EmptySet
  234. assert solveset_real(x - 1, 1) == FiniteSet(x)
  235. assert solveset_real(x, pi) == S.EmptySet
  236. assert solveset_real(x, x**2) == S.EmptySet
  237. raises(ValueError, lambda: solveset_complex([x], x))
  238. assert solveset_complex(x, pi) == S.EmptySet
  239. raises(ValueError, lambda: solveset((x, y), x))
  240. raises(ValueError, lambda: solveset(x + 1, S.Reals))
  241. raises(ValueError, lambda: solveset(x + 1, x, 2))
  242. def test_solve_mul():
  243. assert solveset_real((a*x + b)*(exp(x) - 3), x) == \
  244. Union({log(3)}, Intersection({-b/a}, S.Reals))
  245. anz = Symbol('anz', nonzero=True)
  246. bb = Symbol('bb', real=True)
  247. assert solveset_real((anz*x + bb)*(exp(x) - 3), x) == \
  248. FiniteSet(-bb/anz, log(3))
  249. assert solveset_real((2*x + 8)*(8 + exp(x)), x) == FiniteSet(S(-4))
  250. assert solveset_real(x/log(x), x) is S.EmptySet
  251. def test_solve_invert():
  252. assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3))
  253. assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3))
  254. assert solveset_real(3**(x + 2), x) == FiniteSet()
  255. assert solveset_real(3**(2 - x), x) == FiniteSet()
  256. assert solveset_real(y - b*exp(a/x), x) == Intersection(
  257. S.Reals, FiniteSet(a/log(y/b)))
  258. # issue 4504
  259. assert solveset_real(2**x - 10, x) == FiniteSet(1 + log(5)/log(2))
  260. def test_errorinverses():
  261. assert solveset_real(erf(x) - S.Half, x) == \
  262. FiniteSet(erfinv(S.Half))
  263. assert solveset_real(erfinv(x) - 2, x) == \
  264. FiniteSet(erf(2))
  265. assert solveset_real(erfc(x) - S.One, x) == \
  266. FiniteSet(erfcinv(S.One))
  267. assert solveset_real(erfcinv(x) - 2, x) == FiniteSet(erfc(2))
  268. def test_solve_polynomial():
  269. x = Symbol('x', real=True)
  270. y = Symbol('y', real=True)
  271. assert solveset_real(3*x - 2, x) == FiniteSet(Rational(2, 3))
  272. assert solveset_real(x**2 - 1, x) == FiniteSet(-S.One, S.One)
  273. assert solveset_real(x - y**3, x) == FiniteSet(y ** 3)
  274. assert solveset_real(x**3 - 15*x - 4, x) == FiniteSet(
  275. -2 + 3 ** S.Half,
  276. S(4),
  277. -2 - 3 ** S.Half)
  278. assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1)
  279. assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4)
  280. assert solveset_real(x**Rational(1, 4) - 2, x) == FiniteSet(16)
  281. assert solveset_real(x**Rational(1, 3) - 3, x) == FiniteSet(27)
  282. assert len(solveset_real(x**5 + x**3 + 1, x)) == 1
  283. assert len(solveset_real(-2*x**3 + 4*x**2 - 2*x + 6, x)) > 0
  284. assert solveset_real(x**6 + x**4 + I, x) is S.EmptySet
  285. def test_return_root_of():
  286. f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20
  287. s = list(solveset_complex(f, x))
  288. for root in s:
  289. assert root.func == CRootOf
  290. # if one uses solve to get the roots of a polynomial that has a CRootOf
  291. # solution, make sure that the use of nfloat during the solve process
  292. # doesn't fail. Note: if you want numerical solutions to a polynomial
  293. # it is *much* faster to use nroots to get them than to solve the
  294. # equation only to get CRootOf solutions which are then numerically
  295. # evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather
  296. # than [i.n() for i in solve(eq)] to get the numerical roots of eq.
  297. assert nfloat(list(solveset_complex(x**5 + 3*x**3 + 7, x))[0],
  298. exponent=False) == CRootOf(x**5 + 3*x**3 + 7, 0).n()
  299. sol = list(solveset_complex(x**6 - 2*x + 2, x))
  300. assert all(isinstance(i, CRootOf) for i in sol) and len(sol) == 6
  301. f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20
  302. s = list(solveset_complex(f, x))
  303. for root in s:
  304. assert root.func == CRootOf
  305. s = x**5 + 4*x**3 + 3*x**2 + Rational(7, 4)
  306. assert solveset_complex(s, x) == \
  307. FiniteSet(*Poly(s*4, domain='ZZ').all_roots())
  308. # Refer issue #7876
  309. eq = x*(x - 1)**2*(x + 1)*(x**6 - x + 1)
  310. assert solveset_complex(eq, x) == \
  311. FiniteSet(-1, 0, 1, CRootOf(x**6 - x + 1, 0),
  312. CRootOf(x**6 - x + 1, 1),
  313. CRootOf(x**6 - x + 1, 2),
  314. CRootOf(x**6 - x + 1, 3),
  315. CRootOf(x**6 - x + 1, 4),
  316. CRootOf(x**6 - x + 1, 5))
  317. def test_solveset_sqrt_1():
  318. assert solveset_real(sqrt(5*x + 6) - 2 - x, x) == \
  319. FiniteSet(-S.One, S(2))
  320. assert solveset_real(sqrt(x - 1) - x + 7, x) == FiniteSet(10)
  321. assert solveset_real(sqrt(x - 2) - 5, x) == FiniteSet(27)
  322. assert solveset_real(sqrt(x) - 2 - 5, x) == FiniteSet(49)
  323. assert solveset_real(sqrt(x**3), x) == FiniteSet(0)
  324. assert solveset_real(sqrt(x - 1), x) == FiniteSet(1)
  325. def test_solveset_sqrt_2():
  326. x = Symbol('x', real=True)
  327. y = Symbol('y', real=True)
  328. # http://tutorial.math.lamar.edu/Classes/Alg/SolveRadicalEqns.aspx#Solve_Rad_Ex2_a
  329. assert solveset_real(sqrt(2*x - 1) - sqrt(x - 4) - 2, x) == \
  330. FiniteSet(S(5), S(13))
  331. assert solveset_real(sqrt(x + 7) + 2 - sqrt(3 - x), x) == \
  332. FiniteSet(-6)
  333. # http://www.purplemath.com/modules/solverad.htm
  334. assert solveset_real(sqrt(17*x - sqrt(x**2 - 5)) - 7, x) == \
  335. FiniteSet(3)
  336. eq = x + 1 - (x**4 + 4*x**3 - x)**Rational(1, 4)
  337. assert solveset_real(eq, x) == FiniteSet(Rational(-1, 2), Rational(-1, 3))
  338. eq = sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4)
  339. assert solveset_real(eq, x) == FiniteSet(0)
  340. eq = sqrt(x + 4) + sqrt(2*x - 1) - 3*sqrt(x - 1)
  341. assert solveset_real(eq, x) == FiniteSet(5)
  342. eq = sqrt(x)*sqrt(x - 7) - 12
  343. assert solveset_real(eq, x) == FiniteSet(16)
  344. eq = sqrt(x - 3) + sqrt(x) - 3
  345. assert solveset_real(eq, x) == FiniteSet(4)
  346. eq = sqrt(2*x**2 - 7) - (3 - x)
  347. assert solveset_real(eq, x) == FiniteSet(-S(8), S(2))
  348. # others
  349. eq = sqrt(9*x**2 + 4) - (3*x + 2)
  350. assert solveset_real(eq, x) == FiniteSet(0)
  351. assert solveset_real(sqrt(x - 3) - sqrt(x) - 3, x) == FiniteSet()
  352. eq = (2*x - 5)**Rational(1, 3) - 3
  353. assert solveset_real(eq, x) == FiniteSet(16)
  354. assert solveset_real(sqrt(x) + sqrt(sqrt(x)) - 4, x) == \
  355. FiniteSet((Rational(-1, 2) + sqrt(17)/2)**4)
  356. eq = sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x))
  357. assert solveset_real(eq, x) == FiniteSet()
  358. eq = (x - 4)**2 + (sqrt(x) - 2)**4
  359. assert solveset_real(eq, x) == FiniteSet(-4, 4)
  360. eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5)
  361. ans = solveset_real(eq, x)
  362. ra = S('''-1484/375 - 4*(-1/2 + sqrt(3)*I/2)*(-12459439/52734375 +
  363. 114*sqrt(12657)/78125)**(1/3) - 172564/(140625*(-1/2 +
  364. sqrt(3)*I/2)*(-12459439/52734375 + 114*sqrt(12657)/78125)**(1/3))''')
  365. rb = Rational(4, 5)
  366. assert all(abs(eq.subs(x, i).n()) < 1e-10 for i in (ra, rb)) and \
  367. len(ans) == 2 and \
  368. {i.n(chop=True) for i in ans} == \
  369. {i.n(chop=True) for i in (ra, rb)}
  370. assert solveset_real(sqrt(x) + x**Rational(1, 3) +
  371. x**Rational(1, 4), x) == FiniteSet(0)
  372. assert solveset_real(x/sqrt(x**2 + 1), x) == FiniteSet(0)
  373. eq = (x - y**3)/((y**2)*sqrt(1 - y**2))
  374. assert solveset_real(eq, x) == FiniteSet(y**3)
  375. # issue 4497
  376. assert solveset_real(1/(5 + x)**Rational(1, 5) - 9, x) == \
  377. FiniteSet(Rational(-295244, 59049))
  378. @XFAIL
  379. def test_solve_sqrt_fail():
  380. # this only works if we check real_root(eq.subs(x, Rational(1, 3)))
  381. # but checksol doesn't work like that
  382. eq = (x**3 - 3*x**2)**Rational(1, 3) + 1 - x
  383. assert solveset_real(eq, x) == FiniteSet(Rational(1, 3))
  384. @slow
  385. def test_solve_sqrt_3():
  386. R = Symbol('R')
  387. eq = sqrt(2)*R*sqrt(1/(R + 1)) + (R + 1)*(sqrt(2)*sqrt(1/(R + 1)) - 1)
  388. sol = solveset_complex(eq, R)
  389. fset = [Rational(5, 3) + 4*sqrt(10)*cos(atan(3*sqrt(111)/251)/3)/3,
  390. -sqrt(10)*cos(atan(3*sqrt(111)/251)/3)/3 +
  391. 40*re(1/((Rational(-1, 2) - sqrt(3)*I/2)*(Rational(251, 27) + sqrt(111)*I/9)**Rational(1, 3)))/9 +
  392. sqrt(30)*sin(atan(3*sqrt(111)/251)/3)/3 + Rational(5, 3) +
  393. I*(-sqrt(30)*cos(atan(3*sqrt(111)/251)/3)/3 -
  394. sqrt(10)*sin(atan(3*sqrt(111)/251)/3)/3 +
  395. 40*im(1/((Rational(-1, 2) - sqrt(3)*I/2)*(Rational(251, 27) + sqrt(111)*I/9)**Rational(1, 3)))/9)]
  396. cset = [40*re(1/((Rational(-1, 2) + sqrt(3)*I/2)*(Rational(251, 27) + sqrt(111)*I/9)**Rational(1, 3)))/9 -
  397. sqrt(10)*cos(atan(3*sqrt(111)/251)/3)/3 - sqrt(30)*sin(atan(3*sqrt(111)/251)/3)/3 +
  398. Rational(5, 3) +
  399. I*(40*im(1/((Rational(-1, 2) + sqrt(3)*I/2)*(Rational(251, 27) + sqrt(111)*I/9)**Rational(1, 3)))/9 -
  400. sqrt(10)*sin(atan(3*sqrt(111)/251)/3)/3 +
  401. sqrt(30)*cos(atan(3*sqrt(111)/251)/3)/3)]
  402. assert sol._args[0] == FiniteSet(*fset)
  403. assert sol._args[1] == ConditionSet(
  404. R,
  405. Eq(sqrt(2)*R*sqrt(1/(R + 1)) + (R + 1)*(sqrt(2)*sqrt(1/(R + 1)) - 1), 0),
  406. FiniteSet(*cset))
  407. # the number of real roots will depend on the value of m: for m=1 there are 4
  408. # and for m=-1 there are none.
  409. eq = -sqrt((m - q)**2 + (-m/(2*q) + S.Half)**2) + sqrt((-m**2/2 - sqrt(
  410. 4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2 + (m**2/2 - m - sqrt(
  411. 4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2)
  412. unsolved_object = ConditionSet(q, Eq(sqrt((m - q)**2 + (-m/(2*q) + S.Half)**2) -
  413. sqrt((-m**2/2 - sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2 + (m**2/2 - m -
  414. sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - Rational(1, 4))**2), 0), S.Reals)
  415. assert solveset_real(eq, q) == unsolved_object
  416. def test_solve_polynomial_symbolic_param():
  417. assert solveset_complex((x**2 - 1)**2 - a, x) == \
  418. FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)),
  419. sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a)))
  420. # issue 4507
  421. assert solveset_complex(y - b/(1 + a*x), x) == \
  422. FiniteSet((b/y - 1)/a) - FiniteSet(-1/a)
  423. # issue 4508
  424. assert solveset_complex(y - b*x/(a + x), x) == \
  425. FiniteSet(-a*y/(y - b)) - FiniteSet(-a)
  426. def test_solve_rational():
  427. assert solveset_real(1/x + 1, x) == FiniteSet(-S.One)
  428. assert solveset_real(1/exp(x) - 1, x) == FiniteSet(0)
  429. assert solveset_real(x*(1 - 5/x), x) == FiniteSet(5)
  430. assert solveset_real(2*x/(x + 2) - 1, x) == FiniteSet(2)
  431. assert solveset_real((x**2/(7 - x)).diff(x), x) == \
  432. FiniteSet(S.Zero, S(14))
  433. def test_solveset_real_gen_is_pow():
  434. assert solveset_real(sqrt(1) + 1, x) is S.EmptySet
  435. def test_no_sol():
  436. assert solveset(1 - oo*x) is S.EmptySet
  437. assert solveset(oo*x, x) is S.EmptySet
  438. assert solveset(oo*x - oo, x) is S.EmptySet
  439. assert solveset_real(4, x) is S.EmptySet
  440. assert solveset_real(exp(x), x) is S.EmptySet
  441. assert solveset_real(x**2 + 1, x) is S.EmptySet
  442. assert solveset_real(-3*a/sqrt(x), x) is S.EmptySet
  443. assert solveset_real(1/x, x) is S.EmptySet
  444. assert solveset_real(-(1 + x)/(2 + x)**2 + 1/(2 + x), x
  445. ) is S.EmptySet
  446. def test_sol_zero_real():
  447. assert solveset_real(0, x) == S.Reals
  448. assert solveset(0, x, Interval(1, 2)) == Interval(1, 2)
  449. assert solveset_real(-x**2 - 2*x + (x + 1)**2 - 1, x) == S.Reals
  450. def test_no_sol_rational_extragenous():
  451. assert solveset_real((x/(x + 1) + 3)**(-2), x) is S.EmptySet
  452. assert solveset_real((x - 1)/(1 + 1/(x - 1)), x) is S.EmptySet
  453. def test_solve_polynomial_cv_1a():
  454. """
  455. Test for solving on equations that can be converted to
  456. a polynomial equation using the change of variable y -> x**Rational(p, q)
  457. """
  458. assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1)
  459. assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4)
  460. assert solveset_real(x**Rational(1, 4) - 2, x) == FiniteSet(16)
  461. assert solveset_real(x**Rational(1, 3) - 3, x) == FiniteSet(27)
  462. assert solveset_real(x*(x**(S.One / 3) - 3), x) == \
  463. FiniteSet(S.Zero, S(27))
  464. def test_solveset_real_rational():
  465. """Test solveset_real for rational functions"""
  466. x = Symbol('x', real=True)
  467. y = Symbol('y', real=True)
  468. assert solveset_real((x - y**3) / ((y**2)*sqrt(1 - y**2)), x) \
  469. == FiniteSet(y**3)
  470. # issue 4486
  471. assert solveset_real(2*x/(x + 2) - 1, x) == FiniteSet(2)
  472. def test_solveset_real_log():
  473. assert solveset_real(log((x-1)*(x+1)), x) == \
  474. FiniteSet(sqrt(2), -sqrt(2))
  475. def test_poly_gens():
  476. assert solveset_real(4**(2*(x**2) + 2*x) - 8, x) == \
  477. FiniteSet(Rational(-3, 2), S.Half)
  478. def test_solve_abs():
  479. n = Dummy('n')
  480. raises(ValueError, lambda: solveset(Abs(x) - 1, x))
  481. assert solveset(Abs(x) - n, x, S.Reals).dummy_eq(
  482. ConditionSet(x, Contains(n, Interval(0, oo)), {-n, n}))
  483. assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2)
  484. assert solveset_real(Abs(x) + 2, x) is S.EmptySet
  485. assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \
  486. FiniteSet(1, 9)
  487. assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \
  488. FiniteSet(-1, Rational(1, 3))
  489. sol = ConditionSet(
  490. x,
  491. And(
  492. Contains(b, Interval(0, oo)),
  493. Contains(a + b, Interval(0, oo)),
  494. Contains(a - b, Interval(0, oo))),
  495. FiniteSet(-a - b - 3, -a + b - 3, a - b - 3, a + b - 3))
  496. eq = Abs(Abs(x + 3) - a) - b
  497. assert invert_real(eq, 0, x)[1] == sol
  498. reps = {a: 3, b: 1}
  499. eqab = eq.subs(reps)
  500. for si in sol.subs(reps):
  501. assert not eqab.subs(x, si)
  502. assert dumeq(solveset(Eq(sin(Abs(x)), 1), x, domain=S.Reals), Union(
  503. Intersection(Interval(0, oo),
  504. ImageSet(Lambda(n, (-1)**n*pi/2 + n*pi), S.Integers)),
  505. Intersection(Interval(-oo, 0),
  506. ImageSet(Lambda(n, n*pi - (-1)**(-n)*pi/2), S.Integers))))
  507. def test_issue_9824():
  508. assert dumeq(solveset(sin(x)**2 - 2*sin(x) + 1, x), ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers))
  509. assert dumeq(solveset(cos(x)**2 - 2*cos(x) + 1, x), ImageSet(Lambda(n, 2*n*pi), S.Integers))
  510. def test_issue_9565():
  511. assert solveset_real(Abs((x - 1)/(x - 5)) <= Rational(1, 3), x) == Interval(-1, 2)
  512. def test_issue_10069():
  513. eq = abs(1/(x - 1)) - 1 > 0
  514. assert solveset_real(eq, x) == Union(
  515. Interval.open(0, 1), Interval.open(1, 2))
  516. def test_real_imag_splitting():
  517. a, b = symbols('a b', real=True)
  518. assert solveset_real(sqrt(a**2 - b**2) - 3, a) == \
  519. FiniteSet(-sqrt(b**2 + 9), sqrt(b**2 + 9))
  520. assert solveset_real(sqrt(a**2 + b**2) - 3, a) != \
  521. S.EmptySet
  522. def test_units():
  523. assert solveset_real(1/x - 1/(2*cm), x) == FiniteSet(2*cm)
  524. def test_solve_only_exp_1():
  525. y = Symbol('y', positive=True)
  526. assert solveset_real(exp(x) - y, x) == FiniteSet(log(y))
  527. assert solveset_real(exp(x) + exp(-x) - 4, x) == \
  528. FiniteSet(log(-sqrt(3) + 2), log(sqrt(3) + 2))
  529. assert solveset_real(exp(x) + exp(-x) - y, x) != S.EmptySet
  530. def test_atan2():
  531. # The .inverse() method on atan2 works only if x.is_real is True and the
  532. # second argument is a real constant
  533. assert solveset_real(atan2(x, 2) - pi/3, x) == FiniteSet(2*sqrt(3))
  534. def test_piecewise_solveset():
  535. eq = Piecewise((x - 2, Gt(x, 2)), (2 - x, True)) - 3
  536. assert set(solveset_real(eq, x)) == set(FiniteSet(-1, 5))
  537. absxm3 = Piecewise(
  538. (x - 3, 0 <= x - 3),
  539. (3 - x, 0 > x - 3))
  540. y = Symbol('y', positive=True)
  541. assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3)
  542. f = Piecewise(((x - 2)**2, x >= 0), (0, True))
  543. assert solveset(f, x, domain=S.Reals) == Union(FiniteSet(2), Interval(-oo, 0, True, True))
  544. assert solveset(
  545. Piecewise((x + 1, x > 0), (I, True)) - I, x, S.Reals
  546. ) == Interval(-oo, 0)
  547. assert solveset(Piecewise((x - 1, Ne(x, I)), (x, True)), x) == FiniteSet(1)
  548. # issue 19718
  549. g = Piecewise((1, x > 10), (0, True))
  550. assert solveset(g > 0, x, S.Reals) == Interval.open(10, oo)
  551. from sympy.logic.boolalg import BooleanTrue
  552. f = BooleanTrue()
  553. assert solveset(f, x, domain=Interval(-3, 10)) == Interval(-3, 10)
  554. # issue 20552
  555. f = Piecewise((0, Eq(x, 0)), (x**2/Abs(x), True))
  556. g = Piecewise((0, Eq(x, pi)), ((x - pi)/sin(x), True))
  557. assert solveset(f, x, domain=S.Reals) == FiniteSet(0)
  558. assert solveset(g) == FiniteSet(pi)
  559. def test_solveset_complex_polynomial():
  560. assert solveset_complex(a*x**2 + b*x + c, x) == \
  561. FiniteSet(-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a),
  562. -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a))
  563. assert solveset_complex(x - y**3, y) == FiniteSet(
  564. (-x**Rational(1, 3))/2 + I*sqrt(3)*x**Rational(1, 3)/2,
  565. x**Rational(1, 3),
  566. (-x**Rational(1, 3))/2 - I*sqrt(3)*x**Rational(1, 3)/2)
  567. assert solveset_complex(x + 1/x - 1, x) == \
  568. FiniteSet(S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2)
  569. def test_sol_zero_complex():
  570. assert solveset_complex(0, x) is S.Complexes
  571. def test_solveset_complex_rational():
  572. assert solveset_complex((x - 1)*(x - I)/(x - 3), x) == \
  573. FiniteSet(1, I)
  574. assert solveset_complex((x - y**3)/((y**2)*sqrt(1 - y**2)), x) == \
  575. FiniteSet(y**3)
  576. assert solveset_complex(-x**2 - I, x) == \
  577. FiniteSet(-sqrt(2)/2 + sqrt(2)*I/2, sqrt(2)/2 - sqrt(2)*I/2)
  578. def test_solve_quintics():
  579. skip("This test is too slow")
  580. f = x**5 - 110*x**3 - 55*x**2 + 2310*x + 979
  581. s = solveset_complex(f, x)
  582. for root in s:
  583. res = f.subs(x, root.n()).n()
  584. assert tn(res, 0)
  585. f = x**5 + 15*x + 12
  586. s = solveset_complex(f, x)
  587. for root in s:
  588. res = f.subs(x, root.n()).n()
  589. assert tn(res, 0)
  590. def test_solveset_complex_exp():
  591. assert dumeq(solveset_complex(exp(x) - 1, x),
  592. imageset(Lambda(n, I*2*n*pi), S.Integers))
  593. assert dumeq(solveset_complex(exp(x) - I, x),
  594. imageset(Lambda(n, I*(2*n*pi + pi/2)), S.Integers))
  595. assert solveset_complex(1/exp(x), x) == S.EmptySet
  596. assert dumeq(solveset_complex(sinh(x).rewrite(exp), x),
  597. imageset(Lambda(n, n*pi*I), S.Integers))
  598. def test_solveset_real_exp():
  599. assert solveset(Eq((-2)**x, 4), x, S.Reals) == FiniteSet(2)
  600. assert solveset(Eq(-2**x, 4), x, S.Reals) == S.EmptySet
  601. assert solveset(Eq((-3)**x, 27), x, S.Reals) == S.EmptySet
  602. assert solveset(Eq((-5)**(x+1), 625), x, S.Reals) == FiniteSet(3)
  603. assert solveset(Eq(2**(x-3), -16), x, S.Reals) == S.EmptySet
  604. assert solveset(Eq((-3)**(x - 3), -3**39), x, S.Reals) == FiniteSet(42)
  605. assert solveset(Eq(2**x, y), x, S.Reals) == Intersection(S.Reals, FiniteSet(log(y)/log(2)))
  606. assert invert_real((-2)**(2*x) - 16, 0, x) == (x, FiniteSet(2))
  607. def test_solve_complex_log():
  608. assert solveset_complex(log(x), x) == FiniteSet(1)
  609. assert solveset_complex(1 - log(a + 4*x**2), x) == \
  610. FiniteSet(-sqrt(-a + E)/2, sqrt(-a + E)/2)
  611. def test_solve_complex_sqrt():
  612. assert solveset_complex(sqrt(5*x + 6) - 2 - x, x) == \
  613. FiniteSet(-S.One, S(2))
  614. assert solveset_complex(sqrt(5*x + 6) - (2 + 2*I) - x, x) == \
  615. FiniteSet(-S(2), 3 - 4*I)
  616. assert solveset_complex(4*x*(1 - a * sqrt(x)), x) == \
  617. FiniteSet(S.Zero, 1 / a ** 2)
  618. def test_solveset_complex_tan():
  619. s = solveset_complex(tan(x).rewrite(exp), x)
  620. assert dumeq(s, imageset(Lambda(n, pi*n), S.Integers) - \
  621. imageset(Lambda(n, pi*n + pi/2), S.Integers))
  622. @_both_exp_pow
  623. def test_solve_trig():
  624. assert dumeq(solveset_real(sin(x), x),
  625. Union(imageset(Lambda(n, 2*pi*n), S.Integers),
  626. imageset(Lambda(n, 2*pi*n + pi), S.Integers)))
  627. assert dumeq(solveset_real(sin(x) - 1, x),
  628. imageset(Lambda(n, 2*pi*n + pi/2), S.Integers))
  629. assert dumeq(solveset_real(cos(x), x),
  630. Union(imageset(Lambda(n, 2*pi*n + pi/2), S.Integers),
  631. imageset(Lambda(n, 2*pi*n + pi*Rational(3, 2)), S.Integers)))
  632. assert dumeq(solveset_real(sin(x) + cos(x), x),
  633. Union(imageset(Lambda(n, 2*n*pi + pi*Rational(3, 4)), S.Integers),
  634. imageset(Lambda(n, 2*n*pi + pi*Rational(7, 4)), S.Integers)))
  635. assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet
  636. assert dumeq(solveset_complex(cos(x) - S.Half, x),
  637. Union(imageset(Lambda(n, 2*n*pi + pi*Rational(5, 3)), S.Integers),
  638. imageset(Lambda(n, 2*n*pi + pi/3), S.Integers)))
  639. assert dumeq(solveset(sin(y + a) - sin(y), a, domain=S.Reals),
  640. Union(ImageSet(Lambda(n, 2*n*pi), S.Integers),
  641. Intersection(ImageSet(Lambda(n, -I*(I*(
  642. 2*n*pi + arg(-exp(-2*I*y))) +
  643. 2*im(y))), S.Integers), S.Reals)))
  644. assert dumeq(solveset_real(sin(2*x)*cos(x) + cos(2*x)*sin(x)-1, x),
  645. ImageSet(Lambda(n, n*pi*Rational(2, 3) + pi/6), S.Integers))
  646. assert dumeq(solveset_real(2*tan(x)*sin(x) + 1, x), Union(
  647. ImageSet(Lambda(n, 2*n*pi + atan(sqrt(2)*sqrt(-1 + sqrt(17))/
  648. (1 - sqrt(17))) + pi), S.Integers),
  649. ImageSet(Lambda(n, 2*n*pi - atan(sqrt(2)*sqrt(-1 + sqrt(17))/
  650. (1 - sqrt(17))) + pi), S.Integers)))
  651. assert dumeq(solveset_real(cos(2*x)*cos(4*x) - 1, x),
  652. ImageSet(Lambda(n, n*pi), S.Integers))
  653. assert dumeq(solveset(sin(x/10) + Rational(3, 4)), Union(
  654. ImageSet(Lambda(n, 20*n*pi + 10*atan(3*sqrt(7)/7) + 10*pi), S.Integers),
  655. ImageSet(Lambda(n, 20*n*pi - 10*atan(3*sqrt(7)/7) + 20*pi), S.Integers)))
  656. assert dumeq(solveset(cos(x/15) + cos(x/5)), Union(
  657. ImageSet(Lambda(n, 30*n*pi + 15*pi/2), S.Integers),
  658. ImageSet(Lambda(n, 30*n*pi + 45*pi/2), S.Integers),
  659. ImageSet(Lambda(n, 30*n*pi + 75*pi/4), S.Integers),
  660. ImageSet(Lambda(n, 30*n*pi + 45*pi/4), S.Integers),
  661. ImageSet(Lambda(n, 30*n*pi + 105*pi/4), S.Integers),
  662. ImageSet(Lambda(n, 30*n*pi + 15*pi/4), S.Integers)))
  663. assert dumeq(solveset(sec(sqrt(2)*x/3) + 5), Union(
  664. ImageSet(Lambda(n, 3*sqrt(2)*(2*n*pi - pi + atan(2*sqrt(6)))/2), S.Integers),
  665. ImageSet(Lambda(n, 3*sqrt(2)*(2*n*pi - atan(2*sqrt(6)) + pi)/2), S.Integers)))
  666. assert dumeq(simplify(solveset(tan(pi*x) - cot(pi/2*x))), Union(
  667. ImageSet(Lambda(n, 4*n + 1), S.Integers),
  668. ImageSet(Lambda(n, 4*n + 3), S.Integers),
  669. ImageSet(Lambda(n, 4*n + Rational(7, 3)), S.Integers),
  670. ImageSet(Lambda(n, 4*n + Rational(5, 3)), S.Integers),
  671. ImageSet(Lambda(n, 4*n + Rational(11, 3)), S.Integers),
  672. ImageSet(Lambda(n, 4*n + Rational(1, 3)), S.Integers)))
  673. assert dumeq(solveset(cos(9*x)), Union(
  674. ImageSet(Lambda(n, 2*n*pi/9 + pi/18), S.Integers),
  675. ImageSet(Lambda(n, 2*n*pi/9 + pi/6), S.Integers)))
  676. assert dumeq(solveset(sin(8*x) + cot(12*x), x, S.Reals), Union(
  677. ImageSet(Lambda(n, n*pi/2 + pi/8), S.Integers),
  678. ImageSet(Lambda(n, n*pi/2 + 3*pi/8), S.Integers),
  679. ImageSet(Lambda(n, n*pi/2 + 5*pi/16), S.Integers),
  680. ImageSet(Lambda(n, n*pi/2 + 3*pi/16), S.Integers),
  681. ImageSet(Lambda(n, n*pi/2 + 7*pi/16), S.Integers),
  682. ImageSet(Lambda(n, n*pi/2 + pi/16), S.Integers)))
  683. # This is the only remaining solveset test that actually ends up being solved
  684. # by _solve_trig2(). All others are handled by the improved _solve_trig1.
  685. assert dumeq(solveset_real(2*cos(x)*cos(2*x) - 1, x),
  686. Union(ImageSet(Lambda(n, 2*n*pi + 2*atan(sqrt(-2*2**Rational(1, 3)*(67 +
  687. 9*sqrt(57))**Rational(2, 3) + 8*2**Rational(2, 3) + 11*(67 +
  688. 9*sqrt(57))**Rational(1, 3))/(3*(67 + 9*sqrt(57))**Rational(1, 6)))), S.Integers),
  689. ImageSet(Lambda(n, 2*n*pi - 2*atan(sqrt(-2*2**Rational(1, 3)*(67 +
  690. 9*sqrt(57))**Rational(2, 3) + 8*2**Rational(2, 3) + 11*(67 +
  691. 9*sqrt(57))**Rational(1, 3))/(3*(67 + 9*sqrt(57))**Rational(1, 6))) +
  692. 2*pi), S.Integers)))
  693. # issue #16870
  694. assert dumeq(simplify(solveset(sin(x/180*pi) - S.Half, x, S.Reals)), Union(
  695. ImageSet(Lambda(n, 360*n + 150), S.Integers),
  696. ImageSet(Lambda(n, 360*n + 30), S.Integers)))
  697. def test_solve_hyperbolic():
  698. # actual solver: _solve_trig1
  699. n = Dummy('n')
  700. assert solveset(sinh(x) + cosh(x), x) == S.EmptySet
  701. assert solveset(sinh(x) + cos(x), x) == ConditionSet(x,
  702. Eq(cos(x) + sinh(x), 0), S.Complexes)
  703. assert solveset_real(sinh(x) + sech(x), x) == FiniteSet(
  704. log(sqrt(sqrt(5) - 2)))
  705. assert solveset_real(3*cosh(2*x) - 5, x) == FiniteSet(
  706. -log(3)/2, log(3)/2)
  707. assert solveset_real(sinh(x - 3) - 2, x) == FiniteSet(
  708. log((2 + sqrt(5))*exp(3)))
  709. assert solveset_real(cosh(2*x) + 2*sinh(x) - 5, x) == FiniteSet(
  710. log(-2 + sqrt(5)), log(1 + sqrt(2)))
  711. assert solveset_real((coth(x) + sinh(2*x))/cosh(x) - 3, x) == FiniteSet(
  712. log(S.Half + sqrt(5)/2), log(1 + sqrt(2)))
  713. assert solveset_real(cosh(x)*sinh(x) - 2, x) == FiniteSet(
  714. log(4 + sqrt(17))/2)
  715. assert solveset_real(sinh(x) + tanh(x) - 1, x) == FiniteSet(
  716. log(sqrt(2)/2 + sqrt(-S(1)/2 + sqrt(2))))
  717. assert dumeq(solveset_complex(sinh(x) - I/2, x), Union(
  718. ImageSet(Lambda(n, I*(2*n*pi + 5*pi/6)), S.Integers),
  719. ImageSet(Lambda(n, I*(2*n*pi + pi/6)), S.Integers)))
  720. assert dumeq(solveset_complex(sinh(x) + sech(x), x), Union(
  721. ImageSet(Lambda(n, 2*n*I*pi + log(sqrt(-2 + sqrt(5)))), S.Integers),
  722. ImageSet(Lambda(n, I*(2*n*pi + pi/2) + log(sqrt(2 + sqrt(5)))), S.Integers),
  723. ImageSet(Lambda(n, I*(2*n*pi + pi) + log(sqrt(-2 + sqrt(5)))), S.Integers),
  724. ImageSet(Lambda(n, I*(2*n*pi - pi/2) + log(sqrt(2 + sqrt(5)))), S.Integers)))
  725. assert dumeq(solveset(sinh(x/10) + Rational(3, 4)), Union(
  726. ImageSet(Lambda(n, 10*I*(2*n*pi + pi) + 10*log(2)), S.Integers),
  727. ImageSet(Lambda(n, 20*n*I*pi - 10*log(2)), S.Integers)))
  728. assert dumeq(solveset(cosh(x/15) + cosh(x/5)), Union(
  729. ImageSet(Lambda(n, 15*I*(2*n*pi + pi/2)), S.Integers),
  730. ImageSet(Lambda(n, 15*I*(2*n*pi - pi/2)), S.Integers),
  731. ImageSet(Lambda(n, 15*I*(2*n*pi - 3*pi/4)), S.Integers),
  732. ImageSet(Lambda(n, 15*I*(2*n*pi + 3*pi/4)), S.Integers),
  733. ImageSet(Lambda(n, 15*I*(2*n*pi - pi/4)), S.Integers),
  734. ImageSet(Lambda(n, 15*I*(2*n*pi + pi/4)), S.Integers)))
  735. assert dumeq(solveset(sech(sqrt(2)*x/3) + 5), Union(
  736. ImageSet(Lambda(n, 3*sqrt(2)*I*(2*n*pi - pi + atan(2*sqrt(6)))/2), S.Integers),
  737. ImageSet(Lambda(n, 3*sqrt(2)*I*(2*n*pi - atan(2*sqrt(6)) + pi)/2), S.Integers)))
  738. assert dumeq(solveset(tanh(pi*x) - coth(pi/2*x)), Union(
  739. ImageSet(Lambda(n, 2*I*(2*n*pi + pi/2)/pi), S.Integers),
  740. ImageSet(Lambda(n, 2*I*(2*n*pi - pi/2)/pi), S.Integers)))
  741. assert dumeq(solveset(cosh(9*x)), Union(
  742. ImageSet(Lambda(n, I*(2*n*pi + pi/2)/9), S.Integers),
  743. ImageSet(Lambda(n, I*(2*n*pi - pi/2)/9), S.Integers)))
  744. # issues #9606 / #9531:
  745. assert solveset(sinh(x), x, S.Reals) == FiniteSet(0)
  746. assert dumeq(solveset(sinh(x), x, S.Complexes), Union(
  747. ImageSet(Lambda(n, I*(2*n*pi + pi)), S.Integers),
  748. ImageSet(Lambda(n, 2*n*I*pi), S.Integers)))
  749. # issues #11218 / #18427
  750. assert dumeq(solveset(sin(pi*x), x, S.Reals), Union(
  751. ImageSet(Lambda(n, (2*n*pi + pi)/pi), S.Integers),
  752. ImageSet(Lambda(n, 2*n), S.Integers)))
  753. assert dumeq(solveset(sin(pi*x), x), Union(
  754. ImageSet(Lambda(n, (2*n*pi + pi)/pi), S.Integers),
  755. ImageSet(Lambda(n, 2*n), S.Integers)))
  756. # issue #17543
  757. assert dumeq(simplify(solveset(I*cot(8*x - 8*E), x)), Union(
  758. ImageSet(Lambda(n, n*pi/4 - 13*pi/16 + E), S.Integers),
  759. ImageSet(Lambda(n, n*pi/4 - 11*pi/16 + E), S.Integers)))
  760. # issues #18490 / #19489
  761. assert solveset(cosh(x) + cosh(3*x) - cosh(5*x), x, S.Reals
  762. ).dummy_eq(ConditionSet(x,
  763. Eq(cosh(x) + cosh(3*x) - cosh(5*x), 0), S.Reals))
  764. assert solveset(sinh(8*x) + coth(12*x)).dummy_eq(
  765. ConditionSet(x, Eq(sinh(8*x) + coth(12*x), 0), S.Complexes))
  766. def test_solve_trig_hyp_symbolic():
  767. # actual solver: _solve_trig1
  768. assert dumeq(solveset(sin(a*x), x), ConditionSet(x, Ne(a, 0), Union(
  769. ImageSet(Lambda(n, (2*n*pi + pi)/a), S.Integers),
  770. ImageSet(Lambda(n, 2*n*pi/a), S.Integers))))
  771. assert dumeq(solveset(cosh(x/a), x), ConditionSet(x, Ne(a, 0), Union(
  772. ImageSet(Lambda(n, I*a*(2*n*pi + pi/2)), S.Integers),
  773. ImageSet(Lambda(n, I*a*(2*n*pi - pi/2)), S.Integers))))
  774. assert dumeq(solveset(sin(2*sqrt(3)/3*a**2/(b*pi)*x)
  775. + cos(4*sqrt(3)/3*a**2/(b*pi)*x), x),
  776. ConditionSet(x, Ne(b, 0) & Ne(a**2, 0), Union(
  777. ImageSet(Lambda(n, sqrt(3)*pi*b*(2*n*pi + pi/2)/(2*a**2)), S.Integers),
  778. ImageSet(Lambda(n, sqrt(3)*pi*b*(2*n*pi - 5*pi/6)/(2*a**2)), S.Integers),
  779. ImageSet(Lambda(n, sqrt(3)*pi*b*(2*n*pi - pi/6)/(2*a**2)), S.Integers))))
  780. assert dumeq(simplify(solveset(cot((1 + I)*x) - cot((3 + 3*I)*x), x)), Union(
  781. ImageSet(Lambda(n, pi*(1 - I)*(4*n + 1)/4), S.Integers),
  782. ImageSet(Lambda(n, pi*(1 - I)*(4*n - 1)/4), S.Integers)))
  783. assert dumeq(solveset(cosh((a**2 + 1)*x) - 3, x),
  784. ConditionSet(x, Ne(a**2 + 1, 0), Union(
  785. ImageSet(Lambda(n, (2*n*I*pi + log(3 - 2*sqrt(2)))/(a**2 + 1)), S.Integers),
  786. ImageSet(Lambda(n, (2*n*I*pi + log(2*sqrt(2) + 3))/(a**2 + 1)), S.Integers))))
  787. ar = Symbol('ar', real=True)
  788. assert solveset(cosh((ar**2 + 1)*x) - 2, x, S.Reals) == FiniteSet(
  789. log(sqrt(3) + 2)/(ar**2 + 1), log(2 - sqrt(3))/(ar**2 + 1))
  790. def test_issue_9616():
  791. assert dumeq(solveset(sinh(x) + tanh(x) - 1, x), Union(
  792. ImageSet(Lambda(n, 2*n*I*pi + log(sqrt(2)/2 + sqrt(-S.Half + sqrt(2)))), S.Integers),
  793. ImageSet(Lambda(n, I*(2*n*pi - atan(sqrt(2)*sqrt(S.Half + sqrt(2))) + pi)
  794. + log(sqrt(1 + sqrt(2)))), S.Integers),
  795. ImageSet(Lambda(n, I*(2*n*pi + pi) + log(-sqrt(2)/2 + sqrt(-S.Half + sqrt(2)))), S.Integers),
  796. ImageSet(Lambda(n, I*(2*n*pi - pi + atan(sqrt(2)*sqrt(S.Half + sqrt(2))))
  797. + log(sqrt(1 + sqrt(2)))), S.Integers)))
  798. f1 = (sinh(x)).rewrite(exp)
  799. f2 = (tanh(x)).rewrite(exp)
  800. assert dumeq(solveset(f1 + f2 - 1, x), Union(
  801. Complement(ImageSet(
  802. Lambda(n, I*(2*n*pi + pi) + log(-sqrt(2)/2 + sqrt(-S.Half + sqrt(2)))), S.Integers),
  803. ImageSet(Lambda(n, I*(2*n*pi + pi)/2), S.Integers)),
  804. Complement(ImageSet(Lambda(n, I*(2*n*pi - pi + atan(sqrt(2)*sqrt(S.Half + sqrt(2))))
  805. + log(sqrt(1 + sqrt(2)))), S.Integers),
  806. ImageSet(Lambda(n, I*(2*n*pi + pi)/2), S.Integers)),
  807. Complement(ImageSet(Lambda(n, I*(2*n*pi - atan(sqrt(2)*sqrt(S.Half + sqrt(2))) + pi)
  808. + log(sqrt(1 + sqrt(2)))), S.Integers),
  809. ImageSet(Lambda(n, I*(2*n*pi + pi)/2), S.Integers)),
  810. Complement(
  811. ImageSet(Lambda(n, 2*n*I*pi + log(sqrt(2)/2 + sqrt(-S.Half + sqrt(2)))), S.Integers),
  812. ImageSet(Lambda(n, I*(2*n*pi + pi)/2), S.Integers))))
  813. def test_solve_invalid_sol():
  814. assert 0 not in solveset_real(sin(x)/x, x)
  815. assert 0 not in solveset_complex((exp(x) - 1)/x, x)
  816. @XFAIL
  817. def test_solve_trig_simplified():
  818. n = Dummy('n')
  819. assert dumeq(solveset_real(sin(x), x),
  820. imageset(Lambda(n, n*pi), S.Integers))
  821. assert dumeq(solveset_real(cos(x), x),
  822. imageset(Lambda(n, n*pi + pi/2), S.Integers))
  823. assert dumeq(solveset_real(cos(x) + sin(x), x),
  824. imageset(Lambda(n, n*pi - pi/4), S.Integers))
  825. @XFAIL
  826. def test_solve_lambert():
  827. assert solveset_real(x*exp(x) - 1, x) == FiniteSet(LambertW(1))
  828. assert solveset_real(exp(x) + x, x) == FiniteSet(-LambertW(1))
  829. assert solveset_real(x + 2**x, x) == \
  830. FiniteSet(-LambertW(log(2))/log(2))
  831. # issue 4739
  832. ans = solveset_real(3*x + 5 + 2**(-5*x + 3), x)
  833. assert ans == FiniteSet(Rational(-5, 3) +
  834. LambertW(-10240*2**Rational(1, 3)*log(2)/3)/(5*log(2)))
  835. eq = 2*(3*x + 4)**5 - 6*7**(3*x + 9)
  836. result = solveset_real(eq, x)
  837. ans = FiniteSet((log(2401) +
  838. 5*LambertW(-log(7**(7*3**Rational(1, 5)/5))))/(3*log(7))/-1)
  839. assert result == ans
  840. assert solveset_real(eq.expand(), x) == result
  841. assert solveset_real(5*x - 1 + 3*exp(2 - 7*x), x) == \
  842. FiniteSet(Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7)
  843. assert solveset_real(2*x + 5 + log(3*x - 2), x) == \
  844. FiniteSet(Rational(2, 3) + LambertW(2*exp(Rational(-19, 3))/3)/2)
  845. assert solveset_real(3*x + log(4*x), x) == \
  846. FiniteSet(LambertW(Rational(3, 4))/3)
  847. assert solveset_real(x**x - 2) == FiniteSet(exp(LambertW(log(2))))
  848. a = Symbol('a')
  849. assert solveset_real(-a*x + 2*x*log(x), x) == FiniteSet(exp(a/2))
  850. a = Symbol('a', real=True)
  851. assert solveset_real(a/x + exp(x/2), x) == \
  852. FiniteSet(2*LambertW(-a/2))
  853. assert solveset_real((a/x + exp(x/2)).diff(x), x) == \
  854. FiniteSet(4*LambertW(sqrt(2)*sqrt(a)/4))
  855. # coverage test
  856. assert solveset_real(tanh(x + 3)*tanh(x - 3) - 1, x) is S.EmptySet
  857. assert solveset_real((x**2 - 2*x + 1).subs(x, log(x) + 3*x), x) == \
  858. FiniteSet(LambertW(3*S.Exp1)/3)
  859. assert solveset_real((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) == \
  860. FiniteSet(LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3)
  861. assert solveset_real((x**2 - 2*x - 2).subs(x, log(x) + 3*x), x) == \
  862. FiniteSet(LambertW(3*exp(1 + sqrt(3)))/3, LambertW(3*exp(-sqrt(3) + 1))/3)
  863. assert solveset_real(x*log(x) + 3*x + 1, x) == \
  864. FiniteSet(exp(-3 + LambertW(-exp(3))))
  865. eq = (x*exp(x) - 3).subs(x, x*exp(x))
  866. assert solveset_real(eq, x) == \
  867. FiniteSet(LambertW(3*exp(-LambertW(3))))
  868. assert solveset_real(3*log(a**(3*x + 5)) + a**(3*x + 5), x) == \
  869. FiniteSet(-((log(a**5) + LambertW(Rational(1, 3)))/(3*log(a))))
  870. p = symbols('p', positive=True)
  871. assert solveset_real(3*log(p**(3*x + 5)) + p**(3*x + 5), x) == \
  872. FiniteSet(
  873. log((-3**Rational(1, 3) - 3**Rational(5, 6)*I)*LambertW(Rational(1, 3))**Rational(1, 3)/(2*p**Rational(5, 3)))/log(p),
  874. log((-3**Rational(1, 3) + 3**Rational(5, 6)*I)*LambertW(Rational(1, 3))**Rational(1, 3)/(2*p**Rational(5, 3)))/log(p),
  875. log((3*LambertW(Rational(1, 3))/p**5)**(1/(3*log(p)))),) # checked numerically
  876. # check collection
  877. b = Symbol('b')
  878. eq = 3*log(a**(3*x + 5)) + b*log(a**(3*x + 5)) + a**(3*x + 5)
  879. assert solveset_real(eq, x) == FiniteSet(
  880. -((log(a**5) + LambertW(1/(b + 3)))/(3*log(a))))
  881. # issue 4271
  882. assert solveset_real((a/x + exp(x/2)).diff(x, 2), x) == FiniteSet(
  883. 6*LambertW((-1)**Rational(1, 3)*a**Rational(1, 3)/3))
  884. assert solveset_real(x**3 - 3**x, x) == \
  885. FiniteSet(-3/log(3)*LambertW(-log(3)/3))
  886. assert solveset_real(3**cos(x) - cos(x)**3) == FiniteSet(
  887. acos(-3*LambertW(-log(3)/3)/log(3)))
  888. assert solveset_real(x**2 - 2**x, x) == \
  889. solveset_real(-x**2 + 2**x, x)
  890. assert solveset_real(3*log(x) - x*log(3)) == FiniteSet(
  891. -3*LambertW(-log(3)/3)/log(3),
  892. -3*LambertW(-log(3)/3, -1)/log(3))
  893. assert solveset_real(LambertW(2*x) - y) == FiniteSet(
  894. y*exp(y)/2)
  895. @XFAIL
  896. def test_other_lambert():
  897. a = Rational(6, 5)
  898. assert solveset_real(x**a - a**x, x) == FiniteSet(
  899. a, -a*LambertW(-log(a)/a)/log(a))
  900. @_both_exp_pow
  901. def test_solveset():
  902. f = Function('f')
  903. raises(ValueError, lambda: solveset(x + y))
  904. assert solveset(x, 1) == S.EmptySet
  905. assert solveset(f(1)**2 + y + 1, f(1)
  906. ) == FiniteSet(-sqrt(-y - 1), sqrt(-y - 1))
  907. assert solveset(f(1)**2 - 1, f(1), S.Reals) == FiniteSet(-1, 1)
  908. assert solveset(f(1)**2 + 1, f(1)) == FiniteSet(-I, I)
  909. assert solveset(x - 1, 1) == FiniteSet(x)
  910. assert solveset(sin(x) - cos(x), sin(x)) == FiniteSet(cos(x))
  911. assert solveset(0, domain=S.Reals) == S.Reals
  912. assert solveset(1) == S.EmptySet
  913. assert solveset(True, domain=S.Reals) == S.Reals # issue 10197
  914. assert solveset(False, domain=S.Reals) == S.EmptySet
  915. assert solveset(exp(x) - 1, domain=S.Reals) == FiniteSet(0)
  916. assert solveset(exp(x) - 1, x, S.Reals) == FiniteSet(0)
  917. assert solveset(Eq(exp(x), 1), x, S.Reals) == FiniteSet(0)
  918. assert solveset(exp(x) - 1, exp(x), S.Reals) == FiniteSet(1)
  919. A = Indexed('A', x)
  920. assert solveset(A - 1, A, S.Reals) == FiniteSet(1)
  921. assert solveset(x - 1 >= 0, x, S.Reals) == Interval(1, oo)
  922. assert solveset(exp(x) - 1 >= 0, x, S.Reals) == Interval(0, oo)
  923. assert dumeq(solveset(exp(x) - 1, x), imageset(Lambda(n, 2*I*pi*n), S.Integers))
  924. assert dumeq(solveset(Eq(exp(x), 1), x), imageset(Lambda(n, 2*I*pi*n),
  925. S.Integers))
  926. # issue 13825
  927. assert solveset(x**2 + f(0) + 1, x) == {-sqrt(-f(0) - 1), sqrt(-f(0) - 1)}
  928. # issue 19977
  929. assert solveset(atan(log(x)) > 0, x, domain=Interval.open(0, oo)) == Interval.open(1, oo)
  930. @_both_exp_pow
  931. def test_multi_exp():
  932. k1, k2, k3 = symbols('k1, k2, k3')
  933. assert dumeq(solveset(exp(exp(x)) - 5, x),\
  934. imageset(Lambda(((k1, n),), I*(2*k1*pi + arg(2*n*I*pi + log(5))) + log(Abs(2*n*I*pi + log(5)))),\
  935. ProductSet(S.Integers, S.Integers)))
  936. assert dumeq(solveset((d*exp(exp(a*x + b)) + c), x),\
  937. imageset(Lambda(x, (-b + x)/a), ImageSet(Lambda(((k1, n),), \
  938. I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))) + log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d))))), \
  939. ProductSet(S.Integers, S.Integers))))
  940. assert dumeq(solveset((d*exp(exp(exp(a*x + b))) + c), x),\
  941. imageset(Lambda(x, (-b + x)/a), ImageSet(Lambda(((k2, k1, n),), \
  942. I*(2*k2*pi + arg(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))) + \
  943. log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))))) + log(Abs(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + \
  944. log(Abs(c/d)))) + log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d))))))), \
  945. ProductSet(S.Integers, S.Integers, S.Integers))))
  946. assert dumeq(solveset((d*exp(exp(exp(exp(a*x + b)))) + c), x),\
  947. ImageSet(Lambda(x, (-b + x)/a), ImageSet(Lambda(((k3, k2, k1, n),), \
  948. I*(2*k3*pi + arg(I*(2*k2*pi + arg(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))) + \
  949. log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))))) + log(Abs(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + \
  950. log(Abs(c/d)))) + log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))))))) + log(Abs(I*(2*k2*pi + \
  951. arg(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))) + log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))))) + \
  952. log(Abs(I*(2*k1*pi + arg(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d)))) + log(Abs(I*(2*n*pi + arg(-c/d)) + log(Abs(c/d))))))))), \
  953. ProductSet(S.Integers, S.Integers, S.Integers, S.Integers))))
  954. def test__solveset_multi():
  955. from sympy.solvers.solveset import _solveset_multi
  956. from sympy.sets import Reals
  957. # Basic univariate case:
  958. assert _solveset_multi([x**2-1], [x], [S.Reals]) == FiniteSet((1,), (-1,))
  959. # Linear systems of two equations
  960. assert _solveset_multi([x+y, x+1], [x, y], [Reals, Reals]) == FiniteSet((-1, 1))
  961. assert _solveset_multi([x+y, x+1], [y, x], [Reals, Reals]) == FiniteSet((1, -1))
  962. assert _solveset_multi([x+y, x-y-1], [x, y], [Reals, Reals]) == FiniteSet((S(1)/2, -S(1)/2))
  963. assert _solveset_multi([x-1, y-2], [x, y], [Reals, Reals]) == FiniteSet((1, 2))
  964. # assert dumeq(_solveset_multi([x+y], [x, y], [Reals, Reals]), ImageSet(Lambda(x, (x, -x)), Reals))
  965. assert dumeq(_solveset_multi([x+y], [x, y], [Reals, Reals]), Union(
  966. ImageSet(Lambda(((x,),), (x, -x)), ProductSet(Reals)),
  967. ImageSet(Lambda(((y,),), (-y, y)), ProductSet(Reals))))
  968. assert _solveset_multi([x+y, x+y+1], [x, y], [Reals, Reals]) == S.EmptySet
  969. assert _solveset_multi([x+y, x-y, x-1], [x, y], [Reals, Reals]) == S.EmptySet
  970. assert _solveset_multi([x+y, x-y, x-1], [y, x], [Reals, Reals]) == S.EmptySet
  971. # Systems of three equations:
  972. assert _solveset_multi([x+y+z-1, x+y-z-2, x-y-z-3], [x, y, z], [Reals,
  973. Reals, Reals]) == FiniteSet((2, -S.Half, -S.Half))
  974. # Nonlinear systems:
  975. from sympy.abc import theta
  976. assert _solveset_multi([x**2+y**2-2, x+y], [x, y], [Reals, Reals]) == FiniteSet((-1, 1), (1, -1))
  977. assert _solveset_multi([x**2-1, y], [x, y], [Reals, Reals]) == FiniteSet((1, 0), (-1, 0))
  978. #assert _solveset_multi([x**2-y**2], [x, y], [Reals, Reals]) == Union(
  979. # ImageSet(Lambda(x, (x, -x)), Reals), ImageSet(Lambda(x, (x, x)), Reals))
  980. assert dumeq(_solveset_multi([x**2-y**2], [x, y], [Reals, Reals]), Union(
  981. ImageSet(Lambda(((x,),), (x, -Abs(x))), ProductSet(Reals)),
  982. ImageSet(Lambda(((x,),), (x, Abs(x))), ProductSet(Reals)),
  983. ImageSet(Lambda(((y,),), (-Abs(y), y)), ProductSet(Reals)),
  984. ImageSet(Lambda(((y,),), (Abs(y), y)), ProductSet(Reals))))
  985. assert _solveset_multi([r*cos(theta)-1, r*sin(theta)], [theta, r],
  986. [Interval(0, pi), Interval(-1, 1)]) == FiniteSet((0, 1), (pi, -1))
  987. assert _solveset_multi([r*cos(theta)-1, r*sin(theta)], [r, theta],
  988. [Interval(0, 1), Interval(0, pi)]) == FiniteSet((1, 0))
  989. #assert _solveset_multi([r*cos(theta)-r, r*sin(theta)], [r, theta],
  990. # [Interval(0, 1), Interval(0, pi)]) == ?
  991. assert dumeq(_solveset_multi([r*cos(theta)-r, r*sin(theta)], [r, theta],
  992. [Interval(0, 1), Interval(0, pi)]), Union(
  993. ImageSet(Lambda(((r,),), (r, 0)), ImageSet(Lambda(r, (r,)), Interval(0, 1))),
  994. ImageSet(Lambda(((theta,),), (0, theta)), ImageSet(Lambda(theta, (theta,)), Interval(0, pi)))))
  995. def test_conditionset():
  996. assert solveset(Eq(sin(x)**2 + cos(x)**2, 1), x, domain=S.Reals
  997. ) is S.Reals
  998. assert solveset(Eq(x**2 + x*sin(x), 1), x, domain=S.Reals
  999. ).dummy_eq(ConditionSet(x, Eq(x**2 + x*sin(x) - 1, 0), S.Reals))
  1000. assert dumeq(solveset(Eq(-I*(exp(I*x) - exp(-I*x))/2, 1), x
  1001. ), imageset(Lambda(n, 2*n*pi + pi/2), S.Integers))
  1002. assert solveset(x + sin(x) > 1, x, domain=S.Reals
  1003. ).dummy_eq(ConditionSet(x, x + sin(x) > 1, S.Reals))
  1004. assert solveset(Eq(sin(Abs(x)), x), x, domain=S.Reals
  1005. ).dummy_eq(ConditionSet(x, Eq(-x + sin(Abs(x)), 0), S.Reals))
  1006. assert solveset(y**x-z, x, S.Reals
  1007. ).dummy_eq(ConditionSet(x, Eq(y**x - z, 0), S.Reals))
  1008. @XFAIL
  1009. def test_conditionset_equality():
  1010. ''' Checking equality of different representations of ConditionSet'''
  1011. assert solveset(Eq(tan(x), y), x) == ConditionSet(x, Eq(tan(x), y), S.Complexes)
  1012. def test_solveset_domain():
  1013. assert solveset(x**2 - x - 6, x, Interval(0, oo)) == FiniteSet(3)
  1014. assert solveset(x**2 - 1, x, Interval(0, oo)) == FiniteSet(1)
  1015. assert solveset(x**4 - 16, x, Interval(0, 10)) == FiniteSet(2)
  1016. def test_improve_coverage():
  1017. solution = solveset(exp(x) + sin(x), x, S.Reals)
  1018. unsolved_object = ConditionSet(x, Eq(exp(x) + sin(x), 0), S.Reals)
  1019. assert solution.dummy_eq(unsolved_object)
  1020. def test_issue_9522():
  1021. expr1 = Eq(1/(x**2 - 4) + x, 1/(x**2 - 4) + 2)
  1022. expr2 = Eq(1/x + x, 1/x)
  1023. assert solveset(expr1, x, S.Reals) is S.EmptySet
  1024. assert solveset(expr2, x, S.Reals) is S.EmptySet
  1025. def test_solvify():
  1026. assert solvify(x**2 + 10, x, S.Reals) == []
  1027. assert solvify(x**3 + 1, x, S.Complexes) == [-1, S.Half - sqrt(3)*I/2,
  1028. S.Half + sqrt(3)*I/2]
  1029. assert solvify(log(x), x, S.Reals) == [1]
  1030. assert solvify(cos(x), x, S.Reals) == [pi/2, pi*Rational(3, 2)]
  1031. assert solvify(sin(x) + 1, x, S.Reals) == [pi*Rational(3, 2)]
  1032. raises(NotImplementedError, lambda: solvify(sin(exp(x)), x, S.Complexes))
  1033. def test_solvify_piecewise():
  1034. p1 = Piecewise((0, x < -1), (x**2, x <= 1), (log(x), True))
  1035. p2 = Piecewise((0, x < -10), (x**2 + 5*x - 6, x >= -9))
  1036. p3 = Piecewise((0, Eq(x, 0)), (x**2/Abs(x), True))
  1037. p4 = Piecewise((0, Eq(x, pi)), ((x - pi)/sin(x), True))
  1038. # issue 21079
  1039. assert solvify(p1, x, S.Reals) == [0]
  1040. assert solvify(p2, x, S.Reals) == [-6, 1]
  1041. assert solvify(p3, x, S.Reals) == [0]
  1042. assert solvify(p4, x, S.Reals) == [pi]
  1043. def test_abs_invert_solvify():
  1044. x = Symbol('x',positive=True)
  1045. assert solvify(sin(Abs(x)), x, S.Reals) == [0, pi]
  1046. x = Symbol('x')
  1047. assert solvify(sin(Abs(x)), x, S.Reals) is None
  1048. def test_linear_eq_to_matrix():
  1049. eqns1 = [2*x + y - 2*z - 3, x - y - z, x + y + 3*z - 12]
  1050. eqns2 = [Eq(3*x + 2*y - z, 1), Eq(2*x - 2*y + 4*z, -2), -2*x + y - 2*z]
  1051. A, B = linear_eq_to_matrix(eqns1, x, y, z)
  1052. assert A == Matrix([[2, 1, -2], [1, -1, -1], [1, 1, 3]])
  1053. assert B == Matrix([[3], [0], [12]])
  1054. A, B = linear_eq_to_matrix(eqns2, x, y, z)
  1055. assert A == Matrix([[3, 2, -1], [2, -2, 4], [-2, 1, -2]])
  1056. assert B == Matrix([[1], [-2], [0]])
  1057. # Pure symbolic coefficients
  1058. eqns3 = [a*b*x + b*y + c*z - d, e*x + d*x + f*y + g*z - h, i*x + j*y + k*z - l]
  1059. A, B = linear_eq_to_matrix(eqns3, x, y, z)
  1060. assert A == Matrix([[a*b, b, c], [d + e, f, g], [i, j, k]])
  1061. assert B == Matrix([[d], [h], [l]])
  1062. # raise ValueError if
  1063. # 1) no symbols are given
  1064. raises(ValueError, lambda: linear_eq_to_matrix(eqns3))
  1065. # 2) there are duplicates
  1066. raises(ValueError, lambda: linear_eq_to_matrix(eqns3, [x, x, y]))
  1067. # 3) there are non-symbols
  1068. raises(ValueError, lambda: linear_eq_to_matrix(eqns3, [x, 1/a, y]))
  1069. # 4) a nonlinear term is detected in the original expression
  1070. raises(NonlinearError, lambda: linear_eq_to_matrix(Eq(1/x + x, 1/x), [x]))
  1071. assert linear_eq_to_matrix(1, x) == (Matrix([[0]]), Matrix([[-1]]))
  1072. # issue 15195
  1073. assert linear_eq_to_matrix(x + y*(z*(3*x + 2) + 3), x) == (
  1074. Matrix([[3*y*z + 1]]), Matrix([[-y*(2*z + 3)]]))
  1075. assert linear_eq_to_matrix(Matrix(
  1076. [[a*x + b*y - 7], [5*x + 6*y - c]]), x, y) == (
  1077. Matrix([[a, b], [5, 6]]), Matrix([[7], [c]]))
  1078. # issue 15312
  1079. assert linear_eq_to_matrix(Eq(x + 2, 1), x) == (
  1080. Matrix([[1]]), Matrix([[-1]]))
  1081. def test_issue_16577():
  1082. assert linear_eq_to_matrix(Eq(a*(2*x + 3*y) + 4*y, 5), x, y) == (
  1083. Matrix([[2*a, 3*a + 4]]), Matrix([[5]]))
  1084. def test_issue_10085():
  1085. assert invert_real(exp(x),0,x) == (x, S.EmptySet)
  1086. def test_linsolve():
  1087. x1, x2, x3, x4 = symbols('x1, x2, x3, x4')
  1088. # Test for different input forms
  1089. M = Matrix([[1, 2, 1, 1, 7], [1, 2, 2, -1, 12], [2, 4, 0, 6, 4]])
  1090. system1 = A, B = M[:, :-1], M[:, -1]
  1091. Eqns = [x1 + 2*x2 + x3 + x4 - 7, x1 + 2*x2 + 2*x3 - x4 - 12,
  1092. 2*x1 + 4*x2 + 6*x4 - 4]
  1093. sol = FiniteSet((-2*x2 - 3*x4 + 2, x2, 2*x4 + 5, x4))
  1094. assert linsolve(Eqns, (x1, x2, x3, x4)) == sol
  1095. assert linsolve(Eqns, *(x1, x2, x3, x4)) == sol
  1096. assert linsolve(system1, (x1, x2, x3, x4)) == sol
  1097. assert linsolve(system1, *(x1, x2, x3, x4)) == sol
  1098. # issue 9667 - symbols can be Dummy symbols
  1099. x1, x2, x3, x4 = symbols('x:4', cls=Dummy)
  1100. assert linsolve(system1, x1, x2, x3, x4) == FiniteSet(
  1101. (-2*x2 - 3*x4 + 2, x2, 2*x4 + 5, x4))
  1102. # raise ValueError for garbage value
  1103. raises(ValueError, lambda: linsolve(Eqns))
  1104. raises(ValueError, lambda: linsolve(x1))
  1105. raises(ValueError, lambda: linsolve(x1, x2))
  1106. raises(ValueError, lambda: linsolve((A,), x1, x2))
  1107. raises(ValueError, lambda: linsolve(A, B, x1, x2))
  1108. #raise ValueError if equations are non-linear in given variables
  1109. raises(NonlinearError, lambda: linsolve([x + y - 1, x ** 2 + y - 3], [x, y]))
  1110. raises(NonlinearError, lambda: linsolve([cos(x) + y, x + y], [x, y]))
  1111. assert linsolve([x + z - 1, x ** 2 + y - 3], [z, y]) == {(-x + 1, -x**2 + 3)}
  1112. # Fully symbolic test
  1113. A = Matrix([[a, b], [c, d]])
  1114. B = Matrix([[e], [g]])
  1115. system2 = (A, B)
  1116. sol = FiniteSet(((-b*g + d*e)/(a*d - b*c), (a*g - c*e)/(a*d - b*c)))
  1117. assert linsolve(system2, [x, y]) == sol
  1118. # No solution
  1119. A = Matrix([[1, 2, 3], [2, 4, 6], [3, 6, 9]])
  1120. B = Matrix([0, 0, 1])
  1121. assert linsolve((A, B), (x, y, z)) is S.EmptySet
  1122. # Issue #10056
  1123. A, B, J1, J2 = symbols('A B J1 J2')
  1124. Augmatrix = Matrix([
  1125. [2*I*J1, 2*I*J2, -2/J1],
  1126. [-2*I*J2, -2*I*J1, 2/J2],
  1127. [0, 2, 2*I/(J1*J2)],
  1128. [2, 0, 0],
  1129. ])
  1130. assert linsolve(Augmatrix, A, B) == FiniteSet((0, I/(J1*J2)))
  1131. # Issue #10121 - Assignment of free variables
  1132. Augmatrix = Matrix([[0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]])
  1133. assert linsolve(Augmatrix, a, b, c, d, e) == FiniteSet((a, 0, c, 0, e))
  1134. #raises(IndexError, lambda: linsolve(Augmatrix, a, b, c))
  1135. x0, x1, x2, _x0 = symbols('tau0 tau1 tau2 _tau0')
  1136. assert linsolve(Matrix([[0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, _x0]])
  1137. ) == FiniteSet((x0, 0, x1, _x0, x2))
  1138. x0, x1, x2, _x0 = symbols('tau00 tau01 tau02 tau0')
  1139. assert linsolve(Matrix([[0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, _x0]])
  1140. ) == FiniteSet((x0, 0, x1, _x0, x2))
  1141. x0, x1, x2, _x0 = symbols('tau00 tau01 tau02 tau1')
  1142. assert linsolve(Matrix([[0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, _x0]])
  1143. ) == FiniteSet((x0, 0, x1, _x0, x2))
  1144. # symbols can be given as generators
  1145. x0, x2, x4 = symbols('x0, x2, x4')
  1146. assert linsolve(Augmatrix, numbered_symbols('x')
  1147. ) == FiniteSet((x0, 0, x2, 0, x4))
  1148. Augmatrix[-1, -1] = x0
  1149. # use Dummy to avoid clash; the names may clash but the symbols
  1150. # will not
  1151. Augmatrix[-1, -1] = symbols('_x0')
  1152. assert len(linsolve(
  1153. Augmatrix, numbered_symbols('x', cls=Dummy)).free_symbols) == 4
  1154. # Issue #12604
  1155. f = Function('f')
  1156. assert linsolve([f(x) - 5], f(x)) == FiniteSet((5,))
  1157. # Issue #14860
  1158. from sympy.physics.units import meter, newton, kilo
  1159. kN = kilo*newton
  1160. Eqns = [8*kN + x + y, 28*kN*meter + 3*x*meter]
  1161. assert linsolve(Eqns, x, y) == {
  1162. (kilo*newton*Rational(-28, 3), kN*Rational(4, 3))}
  1163. # linsolve fully expands expressions, so removable singularities
  1164. # and other nonlinearity does not raise an error
  1165. assert linsolve([Eq(x, x + y)], [x, y]) == {(x, 0)}
  1166. assert linsolve([Eq(1/x, 1/x + y)], [x, y]) == {(x, 0)}
  1167. assert linsolve([Eq(y/x, y/x + y)], [x, y]) == {(x, 0)}
  1168. assert linsolve([Eq(x*(x + 1), x**2 + y)], [x, y]) == {(y, y)}
  1169. # corner cases
  1170. #
  1171. # XXX: The case below should give the same as for [0]
  1172. # assert linsolve([], [x]) == {(x,)}
  1173. assert linsolve([], [x]) is S.EmptySet
  1174. assert linsolve([0], [x]) == {(x,)}
  1175. assert linsolve([x], [x, y]) == {(0, y)}
  1176. assert linsolve([x, 0], [x, y]) == {(0, y)}
  1177. def test_linsolve_large_sparse():
  1178. #
  1179. # This is mainly a performance test
  1180. #
  1181. def _mk_eqs_sol(n):
  1182. xs = symbols('x:{}'.format(n))
  1183. ys = symbols('y:{}'.format(n))
  1184. syms = xs + ys
  1185. eqs = []
  1186. sol = (-S.Half,) * n + (S.Half,) * n
  1187. for xi, yi in zip(xs, ys):
  1188. eqs.extend([xi + yi, xi - yi + 1])
  1189. return eqs, syms, FiniteSet(sol)
  1190. n = 500
  1191. eqs, syms, sol = _mk_eqs_sol(n)
  1192. assert linsolve(eqs, syms) == sol
  1193. def test_linsolve_immutable():
  1194. A = ImmutableDenseMatrix([[1, 1, 2], [0, 1, 2], [0, 0, 1]])
  1195. B = ImmutableDenseMatrix([2, 1, -1])
  1196. assert linsolve([A, B], (x, y, z)) == FiniteSet((1, 3, -1))
  1197. A = ImmutableDenseMatrix([[1, 1, 7], [1, -1, 3]])
  1198. assert linsolve(A) == FiniteSet((5, 2))
  1199. def test_solve_decomposition():
  1200. n = Dummy('n')
  1201. f1 = exp(3*x) - 6*exp(2*x) + 11*exp(x) - 6
  1202. f2 = sin(x)**2 - 2*sin(x) + 1
  1203. f3 = sin(x)**2 - sin(x)
  1204. f4 = sin(x + 1)
  1205. f5 = exp(x + 2) - 1
  1206. f6 = 1/log(x)
  1207. f7 = 1/x
  1208. s1 = ImageSet(Lambda(n, 2*n*pi), S.Integers)
  1209. s2 = ImageSet(Lambda(n, 2*n*pi + pi), S.Integers)
  1210. s3 = ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers)
  1211. s4 = ImageSet(Lambda(n, 2*n*pi - 1), S.Integers)
  1212. s5 = ImageSet(Lambda(n, 2*n*pi - 1 + pi), S.Integers)
  1213. assert solve_decomposition(f1, x, S.Reals) == FiniteSet(0, log(2), log(3))
  1214. assert dumeq(solve_decomposition(f2, x, S.Reals), s3)
  1215. assert dumeq(solve_decomposition(f3, x, S.Reals), Union(s1, s2, s3))
  1216. assert dumeq(solve_decomposition(f4, x, S.Reals), Union(s4, s5))
  1217. assert solve_decomposition(f5, x, S.Reals) == FiniteSet(-2)
  1218. assert solve_decomposition(f6, x, S.Reals) == S.EmptySet
  1219. assert solve_decomposition(f7, x, S.Reals) == S.EmptySet
  1220. assert solve_decomposition(x, x, Interval(1, 2)) == S.EmptySet
  1221. # nonlinsolve testcases
  1222. def test_nonlinsolve_basic():
  1223. assert nonlinsolve([],[]) == S.EmptySet
  1224. assert nonlinsolve([],[x, y]) == S.EmptySet
  1225. system = [x, y - x - 5]
  1226. assert nonlinsolve([x],[x, y]) == FiniteSet((0, y))
  1227. assert nonlinsolve(system, [y]) == FiniteSet((x + 5,))
  1228. soln = (ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers),)
  1229. assert dumeq(nonlinsolve([sin(x) - 1], [x]), FiniteSet(tuple(soln)))
  1230. assert nonlinsolve([x**2 - 1], [x]) == FiniteSet((-1,), (1,))
  1231. soln = FiniteSet((y, y))
  1232. assert nonlinsolve([x - y, 0], x, y) == soln
  1233. assert nonlinsolve([0, x - y], x, y) == soln
  1234. assert nonlinsolve([x - y, x - y], x, y) == soln
  1235. assert nonlinsolve([x, 0], x, y) == FiniteSet((0, y))
  1236. f = Function('f')
  1237. assert nonlinsolve([f(x), 0], f(x), y) == FiniteSet((0, y))
  1238. assert nonlinsolve([f(x), 0], f(x), f(y)) == FiniteSet((0, f(y)))
  1239. A = Indexed('A', x)
  1240. assert nonlinsolve([A, 0], A, y) == FiniteSet((0, y))
  1241. assert nonlinsolve([x**2 -1], [sin(x)]) == FiniteSet((S.EmptySet,))
  1242. assert nonlinsolve([x**2 -1], sin(x)) == FiniteSet((S.EmptySet,))
  1243. assert nonlinsolve([x**2 -1], 1) == FiniteSet((x**2,))
  1244. assert nonlinsolve([x**2 -1], x + y) == FiniteSet((S.EmptySet,))
  1245. assert nonlinsolve([Eq(1, x + y), Eq(1, -x + y - 1), Eq(1, -x + y - 1)], x, y) == FiniteSet(
  1246. (-S.Half, 3*S.Half))
  1247. def test_nonlinsolve_abs():
  1248. soln = FiniteSet((y, y), (-y, y))
  1249. assert nonlinsolve([Abs(x) - y], x, y) == soln
  1250. def test_raise_exception_nonlinsolve():
  1251. raises(IndexError, lambda: nonlinsolve([x**2 -1], []))
  1252. raises(ValueError, lambda: nonlinsolve([x**2 -1]))
  1253. raises(NotImplementedError, lambda: nonlinsolve([(x+y)**2 - 9, x**2 - y**2 - 0.75], (x, y)))
  1254. def test_trig_system():
  1255. # TODO: add more simple testcases when solveset returns
  1256. # simplified soln for Trig eq
  1257. assert nonlinsolve([sin(x) - 1, cos(x) -1 ], x) == S.EmptySet
  1258. soln1 = (ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers),)
  1259. soln = FiniteSet(soln1)
  1260. assert dumeq(nonlinsolve([sin(x) - 1, cos(x)], x), soln)
  1261. @XFAIL
  1262. def test_trig_system_fail():
  1263. # fails because solveset trig solver is not much smart.
  1264. sys = [x + y - pi/2, sin(x) + sin(y) - 1]
  1265. # solveset returns conditionset for sin(x) + sin(y) - 1
  1266. soln_1 = (ImageSet(Lambda(n, n*pi + pi/2), S.Integers),
  1267. ImageSet(Lambda(n, n*pi), S.Integers))
  1268. soln_1 = FiniteSet(soln_1)
  1269. soln_2 = (ImageSet(Lambda(n, n*pi), S.Integers),
  1270. ImageSet(Lambda(n, n*pi+ pi/2), S.Integers))
  1271. soln_2 = FiniteSet(soln_2)
  1272. soln = soln_1 + soln_2
  1273. assert dumeq(nonlinsolve(sys, [x, y]), soln)
  1274. # Add more cases from here
  1275. # http://www.vitutor.com/geometry/trigonometry/equations_systems.html#uno
  1276. sys = [sin(x) + sin(y) - (sqrt(3)+1)/2, sin(x) - sin(y) - (sqrt(3) - 1)/2]
  1277. soln_x = Union(ImageSet(Lambda(n, 2*n*pi + pi/3), S.Integers),
  1278. ImageSet(Lambda(n, 2*n*pi + pi*Rational(2, 3)), S.Integers))
  1279. soln_y = Union(ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers),
  1280. ImageSet(Lambda(n, 2*n*pi + pi*Rational(5, 6)), S.Integers))
  1281. assert dumeq(nonlinsolve(sys, [x, y]), FiniteSet((soln_x, soln_y)))
  1282. def test_nonlinsolve_positive_dimensional():
  1283. x, y, a, b, c, d = symbols('x, y, a, b, c, d', extended_real=True)
  1284. assert nonlinsolve([x*y, x*y - x], [x, y]) == FiniteSet((0, y))
  1285. system = [a**2 + a*c, a - b]
  1286. assert nonlinsolve(system, [a, b]) == FiniteSet((0, 0), (-c, -c))
  1287. # here (a= 0, b = 0) is independent soln so both is printed.
  1288. # if symbols = [a, b, c] then only {a : -c ,b : -c}
  1289. eq1 = a + b + c + d
  1290. eq2 = a*b + b*c + c*d + d*a
  1291. eq3 = a*b*c + b*c*d + c*d*a + d*a*b
  1292. eq4 = a*b*c*d - 1
  1293. system = [eq1, eq2, eq3, eq4]
  1294. sol1 = (-1/d, -d, 1/d, FiniteSet(d) - FiniteSet(0))
  1295. sol2 = (1/d, -d, -1/d, FiniteSet(d) - FiniteSet(0))
  1296. soln = FiniteSet(sol1, sol2)
  1297. assert nonlinsolve(system, [a, b, c, d]) == soln
  1298. def test_nonlinsolve_polysys():
  1299. x, y = symbols('x, y', real=True)
  1300. assert nonlinsolve([x**2 + y - 2, x**2 + y], [x, y]) == S.EmptySet
  1301. s = (-y + 2, y)
  1302. assert nonlinsolve([(x + y)**2 - 4, x + y - 2], [x, y]) == FiniteSet(s)
  1303. system = [x**2 - y**2]
  1304. soln_real = FiniteSet((-y, y), (y, y))
  1305. soln_complex = FiniteSet((-Abs(y), y), (Abs(y), y))
  1306. soln =soln_real + soln_complex
  1307. assert nonlinsolve(system, [x, y]) == soln
  1308. system = [x**2 - y**2]
  1309. soln_real= FiniteSet((y, -y), (y, y))
  1310. soln_complex = FiniteSet((y, -Abs(y)), (y, Abs(y)))
  1311. soln = soln_real + soln_complex
  1312. assert nonlinsolve(system, [y, x]) == soln
  1313. system = [x**2 + y - 3, x - y - 4]
  1314. assert nonlinsolve(system, (x, y)) != nonlinsolve(system, (y, x))
  1315. def test_nonlinsolve_using_substitution():
  1316. x, y, z, n = symbols('x, y, z, n', real = True)
  1317. system = [(x + y)*n - y**2 + 2]
  1318. s_x = (n*y - y**2 + 2)/n
  1319. soln = (-s_x, y)
  1320. assert nonlinsolve(system, [x, y]) == FiniteSet(soln)
  1321. system = [z**2*x**2 - z**2*y**2/exp(x)]
  1322. soln_real_1 = (y, x, 0)
  1323. soln_real_2 = (-exp(x/2)*Abs(x), x, z)
  1324. soln_real_3 = (exp(x/2)*Abs(x), x, z)
  1325. soln_complex_1 = (-x*exp(x/2), x, z)
  1326. soln_complex_2 = (x*exp(x/2), x, z)
  1327. syms = [y, x, z]
  1328. soln = FiniteSet(soln_real_1, soln_complex_1, soln_complex_2,\
  1329. soln_real_2, soln_real_3)
  1330. assert nonlinsolve(system,syms) == soln
  1331. def test_nonlinsolve_complex():
  1332. n = Dummy('n')
  1333. assert dumeq(nonlinsolve([exp(x) - sin(y), 1/y - 3], [x, y]), {
  1334. (ImageSet(Lambda(n, 2*n*I*pi + log(sin(Rational(1, 3)))), S.Integers), Rational(1, 3))})
  1335. system = [exp(x) - sin(y), 1/exp(y) - 3]
  1336. assert dumeq(nonlinsolve(system, [x, y]), {
  1337. (ImageSet(Lambda(n, I*(2*n*pi + pi)
  1338. + log(sin(log(3)))), S.Integers), -log(3)),
  1339. (ImageSet(Lambda(n, I*(2*n*pi + arg(sin(2*n*I*pi - log(3))))
  1340. + log(Abs(sin(2*n*I*pi - log(3))))), S.Integers),
  1341. ImageSet(Lambda(n, 2*n*I*pi - log(3)), S.Integers))})
  1342. system = [exp(x) - sin(y), y**2 - 4]
  1343. assert dumeq(nonlinsolve(system, [x, y]), {
  1344. (ImageSet(Lambda(n, I*(2*n*pi + pi) + log(sin(2))), S.Integers), -2),
  1345. (ImageSet(Lambda(n, 2*n*I*pi + log(sin(2))), S.Integers), 2)})
  1346. @XFAIL
  1347. def test_solve_nonlinear_trans():
  1348. # After the transcendental equation solver these will work
  1349. x, y = symbols('x, y', real=True)
  1350. soln1 = FiniteSet((2*LambertW(y/2), y))
  1351. soln2 = FiniteSet((-x*sqrt(exp(x)), y), (x*sqrt(exp(x)), y))
  1352. soln3 = FiniteSet((x*exp(x/2), x))
  1353. soln4 = FiniteSet(2*LambertW(y/2), y)
  1354. assert nonlinsolve([x**2 - y**2/exp(x)], [x, y]) == soln1
  1355. assert nonlinsolve([x**2 - y**2/exp(x)], [y, x]) == soln2
  1356. assert nonlinsolve([x**2 - y**2/exp(x)], [y, x]) == soln3
  1357. assert nonlinsolve([x**2 - y**2/exp(x)], [x, y]) == soln4
  1358. def test_issue_14642():
  1359. x = Symbol('x')
  1360. n1 = 0.5*x**3+x**2+0.5+I #add I in the Polynomials
  1361. solution = solveset(n1, x)
  1362. assert abs(solution.args[0] - (-2.28267560928153 - 0.312325580497716*I)) <= 1e-9
  1363. assert abs(solution.args[1] - (-0.297354141679308 + 1.01904778618762*I)) <= 1e-9
  1364. assert abs(solution.args[2] - (0.580029750960839 - 0.706722205689907*I)) <= 1e-9
  1365. # Symbolic
  1366. n1 = S.Half*x**3+x**2+S.Half+I
  1367. res = FiniteSet(-((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1368. S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)
  1369. /2)/2)**2)**(S(1)/6)*cos(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*
  1370. cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(
  1371. S(172)/49)/2)/2 + S(43)/2))/3)/3 - S(2)/3 - 4*cos(atan((27 +
  1372. 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*
  1373. 31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 + S(43)/2))/3)/(3*((3*
  1374. sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 + S(43)/2)**2 +
  1375. (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)/2)**2)**(S(1)/
  1376. 6)) + I*(-((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1377. S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/
  1378. 2)/2)**2)**(S(1)/6)*sin(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*cos(
  1379. atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)
  1380. /2)/2 + S(43)/2))/3)/3 + 4*sin(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*
  1381. cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)
  1382. /49)/2)/2 + S(43)/2))/3)/(3*((3*sqrt(3)*31985**(S(1)/4)*sin(atan(
  1383. S(172)/49)/2)/2 + S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*
  1384. cos(atan(S(172)/49)/2)/2)**2)**(S(1)/6))), -S(2)/3 - sqrt(3)*((3*
  1385. sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 + S(43)/2)**2 +
  1386. (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)/2)**2)**(S(1)
  1387. /6)*sin(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)
  1388. /2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 + S(43)/2))
  1389. /3)/6 - 4*re(1/((-S(1)/2 - sqrt(3)*I/2)*(S(43)/2 + 27*I + sqrt(-256 +
  1390. (43 + 54*I)**2)/2)**(S(1)/3)))/3 + ((3*sqrt(3)*31985**(S(1)/4)*sin(
  1391. atan(S(172)/49)/2)/2 + S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*
  1392. cos(atan(S(172)/49)/2)/2)**2)**(S(1)/6)*cos(atan((27 + 3*sqrt(3)*
  1393. 31985**(S(1)/4)*cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*
  1394. sin(atan(S(172)/49)/2)/2 + S(43)/2))/3)/6 + I*(-4*im(1/((-S(1)/2 -
  1395. sqrt(3)*I/2)*(S(43)/2 + 27*I + sqrt(-256 + (43 + 54*I)**2)/2)**(S(1)/
  1396. 3)))/3 + ((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1397. S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)
  1398. /2)**2)**(S(1)/6)*sin(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(
  1399. S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1400. S(43)/2))/3)/6 + sqrt(3)*((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/
  1401. 49)/2)/2 + S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(
  1402. S(172)/49)/2)/2)**2)**(S(1)/6)*cos(atan((27 + 3*sqrt(3)*31985**(S(1)/
  1403. 4)*cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(
  1404. S(172)/49)/2)/2 + S(43)/2))/3)/6), -S(2)/3 - 4*re(1/((-S(1)/2 +
  1405. sqrt(3)*I/2)*(S(43)/2 + 27*I + sqrt(-256 + (43 + 54*I)**2)/2)**(S(1)
  1406. /3)))/3 + sqrt(3)*((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1407. S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)
  1408. /2)**2)**(S(1)/6)*sin(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(
  1409. S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1410. S(43)/2))/3)/6 + ((3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1411. S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(S(172)/49)/2)
  1412. /2)**2)**(S(1)/6)*cos(atan((27 + 3*sqrt(3)*31985**(S(1)/4)*cos(atan(
  1413. S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(atan(S(172)/49)/2)/2 +
  1414. S(43)/2))/3)/6 + I*(-sqrt(3)*((3*sqrt(3)*31985**(S(1)/4)*sin(atan(
  1415. S(172)/49)/2)/2 + S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*cos(
  1416. atan(S(172)/49)/2)/2)**2)**(S(1)/6)*cos(atan((27 + 3*sqrt(3)*31985**(
  1417. S(1)/4)*cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(
  1418. atan(S(172)/49)/2)/2 + S(43)/2))/3)/6 + ((3*sqrt(3)*31985**(S(1)/4)*
  1419. sin(atan(S(172)/49)/2)/2 + S(43)/2)**2 + (27 + 3*sqrt(3)*31985**(S(1)/4)*
  1420. cos(atan(S(172)/49)/2)/2)**2)**(S(1)/6)*sin(atan((27 + 3*sqrt(3)*31985**(
  1421. S(1)/4)*cos(atan(S(172)/49)/2)/2)/(3*sqrt(3)*31985**(S(1)/4)*sin(
  1422. atan(S(172)/49)/2)/2 + S(43)/2))/3)/6 - 4*im(1/((-S(1)/2 + sqrt(3)*I/2)*
  1423. (S(43)/2 + 27*I + sqrt(-256 + (43 + 54*I)**2)/2)**(S(1)/3)))/3))
  1424. assert solveset(n1, x) == res
  1425. def test_issue_13961():
  1426. V = (ax, bx, cx, gx, jx, lx, mx, nx, q) = symbols('ax bx cx gx jx lx mx nx q')
  1427. S = (ax*q - lx*q - mx, ax - gx*q - lx, bx*q**2 + cx*q - jx*q - nx, q*(-ax*q + lx*q + mx), q*(-ax + gx*q + lx))
  1428. sol = FiniteSet((lx + mx/q, (-cx*q + jx*q + nx)/q**2, cx, mx/q**2, jx, lx, mx, nx, q),
  1429. (lx + mx/q, (cx*q - jx*q - nx)/q**2*-1, cx, mx/q**2, jx, lx, mx, nx, q))
  1430. assert nonlinsolve(S, *V) == sol
  1431. # The two solutions are in fact identical, so even better if only one is returned
  1432. def test_issue_14541():
  1433. solutions = solveset(sqrt(-x**2 - 2.0), x)
  1434. assert abs(solutions.args[0]+1.4142135623731*I) <= 1e-9
  1435. assert abs(solutions.args[1]-1.4142135623731*I) <= 1e-9
  1436. def test_issue_13396():
  1437. expr = -2*y*exp(-x**2 - y**2)*Abs(x)
  1438. sol = FiniteSet(0)
  1439. assert solveset(expr, y, domain=S.Reals) == sol
  1440. # Related type of equation also solved here
  1441. assert solveset(atan(x**2 - y**2)-pi/2, y, S.Reals) is S.EmptySet
  1442. def test_issue_12032():
  1443. sol = FiniteSet(-sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1444. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))/2 +
  1445. sqrt(Abs(-2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)) +
  1446. 2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1447. 2/sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1448. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))))/2,
  1449. -sqrt(Abs(-2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)) +
  1450. 2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1451. 2/sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1452. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))))/2 -
  1453. sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1454. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))/2,
  1455. sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1456. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))/2 -
  1457. I*sqrt(Abs(-2/sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1458. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) -
  1459. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)) +
  1460. 2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))))/2,
  1461. sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1462. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)))/2 +
  1463. I*sqrt(Abs(-2/sqrt(-2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) +
  1464. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3))) -
  1465. 2*(Rational(1, 16) + sqrt(849)/144)**(Rational(1, 3)) +
  1466. 2/(3*(Rational(1, 16) + sqrt(849)/144)**(Rational(1,3)))))/2)
  1467. assert solveset(x**4 + x - 1, x) == sol
  1468. def test_issue_10876():
  1469. assert solveset(1/sqrt(x), x) == S.EmptySet
  1470. def test_issue_19050():
  1471. # test_issue_19050 --> TypeError removed
  1472. assert dumeq(nonlinsolve([x + y, sin(y)], [x, y]),
  1473. FiniteSet((ImageSet(Lambda(n, -2*n*pi), S.Integers), ImageSet(Lambda(n, 2*n*pi), S.Integers)),\
  1474. (ImageSet(Lambda(n, -2*n*pi - pi), S.Integers), ImageSet(Lambda(n, 2*n*pi + pi), S.Integers))))
  1475. assert dumeq(nonlinsolve([x + y, sin(y) + cos(y)], [x, y]),
  1476. FiniteSet((ImageSet(Lambda(n, -2*n*pi - 3*pi/4), S.Integers), ImageSet(Lambda(n, 2*n*pi + 3*pi/4), S.Integers)), \
  1477. (ImageSet(Lambda(n, -2*n*pi - 7*pi/4), S.Integers), ImageSet(Lambda(n, 2*n*pi + 7*pi/4), S.Integers))))
  1478. def test_issue_16618():
  1479. # AttributeError is removed !
  1480. eqn = [sin(x)*sin(y), cos(x)*cos(y) - 1]
  1481. ans = FiniteSet((x, 2*n*pi), (2*n*pi, y), (x, 2*n*pi + pi), (2*n*pi + pi, y))
  1482. sol = nonlinsolve(eqn, [x, y])
  1483. for i0, j0 in zip(ordered(sol), ordered(ans)):
  1484. assert len(i0) == len(j0) == 2
  1485. assert all(a.dummy_eq(b) for a, b in zip(i0, j0))
  1486. assert len(sol) == len(ans)
  1487. def test_issue_17566():
  1488. assert nonlinsolve([32*(2**x)/2**(-y) - 4**y, 27*(3**x) - 1/3**y], x, y) ==\
  1489. FiniteSet((-log(81)/log(3), 1))
  1490. def test_issue_16643():
  1491. n = Dummy('n')
  1492. assert solveset(x**2*sin(x), x).dummy_eq(Union(ImageSet(Lambda(n, 2*n*pi + pi), S.Integers),
  1493. ImageSet(Lambda(n, 2*n*pi), S.Integers)))
  1494. def test_issue_19587():
  1495. n,m = symbols('n m')
  1496. assert nonlinsolve([32*2**m*2**n - 4**n, 27*3**m - 3**(-n)], m, n) ==\
  1497. FiniteSet((-log(81)/log(3), 1))
  1498. def test_issue_5132_1():
  1499. system = [sqrt(x**2 + y**2) - sqrt(10), x + y - 4]
  1500. assert nonlinsolve(system, [x, y]) == FiniteSet((1, 3), (3, 1))
  1501. n = Dummy('n')
  1502. eqs = [exp(x)**2 - sin(y) + z**2, 1/exp(y) - 3]
  1503. s_real_y = -log(3)
  1504. s_real_z = sqrt(-exp(2*x) - sin(log(3)))
  1505. soln_real = FiniteSet((s_real_y, s_real_z), (s_real_y, -s_real_z))
  1506. lam = Lambda(n, 2*n*I*pi + -log(3))
  1507. s_complex_y = ImageSet(lam, S.Integers)
  1508. lam = Lambda(n, sqrt(-exp(2*x) + sin(2*n*I*pi + -log(3))))
  1509. s_complex_z_1 = ImageSet(lam, S.Integers)
  1510. lam = Lambda(n, -sqrt(-exp(2*x) + sin(2*n*I*pi + -log(3))))
  1511. s_complex_z_2 = ImageSet(lam, S.Integers)
  1512. soln_complex = FiniteSet(
  1513. (s_complex_y, s_complex_z_1),
  1514. (s_complex_y, s_complex_z_2)
  1515. )
  1516. soln = soln_real + soln_complex
  1517. assert dumeq(nonlinsolve(eqs, [y, z]), soln)
  1518. def test_issue_5132_2():
  1519. x, y = symbols('x, y', real=True)
  1520. eqs = [exp(x)**2 - sin(y) + z**2, 1/exp(y) - 3]
  1521. n = Dummy('n')
  1522. soln_real = (log(-z**2 + sin(y))/2, z)
  1523. lam = Lambda( n, I*(2*n*pi + arg(-z**2 + sin(y)))/2 + log(Abs(z**2 - sin(y)))/2)
  1524. img = ImageSet(lam, S.Integers)
  1525. # not sure about the complex soln. But it looks correct.
  1526. soln_complex = (img, z)
  1527. soln = FiniteSet(soln_real, soln_complex)
  1528. assert dumeq(nonlinsolve(eqs, [x, z]), soln)
  1529. system = [r - x**2 - y**2, tan(t) - y/x]
  1530. s_x = sqrt(r/(tan(t)**2 + 1))
  1531. s_y = sqrt(r/(tan(t)**2 + 1))*tan(t)
  1532. soln = FiniteSet((s_x, s_y), (-s_x, -s_y))
  1533. assert nonlinsolve(system, [x, y]) == soln
  1534. def test_issue_6752():
  1535. a, b = symbols('a, b', real=True)
  1536. assert nonlinsolve([a**2 + a, a - b], [a, b]) == {(-1, -1), (0, 0)}
  1537. @SKIP("slow")
  1538. def test_issue_5114_solveset():
  1539. # slow testcase
  1540. from sympy.abc import o, p
  1541. # there is no 'a' in the equation set but this is how the
  1542. # problem was originally posed
  1543. syms = [a, b, c, f, h, k, n]
  1544. eqs = [b + r/d - c/d,
  1545. c*(1/d + 1/e + 1/g) - f/g - r/d,
  1546. f*(1/g + 1/i + 1/j) - c/g - h/i,
  1547. h*(1/i + 1/l + 1/m) - f/i - k/m,
  1548. k*(1/m + 1/o + 1/p) - h/m - n/p,
  1549. n*(1/p + 1/q) - k/p]
  1550. assert len(nonlinsolve(eqs, syms)) == 1
  1551. @SKIP("Hangs")
  1552. def _test_issue_5335():
  1553. # Not able to check zero dimensional system.
  1554. # is_zero_dimensional Hangs
  1555. lam, a0, conc = symbols('lam a0 conc')
  1556. eqs = [lam + 2*y - a0*(1 - x/2)*x - 0.005*x/2*x,
  1557. a0*(1 - x/2)*x - 1*y - 0.743436700916726*y,
  1558. x + y - conc]
  1559. sym = [x, y, a0]
  1560. # there are 4 solutions but only two are valid
  1561. assert len(nonlinsolve(eqs, sym)) == 2
  1562. # float
  1563. eqs = [lam + 2*y - a0*(1 - x/2)*x - 0.005*x/2*x,
  1564. a0*(1 - x/2)*x - 1*y - 0.743436700916726*y,
  1565. x + y - conc]
  1566. sym = [x, y, a0]
  1567. assert len(nonlinsolve(eqs, sym)) == 2
  1568. def test_issue_2777():
  1569. # the equations represent two circles
  1570. x, y = symbols('x y', real=True)
  1571. e1, e2 = sqrt(x**2 + y**2) - 10, sqrt(y**2 + (-x + 10)**2) - 3
  1572. a, b = Rational(191, 20), 3*sqrt(391)/20
  1573. ans = {(a, -b), (a, b)}
  1574. assert nonlinsolve((e1, e2), (x, y)) == ans
  1575. assert nonlinsolve((e1, e2/(x - a)), (x, y)) == S.EmptySet
  1576. # make the 2nd circle's radius be -3
  1577. e2 += 6
  1578. assert nonlinsolve((e1, e2), (x, y)) == S.EmptySet
  1579. def test_issue_8828():
  1580. x1 = 0
  1581. y1 = -620
  1582. r1 = 920
  1583. x2 = 126
  1584. y2 = 276
  1585. x3 = 51
  1586. y3 = 205
  1587. r3 = 104
  1588. v = [x, y, z]
  1589. f1 = (x - x1)**2 + (y - y1)**2 - (r1 - z)**2
  1590. f2 = (x2 - x)**2 + (y2 - y)**2 - z**2
  1591. f3 = (x - x3)**2 + (y - y3)**2 - (r3 - z)**2
  1592. F = [f1, f2, f3]
  1593. g1 = sqrt((x - x1)**2 + (y - y1)**2) + z - r1
  1594. g2 = f2
  1595. g3 = sqrt((x - x3)**2 + (y - y3)**2) + z - r3
  1596. G = [g1, g2, g3]
  1597. # both soln same
  1598. A = nonlinsolve(F, v)
  1599. B = nonlinsolve(G, v)
  1600. assert A == B
  1601. def test_nonlinsolve_conditionset():
  1602. # when solveset failed to solve all the eq
  1603. # return conditionset
  1604. f = Function('f')
  1605. f1 = f(x) - pi/2
  1606. f2 = f(y) - pi*Rational(3, 2)
  1607. intermediate_system = Eq(2*f(x) - pi, 0) & Eq(2*f(y) - 3*pi, 0)
  1608. syms = Tuple(x, y)
  1609. soln = ConditionSet(
  1610. syms,
  1611. intermediate_system,
  1612. S.Complexes**2)
  1613. assert nonlinsolve([f1, f2], [x, y]) == soln
  1614. def test_substitution_basic():
  1615. assert substitution([], [x, y]) == S.EmptySet
  1616. assert substitution([], []) == S.EmptySet
  1617. system = [2*x**2 + 3*y**2 - 30, 3*x**2 - 2*y**2 - 19]
  1618. soln = FiniteSet((-3, -2), (-3, 2), (3, -2), (3, 2))
  1619. assert substitution(system, [x, y]) == soln
  1620. soln = FiniteSet((-1, 1))
  1621. assert substitution([x + y], [x], [{y: 1}], [y], set(), [x, y]) == soln
  1622. assert substitution(
  1623. [x + y], [x], [{y: 1}], [y],
  1624. {x + 1}, [y, x]) == S.EmptySet
  1625. def test_issue_5132_substitution():
  1626. x, y, z, r, t = symbols('x, y, z, r, t', real=True)
  1627. system = [r - x**2 - y**2, tan(t) - y/x]
  1628. s_x_1 = Complement(FiniteSet(-sqrt(r/(tan(t)**2 + 1))), FiniteSet(0))
  1629. s_x_2 = Complement(FiniteSet(sqrt(r/(tan(t)**2 + 1))), FiniteSet(0))
  1630. s_y = sqrt(r/(tan(t)**2 + 1))*tan(t)
  1631. soln = FiniteSet((s_x_2, s_y)) + FiniteSet((s_x_1, -s_y))
  1632. assert substitution(system, [x, y]) == soln
  1633. n = Dummy('n')
  1634. eqs = [exp(x)**2 - sin(y) + z**2, 1/exp(y) - 3]
  1635. s_real_y = -log(3)
  1636. s_real_z = sqrt(-exp(2*x) - sin(log(3)))
  1637. soln_real = FiniteSet((s_real_y, s_real_z), (s_real_y, -s_real_z))
  1638. lam = Lambda(n, 2*n*I*pi + -log(3))
  1639. s_complex_y = ImageSet(lam, S.Integers)
  1640. lam = Lambda(n, sqrt(-exp(2*x) + sin(2*n*I*pi + -log(3))))
  1641. s_complex_z_1 = ImageSet(lam, S.Integers)
  1642. lam = Lambda(n, -sqrt(-exp(2*x) + sin(2*n*I*pi + -log(3))))
  1643. s_complex_z_2 = ImageSet(lam, S.Integers)
  1644. soln_complex = FiniteSet(
  1645. (s_complex_y, s_complex_z_1),
  1646. (s_complex_y, s_complex_z_2))
  1647. soln = soln_real + soln_complex
  1648. assert dumeq(substitution(eqs, [y, z]), soln)
  1649. def test_raises_substitution():
  1650. raises(ValueError, lambda: substitution([x**2 -1], []))
  1651. raises(TypeError, lambda: substitution([x**2 -1]))
  1652. raises(ValueError, lambda: substitution([x**2 -1], [sin(x)]))
  1653. raises(TypeError, lambda: substitution([x**2 -1], x))
  1654. raises(TypeError, lambda: substitution([x**2 -1], 1))
  1655. def test_issue_21022():
  1656. from sympy.core.sympify import sympify
  1657. eqs = [
  1658. 'k-16',
  1659. 'p-8',
  1660. 'y*y+z*z-x*x',
  1661. 'd - x + p',
  1662. 'd*d+k*k-y*y',
  1663. 'z*z-p*p-k*k',
  1664. 'abc-efg',
  1665. ]
  1666. efg = Symbol('efg')
  1667. eqs = [sympify(x) for x in eqs]
  1668. syb = list(ordered(set.union(*[x.free_symbols for x in eqs])))
  1669. res = nonlinsolve(eqs, syb)
  1670. ans = FiniteSet(
  1671. (efg, sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1672. efg, 16, 8, 8 + sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1673. sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16, -8*sqrt(5)),
  1674. (efg, sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1675. efg, 16, 8, 8 + sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1676. sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16, 8*sqrt(5)),
  1677. (efg, -sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1678. efg, 16, 8, -sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16) + 8,
  1679. sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16, -8*sqrt(5)),
  1680. (efg, -sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16),
  1681. efg, 16, 8, -sqrt(-16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16)*sqrt(16 + sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16) + 8,
  1682. sqrt(640 - 128*sqrt(5))*sqrt(128*sqrt(5) + 640)/16, 8*sqrt(5))
  1683. )
  1684. assert len(res) == len(ans) == 4
  1685. assert res == ans
  1686. for result in res.args:
  1687. assert len(result) == 8
  1688. def test_issue_17940():
  1689. n = Dummy('n')
  1690. k1 = Dummy('k1')
  1691. sol = ImageSet(Lambda(((k1, n),), I*(2*k1*pi + arg(2*n*I*pi + log(5)))
  1692. + log(Abs(2*n*I*pi + log(5)))),
  1693. ProductSet(S.Integers, S.Integers))
  1694. assert solveset(exp(exp(x)) - 5, x).dummy_eq(sol)
  1695. def test_issue_17906():
  1696. assert solveset(7**(x**2 - 80) - 49**x, x) == FiniteSet(-8, 10)
  1697. def test_issue_17933():
  1698. eq1 = x*sin(45) - y*cos(q)
  1699. eq2 = x*cos(45) - y*sin(q)
  1700. eq3 = 9*x*sin(45)/10 + y*cos(q)
  1701. eq4 = 9*x*cos(45)/10 + y*sin(z) - z
  1702. assert nonlinsolve([eq1, eq2, eq3, eq4], x, y, z, q) ==\
  1703. FiniteSet((0, 0, 0, q))
  1704. def test_issue_14565():
  1705. # removed redundancy
  1706. assert dumeq(nonlinsolve([k + m, k + m*exp(-2*pi*k)], [k, m]) ,
  1707. FiniteSet((-n*I, ImageSet(Lambda(n, n*I), S.Integers))))
  1708. # end of tests for nonlinsolve
  1709. def test_issue_9556():
  1710. b = Symbol('b', positive=True)
  1711. assert solveset(Abs(x) + 1, x, S.Reals) is S.EmptySet
  1712. assert solveset(Abs(x) + b, x, S.Reals) is S.EmptySet
  1713. assert solveset(Eq(b, -1), b, S.Reals) is S.EmptySet
  1714. def test_issue_9611():
  1715. assert solveset(Eq(x - x + a, a), x, S.Reals) == S.Reals
  1716. assert solveset(Eq(y - y + a, a), y) == S.Complexes
  1717. def test_issue_9557():
  1718. assert solveset(x**2 + a, x, S.Reals) == Intersection(S.Reals,
  1719. FiniteSet(-sqrt(-a), sqrt(-a)))
  1720. def test_issue_9778():
  1721. x = Symbol('x', real=True)
  1722. y = Symbol('y', real=True)
  1723. assert solveset(x**3 + 1, x, S.Reals) == FiniteSet(-1)
  1724. assert solveset(x**Rational(3, 5) + 1, x, S.Reals) == S.EmptySet
  1725. assert solveset(x**3 + y, x, S.Reals) == \
  1726. FiniteSet(-Abs(y)**Rational(1, 3)*sign(y))
  1727. def test_issue_10214():
  1728. assert solveset(x**Rational(3, 2) + 4, x, S.Reals) == S.EmptySet
  1729. assert solveset(x**(Rational(-3, 2)) + 4, x, S.Reals) == S.EmptySet
  1730. ans = FiniteSet(-2**Rational(2, 3))
  1731. assert solveset(x**(S(3)) + 4, x, S.Reals) == ans
  1732. assert (x**(S(3)) + 4).subs(x,list(ans)[0]) == 0 # substituting ans and verifying the result.
  1733. assert (x**(S(3)) + 4).subs(x,-(-2)**Rational(2, 3)) == 0
  1734. def test_issue_9849():
  1735. assert solveset(Abs(sin(x)) + 1, x, S.Reals) == S.EmptySet
  1736. def test_issue_9953():
  1737. assert linsolve([ ], x) == S.EmptySet
  1738. def test_issue_9913():
  1739. assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \
  1740. FiniteSet(-(3*sqrt(24081)/4 + Rational(4027, 4))**Rational(1, 3)/3 - 100/
  1741. (3*(3*sqrt(24081)/4 + Rational(4027, 4))**Rational(1, 3)) + Rational(20, 3))
  1742. def test_issue_10397():
  1743. assert solveset(sqrt(x), x, S.Complexes) == FiniteSet(0)
  1744. def test_issue_14987():
  1745. raises(ValueError, lambda: linear_eq_to_matrix(
  1746. [x**2], x))
  1747. raises(ValueError, lambda: linear_eq_to_matrix(
  1748. [x*(-3/x + 1) + 2*y - a], [x, y]))
  1749. raises(ValueError, lambda: linear_eq_to_matrix(
  1750. [(x**2 - 3*x)/(x - 3) - 3], x))
  1751. raises(ValueError, lambda: linear_eq_to_matrix(
  1752. [(x + 1)**3 - x**3 - 3*x**2 + 7], x))
  1753. raises(ValueError, lambda: linear_eq_to_matrix(
  1754. [x*(1/x + 1) + y], [x, y]))
  1755. raises(ValueError, lambda: linear_eq_to_matrix(
  1756. [(x + 1)*y], [x, y]))
  1757. raises(ValueError, lambda: linear_eq_to_matrix(
  1758. [Eq(1/x, 1/x + y)], [x, y]))
  1759. raises(ValueError, lambda: linear_eq_to_matrix(
  1760. [Eq(y/x, y/x + y)], [x, y]))
  1761. raises(ValueError, lambda: linear_eq_to_matrix(
  1762. [Eq(x*(x + 1), x**2 + y)], [x, y]))
  1763. def test_simplification():
  1764. eq = x + (a - b)/(-2*a + 2*b)
  1765. assert solveset(eq, x) == FiniteSet(S.Half)
  1766. assert solveset(eq, x, S.Reals) == Intersection({-((a - b)/(-2*a + 2*b))}, S.Reals)
  1767. # So that ap - bn is not zero:
  1768. ap = Symbol('ap', positive=True)
  1769. bn = Symbol('bn', negative=True)
  1770. eq = x + (ap - bn)/(-2*ap + 2*bn)
  1771. assert solveset(eq, x) == FiniteSet(S.Half)
  1772. assert solveset(eq, x, S.Reals) == FiniteSet(S.Half)
  1773. def test_integer_domain_relational():
  1774. eq1 = 2*x + 3 > 0
  1775. eq2 = x**2 + 3*x - 2 >= 0
  1776. eq3 = x + 1/x > -2 + 1/x
  1777. eq4 = x + sqrt(x**2 - 5) > 0
  1778. eq = x + 1/x > -2 + 1/x
  1779. eq5 = eq.subs(x,log(x))
  1780. eq6 = log(x)/x <= 0
  1781. eq7 = log(x)/x < 0
  1782. eq8 = x/(x-3) < 3
  1783. eq9 = x/(x**2-3) < 3
  1784. assert solveset(eq1, x, S.Integers) == Range(-1, oo, 1)
  1785. assert solveset(eq2, x, S.Integers) == Union(Range(-oo, -3, 1), Range(1, oo, 1))
  1786. assert solveset(eq3, x, S.Integers) == Union(Range(-1, 0, 1), Range(1, oo, 1))
  1787. assert solveset(eq4, x, S.Integers) == Range(3, oo, 1)
  1788. assert solveset(eq5, x, S.Integers) == Range(2, oo, 1)
  1789. assert solveset(eq6, x, S.Integers) == Range(1, 2, 1)
  1790. assert solveset(eq7, x, S.Integers) == S.EmptySet
  1791. assert solveset(eq8, x, domain=Range(0,5)) == Range(0, 3, 1)
  1792. assert solveset(eq9, x, domain=Range(0,5)) == Union(Range(0, 2, 1), Range(2, 5, 1))
  1793. # test_issue_19794
  1794. assert solveset(x + 2 < 0, x, S.Integers) == Range(-oo, -2, 1)
  1795. def test_issue_10555():
  1796. f = Function('f')
  1797. g = Function('g')
  1798. assert solveset(f(x) - pi/2, x, S.Reals).dummy_eq(
  1799. ConditionSet(x, Eq(f(x) - pi/2, 0), S.Reals))
  1800. assert solveset(f(g(x)) - pi/2, g(x), S.Reals).dummy_eq(
  1801. ConditionSet(g(x), Eq(f(g(x)) - pi/2, 0), S.Reals))
  1802. def test_issue_8715():
  1803. eq = x + 1/x > -2 + 1/x
  1804. assert solveset(eq, x, S.Reals) == \
  1805. (Interval.open(-2, oo) - FiniteSet(0))
  1806. assert solveset(eq.subs(x,log(x)), x, S.Reals) == \
  1807. Interval.open(exp(-2), oo) - FiniteSet(1)
  1808. def test_issue_11174():
  1809. eq = z**2 + exp(2*x) - sin(y)
  1810. soln = Intersection(S.Reals, FiniteSet(log(-z**2 + sin(y))/2))
  1811. assert solveset(eq, x, S.Reals) == soln
  1812. eq = sqrt(r)*Abs(tan(t))/sqrt(tan(t)**2 + 1) + x*tan(t)
  1813. s = -sqrt(r)*Abs(tan(t))/(sqrt(tan(t)**2 + 1)*tan(t))
  1814. soln = Intersection(S.Reals, FiniteSet(s))
  1815. assert solveset(eq, x, S.Reals) == soln
  1816. def test_issue_11534():
  1817. # eq and eq2 should give the same solution as a Complement
  1818. x = Symbol('x', real=True)
  1819. y = Symbol('y', real=True)
  1820. eq = -y + x/sqrt(-x**2 + 1)
  1821. eq2 = -y**2 + x**2/(-x**2 + 1)
  1822. soln = Complement(FiniteSet(-y/sqrt(y**2 + 1), y/sqrt(y**2 + 1)), FiniteSet(-1, 1))
  1823. assert solveset(eq, x, S.Reals) == soln
  1824. assert solveset(eq2, x, S.Reals) == soln
  1825. def test_issue_10477():
  1826. assert solveset((x**2 + 4*x - 3)/x < 2, x, S.Reals) == \
  1827. Union(Interval.open(-oo, -3), Interval.open(0, 1))
  1828. def test_issue_10671():
  1829. assert solveset(sin(y), y, Interval(0, pi)) == FiniteSet(0, pi)
  1830. i = Interval(1, 10)
  1831. assert solveset((1/x).diff(x) < 0, x, i) == i
  1832. def test_issue_11064():
  1833. eq = x + sqrt(x**2 - 5)
  1834. assert solveset(eq > 0, x, S.Reals) == \
  1835. Interval(sqrt(5), oo)
  1836. assert solveset(eq < 0, x, S.Reals) == \
  1837. Interval(-oo, -sqrt(5))
  1838. assert solveset(eq > sqrt(5), x, S.Reals) == \
  1839. Interval.Lopen(sqrt(5), oo)
  1840. def test_issue_12478():
  1841. eq = sqrt(x - 2) + 2
  1842. soln = solveset_real(eq, x)
  1843. assert soln is S.EmptySet
  1844. assert solveset(eq < 0, x, S.Reals) is S.EmptySet
  1845. assert solveset(eq > 0, x, S.Reals) == Interval(2, oo)
  1846. def test_issue_12429():
  1847. eq = solveset(log(x)/x <= 0, x, S.Reals)
  1848. sol = Interval.Lopen(0, 1)
  1849. assert eq == sol
  1850. def test_issue_19506():
  1851. eq = arg(x + I)
  1852. C = Dummy('C')
  1853. assert solveset(eq).dummy_eq(Intersection(ConditionSet(C, Eq(im(C) + 1, 0), S.Complexes),
  1854. ConditionSet(C, re(C) > 0, S.Complexes)))
  1855. def test_solveset_arg():
  1856. assert solveset(arg(x), x, S.Reals) == Interval.open(0, oo)
  1857. assert solveset(arg(4*x -3), x, S.Reals) == Interval.open(Rational(3, 4), oo)
  1858. def test__is_finite_with_finite_vars():
  1859. f = _is_finite_with_finite_vars
  1860. # issue 12482
  1861. assert all(f(1/x) is None for x in (
  1862. Dummy(), Dummy(real=True), Dummy(complex=True)))
  1863. assert f(1/Dummy(real=False)) is True # b/c it's finite but not 0
  1864. def test_issue_13550():
  1865. assert solveset(x**2 - 2*x - 15, symbol = x, domain = Interval(-oo, 0)) == FiniteSet(-3)
  1866. def test_issue_13849():
  1867. assert nonlinsolve((t*(sqrt(5) + sqrt(2)) - sqrt(2), t), t) is S.EmptySet
  1868. def test_issue_14223():
  1869. assert solveset((Abs(x + Min(x, 2)) - 2).rewrite(Piecewise), x,
  1870. S.Reals) == FiniteSet(-1, 1)
  1871. assert solveset((Abs(x + Min(x, 2)) - 2).rewrite(Piecewise), x,
  1872. Interval(0, 2)) == FiniteSet(1)
  1873. assert solveset(x, x, FiniteSet(1, 2)) is S.EmptySet
  1874. def test_issue_10158():
  1875. dom = S.Reals
  1876. assert solveset(x*Max(x, 15) - 10, x, dom) == FiniteSet(Rational(2, 3))
  1877. assert solveset(x*Min(x, 15) - 10, x, dom) == FiniteSet(-sqrt(10), sqrt(10))
  1878. assert solveset(Max(Abs(x - 3) - 1, x + 2) - 3, x, dom) == FiniteSet(-1, 1)
  1879. assert solveset(Abs(x - 1) - Abs(y), x, dom) == FiniteSet(-Abs(y) + 1, Abs(y) + 1)
  1880. assert solveset(Abs(x + 4*Abs(x + 1)), x, dom) == FiniteSet(Rational(-4, 3), Rational(-4, 5))
  1881. assert solveset(2*Abs(x + Abs(x + Max(3, x))) - 2, x, S.Reals) == FiniteSet(-1, -2)
  1882. dom = S.Complexes
  1883. raises(ValueError, lambda: solveset(x*Max(x, 15) - 10, x, dom))
  1884. raises(ValueError, lambda: solveset(x*Min(x, 15) - 10, x, dom))
  1885. raises(ValueError, lambda: solveset(Max(Abs(x - 3) - 1, x + 2) - 3, x, dom))
  1886. raises(ValueError, lambda: solveset(Abs(x - 1) - Abs(y), x, dom))
  1887. raises(ValueError, lambda: solveset(Abs(x + 4*Abs(x + 1)), x, dom))
  1888. def test_issue_14300():
  1889. f = 1 - exp(-18000000*x) - y
  1890. a1 = FiniteSet(-log(-y + 1)/18000000)
  1891. assert solveset(f, x, S.Reals) == \
  1892. Intersection(S.Reals, a1)
  1893. assert dumeq(solveset(f, x),
  1894. ImageSet(Lambda(n, -I*(2*n*pi + arg(-y + 1))/18000000 -
  1895. log(Abs(y - 1))/18000000), S.Integers))
  1896. def test_issue_14454():
  1897. number = CRootOf(x**4 + x - 1, 2)
  1898. raises(ValueError, lambda: invert_real(number, 0, x))
  1899. assert invert_real(x**2, number, x) # no error
  1900. def test_issue_17882():
  1901. assert solveset(-8*x**2/(9*(x**2 - 1)**(S(4)/3)) + 4/(3*(x**2 - 1)**(S(1)/3)), x, S.Complexes) == \
  1902. FiniteSet(sqrt(3), -sqrt(3))
  1903. def test_term_factors():
  1904. assert list(_term_factors(3**x - 2)) == [-2, 3**x]
  1905. expr = 4**(x + 1) + 4**(x + 2) + 4**(x - 1) - 3**(x + 2) - 3**(x + 3)
  1906. assert set(_term_factors(expr)) == {
  1907. 3**(x + 2), 4**(x + 2), 3**(x + 3), 4**(x - 1), -1, 4**(x + 1)}
  1908. #################### tests for transolve and its helpers ###############
  1909. def test_transolve():
  1910. assert _transolve(3**x, x, S.Reals) == S.EmptySet
  1911. assert _transolve(3**x - 9**(x + 5), x, S.Reals) == FiniteSet(-10)
  1912. def test_issue_21276():
  1913. eq = (2*x*(y - z) - y*erf(y - z) - y + z*erf(y - z) + z)**2
  1914. assert solveset(eq.expand(), y) == FiniteSet(z, z + erfinv(2*x - 1))
  1915. # exponential tests
  1916. def test_exponential_real():
  1917. from sympy.abc import y
  1918. e1 = 3**(2*x) - 2**(x + 3)
  1919. e2 = 4**(5 - 9*x) - 8**(2 - x)
  1920. e3 = 2**x + 4**x
  1921. e4 = exp(log(5)*x) - 2**x
  1922. e5 = exp(x/y)*exp(-z/y) - 2
  1923. e6 = 5**(x/2) - 2**(x/3)
  1924. e7 = 4**(x + 1) + 4**(x + 2) + 4**(x - 1) - 3**(x + 2) - 3**(x + 3)
  1925. e8 = -9*exp(-2*x + 5) + 4*exp(3*x + 1)
  1926. e9 = 2**x + 4**x + 8**x - 84
  1927. e10 = 29*2**(x + 1)*615**(x) - 123*2726**(x)
  1928. assert solveset(e1, x, S.Reals) == FiniteSet(
  1929. -3*log(2)/(-2*log(3) + log(2)))
  1930. assert solveset(e2, x, S.Reals) == FiniteSet(Rational(4, 15))
  1931. assert solveset(e3, x, S.Reals) == S.EmptySet
  1932. assert solveset(e4, x, S.Reals) == FiniteSet(0)
  1933. assert solveset(e5, x, S.Reals) == Intersection(
  1934. S.Reals, FiniteSet(y*log(2*exp(z/y))))
  1935. assert solveset(e6, x, S.Reals) == FiniteSet(0)
  1936. assert solveset(e7, x, S.Reals) == FiniteSet(2)
  1937. assert solveset(e8, x, S.Reals) == FiniteSet(-2*log(2)/5 + 2*log(3)/5 + Rational(4, 5))
  1938. assert solveset(e9, x, S.Reals) == FiniteSet(2)
  1939. assert solveset(e10,x, S.Reals) == FiniteSet((-log(29) - log(2) + log(123))/(-log(2726) + log(2) + log(615)))
  1940. assert solveset_real(-9*exp(-2*x + 5) + 2**(x + 1), x) == FiniteSet(
  1941. -((-5 - 2*log(3) + log(2))/(log(2) + 2)))
  1942. assert solveset_real(4**(x/2) - 2**(x/3), x) == FiniteSet(0)
  1943. b = sqrt(6)*sqrt(log(2))/sqrt(log(5))
  1944. assert solveset_real(5**(x/2) - 2**(3/x), x) == FiniteSet(-b, b)
  1945. # coverage test
  1946. C1, C2 = symbols('C1 C2')
  1947. f = Function('f')
  1948. assert solveset_real(C1 + C2/x**2 - exp(-f(x)), f(x)) == Intersection(
  1949. S.Reals, FiniteSet(-log(C1 + C2/x**2)))
  1950. y = symbols('y', positive=True)
  1951. assert solveset_real(x**2 - y**2/exp(x), y) == Intersection(
  1952. S.Reals, FiniteSet(-sqrt(x**2*exp(x)), sqrt(x**2*exp(x))))
  1953. p = Symbol('p', positive=True)
  1954. assert solveset_real((1/p + 1)**(p + 1), p).dummy_eq(
  1955. ConditionSet(x, Eq((1 + 1/x)**(x + 1), 0), S.Reals))
  1956. @XFAIL
  1957. def test_exponential_complex():
  1958. n = Dummy('n')
  1959. assert dumeq(solveset_complex(2**x + 4**x, x),imageset(
  1960. Lambda(n, I*(2*n*pi + pi)/log(2)), S.Integers))
  1961. assert solveset_complex(x**z*y**z - 2, z) == FiniteSet(
  1962. log(2)/(log(x) + log(y)))
  1963. assert dumeq(solveset_complex(4**(x/2) - 2**(x/3), x), imageset(
  1964. Lambda(n, 3*n*I*pi/log(2)), S.Integers))
  1965. assert dumeq(solveset(2**x + 32, x), imageset(
  1966. Lambda(n, (I*(2*n*pi + pi) + 5*log(2))/log(2)), S.Integers))
  1967. eq = (2**exp(y**2/x) + 2)/(x**2 + 15)
  1968. a = sqrt(x)*sqrt(-log(log(2)) + log(log(2) + 2*n*I*pi))
  1969. assert solveset_complex(eq, y) == FiniteSet(-a, a)
  1970. union1 = imageset(Lambda(n, I*(2*n*pi - pi*Rational(2, 3))/log(2)), S.Integers)
  1971. union2 = imageset(Lambda(n, I*(2*n*pi + pi*Rational(2, 3))/log(2)), S.Integers)
  1972. assert dumeq(solveset(2**x + 4**x + 8**x, x), Union(union1, union2))
  1973. eq = 4**(x + 1) + 4**(x + 2) + 4**(x - 1) - 3**(x + 2) - 3**(x + 3)
  1974. res = solveset(eq, x)
  1975. num = 2*n*I*pi - 4*log(2) + 2*log(3)
  1976. den = -2*log(2) + log(3)
  1977. ans = imageset(Lambda(n, num/den), S.Integers)
  1978. assert dumeq(res, ans)
  1979. def test_expo_conditionset():
  1980. f1 = (exp(x) + 1)**x - 2
  1981. f2 = (x + 2)**y*x - 3
  1982. f3 = 2**x - exp(x) - 3
  1983. f4 = log(x) - exp(x)
  1984. f5 = 2**x + 3**x - 5**x
  1985. assert solveset(f1, x, S.Reals).dummy_eq(ConditionSet(
  1986. x, Eq((exp(x) + 1)**x - 2, 0), S.Reals))
  1987. assert solveset(f2, x, S.Reals).dummy_eq(ConditionSet(
  1988. x, Eq(x*(x + 2)**y - 3, 0), S.Reals))
  1989. assert solveset(f3, x, S.Reals).dummy_eq(ConditionSet(
  1990. x, Eq(2**x - exp(x) - 3, 0), S.Reals))
  1991. assert solveset(f4, x, S.Reals).dummy_eq(ConditionSet(
  1992. x, Eq(-exp(x) + log(x), 0), S.Reals))
  1993. assert solveset(f5, x, S.Reals).dummy_eq(ConditionSet(
  1994. x, Eq(2**x + 3**x - 5**x, 0), S.Reals))
  1995. def test_exponential_symbols():
  1996. x, y, z = symbols('x y z', positive=True)
  1997. xr, zr = symbols('xr, zr', real=True)
  1998. assert solveset(z**x - y, x, S.Reals) == Intersection(
  1999. S.Reals, FiniteSet(log(y)/log(z)))
  2000. f1 = 2*x**w - 4*y**w
  2001. f2 = (x/y)**w - 2
  2002. sol1 = Intersection({log(2)/(log(x) - log(y))}, S.Reals)
  2003. sol2 = Intersection({log(2)/log(x/y)}, S.Reals)
  2004. assert solveset(f1, w, S.Reals) == sol1, solveset(f1, w, S.Reals)
  2005. assert solveset(f2, w, S.Reals) == sol2, solveset(f2, w, S.Reals)
  2006. assert solveset(x**x, x, Interval.Lopen(0,oo)).dummy_eq(
  2007. ConditionSet(w, Eq(w**w, 0), Interval.open(0, oo)))
  2008. assert solveset(x**y - 1, y, S.Reals) == FiniteSet(0)
  2009. assert solveset(exp(x/y)*exp(-z/y) - 2, y, S.Reals) == \
  2010. Complement(ConditionSet(y, Eq(im(x)/y, 0) & Eq(im(z)/y, 0), \
  2011. Complement(Intersection(FiniteSet((x - z)/log(2)), S.Reals), FiniteSet(0))), FiniteSet(0))
  2012. assert solveset(exp(xr/y)*exp(-zr/y) - 2, y, S.Reals) == \
  2013. Complement(FiniteSet((xr - zr)/log(2)), FiniteSet(0))
  2014. assert solveset(a**x - b**x, x).dummy_eq(ConditionSet(
  2015. w, Ne(a, 0) & Ne(b, 0), FiniteSet(0)))
  2016. def test_ignore_assumptions():
  2017. # make sure assumptions are ignored
  2018. xpos = symbols('x', positive=True)
  2019. x = symbols('x')
  2020. assert solveset_complex(xpos**2 - 4, xpos
  2021. ) == solveset_complex(x**2 - 4, x)
  2022. @XFAIL
  2023. def test_issue_10864():
  2024. assert solveset(x**(y*z) - x, x, S.Reals) == FiniteSet(1)
  2025. @XFAIL
  2026. def test_solve_only_exp_2():
  2027. assert solveset_real(sqrt(exp(x)) + sqrt(exp(-x)) - 4, x) == \
  2028. FiniteSet(2*log(-sqrt(3) + 2), 2*log(sqrt(3) + 2))
  2029. def test_is_exponential():
  2030. assert _is_exponential(y, x) is False
  2031. assert _is_exponential(3**x - 2, x) is True
  2032. assert _is_exponential(5**x - 7**(2 - x), x) is True
  2033. assert _is_exponential(sin(2**x) - 4*x, x) is False
  2034. assert _is_exponential(x**y - z, y) is True
  2035. assert _is_exponential(x**y - z, x) is False
  2036. assert _is_exponential(2**x + 4**x - 1, x) is True
  2037. assert _is_exponential(x**(y*z) - x, x) is False
  2038. assert _is_exponential(x**(2*x) - 3**x, x) is False
  2039. assert _is_exponential(x**y - y*z, y) is False
  2040. assert _is_exponential(x**y - x*z, y) is True
  2041. def test_solve_exponential():
  2042. assert _solve_exponential(3**(2*x) - 2**(x + 3), 0, x, S.Reals) == \
  2043. FiniteSet(-3*log(2)/(-2*log(3) + log(2)))
  2044. assert _solve_exponential(2**y + 4**y, 1, y, S.Reals) == \
  2045. FiniteSet(log(Rational(-1, 2) + sqrt(5)/2)/log(2))
  2046. assert _solve_exponential(2**y + 4**y, 0, y, S.Reals) == \
  2047. S.EmptySet
  2048. assert _solve_exponential(2**x + 3**x - 5**x, 0, x, S.Reals) == \
  2049. ConditionSet(x, Eq(2**x + 3**x - 5**x, 0), S.Reals)
  2050. # end of exponential tests
  2051. # logarithmic tests
  2052. def test_logarithmic():
  2053. assert solveset_real(log(x - 3) + log(x + 3), x) == FiniteSet(
  2054. -sqrt(10), sqrt(10))
  2055. assert solveset_real(log(x + 1) - log(2*x - 1), x) == FiniteSet(2)
  2056. assert solveset_real(log(x + 3) + log(1 + 3/x) - 3, x) == FiniteSet(
  2057. -3 + sqrt(-12 + exp(3))*exp(Rational(3, 2))/2 + exp(3)/2,
  2058. -sqrt(-12 + exp(3))*exp(Rational(3, 2))/2 - 3 + exp(3)/2)
  2059. eq = z - log(x) + log(y/(x*(-1 + y**2/x**2)))
  2060. assert solveset_real(eq, x) == \
  2061. Intersection(S.Reals, FiniteSet(-sqrt(y**2 - y*exp(z)),
  2062. sqrt(y**2 - y*exp(z)))) - \
  2063. Intersection(S.Reals, FiniteSet(-sqrt(y**2), sqrt(y**2)))
  2064. assert solveset_real(
  2065. log(3*x) - log(-x + 1) - log(4*x + 1), x) == FiniteSet(Rational(-1, 2), S.Half)
  2066. assert solveset(log(x**y) - y*log(x), x, S.Reals) == S.Reals
  2067. @XFAIL
  2068. def test_uselogcombine_2():
  2069. eq = log(exp(2*x) + 1) + log(-tanh(x) + 1) - log(2)
  2070. assert solveset_real(eq, x) is S.EmptySet
  2071. eq = log(8*x) - log(sqrt(x) + 1) - 2
  2072. assert solveset_real(eq, x) is S.EmptySet
  2073. def test_is_logarithmic():
  2074. assert _is_logarithmic(y, x) is False
  2075. assert _is_logarithmic(log(x), x) is True
  2076. assert _is_logarithmic(log(x) - 3, x) is True
  2077. assert _is_logarithmic(log(x)*log(y), x) is True
  2078. assert _is_logarithmic(log(x)**2, x) is False
  2079. assert _is_logarithmic(log(x - 3) + log(x + 3), x) is True
  2080. assert _is_logarithmic(log(x**y) - y*log(x), x) is True
  2081. assert _is_logarithmic(sin(log(x)), x) is False
  2082. assert _is_logarithmic(x + y, x) is False
  2083. assert _is_logarithmic(log(3*x) - log(1 - x) + 4, x) is True
  2084. assert _is_logarithmic(log(x) + log(y) + x, x) is False
  2085. assert _is_logarithmic(log(log(x - 3)) + log(x - 3), x) is True
  2086. assert _is_logarithmic(log(log(3) + x) + log(x), x) is True
  2087. assert _is_logarithmic(log(x)*(y + 3) + log(x), y) is False
  2088. def test_solve_logarithm():
  2089. y = Symbol('y')
  2090. assert _solve_logarithm(log(x**y) - y*log(x), 0, x, S.Reals) == S.Reals
  2091. y = Symbol('y', positive=True)
  2092. assert _solve_logarithm(log(x)*log(y), 0, x, S.Reals) == FiniteSet(1)
  2093. # end of logarithmic tests
  2094. # lambert tests
  2095. def test_is_lambert():
  2096. a, b, c = symbols('a,b,c')
  2097. assert _is_lambert(x**2, x) is False
  2098. assert _is_lambert(a**x**2+b*x+c, x) is True
  2099. assert _is_lambert(E**2, x) is False
  2100. assert _is_lambert(x*E**2, x) is False
  2101. assert _is_lambert(3*log(x) - x*log(3), x) is True
  2102. assert _is_lambert(log(log(x - 3)) + log(x-3), x) is True
  2103. assert _is_lambert(5*x - 1 + 3*exp(2 - 7*x), x) is True
  2104. assert _is_lambert((a/x + exp(x/2)).diff(x, 2), x) is True
  2105. assert _is_lambert((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) is True
  2106. assert _is_lambert(x*sinh(x) - 1, x) is True
  2107. assert _is_lambert(x*cos(x) - 5, x) is True
  2108. assert _is_lambert(tanh(x) - 5*x, x) is True
  2109. assert _is_lambert(cosh(x) - sinh(x), x) is False
  2110. # end of lambert tests
  2111. def test_linear_coeffs():
  2112. from sympy.solvers.solveset import linear_coeffs
  2113. assert linear_coeffs(0, x) == [0, 0]
  2114. assert all(i is S.Zero for i in linear_coeffs(0, x))
  2115. assert linear_coeffs(x + 2*y + 3, x, y) == [1, 2, 3]
  2116. assert linear_coeffs(x + 2*y + 3, y, x) == [2, 1, 3]
  2117. assert linear_coeffs(x + 2*x**2 + 3, x, x**2) == [1, 2, 3]
  2118. raises(ValueError, lambda:
  2119. linear_coeffs(x + 2*x**2 + x**3, x, x**2))
  2120. raises(ValueError, lambda:
  2121. linear_coeffs(1/x*(x - 1) + 1/x, x))
  2122. raises(ValueError, lambda:
  2123. linear_coeffs(x, x, x))
  2124. assert linear_coeffs(a*(x + y), x, y) == [a, a, 0]
  2125. assert linear_coeffs(1.0, x, y) == [0, 0, 1.0]
  2126. # modular tests
  2127. def test_is_modular():
  2128. assert _is_modular(y, x) is False
  2129. assert _is_modular(Mod(x, 3) - 1, x) is True
  2130. assert _is_modular(Mod(x**3 - 3*x**2 - x + 1, 3) - 1, x) is True
  2131. assert _is_modular(Mod(exp(x + y), 3) - 2, x) is True
  2132. assert _is_modular(Mod(exp(x + y), 3) - log(x), x) is True
  2133. assert _is_modular(Mod(x, 3) - 1, y) is False
  2134. assert _is_modular(Mod(x, 3)**2 - 5, x) is False
  2135. assert _is_modular(Mod(x, 3)**2 - y, x) is False
  2136. assert _is_modular(exp(Mod(x, 3)) - 1, x) is False
  2137. assert _is_modular(Mod(3, y) - 1, y) is False
  2138. def test_invert_modular():
  2139. n = Dummy('n', integer=True)
  2140. from sympy.solvers.solveset import _invert_modular as invert_modular
  2141. # non invertible cases
  2142. assert invert_modular(Mod(sin(x), 7), S(5), n, x) == (Mod(sin(x), 7), 5)
  2143. assert invert_modular(Mod(exp(x), 7), S(5), n, x) == (Mod(exp(x), 7), 5)
  2144. assert invert_modular(Mod(log(x), 7), S(5), n, x) == (Mod(log(x), 7), 5)
  2145. # a is symbol
  2146. assert dumeq(invert_modular(Mod(x, 7), S(5), n, x),
  2147. (x, ImageSet(Lambda(n, 7*n + 5), S.Integers)))
  2148. # a.is_Add
  2149. assert dumeq(invert_modular(Mod(x + 8, 7), S(5), n, x),
  2150. (x, ImageSet(Lambda(n, 7*n + 4), S.Integers)))
  2151. assert invert_modular(Mod(x**2 + x, 7), S(5), n, x) == \
  2152. (Mod(x**2 + x, 7), 5)
  2153. # a.is_Mul
  2154. assert dumeq(invert_modular(Mod(3*x, 7), S(5), n, x),
  2155. (x, ImageSet(Lambda(n, 7*n + 4), S.Integers)))
  2156. assert invert_modular(Mod((x + 1)*(x + 2), 7), S(5), n, x) == \
  2157. (Mod((x + 1)*(x + 2), 7), 5)
  2158. # a.is_Pow
  2159. assert invert_modular(Mod(x**4, 7), S(5), n, x) == \
  2160. (x, S.EmptySet)
  2161. assert dumeq(invert_modular(Mod(3**x, 4), S(3), n, x),
  2162. (x, ImageSet(Lambda(n, 2*n + 1), S.Naturals0)))
  2163. assert dumeq(invert_modular(Mod(2**(x**2 + x + 1), 7), S(2), n, x),
  2164. (x**2 + x + 1, ImageSet(Lambda(n, 3*n + 1), S.Naturals0)))
  2165. assert invert_modular(Mod(sin(x)**4, 7), S(5), n, x) == (x, S.EmptySet)
  2166. def test_solve_modular():
  2167. n = Dummy('n', integer=True)
  2168. # if rhs has symbol (need to be implemented in future).
  2169. assert solveset(Mod(x, 4) - x, x, S.Integers
  2170. ).dummy_eq(
  2171. ConditionSet(x, Eq(-x + Mod(x, 4), 0),
  2172. S.Integers))
  2173. # when _invert_modular fails to invert
  2174. assert solveset(3 - Mod(sin(x), 7), x, S.Integers
  2175. ).dummy_eq(
  2176. ConditionSet(x, Eq(Mod(sin(x), 7) - 3, 0), S.Integers))
  2177. assert solveset(3 - Mod(log(x), 7), x, S.Integers
  2178. ).dummy_eq(
  2179. ConditionSet(x, Eq(Mod(log(x), 7) - 3, 0), S.Integers))
  2180. assert solveset(3 - Mod(exp(x), 7), x, S.Integers
  2181. ).dummy_eq(ConditionSet(x, Eq(Mod(exp(x), 7) - 3, 0),
  2182. S.Integers))
  2183. # EmptySet solution definitely
  2184. assert solveset(7 - Mod(x, 5), x, S.Integers) is S.EmptySet
  2185. assert solveset(5 - Mod(x, 5), x, S.Integers) is S.EmptySet
  2186. # Negative m
  2187. assert dumeq(solveset(2 + Mod(x, -3), x, S.Integers),
  2188. ImageSet(Lambda(n, -3*n - 2), S.Integers))
  2189. assert solveset(4 + Mod(x, -3), x, S.Integers) is S.EmptySet
  2190. # linear expression in Mod
  2191. assert dumeq(solveset(3 - Mod(x, 5), x, S.Integers),
  2192. ImageSet(Lambda(n, 5*n + 3), S.Integers))
  2193. assert dumeq(solveset(3 - Mod(5*x - 8, 7), x, S.Integers),
  2194. ImageSet(Lambda(n, 7*n + 5), S.Integers))
  2195. assert dumeq(solveset(3 - Mod(5*x, 7), x, S.Integers),
  2196. ImageSet(Lambda(n, 7*n + 2), S.Integers))
  2197. # higher degree expression in Mod
  2198. assert dumeq(solveset(Mod(x**2, 160) - 9, x, S.Integers),
  2199. Union(ImageSet(Lambda(n, 160*n + 3), S.Integers),
  2200. ImageSet(Lambda(n, 160*n + 13), S.Integers),
  2201. ImageSet(Lambda(n, 160*n + 67), S.Integers),
  2202. ImageSet(Lambda(n, 160*n + 77), S.Integers),
  2203. ImageSet(Lambda(n, 160*n + 83), S.Integers),
  2204. ImageSet(Lambda(n, 160*n + 93), S.Integers),
  2205. ImageSet(Lambda(n, 160*n + 147), S.Integers),
  2206. ImageSet(Lambda(n, 160*n + 157), S.Integers)))
  2207. assert solveset(3 - Mod(x**4, 7), x, S.Integers) is S.EmptySet
  2208. assert dumeq(solveset(Mod(x**4, 17) - 13, x, S.Integers),
  2209. Union(ImageSet(Lambda(n, 17*n + 3), S.Integers),
  2210. ImageSet(Lambda(n, 17*n + 5), S.Integers),
  2211. ImageSet(Lambda(n, 17*n + 12), S.Integers),
  2212. ImageSet(Lambda(n, 17*n + 14), S.Integers)))
  2213. # a.is_Pow tests
  2214. assert dumeq(solveset(Mod(7**x, 41) - 15, x, S.Integers),
  2215. ImageSet(Lambda(n, 40*n + 3), S.Naturals0))
  2216. assert dumeq(solveset(Mod(12**x, 21) - 18, x, S.Integers),
  2217. ImageSet(Lambda(n, 6*n + 2), S.Naturals0))
  2218. assert dumeq(solveset(Mod(3**x, 4) - 3, x, S.Integers),
  2219. ImageSet(Lambda(n, 2*n + 1), S.Naturals0))
  2220. assert dumeq(solveset(Mod(2**x, 7) - 2 , x, S.Integers),
  2221. ImageSet(Lambda(n, 3*n + 1), S.Naturals0))
  2222. assert dumeq(solveset(Mod(3**(3**x), 4) - 3, x, S.Integers),
  2223. Intersection(ImageSet(Lambda(n, Intersection({log(2*n + 1)/log(3)},
  2224. S.Integers)), S.Naturals0), S.Integers))
  2225. # Implemented for m without primitive root
  2226. assert solveset(Mod(x**3, 7) - 2, x, S.Integers) is S.EmptySet
  2227. assert dumeq(solveset(Mod(x**3, 8) - 1, x, S.Integers),
  2228. ImageSet(Lambda(n, 8*n + 1), S.Integers))
  2229. assert dumeq(solveset(Mod(x**4, 9) - 4, x, S.Integers),
  2230. Union(ImageSet(Lambda(n, 9*n + 4), S.Integers),
  2231. ImageSet(Lambda(n, 9*n + 5), S.Integers)))
  2232. # domain intersection
  2233. assert dumeq(solveset(3 - Mod(5*x - 8, 7), x, S.Naturals0),
  2234. Intersection(ImageSet(Lambda(n, 7*n + 5), S.Integers), S.Naturals0))
  2235. # Complex args
  2236. assert solveset(Mod(x, 3) - I, x, S.Integers) == \
  2237. S.EmptySet
  2238. assert solveset(Mod(I*x, 3) - 2, x, S.Integers
  2239. ).dummy_eq(
  2240. ConditionSet(x, Eq(Mod(I*x, 3) - 2, 0), S.Integers))
  2241. assert solveset(Mod(I + x, 3) - 2, x, S.Integers
  2242. ).dummy_eq(
  2243. ConditionSet(x, Eq(Mod(x + I, 3) - 2, 0), S.Integers))
  2244. # issue 17373 (https://github.com/sympy/sympy/issues/17373)
  2245. assert dumeq(solveset(Mod(x**4, 14) - 11, x, S.Integers),
  2246. Union(ImageSet(Lambda(n, 14*n + 3), S.Integers),
  2247. ImageSet(Lambda(n, 14*n + 11), S.Integers)))
  2248. assert dumeq(solveset(Mod(x**31, 74) - 43, x, S.Integers),
  2249. ImageSet(Lambda(n, 74*n + 31), S.Integers))
  2250. # issue 13178
  2251. n = symbols('n', integer=True)
  2252. a = 742938285
  2253. b = 1898888478
  2254. m = 2**31 - 1
  2255. c = 20170816
  2256. assert dumeq(solveset(c - Mod(a**n*b, m), n, S.Integers),
  2257. ImageSet(Lambda(n, 2147483646*n + 100), S.Naturals0))
  2258. assert dumeq(solveset(c - Mod(a**n*b, m), n, S.Naturals0),
  2259. Intersection(ImageSet(Lambda(n, 2147483646*n + 100), S.Naturals0),
  2260. S.Naturals0))
  2261. assert dumeq(solveset(c - Mod(a**(2*n)*b, m), n, S.Integers),
  2262. Intersection(ImageSet(Lambda(n, 1073741823*n + 50), S.Naturals0),
  2263. S.Integers))
  2264. assert solveset(c - Mod(a**(2*n + 7)*b, m), n, S.Integers) is S.EmptySet
  2265. assert dumeq(solveset(c - Mod(a**(n - 4)*b, m), n, S.Integers),
  2266. Intersection(ImageSet(Lambda(n, 2147483646*n + 104), S.Naturals0),
  2267. S.Integers))
  2268. # end of modular tests
  2269. def test_issue_17276():
  2270. assert nonlinsolve([Eq(x, 5**(S(1)/5)), Eq(x*y, 25*sqrt(5))], x, y) == \
  2271. FiniteSet((5**(S(1)/5), 25*5**(S(3)/10)))
  2272. def test_issue_10426():
  2273. x = Dummy('x')
  2274. a = Symbol('a')
  2275. n = Dummy('n')
  2276. assert (solveset(sin(x + a) - sin(x), a)).dummy_eq(Dummy('x')) == (Union(
  2277. ImageSet(Lambda(n, 2*n*pi), S.Integers),
  2278. Intersection(S.Complexes, ImageSet(Lambda(n, -I*(I*(2*n*pi + arg(-exp(-2*I*x))) + 2*im(x))),
  2279. S.Integers)))).dummy_eq(Dummy('x,n'))
  2280. def test_solveset_conjugate():
  2281. """Test solveset for simple conjugate functions"""
  2282. assert solveset(conjugate(x) -3 + I) == FiniteSet(3 + I)
  2283. def test_issue_18208():
  2284. variables = symbols('x0:16') + symbols('y0:12')
  2285. x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15,\
  2286. y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11 = variables
  2287. eqs = [x0 + x1 + x2 + x3 - 51,
  2288. x0 + x1 + x4 + x5 - 46,
  2289. x2 + x3 + x6 + x7 - 39,
  2290. x0 + x3 + x4 + x7 - 50,
  2291. x1 + x2 + x5 + x6 - 35,
  2292. x4 + x5 + x6 + x7 - 34,
  2293. x4 + x5 + x8 + x9 - 46,
  2294. x10 + x11 + x6 + x7 - 23,
  2295. x11 + x4 + x7 + x8 - 25,
  2296. x10 + x5 + x6 + x9 - 44,
  2297. x10 + x11 + x8 + x9 - 35,
  2298. x12 + x13 + x8 + x9 - 35,
  2299. x10 + x11 + x14 + x15 - 29,
  2300. x11 + x12 + x15 + x8 - 35,
  2301. x10 + x13 + x14 + x9 - 29,
  2302. x12 + x13 + x14 + x15 - 29,
  2303. y0 + y1 + y2 + y3 - 55,
  2304. y0 + y1 + y4 + y5 - 53,
  2305. y2 + y3 + y6 + y7 - 56,
  2306. y0 + y3 + y4 + y7 - 57,
  2307. y1 + y2 + y5 + y6 - 52,
  2308. y4 + y5 + y6 + y7 - 54,
  2309. y4 + y5 + y8 + y9 - 48,
  2310. y10 + y11 + y6 + y7 - 60,
  2311. y11 + y4 + y7 + y8 - 51,
  2312. y10 + y5 + y6 + y9 - 57,
  2313. y10 + y11 + y8 + y9 - 54,
  2314. x10 - 2,
  2315. x11 - 5,
  2316. x12 - 1,
  2317. x13 - 6,
  2318. x14 - 1,
  2319. x15 - 21,
  2320. y0 - 12,
  2321. y1 - 20]
  2322. expected = [38 - x3, x3 - 10, 23 - x3, x3, 12 - x7, x7 + 6, 16 - x7, x7,
  2323. 8, 20, 2, 5, 1, 6, 1, 21, 12, 20, -y11 + y9 + 2, y11 - y9 + 21,
  2324. -y11 - y7 + y9 + 24, y11 + y7 - y9 - 3, 33 - y7, y7, 27 - y9, y9,
  2325. 27 - y11, y11]
  2326. A, b = linear_eq_to_matrix(eqs, variables)
  2327. # solve
  2328. solve_expected = {v:eq for v, eq in zip(variables, expected) if v != eq}
  2329. assert solve(eqs, variables) == solve_expected
  2330. # linsolve
  2331. linsolve_expected = FiniteSet(Tuple(*expected))
  2332. assert linsolve(eqs, variables) == linsolve_expected
  2333. assert linsolve((A, b), variables) == linsolve_expected
  2334. # gauss_jordan_solve
  2335. gj_solve, new_vars = A.gauss_jordan_solve(b)
  2336. gj_solve = [i for i in gj_solve]
  2337. gj_expected = linsolve_expected.subs(zip([x3, x7, y7, y9, y11], new_vars))
  2338. assert FiniteSet(Tuple(*gj_solve)) == gj_expected
  2339. # nonlinsolve
  2340. # The solution set of nonlinsolve is currently equivalent to linsolve and is
  2341. # also correct. However, we would prefer to use the same symbols as parameters
  2342. # for the solution to the underdetermined system in all cases if possible.
  2343. # We want a solution that is not just equivalent but also given in the same form.
  2344. # This test may be changed should nonlinsolve be modified in this way.
  2345. nonlinsolve_expected = FiniteSet((38 - x3, x3 - 10, 23 - x3, x3, 12 - x7, x7 + 6,
  2346. 16 - x7, x7, 8, 20, 2, 5, 1, 6, 1, 21, 12, 20,
  2347. -y5 + y7 - 1, y5 - y7 + 24, 21 - y5, y5, 33 - y7,
  2348. y7, 27 - y9, y9, -y5 + y7 - y9 + 24, y5 - y7 + y9 + 3))
  2349. assert nonlinsolve(eqs, variables) == nonlinsolve_expected
  2350. @XFAIL
  2351. def test_substitution_with_infeasible_solution():
  2352. a00, a01, a10, a11, l0, l1, l2, l3, m0, m1, m2, m3, m4, m5, m6, m7, c00, c01, c10, c11, p00, p01, p10, p11 = symbols(
  2353. 'a00, a01, a10, a11, l0, l1, l2, l3, m0, m1, m2, m3, m4, m5, m6, m7, c00, c01, c10, c11, p00, p01, p10, p11'
  2354. )
  2355. solvefor = [p00, p01, p10, p11, c00, c01, c10, c11, m0, m1, m3, l0, l1, l2, l3]
  2356. system = [
  2357. -l0 * c00 - l1 * c01 + m0 + c00 + c01,
  2358. -l0 * c10 - l1 * c11 + m1,
  2359. -l2 * c00 - l3 * c01 + c00 + c01,
  2360. -l2 * c10 - l3 * c11 + m3,
  2361. -l0 * p00 - l2 * p10 + p00 + p10,
  2362. -l1 * p00 - l3 * p10 + p00 + p10,
  2363. -l0 * p01 - l2 * p11,
  2364. -l1 * p01 - l3 * p11,
  2365. -a00 + c00 * p00 + c10 * p01,
  2366. -a01 + c01 * p00 + c11 * p01,
  2367. -a10 + c00 * p10 + c10 * p11,
  2368. -a11 + c01 * p10 + c11 * p11,
  2369. -m0 * p00,
  2370. -m1 * p01,
  2371. -m2 * p10,
  2372. -m3 * p11,
  2373. -m4 * c00,
  2374. -m5 * c01,
  2375. -m6 * c10,
  2376. -m7 * c11,
  2377. m2,
  2378. m4,
  2379. m5,
  2380. m6,
  2381. m7
  2382. ]
  2383. sol = FiniteSet(
  2384. (0, Complement(FiniteSet(p01), FiniteSet(0)), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, l2, l3),
  2385. (p00, Complement(FiniteSet(p01), FiniteSet(0)), 0, p11, 0, 0, 0, 0, 0, 0, 0, 1, 1, -p01/p11, -p01/p11),
  2386. (0, Complement(FiniteSet(p01), FiniteSet(0)), 0, p11, 0, 0, 0, 0, 0, 0, 0, 1, -l3*p11/p01, -p01/p11, l3),
  2387. (0, Complement(FiniteSet(p01), FiniteSet(0)), 0, p11, 0, 0, 0, 0, 0, 0, 0, -l2*p11/p01, -l3*p11/p01, l2, l3),
  2388. )
  2389. assert sol != nonlinsolve(system, solvefor)
  2390. def test_issue_20097():
  2391. assert solveset(1/sqrt(x)) is S.EmptySet
  2392. def test_issue_15350():
  2393. assert solveset(diff(sqrt(1/x+x))) == FiniteSet(-1, 1)
  2394. def test_issue_18359():
  2395. c1 = Piecewise((0, x < 0), (Min(1, x)/2 - Min(2, x)/2 + Min(3, x)/2, True))
  2396. c2 = Piecewise((Piecewise((0, x < 0), (Min(1, x)/2 - Min(2, x)/2 + Min(3, x)/2, True)), x >= 0), (0, True))
  2397. correct_result = Interval(1, 2)
  2398. result1 = solveset(c1 - Rational(1, 2), x, Interval(0, 3))
  2399. result2 = solveset(c2 - Rational(1, 2), x, Interval(0, 3))
  2400. assert result1 == correct_result
  2401. assert result2 == correct_result
  2402. def test_issue_17604():
  2403. lhs = -2**(3*x/11)*exp(x/11) + pi**(x/11)
  2404. assert _is_exponential(lhs, x)
  2405. assert _solve_exponential(lhs, 0, x, S.Complexes) == FiniteSet(0)
  2406. def test_issue_17580():
  2407. assert solveset(1/(1 - x**3)**2, x, S.Reals) is S.EmptySet
  2408. def test_issue_17566_actual():
  2409. sys = [2**x + 2**y - 3, 4**x + 9**y - 5]
  2410. # Not clear this is the correct result, but at least no recursion error
  2411. assert nonlinsolve(sys, x, y) == FiniteSet((log(3 - 2**y)/log(2), y))
  2412. def test_issue_17565():
  2413. eq = Ge(2*(x - 2)**2/(3*(x + 1)**(Integer(1)/3)) + 2*(x - 2)*(x + 1)**(Integer(2)/3), 0)
  2414. res = Union(Interval.Lopen(-1, -Rational(1, 4)), Interval(2, oo))
  2415. assert solveset(eq, x, S.Reals) == res
  2416. def test_issue_15024():
  2417. function = (x + 5)/sqrt(-x**2 - 10*x)
  2418. assert solveset(function, x, S.Reals) == FiniteSet(Integer(-5))
  2419. def test_issue_16877():
  2420. assert dumeq(nonlinsolve([x - 1, sin(y)], x, y),
  2421. FiniteSet((FiniteSet(1), ImageSet(Lambda(n, 2*n*pi), S.Integers)),
  2422. (FiniteSet(1), ImageSet(Lambda(n, 2*n*pi + pi), S.Integers))))
  2423. # Even better if (FiniteSet(1), ImageSet(Lambda(n, n*pi), S.Integers)) is obtained
  2424. def test_issue_16876():
  2425. assert dumeq(nonlinsolve([sin(x), 2*x - 4*y], x, y),
  2426. FiniteSet((ImageSet(Lambda(n, 2*n*pi), S.Integers),
  2427. ImageSet(Lambda(n, n*pi), S.Integers)),
  2428. (ImageSet(Lambda(n, 2*n*pi + pi), S.Integers),
  2429. ImageSet(Lambda(n, n*pi + pi/2), S.Integers))))
  2430. # Even better if (ImageSet(Lambda(n, n*pi), S.Integers),
  2431. # ImageSet(Lambda(n, n*pi/2), S.Integers)) is obtained
  2432. def test_issue_21236():
  2433. x, z = symbols("x z")
  2434. y = symbols('y', rational=True)
  2435. assert solveset(x**y - z, x, S.Reals) == ConditionSet(x, Eq(x**y - z, 0), S.Reals)
  2436. e1, e2 = symbols('e1 e2', even=True)
  2437. y = e1/e2 # don't know if num or den will be odd and the other even
  2438. assert solveset(x**y - z, x, S.Reals) == ConditionSet(x, Eq(x**y - z, 0), S.Reals)
  2439. def test_issue_21908():
  2440. assert nonlinsolve([(x**2 + 2*x - y**2)*exp(x), -2*y*exp(x)], x, y
  2441. ) == {(-2, 0), (0, 0)}
  2442. def test_issue_19144():
  2443. # test case 1
  2444. expr1 = [x + y - 1, y**2 + 1]
  2445. eq1 = [Eq(i, 0) for i in expr1]
  2446. soln1 = {(1 - I, I), (1 + I, -I)}
  2447. soln_expr1 = nonlinsolve(expr1, [x, y])
  2448. soln_eq1 = nonlinsolve(eq1, [x, y])
  2449. assert soln_eq1 == soln_expr1 == soln1
  2450. # test case 2 - with denoms
  2451. expr2 = [x/y - 1, y**2 + 1]
  2452. eq2 = [Eq(i, 0) for i in expr2]
  2453. soln2 = {(-I, -I), (I, I)}
  2454. soln_expr2 = nonlinsolve(expr2, [x, y])
  2455. soln_eq2 = nonlinsolve(eq2, [x, y])
  2456. assert soln_eq2 == soln_expr2 == soln2
  2457. # denominators that cancel in expression
  2458. assert nonlinsolve([Eq(x + 1/x, 1/x)], [x]) == FiniteSet((S.EmptySet,))
  2459. def test_issue_22413():
  2460. res = nonlinsolve((4*y*(2*x + 2*exp(y) + 1)*exp(2*x),
  2461. 4*x*exp(2*x) + 4*y*exp(2*x + y) + 4*exp(2*x + y) + 1),
  2462. x, y)
  2463. # First solution is not correct, but the issue was an exception
  2464. sols = FiniteSet((x, S.Zero), (-exp(y) - S.Half, y))
  2465. assert res == sols
  2466. def test_issue_19814():
  2467. assert nonlinsolve([ 2**m - 2**(2*n), 4*2**m - 2**(4*n)], m, n
  2468. ) == FiniteSet((log(2**(2*n))/log(2), S.Complexes))
  2469. def test_issue_22058():
  2470. sol = solveset(-sqrt(t)*x**2 + 2*x + sqrt(t), x, S.Reals)
  2471. # doesn't fail (and following numerical check)
  2472. assert sol.xreplace({t: 1}) == {1 - sqrt(2), 1 + sqrt(2)}, sol.xreplace({t: 1})
  2473. def test_issue_11184():
  2474. assert solveset(20*sqrt(y**2 + (sqrt(-(y - 10)*(y + 10)) + 10)**2) - 60, y, S.Reals) is S.EmptySet
  2475. def test_issue_21890():
  2476. e = S(2)/3
  2477. assert nonlinsolve([4*x**3*y**4 - 2*y, 4*x**4*y**3 - 2*x], x, y) == {
  2478. (2**e/(2*y), y), ((-2**e/4 - 2**e*sqrt(3)*I/4)/y, y),
  2479. ((-2**e/4 + 2**e*sqrt(3)*I/4)/y, y)}
  2480. assert nonlinsolve([(1 - 4*x**2)*exp(-2*x**2 - 2*y**2),
  2481. -4*x*y*exp(-2*x**2)*exp(-2*y**2)], x, y) == {(-S(1)/2, 0), (S(1)/2, 0)}
  2482. rx, ry = symbols('x y', real=True)
  2483. sol = nonlinsolve([4*rx**3*ry**4 - 2*ry, 4*rx**4*ry**3 - 2*rx], rx, ry)
  2484. ans = {(2**(S(2)/3)/(2*ry), ry),
  2485. ((-2**(S(2)/3)/4 - 2**(S(2)/3)*sqrt(3)*I/4)/ry, ry),
  2486. ((-2**(S(2)/3)/4 + 2**(S(2)/3)*sqrt(3)*I/4)/ry, ry)}
  2487. assert sol == ans