test_polytools.py 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606
  1. """Tests for user-friendly public interface to polynomial functions. """
  2. import pickle
  3. from sympy.polys.polytools import (
  4. Poly, PurePoly, poly,
  5. parallel_poly_from_expr,
  6. degree, degree_list,
  7. total_degree,
  8. LC, LM, LT,
  9. pdiv, prem, pquo, pexquo,
  10. div, rem, quo, exquo,
  11. half_gcdex, gcdex, invert,
  12. subresultants,
  13. resultant, discriminant,
  14. terms_gcd, cofactors,
  15. gcd, gcd_list,
  16. lcm, lcm_list,
  17. trunc,
  18. monic, content, primitive,
  19. compose, decompose,
  20. sturm,
  21. gff_list, gff,
  22. sqf_norm, sqf_part, sqf_list, sqf,
  23. factor_list, factor,
  24. intervals, refine_root, count_roots,
  25. real_roots, nroots, ground_roots,
  26. nth_power_roots_poly,
  27. cancel, reduced, groebner,
  28. GroebnerBasis, is_zero_dimensional,
  29. _torational_factor_list,
  30. to_rational_coeffs)
  31. from sympy.polys.polyerrors import (
  32. MultivariatePolynomialError,
  33. ExactQuotientFailed,
  34. PolificationFailed,
  35. ComputationFailed,
  36. UnificationFailed,
  37. RefinementFailed,
  38. GeneratorsNeeded,
  39. GeneratorsError,
  40. PolynomialError,
  41. CoercionFailed,
  42. DomainError,
  43. OptionError,
  44. FlagError)
  45. from sympy.polys.polyclasses import DMP
  46. from sympy.polys.fields import field
  47. from sympy.polys.domains import FF, ZZ, QQ, ZZ_I, QQ_I, RR, EX
  48. from sympy.polys.domains.realfield import RealField
  49. from sympy.polys.domains.complexfield import ComplexField
  50. from sympy.polys.orderings import lex, grlex, grevlex
  51. from sympy.combinatorics.galois import S4TransitiveSubgroups
  52. from sympy.core.add import Add
  53. from sympy.core.basic import _aresame
  54. from sympy.core.containers import Tuple
  55. from sympy.core.expr import Expr
  56. from sympy.core.function import (Derivative, diff, expand)
  57. from sympy.core.mul import _keep_coeff, Mul
  58. from sympy.core.numbers import (Float, I, Integer, Rational, oo, pi)
  59. from sympy.core.power import Pow
  60. from sympy.core.relational import Eq
  61. from sympy.core.singleton import S
  62. from sympy.core.symbol import Symbol
  63. from sympy.functions.elementary.complexes import (im, re)
  64. from sympy.functions.elementary.exponential import exp
  65. from sympy.functions.elementary.hyperbolic import tanh
  66. from sympy.functions.elementary.miscellaneous import sqrt
  67. from sympy.functions.elementary.piecewise import Piecewise
  68. from sympy.functions.elementary.trigonometric import sin
  69. from sympy.matrices.dense import Matrix
  70. from sympy.matrices.expressions.matexpr import MatrixSymbol
  71. from sympy.polys.rootoftools import rootof
  72. from sympy.simplify.simplify import signsimp
  73. from sympy.utilities.iterables import iterable
  74. from sympy.utilities.exceptions import SymPyDeprecationWarning
  75. from sympy.testing.pytest import raises, warns_deprecated_sympy, warns
  76. from sympy.abc import a, b, c, d, p, q, t, w, x, y, z
  77. def _epsilon_eq(a, b):
  78. for u, v in zip(a, b):
  79. if abs(u - v) > 1e-10:
  80. return False
  81. return True
  82. def _strict_eq(a, b):
  83. if type(a) == type(b):
  84. if iterable(a):
  85. if len(a) == len(b):
  86. return all(_strict_eq(c, d) for c, d in zip(a, b))
  87. else:
  88. return False
  89. else:
  90. return isinstance(a, Poly) and a.eq(b, strict=True)
  91. else:
  92. return False
  93. def test_Poly_mixed_operations():
  94. p = Poly(x, x)
  95. with warns_deprecated_sympy():
  96. p * exp(x)
  97. with warns_deprecated_sympy():
  98. p + exp(x)
  99. with warns_deprecated_sympy():
  100. p - exp(x)
  101. def test_Poly_from_dict():
  102. K = FF(3)
  103. assert Poly.from_dict(
  104. {0: 1, 1: 2}, gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  105. assert Poly.from_dict(
  106. {0: 1, 1: 5}, gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  107. assert Poly.from_dict(
  108. {(0,): 1, (1,): 2}, gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  109. assert Poly.from_dict(
  110. {(0,): 1, (1,): 5}, gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  111. assert Poly.from_dict({(0, 0): 1, (1, 1): 2}, gens=(
  112. x, y), domain=K).rep == DMP([[K(2), K(0)], [K(1)]], K)
  113. assert Poly.from_dict({0: 1, 1: 2}, gens=x).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  114. assert Poly.from_dict(
  115. {0: 1, 1: 2}, gens=x, field=True).rep == DMP([QQ(2), QQ(1)], QQ)
  116. assert Poly.from_dict(
  117. {0: 1, 1: 2}, gens=x, domain=ZZ).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  118. assert Poly.from_dict(
  119. {0: 1, 1: 2}, gens=x, domain=QQ).rep == DMP([QQ(2), QQ(1)], QQ)
  120. assert Poly.from_dict(
  121. {(0,): 1, (1,): 2}, gens=x).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  122. assert Poly.from_dict(
  123. {(0,): 1, (1,): 2}, gens=x, field=True).rep == DMP([QQ(2), QQ(1)], QQ)
  124. assert Poly.from_dict(
  125. {(0,): 1, (1,): 2}, gens=x, domain=ZZ).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  126. assert Poly.from_dict(
  127. {(0,): 1, (1,): 2}, gens=x, domain=QQ).rep == DMP([QQ(2), QQ(1)], QQ)
  128. assert Poly.from_dict({(1,): sin(y)}, gens=x, composite=False) == \
  129. Poly(sin(y)*x, x, domain='EX')
  130. assert Poly.from_dict({(1,): y}, gens=x, composite=False) == \
  131. Poly(y*x, x, domain='EX')
  132. assert Poly.from_dict({(1, 1): 1}, gens=(x, y), composite=False) == \
  133. Poly(x*y, x, y, domain='ZZ')
  134. assert Poly.from_dict({(1, 0): y}, gens=(x, z), composite=False) == \
  135. Poly(y*x, x, z, domain='EX')
  136. def test_Poly_from_list():
  137. K = FF(3)
  138. assert Poly.from_list([2, 1], gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  139. assert Poly.from_list([5, 1], gens=x, domain=K).rep == DMP([K(2), K(1)], K)
  140. assert Poly.from_list([2, 1], gens=x).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  141. assert Poly.from_list([2, 1], gens=x, field=True).rep == DMP([QQ(2), QQ(1)], QQ)
  142. assert Poly.from_list([2, 1], gens=x, domain=ZZ).rep == DMP([ZZ(2), ZZ(1)], ZZ)
  143. assert Poly.from_list([2, 1], gens=x, domain=QQ).rep == DMP([QQ(2), QQ(1)], QQ)
  144. assert Poly.from_list([0, 1.0], gens=x).rep == DMP([RR(1.0)], RR)
  145. assert Poly.from_list([1.0, 0], gens=x).rep == DMP([RR(1.0), RR(0.0)], RR)
  146. raises(MultivariatePolynomialError, lambda: Poly.from_list([[]], gens=(x, y)))
  147. def test_Poly_from_poly():
  148. f = Poly(x + 7, x, domain=ZZ)
  149. g = Poly(x + 2, x, modulus=3)
  150. h = Poly(x + y, x, y, domain=ZZ)
  151. K = FF(3)
  152. assert Poly.from_poly(f) == f
  153. assert Poly.from_poly(f, domain=K).rep == DMP([K(1), K(1)], K)
  154. assert Poly.from_poly(f, domain=ZZ).rep == DMP([1, 7], ZZ)
  155. assert Poly.from_poly(f, domain=QQ).rep == DMP([1, 7], QQ)
  156. assert Poly.from_poly(f, gens=x) == f
  157. assert Poly.from_poly(f, gens=x, domain=K).rep == DMP([K(1), K(1)], K)
  158. assert Poly.from_poly(f, gens=x, domain=ZZ).rep == DMP([1, 7], ZZ)
  159. assert Poly.from_poly(f, gens=x, domain=QQ).rep == DMP([1, 7], QQ)
  160. assert Poly.from_poly(f, gens=y) == Poly(x + 7, y, domain='ZZ[x]')
  161. raises(CoercionFailed, lambda: Poly.from_poly(f, gens=y, domain=K))
  162. raises(CoercionFailed, lambda: Poly.from_poly(f, gens=y, domain=ZZ))
  163. raises(CoercionFailed, lambda: Poly.from_poly(f, gens=y, domain=QQ))
  164. assert Poly.from_poly(f, gens=(x, y)) == Poly(x + 7, x, y, domain='ZZ')
  165. assert Poly.from_poly(
  166. f, gens=(x, y), domain=ZZ) == Poly(x + 7, x, y, domain='ZZ')
  167. assert Poly.from_poly(
  168. f, gens=(x, y), domain=QQ) == Poly(x + 7, x, y, domain='QQ')
  169. assert Poly.from_poly(
  170. f, gens=(x, y), modulus=3) == Poly(x + 7, x, y, domain='FF(3)')
  171. K = FF(2)
  172. assert Poly.from_poly(g) == g
  173. assert Poly.from_poly(g, domain=ZZ).rep == DMP([1, -1], ZZ)
  174. raises(CoercionFailed, lambda: Poly.from_poly(g, domain=QQ))
  175. assert Poly.from_poly(g, domain=K).rep == DMP([K(1), K(0)], K)
  176. assert Poly.from_poly(g, gens=x) == g
  177. assert Poly.from_poly(g, gens=x, domain=ZZ).rep == DMP([1, -1], ZZ)
  178. raises(CoercionFailed, lambda: Poly.from_poly(g, gens=x, domain=QQ))
  179. assert Poly.from_poly(g, gens=x, domain=K).rep == DMP([K(1), K(0)], K)
  180. K = FF(3)
  181. assert Poly.from_poly(h) == h
  182. assert Poly.from_poly(
  183. h, domain=ZZ).rep == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
  184. assert Poly.from_poly(
  185. h, domain=QQ).rep == DMP([[QQ(1)], [QQ(1), QQ(0)]], QQ)
  186. assert Poly.from_poly(h, domain=K).rep == DMP([[K(1)], [K(1), K(0)]], K)
  187. assert Poly.from_poly(h, gens=x) == Poly(x + y, x, domain=ZZ[y])
  188. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=x, domain=ZZ))
  189. assert Poly.from_poly(
  190. h, gens=x, domain=ZZ[y]) == Poly(x + y, x, domain=ZZ[y])
  191. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=x, domain=QQ))
  192. assert Poly.from_poly(
  193. h, gens=x, domain=QQ[y]) == Poly(x + y, x, domain=QQ[y])
  194. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=x, modulus=3))
  195. assert Poly.from_poly(h, gens=y) == Poly(x + y, y, domain=ZZ[x])
  196. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=y, domain=ZZ))
  197. assert Poly.from_poly(
  198. h, gens=y, domain=ZZ[x]) == Poly(x + y, y, domain=ZZ[x])
  199. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=y, domain=QQ))
  200. assert Poly.from_poly(
  201. h, gens=y, domain=QQ[x]) == Poly(x + y, y, domain=QQ[x])
  202. raises(CoercionFailed, lambda: Poly.from_poly(h, gens=y, modulus=3))
  203. assert Poly.from_poly(h, gens=(x, y)) == h
  204. assert Poly.from_poly(
  205. h, gens=(x, y), domain=ZZ).rep == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
  206. assert Poly.from_poly(
  207. h, gens=(x, y), domain=QQ).rep == DMP([[QQ(1)], [QQ(1), QQ(0)]], QQ)
  208. assert Poly.from_poly(
  209. h, gens=(x, y), domain=K).rep == DMP([[K(1)], [K(1), K(0)]], K)
  210. assert Poly.from_poly(
  211. h, gens=(y, x)).rep == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
  212. assert Poly.from_poly(
  213. h, gens=(y, x), domain=ZZ).rep == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
  214. assert Poly.from_poly(
  215. h, gens=(y, x), domain=QQ).rep == DMP([[QQ(1)], [QQ(1), QQ(0)]], QQ)
  216. assert Poly.from_poly(
  217. h, gens=(y, x), domain=K).rep == DMP([[K(1)], [K(1), K(0)]], K)
  218. assert Poly.from_poly(
  219. h, gens=(x, y), field=True).rep == DMP([[QQ(1)], [QQ(1), QQ(0)]], QQ)
  220. assert Poly.from_poly(
  221. h, gens=(x, y), field=True).rep == DMP([[QQ(1)], [QQ(1), QQ(0)]], QQ)
  222. def test_Poly_from_expr():
  223. raises(GeneratorsNeeded, lambda: Poly.from_expr(S.Zero))
  224. raises(GeneratorsNeeded, lambda: Poly.from_expr(S(7)))
  225. F3 = FF(3)
  226. assert Poly.from_expr(x + 5, domain=F3).rep == DMP([F3(1), F3(2)], F3)
  227. assert Poly.from_expr(y + 5, domain=F3).rep == DMP([F3(1), F3(2)], F3)
  228. assert Poly.from_expr(x + 5, x, domain=F3).rep == DMP([F3(1), F3(2)], F3)
  229. assert Poly.from_expr(y + 5, y, domain=F3).rep == DMP([F3(1), F3(2)], F3)
  230. assert Poly.from_expr(x + y, domain=F3).rep == DMP([[F3(1)], [F3(1), F3(0)]], F3)
  231. assert Poly.from_expr(x + y, x, y, domain=F3).rep == DMP([[F3(1)], [F3(1), F3(0)]], F3)
  232. assert Poly.from_expr(x + 5).rep == DMP([1, 5], ZZ)
  233. assert Poly.from_expr(y + 5).rep == DMP([1, 5], ZZ)
  234. assert Poly.from_expr(x + 5, x).rep == DMP([1, 5], ZZ)
  235. assert Poly.from_expr(y + 5, y).rep == DMP([1, 5], ZZ)
  236. assert Poly.from_expr(x + 5, domain=ZZ).rep == DMP([1, 5], ZZ)
  237. assert Poly.from_expr(y + 5, domain=ZZ).rep == DMP([1, 5], ZZ)
  238. assert Poly.from_expr(x + 5, x, domain=ZZ).rep == DMP([1, 5], ZZ)
  239. assert Poly.from_expr(y + 5, y, domain=ZZ).rep == DMP([1, 5], ZZ)
  240. assert Poly.from_expr(x + 5, x, y, domain=ZZ).rep == DMP([[1], [5]], ZZ)
  241. assert Poly.from_expr(y + 5, x, y, domain=ZZ).rep == DMP([[1, 5]], ZZ)
  242. def test_poly_from_domain_element():
  243. dom = ZZ[x]
  244. assert Poly(dom(x+1), y, domain=dom).rep == DMP([dom(x+1)], dom)
  245. dom = dom.get_field()
  246. assert Poly(dom(x+1), y, domain=dom).rep == DMP([dom(x+1)], dom)
  247. dom = QQ[x]
  248. assert Poly(dom(x+1), y, domain=dom).rep == DMP([dom(x+1)], dom)
  249. dom = dom.get_field()
  250. assert Poly(dom(x+1), y, domain=dom).rep == DMP([dom(x+1)], dom)
  251. dom = ZZ.old_poly_ring(x)
  252. assert Poly(dom([1, 1]), y, domain=dom).rep == DMP([dom([1, 1])], dom)
  253. dom = dom.get_field()
  254. assert Poly(dom([1, 1]), y, domain=dom).rep == DMP([dom([1, 1])], dom)
  255. dom = QQ.old_poly_ring(x)
  256. assert Poly(dom([1, 1]), y, domain=dom).rep == DMP([dom([1, 1])], dom)
  257. dom = dom.get_field()
  258. assert Poly(dom([1, 1]), y, domain=dom).rep == DMP([dom([1, 1])], dom)
  259. dom = QQ.algebraic_field(I)
  260. assert Poly(dom([1, 1]), x, domain=dom).rep == DMP([dom([1, 1])], dom)
  261. def test_Poly__new__():
  262. raises(GeneratorsError, lambda: Poly(x + 1, x, x))
  263. raises(GeneratorsError, lambda: Poly(x + y, x, y, domain=ZZ[x]))
  264. raises(GeneratorsError, lambda: Poly(x + y, x, y, domain=ZZ[y]))
  265. raises(OptionError, lambda: Poly(x, x, symmetric=True))
  266. raises(OptionError, lambda: Poly(x + 2, x, modulus=3, domain=QQ))
  267. raises(OptionError, lambda: Poly(x + 2, x, domain=ZZ, gaussian=True))
  268. raises(OptionError, lambda: Poly(x + 2, x, modulus=3, gaussian=True))
  269. raises(OptionError, lambda: Poly(x + 2, x, domain=ZZ, extension=[sqrt(3)]))
  270. raises(OptionError, lambda: Poly(x + 2, x, modulus=3, extension=[sqrt(3)]))
  271. raises(OptionError, lambda: Poly(x + 2, x, domain=ZZ, extension=True))
  272. raises(OptionError, lambda: Poly(x + 2, x, modulus=3, extension=True))
  273. raises(OptionError, lambda: Poly(x + 2, x, domain=ZZ, greedy=True))
  274. raises(OptionError, lambda: Poly(x + 2, x, domain=QQ, field=True))
  275. raises(OptionError, lambda: Poly(x + 2, x, domain=ZZ, greedy=False))
  276. raises(OptionError, lambda: Poly(x + 2, x, domain=QQ, field=False))
  277. raises(NotImplementedError, lambda: Poly(x + 1, x, modulus=3, order='grlex'))
  278. raises(NotImplementedError, lambda: Poly(x + 1, x, order='grlex'))
  279. raises(GeneratorsNeeded, lambda: Poly({1: 2, 0: 1}))
  280. raises(GeneratorsNeeded, lambda: Poly([2, 1]))
  281. raises(GeneratorsNeeded, lambda: Poly((2, 1)))
  282. raises(GeneratorsNeeded, lambda: Poly(1))
  283. f = a*x**2 + b*x + c
  284. assert Poly({2: a, 1: b, 0: c}, x) == f
  285. assert Poly(iter([a, b, c]), x) == f
  286. assert Poly([a, b, c], x) == f
  287. assert Poly((a, b, c), x) == f
  288. f = Poly({}, x, y, z)
  289. assert f.gens == (x, y, z) and f.as_expr() == 0
  290. assert Poly(Poly(a*x + b*y, x, y), x) == Poly(a*x + b*y, x)
  291. assert Poly(3*x**2 + 2*x + 1, domain='ZZ').all_coeffs() == [3, 2, 1]
  292. assert Poly(3*x**2 + 2*x + 1, domain='QQ').all_coeffs() == [3, 2, 1]
  293. assert Poly(3*x**2 + 2*x + 1, domain='RR').all_coeffs() == [3.0, 2.0, 1.0]
  294. raises(CoercionFailed, lambda: Poly(3*x**2/5 + x*Rational(2, 5) + 1, domain='ZZ'))
  295. assert Poly(
  296. 3*x**2/5 + x*Rational(2, 5) + 1, domain='QQ').all_coeffs() == [Rational(3, 5), Rational(2, 5), 1]
  297. assert _epsilon_eq(
  298. Poly(3*x**2/5 + x*Rational(2, 5) + 1, domain='RR').all_coeffs(), [0.6, 0.4, 1.0])
  299. assert Poly(3.0*x**2 + 2.0*x + 1, domain='ZZ').all_coeffs() == [3, 2, 1]
  300. assert Poly(3.0*x**2 + 2.0*x + 1, domain='QQ').all_coeffs() == [3, 2, 1]
  301. assert Poly(
  302. 3.0*x**2 + 2.0*x + 1, domain='RR').all_coeffs() == [3.0, 2.0, 1.0]
  303. raises(CoercionFailed, lambda: Poly(3.1*x**2 + 2.1*x + 1, domain='ZZ'))
  304. assert Poly(3.1*x**2 + 2.1*x + 1, domain='QQ').all_coeffs() == [Rational(31, 10), Rational(21, 10), 1]
  305. assert Poly(3.1*x**2 + 2.1*x + 1, domain='RR').all_coeffs() == [3.1, 2.1, 1.0]
  306. assert Poly({(2, 1): 1, (1, 2): 2, (1, 1): 3}, x, y) == \
  307. Poly(x**2*y + 2*x*y**2 + 3*x*y, x, y)
  308. assert Poly(x**2 + 1, extension=I).get_domain() == QQ.algebraic_field(I)
  309. f = 3*x**5 - x**4 + x**3 - x** 2 + 65538
  310. assert Poly(f, x, modulus=65537, symmetric=True) == \
  311. Poly(3*x**5 - x**4 + x**3 - x** 2 + 1, x, modulus=65537,
  312. symmetric=True)
  313. assert Poly(f, x, modulus=65537, symmetric=False) == \
  314. Poly(3*x**5 + 65536*x**4 + x**3 + 65536*x** 2 + 1, x,
  315. modulus=65537, symmetric=False)
  316. assert isinstance(Poly(x**2 + x + 1.0).get_domain(), RealField)
  317. assert isinstance(Poly(x**2 + x + I + 1.0).get_domain(), ComplexField)
  318. def test_Poly__args():
  319. assert Poly(x**2 + 1).args == (x**2 + 1, x)
  320. def test_Poly__gens():
  321. assert Poly((x - p)*(x - q), x).gens == (x,)
  322. assert Poly((x - p)*(x - q), p).gens == (p,)
  323. assert Poly((x - p)*(x - q), q).gens == (q,)
  324. assert Poly((x - p)*(x - q), x, p).gens == (x, p)
  325. assert Poly((x - p)*(x - q), x, q).gens == (x, q)
  326. assert Poly((x - p)*(x - q), x, p, q).gens == (x, p, q)
  327. assert Poly((x - p)*(x - q), p, x, q).gens == (p, x, q)
  328. assert Poly((x - p)*(x - q), p, q, x).gens == (p, q, x)
  329. assert Poly((x - p)*(x - q)).gens == (x, p, q)
  330. assert Poly((x - p)*(x - q), sort='x > p > q').gens == (x, p, q)
  331. assert Poly((x - p)*(x - q), sort='p > x > q').gens == (p, x, q)
  332. assert Poly((x - p)*(x - q), sort='p > q > x').gens == (p, q, x)
  333. assert Poly((x - p)*(x - q), x, p, q, sort='p > q > x').gens == (x, p, q)
  334. assert Poly((x - p)*(x - q), wrt='x').gens == (x, p, q)
  335. assert Poly((x - p)*(x - q), wrt='p').gens == (p, x, q)
  336. assert Poly((x - p)*(x - q), wrt='q').gens == (q, x, p)
  337. assert Poly((x - p)*(x - q), wrt=x).gens == (x, p, q)
  338. assert Poly((x - p)*(x - q), wrt=p).gens == (p, x, q)
  339. assert Poly((x - p)*(x - q), wrt=q).gens == (q, x, p)
  340. assert Poly((x - p)*(x - q), x, p, q, wrt='p').gens == (x, p, q)
  341. assert Poly((x - p)*(x - q), wrt='p', sort='q > x').gens == (p, q, x)
  342. assert Poly((x - p)*(x - q), wrt='q', sort='p > x').gens == (q, p, x)
  343. def test_Poly_zero():
  344. assert Poly(x).zero == Poly(0, x, domain=ZZ)
  345. assert Poly(x/2).zero == Poly(0, x, domain=QQ)
  346. def test_Poly_one():
  347. assert Poly(x).one == Poly(1, x, domain=ZZ)
  348. assert Poly(x/2).one == Poly(1, x, domain=QQ)
  349. def test_Poly__unify():
  350. raises(UnificationFailed, lambda: Poly(x)._unify(y))
  351. F3 = FF(3)
  352. F5 = FF(5)
  353. assert Poly(x, x, modulus=3)._unify(Poly(y, y, modulus=3))[2:] == (
  354. DMP([[F3(1)], []], F3), DMP([[F3(1), F3(0)]], F3))
  355. assert Poly(x, x, modulus=3)._unify(Poly(y, y, modulus=5))[2:] == (
  356. DMP([[F5(1)], []], F5), DMP([[F5(1), F5(0)]], F5))
  357. assert Poly(y, x, y)._unify(Poly(x, x, modulus=3))[2:] == (DMP([[F3(1), F3(0)]], F3), DMP([[F3(1)], []], F3))
  358. assert Poly(x, x, modulus=3)._unify(Poly(y, x, y))[2:] == (DMP([[F3(1)], []], F3), DMP([[F3(1), F3(0)]], F3))
  359. assert Poly(x + 1, x)._unify(Poly(x + 2, x))[2:] == (DMP([1, 1], ZZ), DMP([1, 2], ZZ))
  360. assert Poly(x + 1, x, domain='QQ')._unify(Poly(x + 2, x))[2:] == (DMP([1, 1], QQ), DMP([1, 2], QQ))
  361. assert Poly(x + 1, x)._unify(Poly(x + 2, x, domain='QQ'))[2:] == (DMP([1, 1], QQ), DMP([1, 2], QQ))
  362. assert Poly(x + 1, x)._unify(Poly(x + 2, x, y))[2:] == (DMP([[1], [1]], ZZ), DMP([[1], [2]], ZZ))
  363. assert Poly(x + 1, x, domain='QQ')._unify(Poly(x + 2, x, y))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  364. assert Poly(x + 1, x)._unify(Poly(x + 2, x, y, domain='QQ'))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  365. assert Poly(x + 1, x, y)._unify(Poly(x + 2, x))[2:] == (DMP([[1], [1]], ZZ), DMP([[1], [2]], ZZ))
  366. assert Poly(x + 1, x, y, domain='QQ')._unify(Poly(x + 2, x))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  367. assert Poly(x + 1, x, y)._unify(Poly(x + 2, x, domain='QQ'))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  368. assert Poly(x + 1, x, y)._unify(Poly(x + 2, x, y))[2:] == (DMP([[1], [1]], ZZ), DMP([[1], [2]], ZZ))
  369. assert Poly(x + 1, x, y, domain='QQ')._unify(Poly(x + 2, x, y))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  370. assert Poly(x + 1, x, y)._unify(Poly(x + 2, x, y, domain='QQ'))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  371. assert Poly(x + 1, x)._unify(Poly(x + 2, y, x))[2:] == (DMP([[1, 1]], ZZ), DMP([[1, 2]], ZZ))
  372. assert Poly(x + 1, x, domain='QQ')._unify(Poly(x + 2, y, x))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  373. assert Poly(x + 1, x)._unify(Poly(x + 2, y, x, domain='QQ'))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  374. assert Poly(x + 1, y, x)._unify(Poly(x + 2, x))[2:] == (DMP([[1, 1]], ZZ), DMP([[1, 2]], ZZ))
  375. assert Poly(x + 1, y, x, domain='QQ')._unify(Poly(x + 2, x))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  376. assert Poly(x + 1, y, x)._unify(Poly(x + 2, x, domain='QQ'))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  377. assert Poly(x + 1, x, y)._unify(Poly(x + 2, y, x))[2:] == (DMP([[1], [1]], ZZ), DMP([[1], [2]], ZZ))
  378. assert Poly(x + 1, x, y, domain='QQ')._unify(Poly(x + 2, y, x))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  379. assert Poly(x + 1, x, y)._unify(Poly(x + 2, y, x, domain='QQ'))[2:] == (DMP([[1], [1]], QQ), DMP([[1], [2]], QQ))
  380. assert Poly(x + 1, y, x)._unify(Poly(x + 2, x, y))[2:] == (DMP([[1, 1]], ZZ), DMP([[1, 2]], ZZ))
  381. assert Poly(x + 1, y, x, domain='QQ')._unify(Poly(x + 2, x, y))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  382. assert Poly(x + 1, y, x)._unify(Poly(x + 2, x, y, domain='QQ'))[2:] == (DMP([[1, 1]], QQ), DMP([[1, 2]], QQ))
  383. assert Poly(x**2 + I, x, domain=ZZ_I).unify(Poly(x**2 + sqrt(2), x, extension=True)) == \
  384. (Poly(x**2 + I, x, domain='QQ<sqrt(2) + I>'), Poly(x**2 + sqrt(2), x, domain='QQ<sqrt(2) + I>'))
  385. F, A, B = field("a,b", ZZ)
  386. assert Poly(a*x, x, domain='ZZ[a]')._unify(Poly(a*b*x, x, domain='ZZ(a,b)'))[2:] == \
  387. (DMP([A, F(0)], F.to_domain()), DMP([A*B, F(0)], F.to_domain()))
  388. assert Poly(a*x, x, domain='ZZ(a)')._unify(Poly(a*b*x, x, domain='ZZ(a,b)'))[2:] == \
  389. (DMP([A, F(0)], F.to_domain()), DMP([A*B, F(0)], F.to_domain()))
  390. raises(CoercionFailed, lambda: Poly(Poly(x**2 + x**2*z, y, field=True), domain='ZZ(x)'))
  391. f = Poly(t**2 + t/3 + x, t, domain='QQ(x)')
  392. g = Poly(t**2 + t/3 + x, t, domain='QQ[x]')
  393. assert f._unify(g)[2:] == (f.rep, f.rep)
  394. def test_Poly_free_symbols():
  395. assert Poly(x**2 + 1).free_symbols == {x}
  396. assert Poly(x**2 + y*z).free_symbols == {x, y, z}
  397. assert Poly(x**2 + y*z, x).free_symbols == {x, y, z}
  398. assert Poly(x**2 + sin(y*z)).free_symbols == {x, y, z}
  399. assert Poly(x**2 + sin(y*z), x).free_symbols == {x, y, z}
  400. assert Poly(x**2 + sin(y*z), x, domain=EX).free_symbols == {x, y, z}
  401. assert Poly(1 + x + x**2, x, y, z).free_symbols == {x}
  402. assert Poly(x + sin(y), z).free_symbols == {x, y}
  403. def test_PurePoly_free_symbols():
  404. assert PurePoly(x**2 + 1).free_symbols == set()
  405. assert PurePoly(x**2 + y*z).free_symbols == set()
  406. assert PurePoly(x**2 + y*z, x).free_symbols == {y, z}
  407. assert PurePoly(x**2 + sin(y*z)).free_symbols == set()
  408. assert PurePoly(x**2 + sin(y*z), x).free_symbols == {y, z}
  409. assert PurePoly(x**2 + sin(y*z), x, domain=EX).free_symbols == {y, z}
  410. def test_Poly__eq__():
  411. assert (Poly(x, x) == Poly(x, x)) is True
  412. assert (Poly(x, x, domain=QQ) == Poly(x, x)) is False
  413. assert (Poly(x, x) == Poly(x, x, domain=QQ)) is False
  414. assert (Poly(x, x, domain=ZZ[a]) == Poly(x, x)) is False
  415. assert (Poly(x, x) == Poly(x, x, domain=ZZ[a])) is False
  416. assert (Poly(x*y, x, y) == Poly(x, x)) is False
  417. assert (Poly(x, x, y) == Poly(x, x)) is False
  418. assert (Poly(x, x) == Poly(x, x, y)) is False
  419. assert (Poly(x**2 + 1, x) == Poly(y**2 + 1, y)) is False
  420. assert (Poly(y**2 + 1, y) == Poly(x**2 + 1, x)) is False
  421. f = Poly(x, x, domain=ZZ)
  422. g = Poly(x, x, domain=QQ)
  423. assert f.eq(g) is False
  424. assert f.ne(g) is True
  425. assert f.eq(g, strict=True) is False
  426. assert f.ne(g, strict=True) is True
  427. t0 = Symbol('t0')
  428. f = Poly((t0/2 + x**2)*t**2 - x**2*t, t, domain='QQ[x,t0]')
  429. g = Poly((t0/2 + x**2)*t**2 - x**2*t, t, domain='ZZ(x,t0)')
  430. assert (f == g) is False
  431. def test_PurePoly__eq__():
  432. assert (PurePoly(x, x) == PurePoly(x, x)) is True
  433. assert (PurePoly(x, x, domain=QQ) == PurePoly(x, x)) is True
  434. assert (PurePoly(x, x) == PurePoly(x, x, domain=QQ)) is True
  435. assert (PurePoly(x, x, domain=ZZ[a]) == PurePoly(x, x)) is True
  436. assert (PurePoly(x, x) == PurePoly(x, x, domain=ZZ[a])) is True
  437. assert (PurePoly(x*y, x, y) == PurePoly(x, x)) is False
  438. assert (PurePoly(x, x, y) == PurePoly(x, x)) is False
  439. assert (PurePoly(x, x) == PurePoly(x, x, y)) is False
  440. assert (PurePoly(x**2 + 1, x) == PurePoly(y**2 + 1, y)) is True
  441. assert (PurePoly(y**2 + 1, y) == PurePoly(x**2 + 1, x)) is True
  442. f = PurePoly(x, x, domain=ZZ)
  443. g = PurePoly(x, x, domain=QQ)
  444. assert f.eq(g) is True
  445. assert f.ne(g) is False
  446. assert f.eq(g, strict=True) is False
  447. assert f.ne(g, strict=True) is True
  448. f = PurePoly(x, x, domain=ZZ)
  449. g = PurePoly(y, y, domain=QQ)
  450. assert f.eq(g) is True
  451. assert f.ne(g) is False
  452. assert f.eq(g, strict=True) is False
  453. assert f.ne(g, strict=True) is True
  454. def test_PurePoly_Poly():
  455. assert isinstance(PurePoly(Poly(x**2 + 1)), PurePoly) is True
  456. assert isinstance(Poly(PurePoly(x**2 + 1)), Poly) is True
  457. def test_Poly_get_domain():
  458. assert Poly(2*x).get_domain() == ZZ
  459. assert Poly(2*x, domain='ZZ').get_domain() == ZZ
  460. assert Poly(2*x, domain='QQ').get_domain() == QQ
  461. assert Poly(x/2).get_domain() == QQ
  462. raises(CoercionFailed, lambda: Poly(x/2, domain='ZZ'))
  463. assert Poly(x/2, domain='QQ').get_domain() == QQ
  464. assert isinstance(Poly(0.2*x).get_domain(), RealField)
  465. def test_Poly_set_domain():
  466. assert Poly(2*x + 1).set_domain(ZZ) == Poly(2*x + 1)
  467. assert Poly(2*x + 1).set_domain('ZZ') == Poly(2*x + 1)
  468. assert Poly(2*x + 1).set_domain(QQ) == Poly(2*x + 1, domain='QQ')
  469. assert Poly(2*x + 1).set_domain('QQ') == Poly(2*x + 1, domain='QQ')
  470. assert Poly(Rational(2, 10)*x + Rational(1, 10)).set_domain('RR') == Poly(0.2*x + 0.1)
  471. assert Poly(0.2*x + 0.1).set_domain('QQ') == Poly(Rational(2, 10)*x + Rational(1, 10))
  472. raises(CoercionFailed, lambda: Poly(x/2 + 1).set_domain(ZZ))
  473. raises(CoercionFailed, lambda: Poly(x + 1, modulus=2).set_domain(QQ))
  474. raises(GeneratorsError, lambda: Poly(x*y, x, y).set_domain(ZZ[y]))
  475. def test_Poly_get_modulus():
  476. assert Poly(x**2 + 1, modulus=2).get_modulus() == 2
  477. raises(PolynomialError, lambda: Poly(x**2 + 1).get_modulus())
  478. def test_Poly_set_modulus():
  479. assert Poly(
  480. x**2 + 1, modulus=2).set_modulus(7) == Poly(x**2 + 1, modulus=7)
  481. assert Poly(
  482. x**2 + 5, modulus=7).set_modulus(2) == Poly(x**2 + 1, modulus=2)
  483. assert Poly(x**2 + 1).set_modulus(2) == Poly(x**2 + 1, modulus=2)
  484. raises(CoercionFailed, lambda: Poly(x/2 + 1).set_modulus(2))
  485. def test_Poly_add_ground():
  486. assert Poly(x + 1).add_ground(2) == Poly(x + 3)
  487. def test_Poly_sub_ground():
  488. assert Poly(x + 1).sub_ground(2) == Poly(x - 1)
  489. def test_Poly_mul_ground():
  490. assert Poly(x + 1).mul_ground(2) == Poly(2*x + 2)
  491. def test_Poly_quo_ground():
  492. assert Poly(2*x + 4).quo_ground(2) == Poly(x + 2)
  493. assert Poly(2*x + 3).quo_ground(2) == Poly(x + 1)
  494. def test_Poly_exquo_ground():
  495. assert Poly(2*x + 4).exquo_ground(2) == Poly(x + 2)
  496. raises(ExactQuotientFailed, lambda: Poly(2*x + 3).exquo_ground(2))
  497. def test_Poly_abs():
  498. assert Poly(-x + 1, x).abs() == abs(Poly(-x + 1, x)) == Poly(x + 1, x)
  499. def test_Poly_neg():
  500. assert Poly(-x + 1, x).neg() == -Poly(-x + 1, x) == Poly(x - 1, x)
  501. def test_Poly_add():
  502. assert Poly(0, x).add(Poly(0, x)) == Poly(0, x)
  503. assert Poly(0, x) + Poly(0, x) == Poly(0, x)
  504. assert Poly(1, x).add(Poly(0, x)) == Poly(1, x)
  505. assert Poly(1, x, y) + Poly(0, x) == Poly(1, x, y)
  506. assert Poly(0, x).add(Poly(1, x, y)) == Poly(1, x, y)
  507. assert Poly(0, x, y) + Poly(1, x, y) == Poly(1, x, y)
  508. assert Poly(1, x) + x == Poly(x + 1, x)
  509. with warns_deprecated_sympy():
  510. Poly(1, x) + sin(x)
  511. assert Poly(x, x) + 1 == Poly(x + 1, x)
  512. assert 1 + Poly(x, x) == Poly(x + 1, x)
  513. def test_Poly_sub():
  514. assert Poly(0, x).sub(Poly(0, x)) == Poly(0, x)
  515. assert Poly(0, x) - Poly(0, x) == Poly(0, x)
  516. assert Poly(1, x).sub(Poly(0, x)) == Poly(1, x)
  517. assert Poly(1, x, y) - Poly(0, x) == Poly(1, x, y)
  518. assert Poly(0, x).sub(Poly(1, x, y)) == Poly(-1, x, y)
  519. assert Poly(0, x, y) - Poly(1, x, y) == Poly(-1, x, y)
  520. assert Poly(1, x) - x == Poly(1 - x, x)
  521. with warns_deprecated_sympy():
  522. Poly(1, x) - sin(x)
  523. assert Poly(x, x) - 1 == Poly(x - 1, x)
  524. assert 1 - Poly(x, x) == Poly(1 - x, x)
  525. def test_Poly_mul():
  526. assert Poly(0, x).mul(Poly(0, x)) == Poly(0, x)
  527. assert Poly(0, x) * Poly(0, x) == Poly(0, x)
  528. assert Poly(2, x).mul(Poly(4, x)) == Poly(8, x)
  529. assert Poly(2, x, y) * Poly(4, x) == Poly(8, x, y)
  530. assert Poly(4, x).mul(Poly(2, x, y)) == Poly(8, x, y)
  531. assert Poly(4, x, y) * Poly(2, x, y) == Poly(8, x, y)
  532. assert Poly(1, x) * x == Poly(x, x)
  533. with warns_deprecated_sympy():
  534. Poly(1, x) * sin(x)
  535. assert Poly(x, x) * 2 == Poly(2*x, x)
  536. assert 2 * Poly(x, x) == Poly(2*x, x)
  537. def test_issue_13079():
  538. assert Poly(x)*x == Poly(x**2, x, domain='ZZ')
  539. assert x*Poly(x) == Poly(x**2, x, domain='ZZ')
  540. assert -2*Poly(x) == Poly(-2*x, x, domain='ZZ')
  541. assert S(-2)*Poly(x) == Poly(-2*x, x, domain='ZZ')
  542. assert Poly(x)*S(-2) == Poly(-2*x, x, domain='ZZ')
  543. def test_Poly_sqr():
  544. assert Poly(x*y, x, y).sqr() == Poly(x**2*y**2, x, y)
  545. def test_Poly_pow():
  546. assert Poly(x, x).pow(10) == Poly(x**10, x)
  547. assert Poly(x, x).pow(Integer(10)) == Poly(x**10, x)
  548. assert Poly(2*y, x, y).pow(4) == Poly(16*y**4, x, y)
  549. assert Poly(2*y, x, y).pow(Integer(4)) == Poly(16*y**4, x, y)
  550. assert Poly(7*x*y, x, y)**3 == Poly(343*x**3*y**3, x, y)
  551. raises(TypeError, lambda: Poly(x*y + 1, x, y)**(-1))
  552. raises(TypeError, lambda: Poly(x*y + 1, x, y)**x)
  553. def test_Poly_divmod():
  554. f, g = Poly(x**2), Poly(x)
  555. q, r = g, Poly(0, x)
  556. assert divmod(f, g) == (q, r)
  557. assert f // g == q
  558. assert f % g == r
  559. assert divmod(f, x) == (q, r)
  560. assert f // x == q
  561. assert f % x == r
  562. q, r = Poly(0, x), Poly(2, x)
  563. assert divmod(2, g) == (q, r)
  564. assert 2 // g == q
  565. assert 2 % g == r
  566. assert Poly(x)/Poly(x) == 1
  567. assert Poly(x**2)/Poly(x) == x
  568. assert Poly(x)/Poly(x**2) == 1/x
  569. def test_Poly_eq_ne():
  570. assert (Poly(x + y, x, y) == Poly(x + y, x, y)) is True
  571. assert (Poly(x + y, x) == Poly(x + y, x, y)) is False
  572. assert (Poly(x + y, x, y) == Poly(x + y, x)) is False
  573. assert (Poly(x + y, x) == Poly(x + y, x)) is True
  574. assert (Poly(x + y, y) == Poly(x + y, y)) is True
  575. assert (Poly(x + y, x, y) == x + y) is True
  576. assert (Poly(x + y, x) == x + y) is True
  577. assert (Poly(x + y, x, y) == x + y) is True
  578. assert (Poly(x + y, x) == x + y) is True
  579. assert (Poly(x + y, y) == x + y) is True
  580. assert (Poly(x + y, x, y) != Poly(x + y, x, y)) is False
  581. assert (Poly(x + y, x) != Poly(x + y, x, y)) is True
  582. assert (Poly(x + y, x, y) != Poly(x + y, x)) is True
  583. assert (Poly(x + y, x) != Poly(x + y, x)) is False
  584. assert (Poly(x + y, y) != Poly(x + y, y)) is False
  585. assert (Poly(x + y, x, y) != x + y) is False
  586. assert (Poly(x + y, x) != x + y) is False
  587. assert (Poly(x + y, x, y) != x + y) is False
  588. assert (Poly(x + y, x) != x + y) is False
  589. assert (Poly(x + y, y) != x + y) is False
  590. assert (Poly(x, x) == sin(x)) is False
  591. assert (Poly(x, x) != sin(x)) is True
  592. def test_Poly_nonzero():
  593. assert not bool(Poly(0, x)) is True
  594. assert not bool(Poly(1, x)) is False
  595. def test_Poly_properties():
  596. assert Poly(0, x).is_zero is True
  597. assert Poly(1, x).is_zero is False
  598. assert Poly(1, x).is_one is True
  599. assert Poly(2, x).is_one is False
  600. assert Poly(x - 1, x).is_sqf is True
  601. assert Poly((x - 1)**2, x).is_sqf is False
  602. assert Poly(x - 1, x).is_monic is True
  603. assert Poly(2*x - 1, x).is_monic is False
  604. assert Poly(3*x + 2, x).is_primitive is True
  605. assert Poly(4*x + 2, x).is_primitive is False
  606. assert Poly(1, x).is_ground is True
  607. assert Poly(x, x).is_ground is False
  608. assert Poly(x + y + z + 1).is_linear is True
  609. assert Poly(x*y*z + 1).is_linear is False
  610. assert Poly(x*y + z + 1).is_quadratic is True
  611. assert Poly(x*y*z + 1).is_quadratic is False
  612. assert Poly(x*y).is_monomial is True
  613. assert Poly(x*y + 1).is_monomial is False
  614. assert Poly(x**2 + x*y).is_homogeneous is True
  615. assert Poly(x**3 + x*y).is_homogeneous is False
  616. assert Poly(x).is_univariate is True
  617. assert Poly(x*y).is_univariate is False
  618. assert Poly(x*y).is_multivariate is True
  619. assert Poly(x).is_multivariate is False
  620. assert Poly(
  621. x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1).is_cyclotomic is False
  622. assert Poly(
  623. x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1).is_cyclotomic is True
  624. def test_Poly_is_irreducible():
  625. assert Poly(x**2 + x + 1).is_irreducible is True
  626. assert Poly(x**2 + 2*x + 1).is_irreducible is False
  627. assert Poly(7*x + 3, modulus=11).is_irreducible is True
  628. assert Poly(7*x**2 + 3*x + 1, modulus=11).is_irreducible is False
  629. def test_Poly_subs():
  630. assert Poly(x + 1).subs(x, 0) == 1
  631. assert Poly(x + 1).subs(x, x) == Poly(x + 1)
  632. assert Poly(x + 1).subs(x, y) == Poly(y + 1)
  633. assert Poly(x*y, x).subs(y, x) == x**2
  634. assert Poly(x*y, x).subs(x, y) == y**2
  635. def test_Poly_replace():
  636. assert Poly(x + 1).replace(x) == Poly(x + 1)
  637. assert Poly(x + 1).replace(y) == Poly(y + 1)
  638. raises(PolynomialError, lambda: Poly(x + y).replace(z))
  639. assert Poly(x + 1).replace(x, x) == Poly(x + 1)
  640. assert Poly(x + 1).replace(x, y) == Poly(y + 1)
  641. assert Poly(x + y).replace(x, x) == Poly(x + y)
  642. assert Poly(x + y).replace(x, z) == Poly(z + y, z, y)
  643. assert Poly(x + y).replace(y, y) == Poly(x + y)
  644. assert Poly(x + y).replace(y, z) == Poly(x + z, x, z)
  645. assert Poly(x + y).replace(z, t) == Poly(x + y)
  646. raises(PolynomialError, lambda: Poly(x + y).replace(x, y))
  647. assert Poly(x + y, x).replace(x, z) == Poly(z + y, z)
  648. assert Poly(x + y, y).replace(y, z) == Poly(x + z, z)
  649. raises(PolynomialError, lambda: Poly(x + y, x).replace(x, y))
  650. raises(PolynomialError, lambda: Poly(x + y, y).replace(y, x))
  651. def test_Poly_reorder():
  652. raises(PolynomialError, lambda: Poly(x + y).reorder(x, z))
  653. assert Poly(x + y, x, y).reorder(x, y) == Poly(x + y, x, y)
  654. assert Poly(x + y, x, y).reorder(y, x) == Poly(x + y, y, x)
  655. assert Poly(x + y, y, x).reorder(x, y) == Poly(x + y, x, y)
  656. assert Poly(x + y, y, x).reorder(y, x) == Poly(x + y, y, x)
  657. assert Poly(x + y, x, y).reorder(wrt=x) == Poly(x + y, x, y)
  658. assert Poly(x + y, x, y).reorder(wrt=y) == Poly(x + y, y, x)
  659. def test_Poly_ltrim():
  660. f = Poly(y**2 + y*z**2, x, y, z).ltrim(y)
  661. assert f.as_expr() == y**2 + y*z**2 and f.gens == (y, z)
  662. assert Poly(x*y - x, z, x, y).ltrim(1) == Poly(x*y - x, x, y)
  663. raises(PolynomialError, lambda: Poly(x*y**2 + y**2, x, y).ltrim(y))
  664. raises(PolynomialError, lambda: Poly(x*y - x, x, y).ltrim(-1))
  665. def test_Poly_has_only_gens():
  666. assert Poly(x*y + 1, x, y, z).has_only_gens(x, y) is True
  667. assert Poly(x*y + z, x, y, z).has_only_gens(x, y) is False
  668. raises(GeneratorsError, lambda: Poly(x*y**2 + y**2, x, y).has_only_gens(t))
  669. def test_Poly_to_ring():
  670. assert Poly(2*x + 1, domain='ZZ').to_ring() == Poly(2*x + 1, domain='ZZ')
  671. assert Poly(2*x + 1, domain='QQ').to_ring() == Poly(2*x + 1, domain='ZZ')
  672. raises(CoercionFailed, lambda: Poly(x/2 + 1).to_ring())
  673. raises(DomainError, lambda: Poly(2*x + 1, modulus=3).to_ring())
  674. def test_Poly_to_field():
  675. assert Poly(2*x + 1, domain='ZZ').to_field() == Poly(2*x + 1, domain='QQ')
  676. assert Poly(2*x + 1, domain='QQ').to_field() == Poly(2*x + 1, domain='QQ')
  677. assert Poly(x/2 + 1, domain='QQ').to_field() == Poly(x/2 + 1, domain='QQ')
  678. assert Poly(2*x + 1, modulus=3).to_field() == Poly(2*x + 1, modulus=3)
  679. assert Poly(2.0*x + 1.0).to_field() == Poly(2.0*x + 1.0)
  680. def test_Poly_to_exact():
  681. assert Poly(2*x).to_exact() == Poly(2*x)
  682. assert Poly(x/2).to_exact() == Poly(x/2)
  683. assert Poly(0.1*x).to_exact() == Poly(x/10)
  684. def test_Poly_retract():
  685. f = Poly(x**2 + 1, x, domain=QQ[y])
  686. assert f.retract() == Poly(x**2 + 1, x, domain='ZZ')
  687. assert f.retract(field=True) == Poly(x**2 + 1, x, domain='QQ')
  688. assert Poly(0, x, y).retract() == Poly(0, x, y)
  689. def test_Poly_slice():
  690. f = Poly(x**3 + 2*x**2 + 3*x + 4)
  691. assert f.slice(0, 0) == Poly(0, x)
  692. assert f.slice(0, 1) == Poly(4, x)
  693. assert f.slice(0, 2) == Poly(3*x + 4, x)
  694. assert f.slice(0, 3) == Poly(2*x**2 + 3*x + 4, x)
  695. assert f.slice(0, 4) == Poly(x**3 + 2*x**2 + 3*x + 4, x)
  696. assert f.slice(x, 0, 0) == Poly(0, x)
  697. assert f.slice(x, 0, 1) == Poly(4, x)
  698. assert f.slice(x, 0, 2) == Poly(3*x + 4, x)
  699. assert f.slice(x, 0, 3) == Poly(2*x**2 + 3*x + 4, x)
  700. assert f.slice(x, 0, 4) == Poly(x**3 + 2*x**2 + 3*x + 4, x)
  701. def test_Poly_coeffs():
  702. assert Poly(0, x).coeffs() == [0]
  703. assert Poly(1, x).coeffs() == [1]
  704. assert Poly(2*x + 1, x).coeffs() == [2, 1]
  705. assert Poly(7*x**2 + 2*x + 1, x).coeffs() == [7, 2, 1]
  706. assert Poly(7*x**4 + 2*x + 1, x).coeffs() == [7, 2, 1]
  707. assert Poly(x*y**7 + 2*x**2*y**3).coeffs('lex') == [2, 1]
  708. assert Poly(x*y**7 + 2*x**2*y**3).coeffs('grlex') == [1, 2]
  709. def test_Poly_monoms():
  710. assert Poly(0, x).monoms() == [(0,)]
  711. assert Poly(1, x).monoms() == [(0,)]
  712. assert Poly(2*x + 1, x).monoms() == [(1,), (0,)]
  713. assert Poly(7*x**2 + 2*x + 1, x).monoms() == [(2,), (1,), (0,)]
  714. assert Poly(7*x**4 + 2*x + 1, x).monoms() == [(4,), (1,), (0,)]
  715. assert Poly(x*y**7 + 2*x**2*y**3).monoms('lex') == [(2, 3), (1, 7)]
  716. assert Poly(x*y**7 + 2*x**2*y**3).monoms('grlex') == [(1, 7), (2, 3)]
  717. def test_Poly_terms():
  718. assert Poly(0, x).terms() == [((0,), 0)]
  719. assert Poly(1, x).terms() == [((0,), 1)]
  720. assert Poly(2*x + 1, x).terms() == [((1,), 2), ((0,), 1)]
  721. assert Poly(7*x**2 + 2*x + 1, x).terms() == [((2,), 7), ((1,), 2), ((0,), 1)]
  722. assert Poly(7*x**4 + 2*x + 1, x).terms() == [((4,), 7), ((1,), 2), ((0,), 1)]
  723. assert Poly(
  724. x*y**7 + 2*x**2*y**3).terms('lex') == [((2, 3), 2), ((1, 7), 1)]
  725. assert Poly(
  726. x*y**7 + 2*x**2*y**3).terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
  727. def test_Poly_all_coeffs():
  728. assert Poly(0, x).all_coeffs() == [0]
  729. assert Poly(1, x).all_coeffs() == [1]
  730. assert Poly(2*x + 1, x).all_coeffs() == [2, 1]
  731. assert Poly(7*x**2 + 2*x + 1, x).all_coeffs() == [7, 2, 1]
  732. assert Poly(7*x**4 + 2*x + 1, x).all_coeffs() == [7, 0, 0, 2, 1]
  733. def test_Poly_all_monoms():
  734. assert Poly(0, x).all_monoms() == [(0,)]
  735. assert Poly(1, x).all_monoms() == [(0,)]
  736. assert Poly(2*x + 1, x).all_monoms() == [(1,), (0,)]
  737. assert Poly(7*x**2 + 2*x + 1, x).all_monoms() == [(2,), (1,), (0,)]
  738. assert Poly(7*x**4 + 2*x + 1, x).all_monoms() == [(4,), (3,), (2,), (1,), (0,)]
  739. def test_Poly_all_terms():
  740. assert Poly(0, x).all_terms() == [((0,), 0)]
  741. assert Poly(1, x).all_terms() == [((0,), 1)]
  742. assert Poly(2*x + 1, x).all_terms() == [((1,), 2), ((0,), 1)]
  743. assert Poly(7*x**2 + 2*x + 1, x).all_terms() == \
  744. [((2,), 7), ((1,), 2), ((0,), 1)]
  745. assert Poly(7*x**4 + 2*x + 1, x).all_terms() == \
  746. [((4,), 7), ((3,), 0), ((2,), 0), ((1,), 2), ((0,), 1)]
  747. def test_Poly_termwise():
  748. f = Poly(x**2 + 20*x + 400)
  749. g = Poly(x**2 + 2*x + 4)
  750. def func(monom, coeff):
  751. (k,) = monom
  752. return coeff//10**(2 - k)
  753. assert f.termwise(func) == g
  754. def func(monom, coeff):
  755. (k,) = monom
  756. return (k,), coeff//10**(2 - k)
  757. assert f.termwise(func) == g
  758. def test_Poly_length():
  759. assert Poly(0, x).length() == 0
  760. assert Poly(1, x).length() == 1
  761. assert Poly(x, x).length() == 1
  762. assert Poly(x + 1, x).length() == 2
  763. assert Poly(x**2 + 1, x).length() == 2
  764. assert Poly(x**2 + x + 1, x).length() == 3
  765. def test_Poly_as_dict():
  766. assert Poly(0, x).as_dict() == {}
  767. assert Poly(0, x, y, z).as_dict() == {}
  768. assert Poly(1, x).as_dict() == {(0,): 1}
  769. assert Poly(1, x, y, z).as_dict() == {(0, 0, 0): 1}
  770. assert Poly(x**2 + 3, x).as_dict() == {(2,): 1, (0,): 3}
  771. assert Poly(x**2 + 3, x, y, z).as_dict() == {(2, 0, 0): 1, (0, 0, 0): 3}
  772. assert Poly(3*x**2*y*z**3 + 4*x*y + 5*x*z).as_dict() == {(2, 1, 3): 3,
  773. (1, 1, 0): 4, (1, 0, 1): 5}
  774. def test_Poly_as_expr():
  775. assert Poly(0, x).as_expr() == 0
  776. assert Poly(0, x, y, z).as_expr() == 0
  777. assert Poly(1, x).as_expr() == 1
  778. assert Poly(1, x, y, z).as_expr() == 1
  779. assert Poly(x**2 + 3, x).as_expr() == x**2 + 3
  780. assert Poly(x**2 + 3, x, y, z).as_expr() == x**2 + 3
  781. assert Poly(
  782. 3*x**2*y*z**3 + 4*x*y + 5*x*z).as_expr() == 3*x**2*y*z**3 + 4*x*y + 5*x*z
  783. f = Poly(x**2 + 2*x*y**2 - y, x, y)
  784. assert f.as_expr() == -y + x**2 + 2*x*y**2
  785. assert f.as_expr({x: 5}) == 25 - y + 10*y**2
  786. assert f.as_expr({y: 6}) == -6 + 72*x + x**2
  787. assert f.as_expr({x: 5, y: 6}) == 379
  788. assert f.as_expr(5, 6) == 379
  789. raises(GeneratorsError, lambda: f.as_expr({z: 7}))
  790. def test_Poly_lift():
  791. assert Poly(x**4 - I*x + 17*I, x, gaussian=True).lift() == \
  792. Poly(x**16 + 2*x**10 + 578*x**8 + x**4 - 578*x**2 + 83521,
  793. x, domain='QQ')
  794. def test_Poly_deflate():
  795. assert Poly(0, x).deflate() == ((1,), Poly(0, x))
  796. assert Poly(1, x).deflate() == ((1,), Poly(1, x))
  797. assert Poly(x, x).deflate() == ((1,), Poly(x, x))
  798. assert Poly(x**2, x).deflate() == ((2,), Poly(x, x))
  799. assert Poly(x**17, x).deflate() == ((17,), Poly(x, x))
  800. assert Poly(
  801. x**2*y*z**11 + x**4*z**11).deflate() == ((2, 1, 11), Poly(x*y*z + x**2*z))
  802. def test_Poly_inject():
  803. f = Poly(x**2*y + x*y**3 + x*y + 1, x)
  804. assert f.inject() == Poly(x**2*y + x*y**3 + x*y + 1, x, y)
  805. assert f.inject(front=True) == Poly(y**3*x + y*x**2 + y*x + 1, y, x)
  806. def test_Poly_eject():
  807. f = Poly(x**2*y + x*y**3 + x*y + 1, x, y)
  808. assert f.eject(x) == Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]')
  809. assert f.eject(y) == Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]')
  810. ex = x + y + z + t + w
  811. g = Poly(ex, x, y, z, t, w)
  812. assert g.eject(x) == Poly(ex, y, z, t, w, domain='ZZ[x]')
  813. assert g.eject(x, y) == Poly(ex, z, t, w, domain='ZZ[x, y]')
  814. assert g.eject(x, y, z) == Poly(ex, t, w, domain='ZZ[x, y, z]')
  815. assert g.eject(w) == Poly(ex, x, y, z, t, domain='ZZ[w]')
  816. assert g.eject(t, w) == Poly(ex, x, y, z, domain='ZZ[t, w]')
  817. assert g.eject(z, t, w) == Poly(ex, x, y, domain='ZZ[z, t, w]')
  818. raises(DomainError, lambda: Poly(x*y, x, y, domain=ZZ[z]).eject(y))
  819. raises(NotImplementedError, lambda: Poly(x*y, x, y, z).eject(y))
  820. def test_Poly_exclude():
  821. assert Poly(x, x, y).exclude() == Poly(x, x)
  822. assert Poly(x*y, x, y).exclude() == Poly(x*y, x, y)
  823. assert Poly(1, x, y).exclude() == Poly(1, x, y)
  824. def test_Poly__gen_to_level():
  825. assert Poly(1, x, y)._gen_to_level(-2) == 0
  826. assert Poly(1, x, y)._gen_to_level(-1) == 1
  827. assert Poly(1, x, y)._gen_to_level( 0) == 0
  828. assert Poly(1, x, y)._gen_to_level( 1) == 1
  829. raises(PolynomialError, lambda: Poly(1, x, y)._gen_to_level(-3))
  830. raises(PolynomialError, lambda: Poly(1, x, y)._gen_to_level( 2))
  831. assert Poly(1, x, y)._gen_to_level(x) == 0
  832. assert Poly(1, x, y)._gen_to_level(y) == 1
  833. assert Poly(1, x, y)._gen_to_level('x') == 0
  834. assert Poly(1, x, y)._gen_to_level('y') == 1
  835. raises(PolynomialError, lambda: Poly(1, x, y)._gen_to_level(z))
  836. raises(PolynomialError, lambda: Poly(1, x, y)._gen_to_level('z'))
  837. def test_Poly_degree():
  838. assert Poly(0, x).degree() is -oo
  839. assert Poly(1, x).degree() == 0
  840. assert Poly(x, x).degree() == 1
  841. assert Poly(0, x).degree(gen=0) is -oo
  842. assert Poly(1, x).degree(gen=0) == 0
  843. assert Poly(x, x).degree(gen=0) == 1
  844. assert Poly(0, x).degree(gen=x) is -oo
  845. assert Poly(1, x).degree(gen=x) == 0
  846. assert Poly(x, x).degree(gen=x) == 1
  847. assert Poly(0, x).degree(gen='x') is -oo
  848. assert Poly(1, x).degree(gen='x') == 0
  849. assert Poly(x, x).degree(gen='x') == 1
  850. raises(PolynomialError, lambda: Poly(1, x).degree(gen=1))
  851. raises(PolynomialError, lambda: Poly(1, x).degree(gen=y))
  852. raises(PolynomialError, lambda: Poly(1, x).degree(gen='y'))
  853. assert Poly(1, x, y).degree() == 0
  854. assert Poly(2*y, x, y).degree() == 0
  855. assert Poly(x*y, x, y).degree() == 1
  856. assert Poly(1, x, y).degree(gen=x) == 0
  857. assert Poly(2*y, x, y).degree(gen=x) == 0
  858. assert Poly(x*y, x, y).degree(gen=x) == 1
  859. assert Poly(1, x, y).degree(gen=y) == 0
  860. assert Poly(2*y, x, y).degree(gen=y) == 1
  861. assert Poly(x*y, x, y).degree(gen=y) == 1
  862. assert degree(0, x) is -oo
  863. assert degree(1, x) == 0
  864. assert degree(x, x) == 1
  865. assert degree(x*y**2, x) == 1
  866. assert degree(x*y**2, y) == 2
  867. assert degree(x*y**2, z) == 0
  868. assert degree(pi) == 1
  869. raises(TypeError, lambda: degree(y**2 + x**3))
  870. raises(TypeError, lambda: degree(y**2 + x**3, 1))
  871. raises(PolynomialError, lambda: degree(x, 1.1))
  872. raises(PolynomialError, lambda: degree(x**2/(x**3 + 1), x))
  873. assert degree(Poly(0,x),z) is -oo
  874. assert degree(Poly(1,x),z) == 0
  875. assert degree(Poly(x**2+y**3,y)) == 3
  876. assert degree(Poly(y**2 + x**3, y, x), 1) == 3
  877. assert degree(Poly(y**2 + x**3, x), z) == 0
  878. assert degree(Poly(y**2 + x**3 + z**4, x), z) == 4
  879. def test_Poly_degree_list():
  880. assert Poly(0, x).degree_list() == (-oo,)
  881. assert Poly(0, x, y).degree_list() == (-oo, -oo)
  882. assert Poly(0, x, y, z).degree_list() == (-oo, -oo, -oo)
  883. assert Poly(1, x).degree_list() == (0,)
  884. assert Poly(1, x, y).degree_list() == (0, 0)
  885. assert Poly(1, x, y, z).degree_list() == (0, 0, 0)
  886. assert Poly(x**2*y + x**3*z**2 + 1).degree_list() == (3, 1, 2)
  887. assert degree_list(1, x) == (0,)
  888. assert degree_list(x, x) == (1,)
  889. assert degree_list(x*y**2) == (1, 2)
  890. raises(ComputationFailed, lambda: degree_list(1))
  891. def test_Poly_total_degree():
  892. assert Poly(x**2*y + x**3*z**2 + 1).total_degree() == 5
  893. assert Poly(x**2 + z**3).total_degree() == 3
  894. assert Poly(x*y*z + z**4).total_degree() == 4
  895. assert Poly(x**3 + x + 1).total_degree() == 3
  896. assert total_degree(x*y + z**3) == 3
  897. assert total_degree(x*y + z**3, x, y) == 2
  898. assert total_degree(1) == 0
  899. assert total_degree(Poly(y**2 + x**3 + z**4)) == 4
  900. assert total_degree(Poly(y**2 + x**3 + z**4, x)) == 3
  901. assert total_degree(Poly(y**2 + x**3 + z**4, x), z) == 4
  902. assert total_degree(Poly(x**9 + x*z*y + x**3*z**2 + z**7,x), z) == 7
  903. def test_Poly_homogenize():
  904. assert Poly(x**2+y).homogenize(z) == Poly(x**2+y*z)
  905. assert Poly(x+y).homogenize(z) == Poly(x+y, x, y, z)
  906. assert Poly(x+y**2).homogenize(y) == Poly(x*y+y**2)
  907. def test_Poly_homogeneous_order():
  908. assert Poly(0, x, y).homogeneous_order() is -oo
  909. assert Poly(1, x, y).homogeneous_order() == 0
  910. assert Poly(x, x, y).homogeneous_order() == 1
  911. assert Poly(x*y, x, y).homogeneous_order() == 2
  912. assert Poly(x + 1, x, y).homogeneous_order() is None
  913. assert Poly(x*y + x, x, y).homogeneous_order() is None
  914. assert Poly(x**5 + 2*x**3*y**2 + 9*x*y**4).homogeneous_order() == 5
  915. assert Poly(x**5 + 2*x**3*y**3 + 9*x*y**4).homogeneous_order() is None
  916. def test_Poly_LC():
  917. assert Poly(0, x).LC() == 0
  918. assert Poly(1, x).LC() == 1
  919. assert Poly(2*x**2 + x, x).LC() == 2
  920. assert Poly(x*y**7 + 2*x**2*y**3).LC('lex') == 2
  921. assert Poly(x*y**7 + 2*x**2*y**3).LC('grlex') == 1
  922. assert LC(x*y**7 + 2*x**2*y**3, order='lex') == 2
  923. assert LC(x*y**7 + 2*x**2*y**3, order='grlex') == 1
  924. def test_Poly_TC():
  925. assert Poly(0, x).TC() == 0
  926. assert Poly(1, x).TC() == 1
  927. assert Poly(2*x**2 + x, x).TC() == 0
  928. def test_Poly_EC():
  929. assert Poly(0, x).EC() == 0
  930. assert Poly(1, x).EC() == 1
  931. assert Poly(2*x**2 + x, x).EC() == 1
  932. assert Poly(x*y**7 + 2*x**2*y**3).EC('lex') == 1
  933. assert Poly(x*y**7 + 2*x**2*y**3).EC('grlex') == 2
  934. def test_Poly_coeff():
  935. assert Poly(0, x).coeff_monomial(1) == 0
  936. assert Poly(0, x).coeff_monomial(x) == 0
  937. assert Poly(1, x).coeff_monomial(1) == 1
  938. assert Poly(1, x).coeff_monomial(x) == 0
  939. assert Poly(x**8, x).coeff_monomial(1) == 0
  940. assert Poly(x**8, x).coeff_monomial(x**7) == 0
  941. assert Poly(x**8, x).coeff_monomial(x**8) == 1
  942. assert Poly(x**8, x).coeff_monomial(x**9) == 0
  943. assert Poly(3*x*y**2 + 1, x, y).coeff_monomial(1) == 1
  944. assert Poly(3*x*y**2 + 1, x, y).coeff_monomial(x*y**2) == 3
  945. p = Poly(24*x*y*exp(8) + 23*x, x, y)
  946. assert p.coeff_monomial(x) == 23
  947. assert p.coeff_monomial(y) == 0
  948. assert p.coeff_monomial(x*y) == 24*exp(8)
  949. assert p.as_expr().coeff(x) == 24*y*exp(8) + 23
  950. raises(NotImplementedError, lambda: p.coeff(x))
  951. raises(ValueError, lambda: Poly(x + 1).coeff_monomial(0))
  952. raises(ValueError, lambda: Poly(x + 1).coeff_monomial(3*x))
  953. raises(ValueError, lambda: Poly(x + 1).coeff_monomial(3*x*y))
  954. def test_Poly_nth():
  955. assert Poly(0, x).nth(0) == 0
  956. assert Poly(0, x).nth(1) == 0
  957. assert Poly(1, x).nth(0) == 1
  958. assert Poly(1, x).nth(1) == 0
  959. assert Poly(x**8, x).nth(0) == 0
  960. assert Poly(x**8, x).nth(7) == 0
  961. assert Poly(x**8, x).nth(8) == 1
  962. assert Poly(x**8, x).nth(9) == 0
  963. assert Poly(3*x*y**2 + 1, x, y).nth(0, 0) == 1
  964. assert Poly(3*x*y**2 + 1, x, y).nth(1, 2) == 3
  965. raises(ValueError, lambda: Poly(x*y + 1, x, y).nth(1))
  966. def test_Poly_LM():
  967. assert Poly(0, x).LM() == (0,)
  968. assert Poly(1, x).LM() == (0,)
  969. assert Poly(2*x**2 + x, x).LM() == (2,)
  970. assert Poly(x*y**7 + 2*x**2*y**3).LM('lex') == (2, 3)
  971. assert Poly(x*y**7 + 2*x**2*y**3).LM('grlex') == (1, 7)
  972. assert LM(x*y**7 + 2*x**2*y**3, order='lex') == x**2*y**3
  973. assert LM(x*y**7 + 2*x**2*y**3, order='grlex') == x*y**7
  974. def test_Poly_LM_custom_order():
  975. f = Poly(x**2*y**3*z + x**2*y*z**3 + x*y*z + 1)
  976. rev_lex = lambda monom: tuple(reversed(monom))
  977. assert f.LM(order='lex') == (2, 3, 1)
  978. assert f.LM(order=rev_lex) == (2, 1, 3)
  979. def test_Poly_EM():
  980. assert Poly(0, x).EM() == (0,)
  981. assert Poly(1, x).EM() == (0,)
  982. assert Poly(2*x**2 + x, x).EM() == (1,)
  983. assert Poly(x*y**7 + 2*x**2*y**3).EM('lex') == (1, 7)
  984. assert Poly(x*y**7 + 2*x**2*y**3).EM('grlex') == (2, 3)
  985. def test_Poly_LT():
  986. assert Poly(0, x).LT() == ((0,), 0)
  987. assert Poly(1, x).LT() == ((0,), 1)
  988. assert Poly(2*x**2 + x, x).LT() == ((2,), 2)
  989. assert Poly(x*y**7 + 2*x**2*y**3).LT('lex') == ((2, 3), 2)
  990. assert Poly(x*y**7 + 2*x**2*y**3).LT('grlex') == ((1, 7), 1)
  991. assert LT(x*y**7 + 2*x**2*y**3, order='lex') == 2*x**2*y**3
  992. assert LT(x*y**7 + 2*x**2*y**3, order='grlex') == x*y**7
  993. def test_Poly_ET():
  994. assert Poly(0, x).ET() == ((0,), 0)
  995. assert Poly(1, x).ET() == ((0,), 1)
  996. assert Poly(2*x**2 + x, x).ET() == ((1,), 1)
  997. assert Poly(x*y**7 + 2*x**2*y**3).ET('lex') == ((1, 7), 1)
  998. assert Poly(x*y**7 + 2*x**2*y**3).ET('grlex') == ((2, 3), 2)
  999. def test_Poly_max_norm():
  1000. assert Poly(-1, x).max_norm() == 1
  1001. assert Poly( 0, x).max_norm() == 0
  1002. assert Poly( 1, x).max_norm() == 1
  1003. def test_Poly_l1_norm():
  1004. assert Poly(-1, x).l1_norm() == 1
  1005. assert Poly( 0, x).l1_norm() == 0
  1006. assert Poly( 1, x).l1_norm() == 1
  1007. def test_Poly_clear_denoms():
  1008. coeff, poly = Poly(x + 2, x).clear_denoms()
  1009. assert coeff == 1 and poly == Poly(
  1010. x + 2, x, domain='ZZ') and poly.get_domain() == ZZ
  1011. coeff, poly = Poly(x/2 + 1, x).clear_denoms()
  1012. assert coeff == 2 and poly == Poly(
  1013. x + 2, x, domain='QQ') and poly.get_domain() == QQ
  1014. coeff, poly = Poly(x/2 + 1, x).clear_denoms(convert=True)
  1015. assert coeff == 2 and poly == Poly(
  1016. x + 2, x, domain='ZZ') and poly.get_domain() == ZZ
  1017. coeff, poly = Poly(x/y + 1, x).clear_denoms(convert=True)
  1018. assert coeff == y and poly == Poly(
  1019. x + y, x, domain='ZZ[y]') and poly.get_domain() == ZZ[y]
  1020. coeff, poly = Poly(x/3 + sqrt(2), x, domain='EX').clear_denoms()
  1021. assert coeff == 3 and poly == Poly(
  1022. x + 3*sqrt(2), x, domain='EX') and poly.get_domain() == EX
  1023. coeff, poly = Poly(
  1024. x/3 + sqrt(2), x, domain='EX').clear_denoms(convert=True)
  1025. assert coeff == 3 and poly == Poly(
  1026. x + 3*sqrt(2), x, domain='EX') and poly.get_domain() == EX
  1027. def test_Poly_rat_clear_denoms():
  1028. f = Poly(x**2/y + 1, x)
  1029. g = Poly(x**3 + y, x)
  1030. assert f.rat_clear_denoms(g) == \
  1031. (Poly(x**2 + y, x), Poly(y*x**3 + y**2, x))
  1032. f = f.set_domain(EX)
  1033. g = g.set_domain(EX)
  1034. assert f.rat_clear_denoms(g) == (f, g)
  1035. def test_issue_20427():
  1036. f = Poly(-117968192370600*18**(S(1)/3)/(217603955769048*(24201 +
  1037. 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 +
  1038. 253*sqrt(9165))**(S(1)/3)) - 15720318185*2**(S(2)/3)*3**(S(1)/3)*(24201
  1039. + 253*sqrt(9165))**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**
  1040. (S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3))
  1041. + 15720318185*12**(S(1)/3)*(24201 + 253*sqrt(9165))**(S(2)/3)/(
  1042. 217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*
  1043. sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) + 117968192370600*2**(
  1044. S(1)/3)*3**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3)
  1045. + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)), x)
  1046. assert f == Poly(0, x, domain='EX')
  1047. def test_Poly_integrate():
  1048. assert Poly(x + 1).integrate() == Poly(x**2/2 + x)
  1049. assert Poly(x + 1).integrate(x) == Poly(x**2/2 + x)
  1050. assert Poly(x + 1).integrate((x, 1)) == Poly(x**2/2 + x)
  1051. assert Poly(x*y + 1).integrate(x) == Poly(x**2*y/2 + x)
  1052. assert Poly(x*y + 1).integrate(y) == Poly(x*y**2/2 + y)
  1053. assert Poly(x*y + 1).integrate(x, x) == Poly(x**3*y/6 + x**2/2)
  1054. assert Poly(x*y + 1).integrate(y, y) == Poly(x*y**3/6 + y**2/2)
  1055. assert Poly(x*y + 1).integrate((x, 2)) == Poly(x**3*y/6 + x**2/2)
  1056. assert Poly(x*y + 1).integrate((y, 2)) == Poly(x*y**3/6 + y**2/2)
  1057. assert Poly(x*y + 1).integrate(x, y) == Poly(x**2*y**2/4 + x*y)
  1058. assert Poly(x*y + 1).integrate(y, x) == Poly(x**2*y**2/4 + x*y)
  1059. def test_Poly_diff():
  1060. assert Poly(x**2 + x).diff() == Poly(2*x + 1)
  1061. assert Poly(x**2 + x).diff(x) == Poly(2*x + 1)
  1062. assert Poly(x**2 + x).diff((x, 1)) == Poly(2*x + 1)
  1063. assert Poly(x**2*y**2 + x*y).diff(x) == Poly(2*x*y**2 + y)
  1064. assert Poly(x**2*y**2 + x*y).diff(y) == Poly(2*x**2*y + x)
  1065. assert Poly(x**2*y**2 + x*y).diff(x, x) == Poly(2*y**2, x, y)
  1066. assert Poly(x**2*y**2 + x*y).diff(y, y) == Poly(2*x**2, x, y)
  1067. assert Poly(x**2*y**2 + x*y).diff((x, 2)) == Poly(2*y**2, x, y)
  1068. assert Poly(x**2*y**2 + x*y).diff((y, 2)) == Poly(2*x**2, x, y)
  1069. assert Poly(x**2*y**2 + x*y).diff(x, y) == Poly(4*x*y + 1)
  1070. assert Poly(x**2*y**2 + x*y).diff(y, x) == Poly(4*x*y + 1)
  1071. def test_issue_9585():
  1072. assert diff(Poly(x**2 + x)) == Poly(2*x + 1)
  1073. assert diff(Poly(x**2 + x), x, evaluate=False) == \
  1074. Derivative(Poly(x**2 + x), x)
  1075. assert Derivative(Poly(x**2 + x), x).doit() == Poly(2*x + 1)
  1076. def test_Poly_eval():
  1077. assert Poly(0, x).eval(7) == 0
  1078. assert Poly(1, x).eval(7) == 1
  1079. assert Poly(x, x).eval(7) == 7
  1080. assert Poly(0, x).eval(0, 7) == 0
  1081. assert Poly(1, x).eval(0, 7) == 1
  1082. assert Poly(x, x).eval(0, 7) == 7
  1083. assert Poly(0, x).eval(x, 7) == 0
  1084. assert Poly(1, x).eval(x, 7) == 1
  1085. assert Poly(x, x).eval(x, 7) == 7
  1086. assert Poly(0, x).eval('x', 7) == 0
  1087. assert Poly(1, x).eval('x', 7) == 1
  1088. assert Poly(x, x).eval('x', 7) == 7
  1089. raises(PolynomialError, lambda: Poly(1, x).eval(1, 7))
  1090. raises(PolynomialError, lambda: Poly(1, x).eval(y, 7))
  1091. raises(PolynomialError, lambda: Poly(1, x).eval('y', 7))
  1092. assert Poly(123, x, y).eval(7) == Poly(123, y)
  1093. assert Poly(2*y, x, y).eval(7) == Poly(2*y, y)
  1094. assert Poly(x*y, x, y).eval(7) == Poly(7*y, y)
  1095. assert Poly(123, x, y).eval(x, 7) == Poly(123, y)
  1096. assert Poly(2*y, x, y).eval(x, 7) == Poly(2*y, y)
  1097. assert Poly(x*y, x, y).eval(x, 7) == Poly(7*y, y)
  1098. assert Poly(123, x, y).eval(y, 7) == Poly(123, x)
  1099. assert Poly(2*y, x, y).eval(y, 7) == Poly(14, x)
  1100. assert Poly(x*y, x, y).eval(y, 7) == Poly(7*x, x)
  1101. assert Poly(x*y + y, x, y).eval({x: 7}) == Poly(8*y, y)
  1102. assert Poly(x*y + y, x, y).eval({y: 7}) == Poly(7*x + 7, x)
  1103. assert Poly(x*y + y, x, y).eval({x: 6, y: 7}) == 49
  1104. assert Poly(x*y + y, x, y).eval({x: 7, y: 6}) == 48
  1105. assert Poly(x*y + y, x, y).eval((6, 7)) == 49
  1106. assert Poly(x*y + y, x, y).eval([6, 7]) == 49
  1107. assert Poly(x + 1, domain='ZZ').eval(S.Half) == Rational(3, 2)
  1108. assert Poly(x + 1, domain='ZZ').eval(sqrt(2)) == sqrt(2) + 1
  1109. raises(ValueError, lambda: Poly(x*y + y, x, y).eval((6, 7, 8)))
  1110. raises(DomainError, lambda: Poly(x + 1, domain='ZZ').eval(S.Half, auto=False))
  1111. # issue 6344
  1112. alpha = Symbol('alpha')
  1113. result = (2*alpha*z - 2*alpha + z**2 + 3)/(z**2 - 2*z + 1)
  1114. f = Poly(x**2 + (alpha - 1)*x - alpha + 1, x, domain='ZZ[alpha]')
  1115. assert f.eval((z + 1)/(z - 1)) == result
  1116. g = Poly(x**2 + (alpha - 1)*x - alpha + 1, x, y, domain='ZZ[alpha]')
  1117. assert g.eval((z + 1)/(z - 1)) == Poly(result, y, domain='ZZ(alpha,z)')
  1118. def test_Poly___call__():
  1119. f = Poly(2*x*y + 3*x + y + 2*z)
  1120. assert f(2) == Poly(5*y + 2*z + 6)
  1121. assert f(2, 5) == Poly(2*z + 31)
  1122. assert f(2, 5, 7) == 45
  1123. def test_parallel_poly_from_expr():
  1124. assert parallel_poly_from_expr(
  1125. [x - 1, x**2 - 1], x)[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1126. assert parallel_poly_from_expr(
  1127. [Poly(x - 1, x), x**2 - 1], x)[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1128. assert parallel_poly_from_expr(
  1129. [x - 1, Poly(x**2 - 1, x)], x)[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1130. assert parallel_poly_from_expr([Poly(
  1131. x - 1, x), Poly(x**2 - 1, x)], x)[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1132. assert parallel_poly_from_expr(
  1133. [x - 1, x**2 - 1], x, y)[0] == [Poly(x - 1, x, y), Poly(x**2 - 1, x, y)]
  1134. assert parallel_poly_from_expr([Poly(
  1135. x - 1, x), x**2 - 1], x, y)[0] == [Poly(x - 1, x, y), Poly(x**2 - 1, x, y)]
  1136. assert parallel_poly_from_expr([x - 1, Poly(
  1137. x**2 - 1, x)], x, y)[0] == [Poly(x - 1, x, y), Poly(x**2 - 1, x, y)]
  1138. assert parallel_poly_from_expr([Poly(x - 1, x), Poly(
  1139. x**2 - 1, x)], x, y)[0] == [Poly(x - 1, x, y), Poly(x**2 - 1, x, y)]
  1140. assert parallel_poly_from_expr(
  1141. [x - 1, x**2 - 1])[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1142. assert parallel_poly_from_expr(
  1143. [Poly(x - 1, x), x**2 - 1])[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1144. assert parallel_poly_from_expr(
  1145. [x - 1, Poly(x**2 - 1, x)])[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1146. assert parallel_poly_from_expr(
  1147. [Poly(x - 1, x), Poly(x**2 - 1, x)])[0] == [Poly(x - 1, x), Poly(x**2 - 1, x)]
  1148. assert parallel_poly_from_expr(
  1149. [1, x**2 - 1])[0] == [Poly(1, x), Poly(x**2 - 1, x)]
  1150. assert parallel_poly_from_expr(
  1151. [1, x**2 - 1])[0] == [Poly(1, x), Poly(x**2 - 1, x)]
  1152. assert parallel_poly_from_expr(
  1153. [1, Poly(x**2 - 1, x)])[0] == [Poly(1, x), Poly(x**2 - 1, x)]
  1154. assert parallel_poly_from_expr(
  1155. [1, Poly(x**2 - 1, x)])[0] == [Poly(1, x), Poly(x**2 - 1, x)]
  1156. assert parallel_poly_from_expr(
  1157. [x**2 - 1, 1])[0] == [Poly(x**2 - 1, x), Poly(1, x)]
  1158. assert parallel_poly_from_expr(
  1159. [x**2 - 1, 1])[0] == [Poly(x**2 - 1, x), Poly(1, x)]
  1160. assert parallel_poly_from_expr(
  1161. [Poly(x**2 - 1, x), 1])[0] == [Poly(x**2 - 1, x), Poly(1, x)]
  1162. assert parallel_poly_from_expr(
  1163. [Poly(x**2 - 1, x), 1])[0] == [Poly(x**2 - 1, x), Poly(1, x)]
  1164. assert parallel_poly_from_expr([Poly(x, x, y), Poly(y, x, y)], x, y, order='lex')[0] == \
  1165. [Poly(x, x, y, domain='ZZ'), Poly(y, x, y, domain='ZZ')]
  1166. raises(PolificationFailed, lambda: parallel_poly_from_expr([0, 1]))
  1167. def test_pdiv():
  1168. f, g = x**2 - y**2, x - y
  1169. q, r = x + y, 0
  1170. F, G, Q, R = [ Poly(h, x, y) for h in (f, g, q, r) ]
  1171. assert F.pdiv(G) == (Q, R)
  1172. assert F.prem(G) == R
  1173. assert F.pquo(G) == Q
  1174. assert F.pexquo(G) == Q
  1175. assert pdiv(f, g) == (q, r)
  1176. assert prem(f, g) == r
  1177. assert pquo(f, g) == q
  1178. assert pexquo(f, g) == q
  1179. assert pdiv(f, g, x, y) == (q, r)
  1180. assert prem(f, g, x, y) == r
  1181. assert pquo(f, g, x, y) == q
  1182. assert pexquo(f, g, x, y) == q
  1183. assert pdiv(f, g, (x, y)) == (q, r)
  1184. assert prem(f, g, (x, y)) == r
  1185. assert pquo(f, g, (x, y)) == q
  1186. assert pexquo(f, g, (x, y)) == q
  1187. assert pdiv(F, G) == (Q, R)
  1188. assert prem(F, G) == R
  1189. assert pquo(F, G) == Q
  1190. assert pexquo(F, G) == Q
  1191. assert pdiv(f, g, polys=True) == (Q, R)
  1192. assert prem(f, g, polys=True) == R
  1193. assert pquo(f, g, polys=True) == Q
  1194. assert pexquo(f, g, polys=True) == Q
  1195. assert pdiv(F, G, polys=False) == (q, r)
  1196. assert prem(F, G, polys=False) == r
  1197. assert pquo(F, G, polys=False) == q
  1198. assert pexquo(F, G, polys=False) == q
  1199. raises(ComputationFailed, lambda: pdiv(4, 2))
  1200. raises(ComputationFailed, lambda: prem(4, 2))
  1201. raises(ComputationFailed, lambda: pquo(4, 2))
  1202. raises(ComputationFailed, lambda: pexquo(4, 2))
  1203. def test_div():
  1204. f, g = x**2 - y**2, x - y
  1205. q, r = x + y, 0
  1206. F, G, Q, R = [ Poly(h, x, y) for h in (f, g, q, r) ]
  1207. assert F.div(G) == (Q, R)
  1208. assert F.rem(G) == R
  1209. assert F.quo(G) == Q
  1210. assert F.exquo(G) == Q
  1211. assert div(f, g) == (q, r)
  1212. assert rem(f, g) == r
  1213. assert quo(f, g) == q
  1214. assert exquo(f, g) == q
  1215. assert div(f, g, x, y) == (q, r)
  1216. assert rem(f, g, x, y) == r
  1217. assert quo(f, g, x, y) == q
  1218. assert exquo(f, g, x, y) == q
  1219. assert div(f, g, (x, y)) == (q, r)
  1220. assert rem(f, g, (x, y)) == r
  1221. assert quo(f, g, (x, y)) == q
  1222. assert exquo(f, g, (x, y)) == q
  1223. assert div(F, G) == (Q, R)
  1224. assert rem(F, G) == R
  1225. assert quo(F, G) == Q
  1226. assert exquo(F, G) == Q
  1227. assert div(f, g, polys=True) == (Q, R)
  1228. assert rem(f, g, polys=True) == R
  1229. assert quo(f, g, polys=True) == Q
  1230. assert exquo(f, g, polys=True) == Q
  1231. assert div(F, G, polys=False) == (q, r)
  1232. assert rem(F, G, polys=False) == r
  1233. assert quo(F, G, polys=False) == q
  1234. assert exquo(F, G, polys=False) == q
  1235. raises(ComputationFailed, lambda: div(4, 2))
  1236. raises(ComputationFailed, lambda: rem(4, 2))
  1237. raises(ComputationFailed, lambda: quo(4, 2))
  1238. raises(ComputationFailed, lambda: exquo(4, 2))
  1239. f, g = x**2 + 1, 2*x - 4
  1240. qz, rz = 0, x**2 + 1
  1241. qq, rq = x/2 + 1, 5
  1242. assert div(f, g) == (qq, rq)
  1243. assert div(f, g, auto=True) == (qq, rq)
  1244. assert div(f, g, auto=False) == (qz, rz)
  1245. assert div(f, g, domain=ZZ) == (qz, rz)
  1246. assert div(f, g, domain=QQ) == (qq, rq)
  1247. assert div(f, g, domain=ZZ, auto=True) == (qq, rq)
  1248. assert div(f, g, domain=ZZ, auto=False) == (qz, rz)
  1249. assert div(f, g, domain=QQ, auto=True) == (qq, rq)
  1250. assert div(f, g, domain=QQ, auto=False) == (qq, rq)
  1251. assert rem(f, g) == rq
  1252. assert rem(f, g, auto=True) == rq
  1253. assert rem(f, g, auto=False) == rz
  1254. assert rem(f, g, domain=ZZ) == rz
  1255. assert rem(f, g, domain=QQ) == rq
  1256. assert rem(f, g, domain=ZZ, auto=True) == rq
  1257. assert rem(f, g, domain=ZZ, auto=False) == rz
  1258. assert rem(f, g, domain=QQ, auto=True) == rq
  1259. assert rem(f, g, domain=QQ, auto=False) == rq
  1260. assert quo(f, g) == qq
  1261. assert quo(f, g, auto=True) == qq
  1262. assert quo(f, g, auto=False) == qz
  1263. assert quo(f, g, domain=ZZ) == qz
  1264. assert quo(f, g, domain=QQ) == qq
  1265. assert quo(f, g, domain=ZZ, auto=True) == qq
  1266. assert quo(f, g, domain=ZZ, auto=False) == qz
  1267. assert quo(f, g, domain=QQ, auto=True) == qq
  1268. assert quo(f, g, domain=QQ, auto=False) == qq
  1269. f, g, q = x**2, 2*x, x/2
  1270. assert exquo(f, g) == q
  1271. assert exquo(f, g, auto=True) == q
  1272. raises(ExactQuotientFailed, lambda: exquo(f, g, auto=False))
  1273. raises(ExactQuotientFailed, lambda: exquo(f, g, domain=ZZ))
  1274. assert exquo(f, g, domain=QQ) == q
  1275. assert exquo(f, g, domain=ZZ, auto=True) == q
  1276. raises(ExactQuotientFailed, lambda: exquo(f, g, domain=ZZ, auto=False))
  1277. assert exquo(f, g, domain=QQ, auto=True) == q
  1278. assert exquo(f, g, domain=QQ, auto=False) == q
  1279. f, g = Poly(x**2), Poly(x)
  1280. q, r = f.div(g)
  1281. assert q.get_domain().is_ZZ and r.get_domain().is_ZZ
  1282. r = f.rem(g)
  1283. assert r.get_domain().is_ZZ
  1284. q = f.quo(g)
  1285. assert q.get_domain().is_ZZ
  1286. q = f.exquo(g)
  1287. assert q.get_domain().is_ZZ
  1288. f, g = Poly(x+y, x), Poly(2*x+y, x)
  1289. q, r = f.div(g)
  1290. assert q.get_domain().is_Frac and r.get_domain().is_Frac
  1291. # https://github.com/sympy/sympy/issues/19579
  1292. p = Poly(2+3*I, x, domain=ZZ_I)
  1293. q = Poly(1-I, x, domain=ZZ_I)
  1294. assert p.div(q, auto=False) == \
  1295. (Poly(0, x, domain='ZZ_I'), Poly(2 + 3*I, x, domain='ZZ_I'))
  1296. assert p.div(q, auto=True) == \
  1297. (Poly(-S(1)/2 + 5*I/2, x, domain='QQ_I'), Poly(0, x, domain='QQ_I'))
  1298. def test_issue_7864():
  1299. q, r = div(a, .408248290463863*a)
  1300. assert abs(q - 2.44948974278318) < 1e-14
  1301. assert r == 0
  1302. def test_gcdex():
  1303. f, g = 2*x, x**2 - 16
  1304. s, t, h = x/32, Rational(-1, 16), 1
  1305. F, G, S, T, H = [ Poly(u, x, domain='QQ') for u in (f, g, s, t, h) ]
  1306. assert F.half_gcdex(G) == (S, H)
  1307. assert F.gcdex(G) == (S, T, H)
  1308. assert F.invert(G) == S
  1309. assert half_gcdex(f, g) == (s, h)
  1310. assert gcdex(f, g) == (s, t, h)
  1311. assert invert(f, g) == s
  1312. assert half_gcdex(f, g, x) == (s, h)
  1313. assert gcdex(f, g, x) == (s, t, h)
  1314. assert invert(f, g, x) == s
  1315. assert half_gcdex(f, g, (x,)) == (s, h)
  1316. assert gcdex(f, g, (x,)) == (s, t, h)
  1317. assert invert(f, g, (x,)) == s
  1318. assert half_gcdex(F, G) == (S, H)
  1319. assert gcdex(F, G) == (S, T, H)
  1320. assert invert(F, G) == S
  1321. assert half_gcdex(f, g, polys=True) == (S, H)
  1322. assert gcdex(f, g, polys=True) == (S, T, H)
  1323. assert invert(f, g, polys=True) == S
  1324. assert half_gcdex(F, G, polys=False) == (s, h)
  1325. assert gcdex(F, G, polys=False) == (s, t, h)
  1326. assert invert(F, G, polys=False) == s
  1327. assert half_gcdex(100, 2004) == (-20, 4)
  1328. assert gcdex(100, 2004) == (-20, 1, 4)
  1329. assert invert(3, 7) == 5
  1330. raises(DomainError, lambda: half_gcdex(x + 1, 2*x + 1, auto=False))
  1331. raises(DomainError, lambda: gcdex(x + 1, 2*x + 1, auto=False))
  1332. raises(DomainError, lambda: invert(x + 1, 2*x + 1, auto=False))
  1333. def test_revert():
  1334. f = Poly(1 - x**2/2 + x**4/24 - x**6/720)
  1335. g = Poly(61*x**6/720 + 5*x**4/24 + x**2/2 + 1)
  1336. assert f.revert(8) == g
  1337. def test_subresultants():
  1338. f, g, h = x**2 - 2*x + 1, x**2 - 1, 2*x - 2
  1339. F, G, H = Poly(f), Poly(g), Poly(h)
  1340. assert F.subresultants(G) == [F, G, H]
  1341. assert subresultants(f, g) == [f, g, h]
  1342. assert subresultants(f, g, x) == [f, g, h]
  1343. assert subresultants(f, g, (x,)) == [f, g, h]
  1344. assert subresultants(F, G) == [F, G, H]
  1345. assert subresultants(f, g, polys=True) == [F, G, H]
  1346. assert subresultants(F, G, polys=False) == [f, g, h]
  1347. raises(ComputationFailed, lambda: subresultants(4, 2))
  1348. def test_resultant():
  1349. f, g, h = x**2 - 2*x + 1, x**2 - 1, 0
  1350. F, G = Poly(f), Poly(g)
  1351. assert F.resultant(G) == h
  1352. assert resultant(f, g) == h
  1353. assert resultant(f, g, x) == h
  1354. assert resultant(f, g, (x,)) == h
  1355. assert resultant(F, G) == h
  1356. assert resultant(f, g, polys=True) == h
  1357. assert resultant(F, G, polys=False) == h
  1358. assert resultant(f, g, includePRS=True) == (h, [f, g, 2*x - 2])
  1359. f, g, h = x - a, x - b, a - b
  1360. F, G, H = Poly(f), Poly(g), Poly(h)
  1361. assert F.resultant(G) == H
  1362. assert resultant(f, g) == h
  1363. assert resultant(f, g, x) == h
  1364. assert resultant(f, g, (x,)) == h
  1365. assert resultant(F, G) == H
  1366. assert resultant(f, g, polys=True) == H
  1367. assert resultant(F, G, polys=False) == h
  1368. raises(ComputationFailed, lambda: resultant(4, 2))
  1369. def test_discriminant():
  1370. f, g = x**3 + 3*x**2 + 9*x - 13, -11664
  1371. F = Poly(f)
  1372. assert F.discriminant() == g
  1373. assert discriminant(f) == g
  1374. assert discriminant(f, x) == g
  1375. assert discriminant(f, (x,)) == g
  1376. assert discriminant(F) == g
  1377. assert discriminant(f, polys=True) == g
  1378. assert discriminant(F, polys=False) == g
  1379. f, g = a*x**2 + b*x + c, b**2 - 4*a*c
  1380. F, G = Poly(f), Poly(g)
  1381. assert F.discriminant() == G
  1382. assert discriminant(f) == g
  1383. assert discriminant(f, x, a, b, c) == g
  1384. assert discriminant(f, (x, a, b, c)) == g
  1385. assert discriminant(F) == G
  1386. assert discriminant(f, polys=True) == G
  1387. assert discriminant(F, polys=False) == g
  1388. raises(ComputationFailed, lambda: discriminant(4))
  1389. def test_dispersion():
  1390. # We test only the API here. For more mathematical
  1391. # tests see the dedicated test file.
  1392. fp = poly((x + 1)*(x + 2), x)
  1393. assert sorted(fp.dispersionset()) == [0, 1]
  1394. assert fp.dispersion() == 1
  1395. fp = poly(x**4 - 3*x**2 + 1, x)
  1396. gp = fp.shift(-3)
  1397. assert sorted(fp.dispersionset(gp)) == [2, 3, 4]
  1398. assert fp.dispersion(gp) == 4
  1399. def test_gcd_list():
  1400. F = [x**3 - 1, x**2 - 1, x**2 - 3*x + 2]
  1401. assert gcd_list(F) == x - 1
  1402. assert gcd_list(F, polys=True) == Poly(x - 1)
  1403. assert gcd_list([]) == 0
  1404. assert gcd_list([1, 2]) == 1
  1405. assert gcd_list([4, 6, 8]) == 2
  1406. assert gcd_list([x*(y + 42) - x*y - x*42]) == 0
  1407. gcd = gcd_list([], x)
  1408. assert gcd.is_Number and gcd is S.Zero
  1409. gcd = gcd_list([], x, polys=True)
  1410. assert gcd.is_Poly and gcd.is_zero
  1411. a = sqrt(2)
  1412. assert gcd_list([a, -a]) == gcd_list([-a, a]) == a
  1413. raises(ComputationFailed, lambda: gcd_list([], polys=True))
  1414. def test_lcm_list():
  1415. F = [x**3 - 1, x**2 - 1, x**2 - 3*x + 2]
  1416. assert lcm_list(F) == x**5 - x**4 - 2*x**3 - x**2 + x + 2
  1417. assert lcm_list(F, polys=True) == Poly(x**5 - x**4 - 2*x**3 - x**2 + x + 2)
  1418. assert lcm_list([]) == 1
  1419. assert lcm_list([1, 2]) == 2
  1420. assert lcm_list([4, 6, 8]) == 24
  1421. assert lcm_list([x*(y + 42) - x*y - x*42]) == 0
  1422. lcm = lcm_list([], x)
  1423. assert lcm.is_Number and lcm is S.One
  1424. lcm = lcm_list([], x, polys=True)
  1425. assert lcm.is_Poly and lcm.is_one
  1426. raises(ComputationFailed, lambda: lcm_list([], polys=True))
  1427. def test_gcd():
  1428. f, g = x**3 - 1, x**2 - 1
  1429. s, t = x**2 + x + 1, x + 1
  1430. h, r = x - 1, x**4 + x**3 - x - 1
  1431. F, G, S, T, H, R = [ Poly(u) for u in (f, g, s, t, h, r) ]
  1432. assert F.cofactors(G) == (H, S, T)
  1433. assert F.gcd(G) == H
  1434. assert F.lcm(G) == R
  1435. assert cofactors(f, g) == (h, s, t)
  1436. assert gcd(f, g) == h
  1437. assert lcm(f, g) == r
  1438. assert cofactors(f, g, x) == (h, s, t)
  1439. assert gcd(f, g, x) == h
  1440. assert lcm(f, g, x) == r
  1441. assert cofactors(f, g, (x,)) == (h, s, t)
  1442. assert gcd(f, g, (x,)) == h
  1443. assert lcm(f, g, (x,)) == r
  1444. assert cofactors(F, G) == (H, S, T)
  1445. assert gcd(F, G) == H
  1446. assert lcm(F, G) == R
  1447. assert cofactors(f, g, polys=True) == (H, S, T)
  1448. assert gcd(f, g, polys=True) == H
  1449. assert lcm(f, g, polys=True) == R
  1450. assert cofactors(F, G, polys=False) == (h, s, t)
  1451. assert gcd(F, G, polys=False) == h
  1452. assert lcm(F, G, polys=False) == r
  1453. f, g = 1.0*x**2 - 1.0, 1.0*x - 1.0
  1454. h, s, t = g, 1.0*x + 1.0, 1.0
  1455. assert cofactors(f, g) == (h, s, t)
  1456. assert gcd(f, g) == h
  1457. assert lcm(f, g) == f
  1458. f, g = 1.0*x**2 - 1.0, 1.0*x - 1.0
  1459. h, s, t = g, 1.0*x + 1.0, 1.0
  1460. assert cofactors(f, g) == (h, s, t)
  1461. assert gcd(f, g) == h
  1462. assert lcm(f, g) == f
  1463. assert cofactors(8, 6) == (2, 4, 3)
  1464. assert gcd(8, 6) == 2
  1465. assert lcm(8, 6) == 24
  1466. f, g = x**2 - 3*x - 4, x**3 - 4*x**2 + x - 4
  1467. l = x**4 - 3*x**3 - 3*x**2 - 3*x - 4
  1468. h, s, t = x - 4, x + 1, x**2 + 1
  1469. assert cofactors(f, g, modulus=11) == (h, s, t)
  1470. assert gcd(f, g, modulus=11) == h
  1471. assert lcm(f, g, modulus=11) == l
  1472. f, g = x**2 + 8*x + 7, x**3 + 7*x**2 + x + 7
  1473. l = x**4 + 8*x**3 + 8*x**2 + 8*x + 7
  1474. h, s, t = x + 7, x + 1, x**2 + 1
  1475. assert cofactors(f, g, modulus=11, symmetric=False) == (h, s, t)
  1476. assert gcd(f, g, modulus=11, symmetric=False) == h
  1477. assert lcm(f, g, modulus=11, symmetric=False) == l
  1478. a, b = sqrt(2), -sqrt(2)
  1479. assert gcd(a, b) == gcd(b, a) == sqrt(2)
  1480. a, b = sqrt(-2), -sqrt(-2)
  1481. assert gcd(a, b) == gcd(b, a) == sqrt(2)
  1482. assert gcd(Poly(x - 2, x), Poly(I*x, x)) == Poly(1, x, domain=ZZ_I)
  1483. raises(TypeError, lambda: gcd(x))
  1484. raises(TypeError, lambda: lcm(x))
  1485. def test_gcd_numbers_vs_polys():
  1486. assert isinstance(gcd(3, 9), Integer)
  1487. assert isinstance(gcd(3*x, 9), Integer)
  1488. assert gcd(3, 9) == 3
  1489. assert gcd(3*x, 9) == 3
  1490. assert isinstance(gcd(Rational(3, 2), Rational(9, 4)), Rational)
  1491. assert isinstance(gcd(Rational(3, 2)*x, Rational(9, 4)), Rational)
  1492. assert gcd(Rational(3, 2), Rational(9, 4)) == Rational(3, 4)
  1493. assert gcd(Rational(3, 2)*x, Rational(9, 4)) == 1
  1494. assert isinstance(gcd(3.0, 9.0), Float)
  1495. assert isinstance(gcd(3.0*x, 9.0), Float)
  1496. assert gcd(3.0, 9.0) == 1.0
  1497. assert gcd(3.0*x, 9.0) == 1.0
  1498. # partial fix of 20597
  1499. assert gcd(Mul(2, 3, evaluate=False), 2) == 2
  1500. def test_terms_gcd():
  1501. assert terms_gcd(1) == 1
  1502. assert terms_gcd(1, x) == 1
  1503. assert terms_gcd(x - 1) == x - 1
  1504. assert terms_gcd(-x - 1) == -x - 1
  1505. assert terms_gcd(2*x + 3) == 2*x + 3
  1506. assert terms_gcd(6*x + 4) == Mul(2, 3*x + 2, evaluate=False)
  1507. assert terms_gcd(x**3*y + x*y**3) == x*y*(x**2 + y**2)
  1508. assert terms_gcd(2*x**3*y + 2*x*y**3) == 2*x*y*(x**2 + y**2)
  1509. assert terms_gcd(x**3*y/2 + x*y**3/2) == x*y/2*(x**2 + y**2)
  1510. assert terms_gcd(x**3*y + 2*x*y**3) == x*y*(x**2 + 2*y**2)
  1511. assert terms_gcd(2*x**3*y + 4*x*y**3) == 2*x*y*(x**2 + 2*y**2)
  1512. assert terms_gcd(2*x**3*y/3 + 4*x*y**3/5) == x*y*Rational(2, 15)*(5*x**2 + 6*y**2)
  1513. assert terms_gcd(2.0*x**3*y + 4.1*x*y**3) == x*y*(2.0*x**2 + 4.1*y**2)
  1514. assert _aresame(terms_gcd(2.0*x + 3), 2.0*x + 3)
  1515. assert terms_gcd((3 + 3*x)*(x + x*y), expand=False) == \
  1516. (3*x + 3)*(x*y + x)
  1517. assert terms_gcd((3 + 3*x)*(x + x*sin(3 + 3*y)), expand=False, deep=True) == \
  1518. 3*x*(x + 1)*(sin(Mul(3, y + 1, evaluate=False)) + 1)
  1519. assert terms_gcd(sin(x + x*y), deep=True) == \
  1520. sin(x*(y + 1))
  1521. eq = Eq(2*x, 2*y + 2*z*y)
  1522. assert terms_gcd(eq) == Eq(2*x, 2*y*(z + 1))
  1523. assert terms_gcd(eq, deep=True) == Eq(2*x, 2*y*(z + 1))
  1524. raises(TypeError, lambda: terms_gcd(x < 2))
  1525. def test_trunc():
  1526. f, g = x**5 + 2*x**4 + 3*x**3 + 4*x**2 + 5*x + 6, x**5 - x**4 + x**2 - x
  1527. F, G = Poly(f), Poly(g)
  1528. assert F.trunc(3) == G
  1529. assert trunc(f, 3) == g
  1530. assert trunc(f, 3, x) == g
  1531. assert trunc(f, 3, (x,)) == g
  1532. assert trunc(F, 3) == G
  1533. assert trunc(f, 3, polys=True) == G
  1534. assert trunc(F, 3, polys=False) == g
  1535. f, g = 6*x**5 + 5*x**4 + 4*x**3 + 3*x**2 + 2*x + 1, -x**4 + x**3 - x + 1
  1536. F, G = Poly(f), Poly(g)
  1537. assert F.trunc(3) == G
  1538. assert trunc(f, 3) == g
  1539. assert trunc(f, 3, x) == g
  1540. assert trunc(f, 3, (x,)) == g
  1541. assert trunc(F, 3) == G
  1542. assert trunc(f, 3, polys=True) == G
  1543. assert trunc(F, 3, polys=False) == g
  1544. f = Poly(x**2 + 2*x + 3, modulus=5)
  1545. assert f.trunc(2) == Poly(x**2 + 1, modulus=5)
  1546. def test_monic():
  1547. f, g = 2*x - 1, x - S.Half
  1548. F, G = Poly(f, domain='QQ'), Poly(g)
  1549. assert F.monic() == G
  1550. assert monic(f) == g
  1551. assert monic(f, x) == g
  1552. assert monic(f, (x,)) == g
  1553. assert monic(F) == G
  1554. assert monic(f, polys=True) == G
  1555. assert monic(F, polys=False) == g
  1556. raises(ComputationFailed, lambda: monic(4))
  1557. assert monic(2*x**2 + 6*x + 4, auto=False) == x**2 + 3*x + 2
  1558. raises(ExactQuotientFailed, lambda: monic(2*x + 6*x + 1, auto=False))
  1559. assert monic(2.0*x**2 + 6.0*x + 4.0) == 1.0*x**2 + 3.0*x + 2.0
  1560. assert monic(2*x**2 + 3*x + 4, modulus=5) == x**2 - x + 2
  1561. def test_content():
  1562. f, F = 4*x + 2, Poly(4*x + 2)
  1563. assert F.content() == 2
  1564. assert content(f) == 2
  1565. raises(ComputationFailed, lambda: content(4))
  1566. f = Poly(2*x, modulus=3)
  1567. assert f.content() == 1
  1568. def test_primitive():
  1569. f, g = 4*x + 2, 2*x + 1
  1570. F, G = Poly(f), Poly(g)
  1571. assert F.primitive() == (2, G)
  1572. assert primitive(f) == (2, g)
  1573. assert primitive(f, x) == (2, g)
  1574. assert primitive(f, (x,)) == (2, g)
  1575. assert primitive(F) == (2, G)
  1576. assert primitive(f, polys=True) == (2, G)
  1577. assert primitive(F, polys=False) == (2, g)
  1578. raises(ComputationFailed, lambda: primitive(4))
  1579. f = Poly(2*x, modulus=3)
  1580. g = Poly(2.0*x, domain=RR)
  1581. assert f.primitive() == (1, f)
  1582. assert g.primitive() == (1.0, g)
  1583. assert primitive(S('-3*x/4 + y + 11/8')) == \
  1584. S('(1/8, -6*x + 8*y + 11)')
  1585. def test_compose():
  1586. f = x**12 + 20*x**10 + 150*x**8 + 500*x**6 + 625*x**4 - 2*x**3 - 10*x + 9
  1587. g = x**4 - 2*x + 9
  1588. h = x**3 + 5*x
  1589. F, G, H = map(Poly, (f, g, h))
  1590. assert G.compose(H) == F
  1591. assert compose(g, h) == f
  1592. assert compose(g, h, x) == f
  1593. assert compose(g, h, (x,)) == f
  1594. assert compose(G, H) == F
  1595. assert compose(g, h, polys=True) == F
  1596. assert compose(G, H, polys=False) == f
  1597. assert F.decompose() == [G, H]
  1598. assert decompose(f) == [g, h]
  1599. assert decompose(f, x) == [g, h]
  1600. assert decompose(f, (x,)) == [g, h]
  1601. assert decompose(F) == [G, H]
  1602. assert decompose(f, polys=True) == [G, H]
  1603. assert decompose(F, polys=False) == [g, h]
  1604. raises(ComputationFailed, lambda: compose(4, 2))
  1605. raises(ComputationFailed, lambda: decompose(4))
  1606. assert compose(x**2 - y**2, x - y, x, y) == x**2 - 2*x*y
  1607. assert compose(x**2 - y**2, x - y, y, x) == -y**2 + 2*x*y
  1608. def test_shift():
  1609. assert Poly(x**2 - 2*x + 1, x).shift(2) == Poly(x**2 + 2*x + 1, x)
  1610. def test_transform():
  1611. # Also test that 3-way unification is done correctly
  1612. assert Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1), Poly(x - 1)) == \
  1613. Poly(4, x) == \
  1614. cancel((x - 1)**2*(x**2 - 2*x + 1).subs(x, (x + 1)/(x - 1)))
  1615. assert Poly(x**2 - x/2 + 1, x).transform(Poly(x + 1), Poly(x - 1)) == \
  1616. Poly(3*x**2/2 + Rational(5, 2), x) == \
  1617. cancel((x - 1)**2*(x**2 - x/2 + 1).subs(x, (x + 1)/(x - 1)))
  1618. assert Poly(x**2 - 2*x + 1, x).transform(Poly(x + S.Half), Poly(x - 1)) == \
  1619. Poly(Rational(9, 4), x) == \
  1620. cancel((x - 1)**2*(x**2 - 2*x + 1).subs(x, (x + S.Half)/(x - 1)))
  1621. assert Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1), Poly(x - S.Half)) == \
  1622. Poly(Rational(9, 4), x) == \
  1623. cancel((x - S.Half)**2*(x**2 - 2*x + 1).subs(x, (x + 1)/(x - S.Half)))
  1624. # Unify ZZ, QQ, and RR
  1625. assert Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1.0), Poly(x - S.Half)) == \
  1626. Poly(Rational(9, 4), x, domain='RR') == \
  1627. cancel((x - S.Half)**2*(x**2 - 2*x + 1).subs(x, (x + 1.0)/(x - S.Half)))
  1628. raises(ValueError, lambda: Poly(x*y).transform(Poly(x + 1), Poly(x - 1)))
  1629. raises(ValueError, lambda: Poly(x).transform(Poly(y + 1), Poly(x - 1)))
  1630. raises(ValueError, lambda: Poly(x).transform(Poly(x + 1), Poly(y - 1)))
  1631. raises(ValueError, lambda: Poly(x).transform(Poly(x*y + 1), Poly(x - 1)))
  1632. raises(ValueError, lambda: Poly(x).transform(Poly(x + 1), Poly(x*y - 1)))
  1633. def test_sturm():
  1634. f, F = x, Poly(x, domain='QQ')
  1635. g, G = 1, Poly(1, x, domain='QQ')
  1636. assert F.sturm() == [F, G]
  1637. assert sturm(f) == [f, g]
  1638. assert sturm(f, x) == [f, g]
  1639. assert sturm(f, (x,)) == [f, g]
  1640. assert sturm(F) == [F, G]
  1641. assert sturm(f, polys=True) == [F, G]
  1642. assert sturm(F, polys=False) == [f, g]
  1643. raises(ComputationFailed, lambda: sturm(4))
  1644. raises(DomainError, lambda: sturm(f, auto=False))
  1645. f = Poly(S(1024)/(15625*pi**8)*x**5
  1646. - S(4096)/(625*pi**8)*x**4
  1647. + S(32)/(15625*pi**4)*x**3
  1648. - S(128)/(625*pi**4)*x**2
  1649. + Rational(1, 62500)*x
  1650. - Rational(1, 625), x, domain='ZZ(pi)')
  1651. assert sturm(f) == \
  1652. [Poly(x**3 - 100*x**2 + pi**4/64*x - 25*pi**4/16, x, domain='ZZ(pi)'),
  1653. Poly(3*x**2 - 200*x + pi**4/64, x, domain='ZZ(pi)'),
  1654. Poly((Rational(20000, 9) - pi**4/96)*x + 25*pi**4/18, x, domain='ZZ(pi)'),
  1655. Poly((-3686400000000*pi**4 - 11520000*pi**8 - 9*pi**12)/(26214400000000 - 245760000*pi**4 + 576*pi**8), x, domain='ZZ(pi)')]
  1656. def test_gff():
  1657. f = x**5 + 2*x**4 - x**3 - 2*x**2
  1658. assert Poly(f).gff_list() == [(Poly(x), 1), (Poly(x + 2), 4)]
  1659. assert gff_list(f) == [(x, 1), (x + 2, 4)]
  1660. raises(NotImplementedError, lambda: gff(f))
  1661. f = x*(x - 1)**3*(x - 2)**2*(x - 4)**2*(x - 5)
  1662. assert Poly(f).gff_list() == [(
  1663. Poly(x**2 - 5*x + 4), 1), (Poly(x**2 - 5*x + 4), 2), (Poly(x), 3)]
  1664. assert gff_list(f) == [(x**2 - 5*x + 4, 1), (x**2 - 5*x + 4, 2), (x, 3)]
  1665. raises(NotImplementedError, lambda: gff(f))
  1666. def test_norm():
  1667. a, b = sqrt(2), sqrt(3)
  1668. f = Poly(a*x + b*y, x, y, extension=(a, b))
  1669. assert f.norm() == Poly(4*x**4 - 12*x**2*y**2 + 9*y**4, x, y, domain='QQ')
  1670. def test_sqf_norm():
  1671. assert sqf_norm(x**2 - 2, extension=sqrt(3)) == \
  1672. (1, x**2 - 2*sqrt(3)*x + 1, x**4 - 10*x**2 + 1)
  1673. assert sqf_norm(x**2 - 3, extension=sqrt(2)) == \
  1674. (1, x**2 - 2*sqrt(2)*x - 1, x**4 - 10*x**2 + 1)
  1675. assert Poly(x**2 - 2, extension=sqrt(3)).sqf_norm() == \
  1676. (1, Poly(x**2 - 2*sqrt(3)*x + 1, x, extension=sqrt(3)),
  1677. Poly(x**4 - 10*x**2 + 1, x, domain='QQ'))
  1678. assert Poly(x**2 - 3, extension=sqrt(2)).sqf_norm() == \
  1679. (1, Poly(x**2 - 2*sqrt(2)*x - 1, x, extension=sqrt(2)),
  1680. Poly(x**4 - 10*x**2 + 1, x, domain='QQ'))
  1681. def test_sqf():
  1682. f = x**5 - x**3 - x**2 + 1
  1683. g = x**3 + 2*x**2 + 2*x + 1
  1684. h = x - 1
  1685. p = x**4 + x**3 - x - 1
  1686. F, G, H, P = map(Poly, (f, g, h, p))
  1687. assert F.sqf_part() == P
  1688. assert sqf_part(f) == p
  1689. assert sqf_part(f, x) == p
  1690. assert sqf_part(f, (x,)) == p
  1691. assert sqf_part(F) == P
  1692. assert sqf_part(f, polys=True) == P
  1693. assert sqf_part(F, polys=False) == p
  1694. assert F.sqf_list() == (1, [(G, 1), (H, 2)])
  1695. assert sqf_list(f) == (1, [(g, 1), (h, 2)])
  1696. assert sqf_list(f, x) == (1, [(g, 1), (h, 2)])
  1697. assert sqf_list(f, (x,)) == (1, [(g, 1), (h, 2)])
  1698. assert sqf_list(F) == (1, [(G, 1), (H, 2)])
  1699. assert sqf_list(f, polys=True) == (1, [(G, 1), (H, 2)])
  1700. assert sqf_list(F, polys=False) == (1, [(g, 1), (h, 2)])
  1701. assert F.sqf_list_include() == [(G, 1), (H, 2)]
  1702. raises(ComputationFailed, lambda: sqf_part(4))
  1703. assert sqf(1) == 1
  1704. assert sqf_list(1) == (1, [])
  1705. assert sqf((2*x**2 + 2)**7) == 128*(x**2 + 1)**7
  1706. assert sqf(f) == g*h**2
  1707. assert sqf(f, x) == g*h**2
  1708. assert sqf(f, (x,)) == g*h**2
  1709. d = x**2 + y**2
  1710. assert sqf(f/d) == (g*h**2)/d
  1711. assert sqf(f/d, x) == (g*h**2)/d
  1712. assert sqf(f/d, (x,)) == (g*h**2)/d
  1713. assert sqf(x - 1) == x - 1
  1714. assert sqf(-x - 1) == -x - 1
  1715. assert sqf(x - 1) == x - 1
  1716. assert sqf(6*x - 10) == Mul(2, 3*x - 5, evaluate=False)
  1717. assert sqf((6*x - 10)/(3*x - 6)) == Rational(2, 3)*((3*x - 5)/(x - 2))
  1718. assert sqf(Poly(x**2 - 2*x + 1)) == (x - 1)**2
  1719. f = 3 + x - x*(1 + x) + x**2
  1720. assert sqf(f) == 3
  1721. f = (x**2 + 2*x + 1)**20000000000
  1722. assert sqf(f) == (x + 1)**40000000000
  1723. assert sqf_list(f) == (1, [(x + 1, 40000000000)])
  1724. def test_factor():
  1725. f = x**5 - x**3 - x**2 + 1
  1726. u = x + 1
  1727. v = x - 1
  1728. w = x**2 + x + 1
  1729. F, U, V, W = map(Poly, (f, u, v, w))
  1730. assert F.factor_list() == (1, [(U, 1), (V, 2), (W, 1)])
  1731. assert factor_list(f) == (1, [(u, 1), (v, 2), (w, 1)])
  1732. assert factor_list(f, x) == (1, [(u, 1), (v, 2), (w, 1)])
  1733. assert factor_list(f, (x,)) == (1, [(u, 1), (v, 2), (w, 1)])
  1734. assert factor_list(F) == (1, [(U, 1), (V, 2), (W, 1)])
  1735. assert factor_list(f, polys=True) == (1, [(U, 1), (V, 2), (W, 1)])
  1736. assert factor_list(F, polys=False) == (1, [(u, 1), (v, 2), (w, 1)])
  1737. assert F.factor_list_include() == [(U, 1), (V, 2), (W, 1)]
  1738. assert factor_list(1) == (1, [])
  1739. assert factor_list(6) == (6, [])
  1740. assert factor_list(sqrt(3), x) == (sqrt(3), [])
  1741. assert factor_list((-1)**x, x) == (1, [(-1, x)])
  1742. assert factor_list((2*x)**y, x) == (1, [(2, y), (x, y)])
  1743. assert factor_list(sqrt(x*y), x) == (1, [(x*y, S.Half)])
  1744. assert factor(6) == 6 and factor(6).is_Integer
  1745. assert factor_list(3*x) == (3, [(x, 1)])
  1746. assert factor_list(3*x**2) == (3, [(x, 2)])
  1747. assert factor(3*x) == 3*x
  1748. assert factor(3*x**2) == 3*x**2
  1749. assert factor((2*x**2 + 2)**7) == 128*(x**2 + 1)**7
  1750. assert factor(f) == u*v**2*w
  1751. assert factor(f, x) == u*v**2*w
  1752. assert factor(f, (x,)) == u*v**2*w
  1753. g, p, q, r = x**2 - y**2, x - y, x + y, x**2 + 1
  1754. assert factor(f/g) == (u*v**2*w)/(p*q)
  1755. assert factor(f/g, x) == (u*v**2*w)/(p*q)
  1756. assert factor(f/g, (x,)) == (u*v**2*w)/(p*q)
  1757. p = Symbol('p', positive=True)
  1758. i = Symbol('i', integer=True)
  1759. r = Symbol('r', real=True)
  1760. assert factor(sqrt(x*y)).is_Pow is True
  1761. assert factor(sqrt(3*x**2 - 3)) == sqrt(3)*sqrt((x - 1)*(x + 1))
  1762. assert factor(sqrt(3*x**2 + 3)) == sqrt(3)*sqrt(x**2 + 1)
  1763. assert factor((y*x**2 - y)**i) == y**i*(x - 1)**i*(x + 1)**i
  1764. assert factor((y*x**2 + y)**i) == y**i*(x**2 + 1)**i
  1765. assert factor((y*x**2 - y)**t) == (y*(x - 1)*(x + 1))**t
  1766. assert factor((y*x**2 + y)**t) == (y*(x**2 + 1))**t
  1767. f = sqrt(expand((r**2 + 1)*(p + 1)*(p - 1)*(p - 2)**3))
  1768. g = sqrt((p - 2)**3*(p - 1))*sqrt(p + 1)*sqrt(r**2 + 1)
  1769. assert factor(f) == g
  1770. assert factor(g) == g
  1771. g = (x - 1)**5*(r**2 + 1)
  1772. f = sqrt(expand(g))
  1773. assert factor(f) == sqrt(g)
  1774. f = Poly(sin(1)*x + 1, x, domain=EX)
  1775. assert f.factor_list() == (1, [(f, 1)])
  1776. f = x**4 + 1
  1777. assert factor(f) == f
  1778. assert factor(f, extension=I) == (x**2 - I)*(x**2 + I)
  1779. assert factor(f, gaussian=True) == (x**2 - I)*(x**2 + I)
  1780. assert factor(
  1781. f, extension=sqrt(2)) == (x**2 + sqrt(2)*x + 1)*(x**2 - sqrt(2)*x + 1)
  1782. assert factor(x**2 + 4*I*x - 4) == (x + 2*I)**2
  1783. f = x**2 + 2*I*x - 4
  1784. assert factor(f) == f
  1785. f = 8192*x**2 + x*(22656 + 175232*I) - 921416 + 242313*I
  1786. f_zzi = I*(x*(64 - 64*I) + 773 + 596*I)**2
  1787. f_qqi = 8192*(x + S(177)/128 + 1369*I/128)**2
  1788. assert factor(f) == f_zzi
  1789. assert factor(f, domain=ZZ_I) == f_zzi
  1790. assert factor(f, domain=QQ_I) == f_qqi
  1791. f = x**2 + 2*sqrt(2)*x + 2
  1792. assert factor(f, extension=sqrt(2)) == (x + sqrt(2))**2
  1793. assert factor(f**3, extension=sqrt(2)) == (x + sqrt(2))**6
  1794. assert factor(x**2 - 2*y**2, extension=sqrt(2)) == \
  1795. (x + sqrt(2)*y)*(x - sqrt(2)*y)
  1796. assert factor(2*x**2 - 4*y**2, extension=sqrt(2)) == \
  1797. 2*((x + sqrt(2)*y)*(x - sqrt(2)*y))
  1798. assert factor(x - 1) == x - 1
  1799. assert factor(-x - 1) == -x - 1
  1800. assert factor(x - 1) == x - 1
  1801. assert factor(6*x - 10) == Mul(2, 3*x - 5, evaluate=False)
  1802. assert factor(x**11 + x + 1, modulus=65537, symmetric=True) == \
  1803. (x**2 + x + 1)*(x**9 - x**8 + x**6 - x**5 + x**3 - x** 2 + 1)
  1804. assert factor(x**11 + x + 1, modulus=65537, symmetric=False) == \
  1805. (x**2 + x + 1)*(x**9 + 65536*x**8 + x**6 + 65536*x**5 +
  1806. x**3 + 65536*x** 2 + 1)
  1807. f = x/pi + x*sin(x)/pi
  1808. g = y/(pi**2 + 2*pi + 1) + y*sin(x)/(pi**2 + 2*pi + 1)
  1809. assert factor(f) == x*(sin(x) + 1)/pi
  1810. assert factor(g) == y*(sin(x) + 1)/(pi + 1)**2
  1811. assert factor(Eq(
  1812. x**2 + 2*x + 1, x**3 + 1)) == Eq((x + 1)**2, (x + 1)*(x**2 - x + 1))
  1813. f = (x**2 - 1)/(x**2 + 4*x + 4)
  1814. assert factor(f) == (x + 1)*(x - 1)/(x + 2)**2
  1815. assert factor(f, x) == (x + 1)*(x - 1)/(x + 2)**2
  1816. f = 3 + x - x*(1 + x) + x**2
  1817. assert factor(f) == 3
  1818. assert factor(f, x) == 3
  1819. assert factor(1/(x**2 + 2*x + 1/x) - 1) == -((1 - x + 2*x**2 +
  1820. x**3)/(1 + 2*x**2 + x**3))
  1821. assert factor(f, expand=False) == f
  1822. raises(PolynomialError, lambda: factor(f, x, expand=False))
  1823. raises(FlagError, lambda: factor(x**2 - 1, polys=True))
  1824. assert factor([x, Eq(x**2 - y**2, Tuple(x**2 - z**2, 1/x + 1/y))]) == \
  1825. [x, Eq((x - y)*(x + y), Tuple((x - z)*(x + z), (x + y)/x/y))]
  1826. assert not isinstance(
  1827. Poly(x**3 + x + 1).factor_list()[1][0][0], PurePoly) is True
  1828. assert isinstance(
  1829. PurePoly(x**3 + x + 1).factor_list()[1][0][0], PurePoly) is True
  1830. assert factor(sqrt(-x)) == sqrt(-x)
  1831. # issue 5917
  1832. e = (-2*x*(-x + 1)*(x - 1)*(-x*(-x + 1)*(x - 1) - x*(x - 1)**2)*(x**2*(x -
  1833. 1) - x*(x - 1) - x) - (-2*x**2*(x - 1)**2 - x*(-x + 1)*(-x*(-x + 1) +
  1834. x*(x - 1)))*(x**2*(x - 1)**4 - x*(-x*(-x + 1)*(x - 1) - x*(x - 1)**2)))
  1835. assert factor(e) == 0
  1836. # deep option
  1837. assert factor(sin(x**2 + x) + x, deep=True) == sin(x*(x + 1)) + x
  1838. assert factor(sin(x**2 + x)*x, deep=True) == sin(x*(x + 1))*x
  1839. assert factor(sqrt(x**2)) == sqrt(x**2)
  1840. # issue 13149
  1841. assert factor(expand((0.5*x+1)*(0.5*y+1))) == Mul(1.0, 0.5*x + 1.0,
  1842. 0.5*y + 1.0, evaluate = False)
  1843. assert factor(expand((0.5*x+0.5)**2)) == 0.25*(1.0*x + 1.0)**2
  1844. eq = x**2*y**2 + 11*x**2*y + 30*x**2 + 7*x*y**2 + 77*x*y + 210*x + 12*y**2 + 132*y + 360
  1845. assert factor(eq, x) == (x + 3)*(x + 4)*(y**2 + 11*y + 30)
  1846. assert factor(eq, x, deep=True) == (x + 3)*(x + 4)*(y**2 + 11*y + 30)
  1847. assert factor(eq, y, deep=True) == (y + 5)*(y + 6)*(x**2 + 7*x + 12)
  1848. # fraction option
  1849. f = 5*x + 3*exp(2 - 7*x)
  1850. assert factor(f, deep=True) == factor(f, deep=True, fraction=True)
  1851. assert factor(f, deep=True, fraction=False) == 5*x + 3*exp(2)*exp(-7*x)
  1852. assert factor_list(x**3 - x*y**2, t, w, x) == (
  1853. 1, [(x, 1), (x - y, 1), (x + y, 1)])
  1854. def test_factor_large():
  1855. f = (x**2 + 4*x + 4)**10000000*(x**2 + 1)*(x**2 + 2*x + 1)**1234567
  1856. g = ((x**2 + 2*x + 1)**3000*y**2 + (x**2 + 2*x + 1)**3000*2*y + (
  1857. x**2 + 2*x + 1)**3000)
  1858. assert factor(f) == (x + 2)**20000000*(x**2 + 1)*(x + 1)**2469134
  1859. assert factor(g) == (x + 1)**6000*(y + 1)**2
  1860. assert factor_list(
  1861. f) == (1, [(x + 1, 2469134), (x + 2, 20000000), (x**2 + 1, 1)])
  1862. assert factor_list(g) == (1, [(y + 1, 2), (x + 1, 6000)])
  1863. f = (x**2 - y**2)**200000*(x**7 + 1)
  1864. g = (x**2 + y**2)**200000*(x**7 + 1)
  1865. assert factor(f) == \
  1866. (x + 1)*(x - y)**200000*(x + y)**200000*(x**6 - x**5 +
  1867. x**4 - x**3 + x**2 - x + 1)
  1868. assert factor(g, gaussian=True) == \
  1869. (x + 1)*(x - I*y)**200000*(x + I*y)**200000*(x**6 - x**5 +
  1870. x**4 - x**3 + x**2 - x + 1)
  1871. assert factor_list(f) == \
  1872. (1, [(x + 1, 1), (x - y, 200000), (x + y, 200000), (x**6 -
  1873. x**5 + x**4 - x**3 + x**2 - x + 1, 1)])
  1874. assert factor_list(g, gaussian=True) == \
  1875. (1, [(x + 1, 1), (x - I*y, 200000), (x + I*y, 200000), (
  1876. x**6 - x**5 + x**4 - x**3 + x**2 - x + 1, 1)])
  1877. def test_factor_noeval():
  1878. assert factor(6*x - 10) == Mul(2, 3*x - 5, evaluate=False)
  1879. assert factor((6*x - 10)/(3*x - 6)) == Mul(Rational(2, 3), 3*x - 5, 1/(x - 2))
  1880. def test_intervals():
  1881. assert intervals(0) == []
  1882. assert intervals(1) == []
  1883. assert intervals(x, sqf=True) == [(0, 0)]
  1884. assert intervals(x) == [((0, 0), 1)]
  1885. assert intervals(x**128) == [((0, 0), 128)]
  1886. assert intervals([x**2, x**4]) == [((0, 0), {0: 2, 1: 4})]
  1887. f = Poly((x*Rational(2, 5) - Rational(17, 3))*(4*x + Rational(1, 257)))
  1888. assert f.intervals(sqf=True) == [(-1, 0), (14, 15)]
  1889. assert f.intervals() == [((-1, 0), 1), ((14, 15), 1)]
  1890. assert f.intervals(fast=True, sqf=True) == [(-1, 0), (14, 15)]
  1891. assert f.intervals(fast=True) == [((-1, 0), 1), ((14, 15), 1)]
  1892. assert f.intervals(eps=Rational(1, 10)) == f.intervals(eps=0.1) == \
  1893. [((Rational(-1, 258), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1894. assert f.intervals(eps=Rational(1, 100)) == f.intervals(eps=0.01) == \
  1895. [((Rational(-1, 258), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1896. assert f.intervals(eps=Rational(1, 1000)) == f.intervals(eps=0.001) == \
  1897. [((Rational(-1, 1002), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1898. assert f.intervals(eps=Rational(1, 10000)) == f.intervals(eps=0.0001) == \
  1899. [((Rational(-1, 1028), Rational(-1, 1028)), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1900. f = (x*Rational(2, 5) - Rational(17, 3))*(4*x + Rational(1, 257))
  1901. assert intervals(f, sqf=True) == [(-1, 0), (14, 15)]
  1902. assert intervals(f) == [((-1, 0), 1), ((14, 15), 1)]
  1903. assert intervals(f, eps=Rational(1, 10)) == intervals(f, eps=0.1) == \
  1904. [((Rational(-1, 258), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1905. assert intervals(f, eps=Rational(1, 100)) == intervals(f, eps=0.01) == \
  1906. [((Rational(-1, 258), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1907. assert intervals(f, eps=Rational(1, 1000)) == intervals(f, eps=0.001) == \
  1908. [((Rational(-1, 1002), 0), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1909. assert intervals(f, eps=Rational(1, 10000)) == intervals(f, eps=0.0001) == \
  1910. [((Rational(-1, 1028), Rational(-1, 1028)), 1), ((Rational(85, 6), Rational(85, 6)), 1)]
  1911. f = Poly((x**2 - 2)*(x**2 - 3)**7*(x + 1)*(7*x + 3)**3)
  1912. assert f.intervals() == \
  1913. [((-2, Rational(-3, 2)), 7), ((Rational(-3, 2), -1), 1),
  1914. ((-1, -1), 1), ((-1, 0), 3),
  1915. ((1, Rational(3, 2)), 1), ((Rational(3, 2), 2), 7)]
  1916. assert intervals([x**5 - 200, x**5 - 201]) == \
  1917. [((Rational(75, 26), Rational(101, 35)), {0: 1}), ((Rational(309, 107), Rational(26, 9)), {1: 1})]
  1918. assert intervals([x**5 - 200, x**5 - 201], fast=True) == \
  1919. [((Rational(75, 26), Rational(101, 35)), {0: 1}), ((Rational(309, 107), Rational(26, 9)), {1: 1})]
  1920. assert intervals([x**2 - 200, x**2 - 201]) == \
  1921. [((Rational(-71, 5), Rational(-85, 6)), {1: 1}), ((Rational(-85, 6), -14), {0: 1}),
  1922. ((14, Rational(85, 6)), {0: 1}), ((Rational(85, 6), Rational(71, 5)), {1: 1})]
  1923. assert intervals([x + 1, x + 2, x - 1, x + 1, 1, x - 1, x - 1, (x - 2)**2]) == \
  1924. [((-2, -2), {1: 1}), ((-1, -1), {0: 1, 3: 1}), ((1, 1), {2:
  1925. 1, 5: 1, 6: 1}), ((2, 2), {7: 2})]
  1926. f, g, h = x**2 - 2, x**4 - 4*x**2 + 4, x - 1
  1927. assert intervals(f, inf=Rational(7, 4), sqf=True) == []
  1928. assert intervals(f, inf=Rational(7, 5), sqf=True) == [(Rational(7, 5), Rational(3, 2))]
  1929. assert intervals(f, sup=Rational(7, 4), sqf=True) == [(-2, -1), (1, Rational(3, 2))]
  1930. assert intervals(f, sup=Rational(7, 5), sqf=True) == [(-2, -1)]
  1931. assert intervals(g, inf=Rational(7, 4)) == []
  1932. assert intervals(g, inf=Rational(7, 5)) == [((Rational(7, 5), Rational(3, 2)), 2)]
  1933. assert intervals(g, sup=Rational(7, 4)) == [((-2, -1), 2), ((1, Rational(3, 2)), 2)]
  1934. assert intervals(g, sup=Rational(7, 5)) == [((-2, -1), 2)]
  1935. assert intervals([g, h], inf=Rational(7, 4)) == []
  1936. assert intervals([g, h], inf=Rational(7, 5)) == [((Rational(7, 5), Rational(3, 2)), {0: 2})]
  1937. assert intervals([g, h], sup=S(
  1938. 7)/4) == [((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, Rational(3, 2)), {0: 2})]
  1939. assert intervals(
  1940. [g, h], sup=Rational(7, 5)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1})]
  1941. assert intervals([x + 2, x**2 - 2]) == \
  1942. [((-2, -2), {0: 1}), ((-2, -1), {1: 1}), ((1, 2), {1: 1})]
  1943. assert intervals([x + 2, x**2 - 2], strict=True) == \
  1944. [((-2, -2), {0: 1}), ((Rational(-3, 2), -1), {1: 1}), ((1, 2), {1: 1})]
  1945. f = 7*z**4 - 19*z**3 + 20*z**2 + 17*z + 20
  1946. assert intervals(f) == []
  1947. real_part, complex_part = intervals(f, all=True, sqf=True)
  1948. assert real_part == []
  1949. assert all(re(a) < re(r) < re(b) and im(
  1950. a) < im(r) < im(b) for (a, b), r in zip(complex_part, nroots(f)))
  1951. assert complex_part == [(Rational(-40, 7) - I*40/7, 0),
  1952. (Rational(-40, 7), I*40/7),
  1953. (I*Rational(-40, 7), Rational(40, 7)),
  1954. (0, Rational(40, 7) + I*40/7)]
  1955. real_part, complex_part = intervals(f, all=True, sqf=True, eps=Rational(1, 10))
  1956. assert real_part == []
  1957. assert all(re(a) < re(r) < re(b) and im(
  1958. a) < im(r) < im(b) for (a, b), r in zip(complex_part, nroots(f)))
  1959. raises(ValueError, lambda: intervals(x**2 - 2, eps=10**-100000))
  1960. raises(ValueError, lambda: Poly(x**2 - 2).intervals(eps=10**-100000))
  1961. raises(
  1962. ValueError, lambda: intervals([x**2 - 2, x**2 - 3], eps=10**-100000))
  1963. def test_refine_root():
  1964. f = Poly(x**2 - 2)
  1965. assert f.refine_root(1, 2, steps=0) == (1, 2)
  1966. assert f.refine_root(-2, -1, steps=0) == (-2, -1)
  1967. assert f.refine_root(1, 2, steps=None) == (1, Rational(3, 2))
  1968. assert f.refine_root(-2, -1, steps=None) == (Rational(-3, 2), -1)
  1969. assert f.refine_root(1, 2, steps=1) == (1, Rational(3, 2))
  1970. assert f.refine_root(-2, -1, steps=1) == (Rational(-3, 2), -1)
  1971. assert f.refine_root(1, 2, steps=1, fast=True) == (1, Rational(3, 2))
  1972. assert f.refine_root(-2, -1, steps=1, fast=True) == (Rational(-3, 2), -1)
  1973. assert f.refine_root(1, 2, eps=Rational(1, 100)) == (Rational(24, 17), Rational(17, 12))
  1974. assert f.refine_root(1, 2, eps=1e-2) == (Rational(24, 17), Rational(17, 12))
  1975. raises(PolynomialError, lambda: (f**2).refine_root(1, 2, check_sqf=True))
  1976. raises(RefinementFailed, lambda: (f**2).refine_root(1, 2))
  1977. raises(RefinementFailed, lambda: (f**2).refine_root(2, 3))
  1978. f = x**2 - 2
  1979. assert refine_root(f, 1, 2, steps=1) == (1, Rational(3, 2))
  1980. assert refine_root(f, -2, -1, steps=1) == (Rational(-3, 2), -1)
  1981. assert refine_root(f, 1, 2, steps=1, fast=True) == (1, Rational(3, 2))
  1982. assert refine_root(f, -2, -1, steps=1, fast=True) == (Rational(-3, 2), -1)
  1983. assert refine_root(f, 1, 2, eps=Rational(1, 100)) == (Rational(24, 17), Rational(17, 12))
  1984. assert refine_root(f, 1, 2, eps=1e-2) == (Rational(24, 17), Rational(17, 12))
  1985. raises(PolynomialError, lambda: refine_root(1, 7, 8, eps=Rational(1, 100)))
  1986. raises(ValueError, lambda: Poly(f).refine_root(1, 2, eps=10**-100000))
  1987. raises(ValueError, lambda: refine_root(f, 1, 2, eps=10**-100000))
  1988. def test_count_roots():
  1989. assert count_roots(x**2 - 2) == 2
  1990. assert count_roots(x**2 - 2, inf=-oo) == 2
  1991. assert count_roots(x**2 - 2, sup=+oo) == 2
  1992. assert count_roots(x**2 - 2, inf=-oo, sup=+oo) == 2
  1993. assert count_roots(x**2 - 2, inf=-2) == 2
  1994. assert count_roots(x**2 - 2, inf=-1) == 1
  1995. assert count_roots(x**2 - 2, sup=1) == 1
  1996. assert count_roots(x**2 - 2, sup=2) == 2
  1997. assert count_roots(x**2 - 2, inf=-1, sup=1) == 0
  1998. assert count_roots(x**2 - 2, inf=-2, sup=2) == 2
  1999. assert count_roots(x**2 - 2, inf=-1, sup=1) == 0
  2000. assert count_roots(x**2 - 2, inf=-2, sup=2) == 2
  2001. assert count_roots(x**2 + 2) == 0
  2002. assert count_roots(x**2 + 2, inf=-2*I) == 2
  2003. assert count_roots(x**2 + 2, sup=+2*I) == 2
  2004. assert count_roots(x**2 + 2, inf=-2*I, sup=+2*I) == 2
  2005. assert count_roots(x**2 + 2, inf=0) == 0
  2006. assert count_roots(x**2 + 2, sup=0) == 0
  2007. assert count_roots(x**2 + 2, inf=-I) == 1
  2008. assert count_roots(x**2 + 2, sup=+I) == 1
  2009. assert count_roots(x**2 + 2, inf=+I/2, sup=+I) == 0
  2010. assert count_roots(x**2 + 2, inf=-I, sup=-I/2) == 0
  2011. raises(PolynomialError, lambda: count_roots(1))
  2012. def test_Poly_root():
  2013. f = Poly(2*x**3 - 7*x**2 + 4*x + 4)
  2014. assert f.root(0) == Rational(-1, 2)
  2015. assert f.root(1) == 2
  2016. assert f.root(2) == 2
  2017. raises(IndexError, lambda: f.root(3))
  2018. assert Poly(x**5 + x + 1).root(0) == rootof(x**3 - x**2 + 1, 0)
  2019. def test_real_roots():
  2020. assert real_roots(x) == [0]
  2021. assert real_roots(x, multiple=False) == [(0, 1)]
  2022. assert real_roots(x**3) == [0, 0, 0]
  2023. assert real_roots(x**3, multiple=False) == [(0, 3)]
  2024. assert real_roots(x*(x**3 + x + 3)) == [rootof(x**3 + x + 3, 0), 0]
  2025. assert real_roots(x*(x**3 + x + 3), multiple=False) == [(rootof(
  2026. x**3 + x + 3, 0), 1), (0, 1)]
  2027. assert real_roots(
  2028. x**3*(x**3 + x + 3)) == [rootof(x**3 + x + 3, 0), 0, 0, 0]
  2029. assert real_roots(x**3*(x**3 + x + 3), multiple=False) == [(rootof(
  2030. x**3 + x + 3, 0), 1), (0, 3)]
  2031. f = 2*x**3 - 7*x**2 + 4*x + 4
  2032. g = x**3 + x + 1
  2033. assert Poly(f).real_roots() == [Rational(-1, 2), 2, 2]
  2034. assert Poly(g).real_roots() == [rootof(g, 0)]
  2035. def test_all_roots():
  2036. f = 2*x**3 - 7*x**2 + 4*x + 4
  2037. g = x**3 + x + 1
  2038. assert Poly(f).all_roots() == [Rational(-1, 2), 2, 2]
  2039. assert Poly(g).all_roots() == [rootof(g, 0), rootof(g, 1), rootof(g, 2)]
  2040. def test_nroots():
  2041. assert Poly(0, x).nroots() == []
  2042. assert Poly(1, x).nroots() == []
  2043. assert Poly(x**2 - 1, x).nroots() == [-1.0, 1.0]
  2044. assert Poly(x**2 + 1, x).nroots() == [-1.0*I, 1.0*I]
  2045. roots = Poly(x**2 - 1, x).nroots()
  2046. assert roots == [-1.0, 1.0]
  2047. roots = Poly(x**2 + 1, x).nroots()
  2048. assert roots == [-1.0*I, 1.0*I]
  2049. roots = Poly(x**2/3 - Rational(1, 3), x).nroots()
  2050. assert roots == [-1.0, 1.0]
  2051. roots = Poly(x**2/3 + Rational(1, 3), x).nroots()
  2052. assert roots == [-1.0*I, 1.0*I]
  2053. assert Poly(x**2 + 2*I, x).nroots() == [-1.0 + 1.0*I, 1.0 - 1.0*I]
  2054. assert Poly(
  2055. x**2 + 2*I, x, extension=I).nroots() == [-1.0 + 1.0*I, 1.0 - 1.0*I]
  2056. assert Poly(0.2*x + 0.1).nroots() == [-0.5]
  2057. roots = nroots(x**5 + x + 1, n=5)
  2058. eps = Float("1e-5")
  2059. assert re(roots[0]).epsilon_eq(-0.75487, eps) is S.true
  2060. assert im(roots[0]) == 0.0
  2061. assert re(roots[1]) == Float(-0.5, 5)
  2062. assert im(roots[1]).epsilon_eq(-0.86602, eps) is S.true
  2063. assert re(roots[2]) == Float(-0.5, 5)
  2064. assert im(roots[2]).epsilon_eq(+0.86602, eps) is S.true
  2065. assert re(roots[3]).epsilon_eq(+0.87743, eps) is S.true
  2066. assert im(roots[3]).epsilon_eq(-0.74486, eps) is S.true
  2067. assert re(roots[4]).epsilon_eq(+0.87743, eps) is S.true
  2068. assert im(roots[4]).epsilon_eq(+0.74486, eps) is S.true
  2069. eps = Float("1e-6")
  2070. assert re(roots[0]).epsilon_eq(-0.75487, eps) is S.false
  2071. assert im(roots[0]) == 0.0
  2072. assert re(roots[1]) == Float(-0.5, 5)
  2073. assert im(roots[1]).epsilon_eq(-0.86602, eps) is S.false
  2074. assert re(roots[2]) == Float(-0.5, 5)
  2075. assert im(roots[2]).epsilon_eq(+0.86602, eps) is S.false
  2076. assert re(roots[3]).epsilon_eq(+0.87743, eps) is S.false
  2077. assert im(roots[3]).epsilon_eq(-0.74486, eps) is S.false
  2078. assert re(roots[4]).epsilon_eq(+0.87743, eps) is S.false
  2079. assert im(roots[4]).epsilon_eq(+0.74486, eps) is S.false
  2080. raises(DomainError, lambda: Poly(x + y, x).nroots())
  2081. raises(MultivariatePolynomialError, lambda: Poly(x + y).nroots())
  2082. assert nroots(x**2 - 1) == [-1.0, 1.0]
  2083. roots = nroots(x**2 - 1)
  2084. assert roots == [-1.0, 1.0]
  2085. assert nroots(x + I) == [-1.0*I]
  2086. assert nroots(x + 2*I) == [-2.0*I]
  2087. raises(PolynomialError, lambda: nroots(0))
  2088. # issue 8296
  2089. f = Poly(x**4 - 1)
  2090. assert f.nroots(2) == [w.n(2) for w in f.all_roots()]
  2091. assert str(Poly(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
  2092. 39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
  2093. 877969).nroots(2)) == ('[-1.7 - 1.9*I, -1.7 + 1.9*I, -1.7 '
  2094. '- 2.5*I, -1.7 + 2.5*I, -1.0*I, 1.0*I, -1.7*I, 1.7*I, -2.8*I, '
  2095. '2.8*I, -3.4*I, 3.4*I, 1.7 - 1.9*I, 1.7 + 1.9*I, 1.7 - 2.5*I, '
  2096. '1.7 + 2.5*I]')
  2097. assert str(Poly(1e-15*x**2 -1).nroots()) == ('[-31622776.6016838, 31622776.6016838]')
  2098. def test_ground_roots():
  2099. f = x**6 - 4*x**4 + 4*x**3 - x**2
  2100. assert Poly(f).ground_roots() == {S.One: 2, S.Zero: 2}
  2101. assert ground_roots(f) == {S.One: 2, S.Zero: 2}
  2102. def test_nth_power_roots_poly():
  2103. f = x**4 - x**2 + 1
  2104. f_2 = (x**2 - x + 1)**2
  2105. f_3 = (x**2 + 1)**2
  2106. f_4 = (x**2 + x + 1)**2
  2107. f_12 = (x - 1)**4
  2108. assert nth_power_roots_poly(f, 1) == f
  2109. raises(ValueError, lambda: nth_power_roots_poly(f, 0))
  2110. raises(ValueError, lambda: nth_power_roots_poly(f, x))
  2111. assert factor(nth_power_roots_poly(f, 2)) == f_2
  2112. assert factor(nth_power_roots_poly(f, 3)) == f_3
  2113. assert factor(nth_power_roots_poly(f, 4)) == f_4
  2114. assert factor(nth_power_roots_poly(f, 12)) == f_12
  2115. raises(MultivariatePolynomialError, lambda: nth_power_roots_poly(
  2116. x + y, 2, x, y))
  2117. def test_same_root():
  2118. f = Poly(x**4 + x**3 + x**2 + x + 1)
  2119. eq = f.same_root
  2120. r0 = exp(2 * I * pi / 5)
  2121. assert [i for i, r in enumerate(f.all_roots()) if eq(r, r0)] == [3]
  2122. raises(PolynomialError,
  2123. lambda: Poly(x + 1, domain=QQ).same_root(0, 0))
  2124. raises(DomainError,
  2125. lambda: Poly(x**2 + 1, domain=FF(7)).same_root(0, 0))
  2126. raises(DomainError,
  2127. lambda: Poly(x ** 2 + 1, domain=ZZ_I).same_root(0, 0))
  2128. raises(DomainError,
  2129. lambda: Poly(y * x**2 + 1, domain=ZZ[y]).same_root(0, 0))
  2130. raises(MultivariatePolynomialError,
  2131. lambda: Poly(x * y + 1, domain=ZZ).same_root(0, 0))
  2132. def test_torational_factor_list():
  2133. p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}))
  2134. assert _torational_factor_list(p, x) == (-2, [
  2135. (-x*(1 + sqrt(2))/2 + 1, 1),
  2136. (-x*(1 + sqrt(2)) - 1, 1),
  2137. (-x*(1 + sqrt(2)) + 1, 1)])
  2138. p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + 2**Rational(1, 4))}))
  2139. assert _torational_factor_list(p, x) is None
  2140. def test_cancel():
  2141. assert cancel(0) == 0
  2142. assert cancel(7) == 7
  2143. assert cancel(x) == x
  2144. assert cancel(oo) is oo
  2145. assert cancel((2, 3)) == (1, 2, 3)
  2146. assert cancel((1, 0), x) == (1, 1, 0)
  2147. assert cancel((0, 1), x) == (1, 0, 1)
  2148. f, g, p, q = 4*x**2 - 4, 2*x - 2, 2*x + 2, 1
  2149. F, G, P, Q = [ Poly(u, x) for u in (f, g, p, q) ]
  2150. assert F.cancel(G) == (1, P, Q)
  2151. assert cancel((f, g)) == (1, p, q)
  2152. assert cancel((f, g), x) == (1, p, q)
  2153. assert cancel((f, g), (x,)) == (1, p, q)
  2154. assert cancel((F, G)) == (1, P, Q)
  2155. assert cancel((f, g), polys=True) == (1, P, Q)
  2156. assert cancel((F, G), polys=False) == (1, p, q)
  2157. f = (x**2 - 2)/(x + sqrt(2))
  2158. assert cancel(f) == f
  2159. assert cancel(f, greedy=False) == x - sqrt(2)
  2160. f = (x**2 - 2)/(x - sqrt(2))
  2161. assert cancel(f) == f
  2162. assert cancel(f, greedy=False) == x + sqrt(2)
  2163. assert cancel((x**2/4 - 1, x/2 - 1)) == (1, x + 2, 2)
  2164. # assert cancel((x**2/4 - 1, x/2 - 1)) == (S.Half, x + 2, 1)
  2165. assert cancel((x**2 - y)/(x - y)) == 1/(x - y)*(x**2 - y)
  2166. assert cancel((x**2 - y**2)/(x - y), x) == x + y
  2167. assert cancel((x**2 - y**2)/(x - y), y) == x + y
  2168. assert cancel((x**2 - y**2)/(x - y)) == x + y
  2169. assert cancel((x**3 - 1)/(x**2 - 1)) == (x**2 + x + 1)/(x + 1)
  2170. assert cancel((x**3/2 - S.Half)/(x**2 - 1)) == (x**2 + x + 1)/(2*x + 2)
  2171. assert cancel((exp(2*x) + 2*exp(x) + 1)/(exp(x) + 1)) == exp(x) + 1
  2172. f = Poly(x**2 - a**2, x)
  2173. g = Poly(x - a, x)
  2174. F = Poly(x + a, x, domain='ZZ[a]')
  2175. G = Poly(1, x, domain='ZZ[a]')
  2176. assert cancel((f, g)) == (1, F, G)
  2177. f = x**3 + (sqrt(2) - 2)*x**2 - (2*sqrt(2) + 3)*x - 3*sqrt(2)
  2178. g = x**2 - 2
  2179. assert cancel((f, g), extension=True) == (1, x**2 - 2*x - 3, x - sqrt(2))
  2180. f = Poly(-2*x + 3, x)
  2181. g = Poly(-x**9 + x**8 + x**6 - x**5 + 2*x**2 - 3*x + 1, x)
  2182. assert cancel((f, g)) == (1, -f, -g)
  2183. f = Poly(y, y, domain='ZZ(x)')
  2184. g = Poly(1, y, domain='ZZ[x]')
  2185. assert f.cancel(
  2186. g) == (1, Poly(y, y, domain='ZZ(x)'), Poly(1, y, domain='ZZ(x)'))
  2187. assert f.cancel(g, include=True) == (
  2188. Poly(y, y, domain='ZZ(x)'), Poly(1, y, domain='ZZ(x)'))
  2189. f = Poly(5*x*y + x, y, domain='ZZ(x)')
  2190. g = Poly(2*x**2*y, y, domain='ZZ(x)')
  2191. assert f.cancel(g, include=True) == (
  2192. Poly(5*y + 1, y, domain='ZZ(x)'), Poly(2*x*y, y, domain='ZZ(x)'))
  2193. f = -(-2*x - 4*y + 0.005*(z - y)**2)/((z - y)*(-z + y + 2))
  2194. assert cancel(f).is_Mul == True
  2195. P = tanh(x - 3.0)
  2196. Q = tanh(x + 3.0)
  2197. f = ((-2*P**2 + 2)*(-P**2 + 1)*Q**2/2 + (-2*P**2 + 2)*(-2*Q**2 + 2)*P*Q - (-2*P**2 + 2)*P**2*Q**2 + (-2*Q**2 + 2)*(-Q**2 + 1)*P**2/2 - (-2*Q**2 + 2)*P**2*Q**2)/(2*sqrt(P**2*Q**2 + 0.0001)) \
  2198. + (-(-2*P**2 + 2)*P*Q**2/2 - (-2*Q**2 + 2)*P**2*Q/2)*((-2*P**2 + 2)*P*Q**2/2 + (-2*Q**2 + 2)*P**2*Q/2)/(2*(P**2*Q**2 + 0.0001)**Rational(3, 2))
  2199. assert cancel(f).is_Mul == True
  2200. # issue 7022
  2201. A = Symbol('A', commutative=False)
  2202. p1 = Piecewise((A*(x**2 - 1)/(x + 1), x > 1), ((x + 2)/(x**2 + 2*x), True))
  2203. p2 = Piecewise((A*(x - 1), x > 1), (1/x, True))
  2204. assert cancel(p1) == p2
  2205. assert cancel(2*p1) == 2*p2
  2206. assert cancel(1 + p1) == 1 + p2
  2207. assert cancel((x**2 - 1)/(x + 1)*p1) == (x - 1)*p2
  2208. assert cancel((x**2 - 1)/(x + 1) + p1) == (x - 1) + p2
  2209. p3 = Piecewise(((x**2 - 1)/(x + 1), x > 1), ((x + 2)/(x**2 + 2*x), True))
  2210. p4 = Piecewise(((x - 1), x > 1), (1/x, True))
  2211. assert cancel(p3) == p4
  2212. assert cancel(2*p3) == 2*p4
  2213. assert cancel(1 + p3) == 1 + p4
  2214. assert cancel((x**2 - 1)/(x + 1)*p3) == (x - 1)*p4
  2215. assert cancel((x**2 - 1)/(x + 1) + p3) == (x - 1) + p4
  2216. # issue 4077
  2217. q = S('''(2*1*(x - 1/x)/(x*(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x -
  2218. 1/x)) - 2/x)) - 2*1*((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x -
  2219. 1/x)))*((-x + 1/x)*((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x -
  2220. 1/x)))/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) -
  2221. 2/x) + 1)*((x - 1/x)/((x - 1/x)**2) - ((x - 1/x)/((x*(x - 1/x)**2)) -
  2222. 1/(x*(x - 1/x)))**2/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x
  2223. - 1/x)) - 2/x) - 1/(x - 1/x))*(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) -
  2224. 1/(x**2*(x - 1/x)) - 2/x)/x - 1/x)*(((-x + 1/x)/((x*(x - 1/x)**2)) +
  2225. 1/(x*(x - 1/x)))*((-(x - 1/x)/(x*(x - 1/x)) - 1/x)*((x - 1/x)/((x*(x -
  2226. 1/x)**2)) - 1/(x*(x - 1/x)))/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) -
  2227. 1/(x**2*(x - 1/x)) - 2/x) - 1 + (x - 1/x)/(x - 1/x))/((x*((x -
  2228. 1/x)/((x - 1/x)**2) - ((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x -
  2229. 1/x)))**2/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) -
  2230. 2/x) - 1/(x - 1/x))*(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x
  2231. - 1/x)) - 2/x))) + ((x - 1/x)/((x*(x - 1/x))) + 1/x)/((x*(2*x - (-x +
  2232. 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) - 2/x))) + 1/x)/(2*x +
  2233. 2*((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x - 1/x)))*((-(x - 1/x)/(x*(x
  2234. - 1/x)) - 1/x)*((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x - 1/x)))/(2*x -
  2235. (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) - 2/x) - 1 + (x -
  2236. 1/x)/(x - 1/x))/((x*((x - 1/x)/((x - 1/x)**2) - ((x - 1/x)/((x*(x -
  2237. 1/x)**2)) - 1/(x*(x - 1/x)))**2/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2)
  2238. - 1/(x**2*(x - 1/x)) - 2/x) - 1/(x - 1/x))*(2*x - (-x + 1/x)/(x**2*(x
  2239. - 1/x)**2) - 1/(x**2*(x - 1/x)) - 2/x))) - 2*((x - 1/x)/((x*(x -
  2240. 1/x))) + 1/x)/(x*(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x -
  2241. 1/x)) - 2/x)) - 2/x) - ((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x -
  2242. 1/x)))*((-x + 1/x)*((x - 1/x)/((x*(x - 1/x)**2)) - 1/(x*(x -
  2243. 1/x)))/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) -
  2244. 2/x) + 1)/(x*((x - 1/x)/((x - 1/x)**2) - ((x - 1/x)/((x*(x - 1/x)**2))
  2245. - 1/(x*(x - 1/x)))**2/(2*x - (-x + 1/x)/(x**2*(x - 1/x)**2) -
  2246. 1/(x**2*(x - 1/x)) - 2/x) - 1/(x - 1/x))*(2*x - (-x + 1/x)/(x**2*(x -
  2247. 1/x)**2) - 1/(x**2*(x - 1/x)) - 2/x)) + (x - 1/x)/((x*(2*x - (-x +
  2248. 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x)) - 2/x))) - 1/x''',
  2249. evaluate=False)
  2250. assert cancel(q, _signsimp=False) is S.NaN
  2251. assert q.subs(x, 2) is S.NaN
  2252. assert signsimp(q) is S.NaN
  2253. # issue 9363
  2254. M = MatrixSymbol('M', 5, 5)
  2255. assert cancel(M[0,0] + 7) == M[0,0] + 7
  2256. expr = sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2] / z
  2257. assert cancel(expr) == (z*sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2]) / z
  2258. assert cancel((x**2 + 1)/(x - I)) == x + I
  2259. def test_make_monic_over_integers_by_scaling_roots():
  2260. f = Poly(x**2 + 3*x + 4, x, domain='ZZ')
  2261. g, c = f.make_monic_over_integers_by_scaling_roots()
  2262. assert g == f
  2263. assert c == ZZ.one
  2264. f = Poly(x**2 + 3*x + 4, x, domain='QQ')
  2265. g, c = f.make_monic_over_integers_by_scaling_roots()
  2266. assert g == f.to_ring()
  2267. assert c == ZZ.one
  2268. f = Poly(x**2/2 + S(1)/4 * x + S(1)/8, x, domain='QQ')
  2269. g, c = f.make_monic_over_integers_by_scaling_roots()
  2270. assert g == Poly(x**2 + 2*x + 4, x, domain='ZZ')
  2271. assert c == 4
  2272. f = Poly(x**3/2 + S(1)/4 * x + S(1)/8, x, domain='QQ')
  2273. g, c = f.make_monic_over_integers_by_scaling_roots()
  2274. assert g == Poly(x**3 + 8*x + 16, x, domain='ZZ')
  2275. assert c == 4
  2276. f = Poly(x*y, x, y)
  2277. raises(ValueError, lambda: f.make_monic_over_integers_by_scaling_roots())
  2278. f = Poly(x, domain='RR')
  2279. raises(ValueError, lambda: f.make_monic_over_integers_by_scaling_roots())
  2280. def test_galois_group():
  2281. f = Poly(x ** 4 - 2)
  2282. G, alt = f.galois_group(by_name=True)
  2283. assert G == S4TransitiveSubgroups.D4
  2284. assert alt is False
  2285. def test_reduced():
  2286. f = 2*x**4 + y**2 - x**2 + y**3
  2287. G = [x**3 - x, y**3 - y]
  2288. Q = [2*x, 1]
  2289. r = x**2 + y**2 + y
  2290. assert reduced(f, G) == (Q, r)
  2291. assert reduced(f, G, x, y) == (Q, r)
  2292. H = groebner(G)
  2293. assert H.reduce(f) == (Q, r)
  2294. Q = [Poly(2*x, x, y), Poly(1, x, y)]
  2295. r = Poly(x**2 + y**2 + y, x, y)
  2296. assert _strict_eq(reduced(f, G, polys=True), (Q, r))
  2297. assert _strict_eq(reduced(f, G, x, y, polys=True), (Q, r))
  2298. H = groebner(G, polys=True)
  2299. assert _strict_eq(H.reduce(f), (Q, r))
  2300. f = 2*x**3 + y**3 + 3*y
  2301. G = groebner([x**2 + y**2 - 1, x*y - 2])
  2302. Q = [x**2 - x*y**3/2 + x*y/2 + y**6/4 - y**4/2 + y**2/4, -y**5/4 + y**3/2 + y*Rational(3, 4)]
  2303. r = 0
  2304. assert reduced(f, G) == (Q, r)
  2305. assert G.reduce(f) == (Q, r)
  2306. assert reduced(f, G, auto=False)[1] != 0
  2307. assert G.reduce(f, auto=False)[1] != 0
  2308. assert G.contains(f) is True
  2309. assert G.contains(f + 1) is False
  2310. assert reduced(1, [1], x) == ([1], 0)
  2311. raises(ComputationFailed, lambda: reduced(1, [1]))
  2312. def test_groebner():
  2313. assert groebner([], x, y, z) == []
  2314. assert groebner([x**2 + 1, y**4*x + x**3], x, y, order='lex') == [1 + x**2, -1 + y**4]
  2315. assert groebner([x**2 + 1, y**4*x + x**3, x*y*z**3], x, y, z, order='grevlex') == [-1 + y**4, z**3, 1 + x**2]
  2316. assert groebner([x**2 + 1, y**4*x + x**3], x, y, order='lex', polys=True) == \
  2317. [Poly(1 + x**2, x, y), Poly(-1 + y**4, x, y)]
  2318. assert groebner([x**2 + 1, y**4*x + x**3, x*y*z**3], x, y, z, order='grevlex', polys=True) == \
  2319. [Poly(-1 + y**4, x, y, z), Poly(z**3, x, y, z), Poly(1 + x**2, x, y, z)]
  2320. assert groebner([x**3 - 1, x**2 - 1]) == [x - 1]
  2321. assert groebner([Eq(x**3, 1), Eq(x**2, 1)]) == [x - 1]
  2322. F = [3*x**2 + y*z - 5*x - 1, 2*x + 3*x*y + y**2, x - 3*y + x*z - 2*z**2]
  2323. f = z**9 - x**2*y**3 - 3*x*y**2*z + 11*y*z**2 + x**2*z**2 - 5
  2324. G = groebner(F, x, y, z, modulus=7, symmetric=False)
  2325. assert G == [1 + x + y + 3*z + 2*z**2 + 2*z**3 + 6*z**4 + z**5,
  2326. 1 + 3*y + y**2 + 6*z**2 + 3*z**3 + 3*z**4 + 3*z**5 + 4*z**6,
  2327. 1 + 4*y + 4*z + y*z + 4*z**3 + z**4 + z**6,
  2328. 6 + 6*z + z**2 + 4*z**3 + 3*z**4 + 6*z**5 + 3*z**6 + z**7]
  2329. Q, r = reduced(f, G, x, y, z, modulus=7, symmetric=False, polys=True)
  2330. assert sum([ q*g for q, g in zip(Q, G.polys)], r) == Poly(f, modulus=7)
  2331. F = [x*y - 2*y, 2*y**2 - x**2]
  2332. assert groebner(F, x, y, order='grevlex') == \
  2333. [y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y]
  2334. assert groebner(F, y, x, order='grevlex') == \
  2335. [x**3 - 2*x**2, -x**2 + 2*y**2, x*y - 2*y]
  2336. assert groebner(F, order='grevlex', field=True) == \
  2337. [y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y]
  2338. assert groebner([1], x) == [1]
  2339. assert groebner([x**2 + 2.0*y], x, y) == [1.0*x**2 + 2.0*y]
  2340. raises(ComputationFailed, lambda: groebner([1]))
  2341. assert groebner([x**2 - 1, x**3 + 1], method='buchberger') == [x + 1]
  2342. assert groebner([x**2 - 1, x**3 + 1], method='f5b') == [x + 1]
  2343. raises(ValueError, lambda: groebner([x, y], method='unknown'))
  2344. def test_fglm():
  2345. F = [a + b + c + d, a*b + a*d + b*c + b*d, a*b*c + a*b*d + a*c*d + b*c*d, a*b*c*d - 1]
  2346. G = groebner(F, a, b, c, d, order=grlex)
  2347. B = [
  2348. 4*a + 3*d**9 - 4*d**5 - 3*d,
  2349. 4*b + 4*c - 3*d**9 + 4*d**5 + 7*d,
  2350. 4*c**2 + 3*d**10 - 4*d**6 - 3*d**2,
  2351. 4*c*d**4 + 4*c - d**9 + 4*d**5 + 5*d,
  2352. d**12 - d**8 - d**4 + 1,
  2353. ]
  2354. assert groebner(F, a, b, c, d, order=lex) == B
  2355. assert G.fglm(lex) == B
  2356. F = [9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9,
  2357. -72*t*x**7 - 252*t*x**6 + 192*t*x**5 + 1260*t*x**4 + 312*t*x**3 - 404*t*x**2 - 576*t*x + \
  2358. 108*t - 72*x**7 - 256*x**6 + 192*x**5 + 1280*x**4 + 312*x**3 - 576*x + 96]
  2359. G = groebner(F, t, x, order=grlex)
  2360. B = [
  2361. 203577793572507451707*t + 627982239411707112*x**7 - 666924143779443762*x**6 - \
  2362. 10874593056632447619*x**5 + 5119998792707079562*x**4 + 72917161949456066376*x**3 + \
  2363. 20362663855832380362*x**2 - 142079311455258371571*x + 183756699868981873194,
  2364. 9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9,
  2365. ]
  2366. assert groebner(F, t, x, order=lex) == B
  2367. assert G.fglm(lex) == B
  2368. F = [x**2 - x - 3*y + 1, -2*x + y**2 + y - 1]
  2369. G = groebner(F, x, y, order=lex)
  2370. B = [
  2371. x**2 - x - 3*y + 1,
  2372. y**2 - 2*x + y - 1,
  2373. ]
  2374. assert groebner(F, x, y, order=grlex) == B
  2375. assert G.fglm(grlex) == B
  2376. def test_is_zero_dimensional():
  2377. assert is_zero_dimensional([x, y], x, y) is True
  2378. assert is_zero_dimensional([x**3 + y**2], x, y) is False
  2379. assert is_zero_dimensional([x, y, z], x, y, z) is True
  2380. assert is_zero_dimensional([x, y, z], x, y, z, t) is False
  2381. F = [x*y - z, y*z - x, x*y - y]
  2382. assert is_zero_dimensional(F, x, y, z) is True
  2383. F = [x**2 - 2*x*z + 5, x*y**2 + y*z**3, 3*y**2 - 8*z**2]
  2384. assert is_zero_dimensional(F, x, y, z) is True
  2385. def test_GroebnerBasis():
  2386. F = [x*y - 2*y, 2*y**2 - x**2]
  2387. G = groebner(F, x, y, order='grevlex')
  2388. H = [y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y]
  2389. P = [ Poly(h, x, y) for h in H ]
  2390. assert groebner(F + [0], x, y, order='grevlex') == G
  2391. assert isinstance(G, GroebnerBasis) is True
  2392. assert len(G) == 3
  2393. assert G[0] == H[0] and not G[0].is_Poly
  2394. assert G[1] == H[1] and not G[1].is_Poly
  2395. assert G[2] == H[2] and not G[2].is_Poly
  2396. assert G[1:] == H[1:] and not any(g.is_Poly for g in G[1:])
  2397. assert G[:2] == H[:2] and not any(g.is_Poly for g in G[1:])
  2398. assert G.exprs == H
  2399. assert G.polys == P
  2400. assert G.gens == (x, y)
  2401. assert G.domain == ZZ
  2402. assert G.order == grevlex
  2403. assert G == H
  2404. assert G == tuple(H)
  2405. assert G == P
  2406. assert G == tuple(P)
  2407. assert G != []
  2408. G = groebner(F, x, y, order='grevlex', polys=True)
  2409. assert G[0] == P[0] and G[0].is_Poly
  2410. assert G[1] == P[1] and G[1].is_Poly
  2411. assert G[2] == P[2] and G[2].is_Poly
  2412. assert G[1:] == P[1:] and all(g.is_Poly for g in G[1:])
  2413. assert G[:2] == P[:2] and all(g.is_Poly for g in G[1:])
  2414. def test_poly():
  2415. assert poly(x) == Poly(x, x)
  2416. assert poly(y) == Poly(y, y)
  2417. assert poly(x + y) == Poly(x + y, x, y)
  2418. assert poly(x + sin(x)) == Poly(x + sin(x), x, sin(x))
  2419. assert poly(x + y, wrt=y) == Poly(x + y, y, x)
  2420. assert poly(x + sin(x), wrt=sin(x)) == Poly(x + sin(x), sin(x), x)
  2421. assert poly(x*y + 2*x*z**2 + 17) == Poly(x*y + 2*x*z**2 + 17, x, y, z)
  2422. assert poly(2*(y + z)**2 - 1) == Poly(2*y**2 + 4*y*z + 2*z**2 - 1, y, z)
  2423. assert poly(
  2424. x*(y + z)**2 - 1) == Poly(x*y**2 + 2*x*y*z + x*z**2 - 1, x, y, z)
  2425. assert poly(2*x*(
  2426. y + z)**2 - 1) == Poly(2*x*y**2 + 4*x*y*z + 2*x*z**2 - 1, x, y, z)
  2427. assert poly(2*(
  2428. y + z)**2 - x - 1) == Poly(2*y**2 + 4*y*z + 2*z**2 - x - 1, x, y, z)
  2429. assert poly(x*(
  2430. y + z)**2 - x - 1) == Poly(x*y**2 + 2*x*y*z + x*z**2 - x - 1, x, y, z)
  2431. assert poly(2*x*(y + z)**2 - x - 1) == Poly(2*x*y**2 + 4*x*y*z + 2*
  2432. x*z**2 - x - 1, x, y, z)
  2433. assert poly(x*y + (x + y)**2 + (x + z)**2) == \
  2434. Poly(2*x*z + 3*x*y + y**2 + z**2 + 2*x**2, x, y, z)
  2435. assert poly(x*y*(x + y)*(x + z)**2) == \
  2436. Poly(x**3*y**2 + x*y**2*z**2 + y*x**2*z**2 + 2*z*x**2*
  2437. y**2 + 2*y*z*x**3 + y*x**4, x, y, z)
  2438. assert poly(Poly(x + y + z, y, x, z)) == Poly(x + y + z, y, x, z)
  2439. assert poly((x + y)**2, x) == Poly(x**2 + 2*x*y + y**2, x, domain=ZZ[y])
  2440. assert poly((x + y)**2, y) == Poly(x**2 + 2*x*y + y**2, y, domain=ZZ[x])
  2441. assert poly(1, x) == Poly(1, x)
  2442. raises(GeneratorsNeeded, lambda: poly(1))
  2443. # issue 6184
  2444. assert poly(x + y, x, y) == Poly(x + y, x, y)
  2445. assert poly(x + y, y, x) == Poly(x + y, y, x)
  2446. def test_keep_coeff():
  2447. u = Mul(2, x + 1, evaluate=False)
  2448. assert _keep_coeff(S.One, x) == x
  2449. assert _keep_coeff(S.NegativeOne, x) == -x
  2450. assert _keep_coeff(S(1.0), x) == 1.0*x
  2451. assert _keep_coeff(S(-1.0), x) == -1.0*x
  2452. assert _keep_coeff(S.One, 2*x) == 2*x
  2453. assert _keep_coeff(S(2), x/2) == x
  2454. assert _keep_coeff(S(2), sin(x)) == 2*sin(x)
  2455. assert _keep_coeff(S(2), x + 1) == u
  2456. assert _keep_coeff(x, 1/x) == 1
  2457. assert _keep_coeff(x + 1, S(2)) == u
  2458. assert _keep_coeff(S.Half, S.One) == S.Half
  2459. p = Pow(2, 3, evaluate=False)
  2460. assert _keep_coeff(S(-1), p) == Mul(-1, p, evaluate=False)
  2461. a = Add(2, p, evaluate=False)
  2462. assert _keep_coeff(S.Half, a, clear=True
  2463. ) == Mul(S.Half, a, evaluate=False)
  2464. assert _keep_coeff(S.Half, a, clear=False
  2465. ) == Add(1, Mul(S.Half, p, evaluate=False), evaluate=False)
  2466. def test_poly_matching_consistency():
  2467. # Test for this issue:
  2468. # https://github.com/sympy/sympy/issues/5514
  2469. assert I * Poly(x, x) == Poly(I*x, x)
  2470. assert Poly(x, x) * I == Poly(I*x, x)
  2471. def test_issue_5786():
  2472. assert expand(factor(expand(
  2473. (x - I*y)*(z - I*t)), extension=[I])) == -I*t*x - t*y + x*z - I*y*z
  2474. def test_noncommutative():
  2475. class foo(Expr):
  2476. is_commutative=False
  2477. e = x/(x + x*y)
  2478. c = 1/( 1 + y)
  2479. assert cancel(foo(e)) == foo(c)
  2480. assert cancel(e + foo(e)) == c + foo(c)
  2481. assert cancel(e*foo(c)) == c*foo(c)
  2482. def test_to_rational_coeffs():
  2483. assert to_rational_coeffs(
  2484. Poly(x**3 + y*x**2 + sqrt(y), x, domain='EX')) is None
  2485. # issue 21268
  2486. assert to_rational_coeffs(
  2487. Poly(y**3 + sqrt(2)*y**2*sin(x) + 1, y)) is None
  2488. assert to_rational_coeffs(Poly(x, y)) is None
  2489. assert to_rational_coeffs(Poly(sqrt(2)*y)) is None
  2490. def test_factor_terms():
  2491. # issue 7067
  2492. assert factor_list(x*(x + y)) == (1, [(x, 1), (x + y, 1)])
  2493. assert sqf_list(x*(x + y)) == (1, [(x**2 + x*y, 1)])
  2494. def test_as_list():
  2495. # issue 14496
  2496. assert Poly(x**3 + 2, x, domain='ZZ').as_list() == [1, 0, 0, 2]
  2497. assert Poly(x**2 + y + 1, x, y, domain='ZZ').as_list() == [[1], [], [1, 1]]
  2498. assert Poly(x**2 + y + 1, x, y, z, domain='ZZ').as_list() == \
  2499. [[[1]], [[]], [[1], [1]]]
  2500. def test_issue_11198():
  2501. assert factor_list(sqrt(2)*x) == (sqrt(2), [(x, 1)])
  2502. assert factor_list(sqrt(2)*sin(x), sin(x)) == (sqrt(2), [(sin(x), 1)])
  2503. def test_Poly_precision():
  2504. # Make sure Poly doesn't lose precision
  2505. p = Poly(pi.evalf(100)*x)
  2506. assert p.as_expr() == pi.evalf(100)*x
  2507. def test_issue_12400():
  2508. # Correction of check for negative exponents
  2509. assert poly(1/(1+sqrt(2)), x) == \
  2510. Poly(1/(1+sqrt(2)), x, domain='EX')
  2511. def test_issue_14364():
  2512. assert gcd(S(6)*(1 + sqrt(3))/5, S(3)*(1 + sqrt(3))/10) == Rational(3, 10) * (1 + sqrt(3))
  2513. assert gcd(sqrt(5)*Rational(4, 7), sqrt(5)*Rational(2, 3)) == sqrt(5)*Rational(2, 21)
  2514. assert lcm(Rational(2, 3)*sqrt(3), Rational(5, 6)*sqrt(3)) == S(10)*sqrt(3)/3
  2515. assert lcm(3*sqrt(3), 4/sqrt(3)) == 12*sqrt(3)
  2516. assert lcm(S(5)*(1 + 2**Rational(1, 3))/6, S(3)*(1 + 2**Rational(1, 3))/8) == Rational(15, 2) * (1 + 2**Rational(1, 3))
  2517. assert gcd(Rational(2, 3)*sqrt(3), Rational(5, 6)/sqrt(3)) == sqrt(3)/18
  2518. assert gcd(S(4)*sqrt(13)/7, S(3)*sqrt(13)/14) == sqrt(13)/14
  2519. # gcd_list and lcm_list
  2520. assert gcd([S(2)*sqrt(47)/7, S(6)*sqrt(47)/5, S(8)*sqrt(47)/5]) == sqrt(47)*Rational(2, 35)
  2521. assert gcd([S(6)*(1 + sqrt(7))/5, S(2)*(1 + sqrt(7))/7, S(4)*(1 + sqrt(7))/13]) == (1 + sqrt(7))*Rational(2, 455)
  2522. assert lcm((Rational(7, 2)/sqrt(15), Rational(5, 6)/sqrt(15), Rational(5, 8)/sqrt(15))) == Rational(35, 2)/sqrt(15)
  2523. assert lcm([S(5)*(2 + 2**Rational(5, 7))/6, S(7)*(2 + 2**Rational(5, 7))/2, S(13)*(2 + 2**Rational(5, 7))/4]) == Rational(455, 2) * (2 + 2**Rational(5, 7))
  2524. def test_issue_15669():
  2525. x = Symbol("x", positive=True)
  2526. expr = (16*x**3/(-x**2 + sqrt(8*x**2 + (x**2 - 2)**2) + 2)**2 -
  2527. 2*2**Rational(4, 5)*x*(-x**2 + sqrt(8*x**2 + (x**2 - 2)**2) + 2)**Rational(3, 5) + 10*x)
  2528. assert factor(expr, deep=True) == x*(x**2 + 2)
  2529. def test_issue_17988():
  2530. x = Symbol('x')
  2531. p = poly(x - 1)
  2532. with warns_deprecated_sympy():
  2533. M = Matrix([[poly(x + 1), poly(x + 1)]])
  2534. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  2535. assert p * M == M * p == Matrix([[poly(x**2 - 1), poly(x**2 - 1)]])
  2536. def test_issue_18205():
  2537. assert cancel((2 + I)*(3 - I)) == 7 + I
  2538. assert cancel((2 + I)*(2 - I)) == 5
  2539. def test_issue_8695():
  2540. p = (x**2 + 1) * (x - 1)**2 * (x - 2)**3 * (x - 3)**3
  2541. result = (1, [(x**2 + 1, 1), (x - 1, 2), (x**2 - 5*x + 6, 3)])
  2542. assert sqf_list(p) == result
  2543. def test_issue_19113():
  2544. eq = sin(x)**3 - sin(x) + 1
  2545. raises(PolynomialError, lambda: refine_root(eq, 1, 2, 1e-2))
  2546. raises(PolynomialError, lambda: count_roots(eq, -1, 1))
  2547. raises(PolynomialError, lambda: real_roots(eq))
  2548. raises(PolynomialError, lambda: nroots(eq))
  2549. raises(PolynomialError, lambda: ground_roots(eq))
  2550. raises(PolynomialError, lambda: nth_power_roots_poly(eq, 2))
  2551. def test_issue_19360():
  2552. f = 2*x**2 - 2*sqrt(2)*x*y + y**2
  2553. assert factor(f, extension=sqrt(2)) == 2*(x - (sqrt(2)*y/2))**2
  2554. f = -I*t*x - t*y + x*z - I*y*z
  2555. assert factor(f, extension=I) == (x - I*y)*(-I*t + z)
  2556. def test_poly_copy_equals_original():
  2557. poly = Poly(x + y, x, y, z)
  2558. copy = poly.copy()
  2559. assert poly == copy, (
  2560. "Copied polynomial not equal to original.")
  2561. assert poly.gens == copy.gens, (
  2562. "Copied polynomial has different generators than original.")
  2563. def test_deserialized_poly_equals_original():
  2564. poly = Poly(x + y, x, y, z)
  2565. deserialized = pickle.loads(pickle.dumps(poly))
  2566. assert poly == deserialized, (
  2567. "Deserialized polynomial not equal to original.")
  2568. assert poly.gens == deserialized.gens, (
  2569. "Deserialized polynomial has different generators than original.")
  2570. def test_issue_20389():
  2571. result = degree(x * (x + 1) - x ** 2 - x, x)
  2572. assert result == -oo
  2573. def test_issue_20985():
  2574. from sympy.core.symbol import symbols
  2575. w, R = symbols('w R')
  2576. poly = Poly(1.0 + I*w/R, w, 1/R)
  2577. assert poly.degree() == S(1)