test_solveset.py 145 KB

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