test_solvers.py 105 KB

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