test_solvers.py 102 KB

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