test_polytools.py 136 KB

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