test_solveset.py 131 KB

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