densebasic.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. """Basic tools for dense recursive polynomials in ``K[x]`` or ``K[X]``. """
  2. from sympy.core.numbers import oo
  3. from sympy.core import igcd
  4. from sympy.polys.monomials import monomial_min, monomial_div
  5. from sympy.polys.orderings import monomial_key
  6. import random
  7. def poly_LC(f, K):
  8. """
  9. Return leading coefficient of ``f``.
  10. Examples
  11. ========
  12. >>> from sympy.polys.domains import ZZ
  13. >>> from sympy.polys.densebasic import poly_LC
  14. >>> poly_LC([], ZZ)
  15. 0
  16. >>> poly_LC([ZZ(1), ZZ(2), ZZ(3)], ZZ)
  17. 1
  18. """
  19. if not f:
  20. return K.zero
  21. else:
  22. return f[0]
  23. def poly_TC(f, K):
  24. """
  25. Return trailing coefficient of ``f``.
  26. Examples
  27. ========
  28. >>> from sympy.polys.domains import ZZ
  29. >>> from sympy.polys.densebasic import poly_TC
  30. >>> poly_TC([], ZZ)
  31. 0
  32. >>> poly_TC([ZZ(1), ZZ(2), ZZ(3)], ZZ)
  33. 3
  34. """
  35. if not f:
  36. return K.zero
  37. else:
  38. return f[-1]
  39. dup_LC = dmp_LC = poly_LC
  40. dup_TC = dmp_TC = poly_TC
  41. def dmp_ground_LC(f, u, K):
  42. """
  43. Return the ground leading coefficient.
  44. Examples
  45. ========
  46. >>> from sympy.polys.domains import ZZ
  47. >>> from sympy.polys.densebasic import dmp_ground_LC
  48. >>> f = ZZ.map([[[1], [2, 3]]])
  49. >>> dmp_ground_LC(f, 2, ZZ)
  50. 1
  51. """
  52. while u:
  53. f = dmp_LC(f, K)
  54. u -= 1
  55. return dup_LC(f, K)
  56. def dmp_ground_TC(f, u, K):
  57. """
  58. Return the ground trailing coefficient.
  59. Examples
  60. ========
  61. >>> from sympy.polys.domains import ZZ
  62. >>> from sympy.polys.densebasic import dmp_ground_TC
  63. >>> f = ZZ.map([[[1], [2, 3]]])
  64. >>> dmp_ground_TC(f, 2, ZZ)
  65. 3
  66. """
  67. while u:
  68. f = dmp_TC(f, K)
  69. u -= 1
  70. return dup_TC(f, K)
  71. def dmp_true_LT(f, u, K):
  72. """
  73. Return the leading term ``c * x_1**n_1 ... x_k**n_k``.
  74. Examples
  75. ========
  76. >>> from sympy.polys.domains import ZZ
  77. >>> from sympy.polys.densebasic import dmp_true_LT
  78. >>> f = ZZ.map([[4], [2, 0], [3, 0, 0]])
  79. >>> dmp_true_LT(f, 1, ZZ)
  80. ((2, 0), 4)
  81. """
  82. monom = []
  83. while u:
  84. monom.append(len(f) - 1)
  85. f, u = f[0], u - 1
  86. if not f:
  87. monom.append(0)
  88. else:
  89. monom.append(len(f) - 1)
  90. return tuple(monom), dup_LC(f, K)
  91. def dup_degree(f):
  92. """
  93. Return the leading degree of ``f`` in ``K[x]``.
  94. Note that the degree of 0 is negative infinity (the SymPy object -oo).
  95. Examples
  96. ========
  97. >>> from sympy.polys.domains import ZZ
  98. >>> from sympy.polys.densebasic import dup_degree
  99. >>> f = ZZ.map([1, 2, 0, 3])
  100. >>> dup_degree(f)
  101. 3
  102. """
  103. if not f:
  104. return -oo
  105. return len(f) - 1
  106. def dmp_degree(f, u):
  107. """
  108. Return the leading degree of ``f`` in ``x_0`` in ``K[X]``.
  109. Note that the degree of 0 is negative infinity (the SymPy object -oo).
  110. Examples
  111. ========
  112. >>> from sympy.polys.domains import ZZ
  113. >>> from sympy.polys.densebasic import dmp_degree
  114. >>> dmp_degree([[[]]], 2)
  115. -oo
  116. >>> f = ZZ.map([[2], [1, 2, 3]])
  117. >>> dmp_degree(f, 1)
  118. 1
  119. """
  120. if dmp_zero_p(f, u):
  121. return -oo
  122. else:
  123. return len(f) - 1
  124. def _rec_degree_in(g, v, i, j):
  125. """Recursive helper function for :func:`dmp_degree_in`."""
  126. if i == j:
  127. return dmp_degree(g, v)
  128. v, i = v - 1, i + 1
  129. return max([ _rec_degree_in(c, v, i, j) for c in g ])
  130. def dmp_degree_in(f, j, u):
  131. """
  132. Return the leading degree of ``f`` in ``x_j`` in ``K[X]``.
  133. Examples
  134. ========
  135. >>> from sympy.polys.domains import ZZ
  136. >>> from sympy.polys.densebasic import dmp_degree_in
  137. >>> f = ZZ.map([[2], [1, 2, 3]])
  138. >>> dmp_degree_in(f, 0, 1)
  139. 1
  140. >>> dmp_degree_in(f, 1, 1)
  141. 2
  142. """
  143. if not j:
  144. return dmp_degree(f, u)
  145. if j < 0 or j > u:
  146. raise IndexError("0 <= j <= %s expected, got %s" % (u, j))
  147. return _rec_degree_in(f, u, 0, j)
  148. def _rec_degree_list(g, v, i, degs):
  149. """Recursive helper for :func:`dmp_degree_list`."""
  150. degs[i] = max(degs[i], dmp_degree(g, v))
  151. if v > 0:
  152. v, i = v - 1, i + 1
  153. for c in g:
  154. _rec_degree_list(c, v, i, degs)
  155. def dmp_degree_list(f, u):
  156. """
  157. Return a list of degrees of ``f`` in ``K[X]``.
  158. Examples
  159. ========
  160. >>> from sympy.polys.domains import ZZ
  161. >>> from sympy.polys.densebasic import dmp_degree_list
  162. >>> f = ZZ.map([[1], [1, 2, 3]])
  163. >>> dmp_degree_list(f, 1)
  164. (1, 2)
  165. """
  166. degs = [-oo]*(u + 1)
  167. _rec_degree_list(f, u, 0, degs)
  168. return tuple(degs)
  169. def dup_strip(f):
  170. """
  171. Remove leading zeros from ``f`` in ``K[x]``.
  172. Examples
  173. ========
  174. >>> from sympy.polys.densebasic import dup_strip
  175. >>> dup_strip([0, 0, 1, 2, 3, 0])
  176. [1, 2, 3, 0]
  177. """
  178. if not f or f[0]:
  179. return f
  180. i = 0
  181. for cf in f:
  182. if cf:
  183. break
  184. else:
  185. i += 1
  186. return f[i:]
  187. def dmp_strip(f, u):
  188. """
  189. Remove leading zeros from ``f`` in ``K[X]``.
  190. Examples
  191. ========
  192. >>> from sympy.polys.densebasic import dmp_strip
  193. >>> dmp_strip([[], [0, 1, 2], [1]], 1)
  194. [[0, 1, 2], [1]]
  195. """
  196. if not u:
  197. return dup_strip(f)
  198. if dmp_zero_p(f, u):
  199. return f
  200. i, v = 0, u - 1
  201. for c in f:
  202. if not dmp_zero_p(c, v):
  203. break
  204. else:
  205. i += 1
  206. if i == len(f):
  207. return dmp_zero(u)
  208. else:
  209. return f[i:]
  210. def _rec_validate(f, g, i, K):
  211. """Recursive helper for :func:`dmp_validate`."""
  212. if not isinstance(g, list):
  213. if K is not None and not K.of_type(g):
  214. raise TypeError("%s in %s in not of type %s" % (g, f, K.dtype))
  215. return {i - 1}
  216. elif not g:
  217. return {i}
  218. else:
  219. levels = set()
  220. for c in g:
  221. levels |= _rec_validate(f, c, i + 1, K)
  222. return levels
  223. def _rec_strip(g, v):
  224. """Recursive helper for :func:`_rec_strip`."""
  225. if not v:
  226. return dup_strip(g)
  227. w = v - 1
  228. return dmp_strip([ _rec_strip(c, w) for c in g ], v)
  229. def dmp_validate(f, K=None):
  230. """
  231. Return the number of levels in ``f`` and recursively strip it.
  232. Examples
  233. ========
  234. >>> from sympy.polys.densebasic import dmp_validate
  235. >>> dmp_validate([[], [0, 1, 2], [1]])
  236. ([[1, 2], [1]], 1)
  237. >>> dmp_validate([[1], 1])
  238. Traceback (most recent call last):
  239. ...
  240. ValueError: invalid data structure for a multivariate polynomial
  241. """
  242. levels = _rec_validate(f, f, 0, K)
  243. u = levels.pop()
  244. if not levels:
  245. return _rec_strip(f, u), u
  246. else:
  247. raise ValueError(
  248. "invalid data structure for a multivariate polynomial")
  249. def dup_reverse(f):
  250. """
  251. Compute ``x**n * f(1/x)``, i.e.: reverse ``f`` in ``K[x]``.
  252. Examples
  253. ========
  254. >>> from sympy.polys.domains import ZZ
  255. >>> from sympy.polys.densebasic import dup_reverse
  256. >>> f = ZZ.map([1, 2, 3, 0])
  257. >>> dup_reverse(f)
  258. [3, 2, 1]
  259. """
  260. return dup_strip(list(reversed(f)))
  261. def dup_copy(f):
  262. """
  263. Create a new copy of a polynomial ``f`` in ``K[x]``.
  264. Examples
  265. ========
  266. >>> from sympy.polys.domains import ZZ
  267. >>> from sympy.polys.densebasic import dup_copy
  268. >>> f = ZZ.map([1, 2, 3, 0])
  269. >>> dup_copy([1, 2, 3, 0])
  270. [1, 2, 3, 0]
  271. """
  272. return list(f)
  273. def dmp_copy(f, u):
  274. """
  275. Create a new copy of a polynomial ``f`` in ``K[X]``.
  276. Examples
  277. ========
  278. >>> from sympy.polys.domains import ZZ
  279. >>> from sympy.polys.densebasic import dmp_copy
  280. >>> f = ZZ.map([[1], [1, 2]])
  281. >>> dmp_copy(f, 1)
  282. [[1], [1, 2]]
  283. """
  284. if not u:
  285. return list(f)
  286. v = u - 1
  287. return [ dmp_copy(c, v) for c in f ]
  288. def dup_to_tuple(f):
  289. """
  290. Convert `f` into a tuple.
  291. This is needed for hashing. This is similar to dup_copy().
  292. Examples
  293. ========
  294. >>> from sympy.polys.domains import ZZ
  295. >>> from sympy.polys.densebasic import dup_copy
  296. >>> f = ZZ.map([1, 2, 3, 0])
  297. >>> dup_copy([1, 2, 3, 0])
  298. [1, 2, 3, 0]
  299. """
  300. return tuple(f)
  301. def dmp_to_tuple(f, u):
  302. """
  303. Convert `f` into a nested tuple of tuples.
  304. This is needed for hashing. This is similar to dmp_copy().
  305. Examples
  306. ========
  307. >>> from sympy.polys.domains import ZZ
  308. >>> from sympy.polys.densebasic import dmp_to_tuple
  309. >>> f = ZZ.map([[1], [1, 2]])
  310. >>> dmp_to_tuple(f, 1)
  311. ((1,), (1, 2))
  312. """
  313. if not u:
  314. return tuple(f)
  315. v = u - 1
  316. return tuple(dmp_to_tuple(c, v) for c in f)
  317. def dup_normal(f, K):
  318. """
  319. Normalize univariate polynomial in the given domain.
  320. Examples
  321. ========
  322. >>> from sympy.polys.domains import ZZ
  323. >>> from sympy.polys.densebasic import dup_normal
  324. >>> dup_normal([0, 1.5, 2, 3], ZZ)
  325. [1, 2, 3]
  326. """
  327. return dup_strip([ K.normal(c) for c in f ])
  328. def dmp_normal(f, u, K):
  329. """
  330. Normalize a multivariate polynomial in the given domain.
  331. Examples
  332. ========
  333. >>> from sympy.polys.domains import ZZ
  334. >>> from sympy.polys.densebasic import dmp_normal
  335. >>> dmp_normal([[], [0, 1.5, 2]], 1, ZZ)
  336. [[1, 2]]
  337. """
  338. if not u:
  339. return dup_normal(f, K)
  340. v = u - 1
  341. return dmp_strip([ dmp_normal(c, v, K) for c in f ], u)
  342. def dup_convert(f, K0, K1):
  343. """
  344. Convert the ground domain of ``f`` from ``K0`` to ``K1``.
  345. Examples
  346. ========
  347. >>> from sympy.polys.rings import ring
  348. >>> from sympy.polys.domains import ZZ
  349. >>> from sympy.polys.densebasic import dup_convert
  350. >>> R, x = ring("x", ZZ)
  351. >>> dup_convert([R(1), R(2)], R.to_domain(), ZZ)
  352. [1, 2]
  353. >>> dup_convert([ZZ(1), ZZ(2)], ZZ, R.to_domain())
  354. [1, 2]
  355. """
  356. if K0 is not None and K0 == K1:
  357. return f
  358. else:
  359. return dup_strip([ K1.convert(c, K0) for c in f ])
  360. def dmp_convert(f, u, K0, K1):
  361. """
  362. Convert the ground domain of ``f`` from ``K0`` to ``K1``.
  363. Examples
  364. ========
  365. >>> from sympy.polys.rings import ring
  366. >>> from sympy.polys.domains import ZZ
  367. >>> from sympy.polys.densebasic import dmp_convert
  368. >>> R, x = ring("x", ZZ)
  369. >>> dmp_convert([[R(1)], [R(2)]], 1, R.to_domain(), ZZ)
  370. [[1], [2]]
  371. >>> dmp_convert([[ZZ(1)], [ZZ(2)]], 1, ZZ, R.to_domain())
  372. [[1], [2]]
  373. """
  374. if not u:
  375. return dup_convert(f, K0, K1)
  376. if K0 is not None and K0 == K1:
  377. return f
  378. v = u - 1
  379. return dmp_strip([ dmp_convert(c, v, K0, K1) for c in f ], u)
  380. def dup_from_sympy(f, K):
  381. """
  382. Convert the ground domain of ``f`` from SymPy to ``K``.
  383. Examples
  384. ========
  385. >>> from sympy import S
  386. >>> from sympy.polys.domains import ZZ
  387. >>> from sympy.polys.densebasic import dup_from_sympy
  388. >>> dup_from_sympy([S(1), S(2)], ZZ) == [ZZ(1), ZZ(2)]
  389. True
  390. """
  391. return dup_strip([ K.from_sympy(c) for c in f ])
  392. def dmp_from_sympy(f, u, K):
  393. """
  394. Convert the ground domain of ``f`` from SymPy to ``K``.
  395. Examples
  396. ========
  397. >>> from sympy import S
  398. >>> from sympy.polys.domains import ZZ
  399. >>> from sympy.polys.densebasic import dmp_from_sympy
  400. >>> dmp_from_sympy([[S(1)], [S(2)]], 1, ZZ) == [[ZZ(1)], [ZZ(2)]]
  401. True
  402. """
  403. if not u:
  404. return dup_from_sympy(f, K)
  405. v = u - 1
  406. return dmp_strip([ dmp_from_sympy(c, v, K) for c in f ], u)
  407. def dup_nth(f, n, K):
  408. """
  409. Return the ``n``-th coefficient of ``f`` in ``K[x]``.
  410. Examples
  411. ========
  412. >>> from sympy.polys.domains import ZZ
  413. >>> from sympy.polys.densebasic import dup_nth
  414. >>> f = ZZ.map([1, 2, 3])
  415. >>> dup_nth(f, 0, ZZ)
  416. 3
  417. >>> dup_nth(f, 4, ZZ)
  418. 0
  419. """
  420. if n < 0:
  421. raise IndexError("'n' must be non-negative, got %i" % n)
  422. elif n >= len(f):
  423. return K.zero
  424. else:
  425. return f[dup_degree(f) - n]
  426. def dmp_nth(f, n, u, K):
  427. """
  428. Return the ``n``-th coefficient of ``f`` in ``K[x]``.
  429. Examples
  430. ========
  431. >>> from sympy.polys.domains import ZZ
  432. >>> from sympy.polys.densebasic import dmp_nth
  433. >>> f = ZZ.map([[1], [2], [3]])
  434. >>> dmp_nth(f, 0, 1, ZZ)
  435. [3]
  436. >>> dmp_nth(f, 4, 1, ZZ)
  437. []
  438. """
  439. if n < 0:
  440. raise IndexError("'n' must be non-negative, got %i" % n)
  441. elif n >= len(f):
  442. return dmp_zero(u - 1)
  443. else:
  444. return f[dmp_degree(f, u) - n]
  445. def dmp_ground_nth(f, N, u, K):
  446. """
  447. Return the ground ``n``-th coefficient of ``f`` in ``K[x]``.
  448. Examples
  449. ========
  450. >>> from sympy.polys.domains import ZZ
  451. >>> from sympy.polys.densebasic import dmp_ground_nth
  452. >>> f = ZZ.map([[1], [2, 3]])
  453. >>> dmp_ground_nth(f, (0, 1), 1, ZZ)
  454. 2
  455. """
  456. v = u
  457. for n in N:
  458. if n < 0:
  459. raise IndexError("`n` must be non-negative, got %i" % n)
  460. elif n >= len(f):
  461. return K.zero
  462. else:
  463. d = dmp_degree(f, v)
  464. if d == -oo:
  465. d = -1
  466. f, v = f[d - n], v - 1
  467. return f
  468. def dmp_zero_p(f, u):
  469. """
  470. Return ``True`` if ``f`` is zero in ``K[X]``.
  471. Examples
  472. ========
  473. >>> from sympy.polys.densebasic import dmp_zero_p
  474. >>> dmp_zero_p([[[[[]]]]], 4)
  475. True
  476. >>> dmp_zero_p([[[[[1]]]]], 4)
  477. False
  478. """
  479. while u:
  480. if len(f) != 1:
  481. return False
  482. f = f[0]
  483. u -= 1
  484. return not f
  485. def dmp_zero(u):
  486. """
  487. Return a multivariate zero.
  488. Examples
  489. ========
  490. >>> from sympy.polys.densebasic import dmp_zero
  491. >>> dmp_zero(4)
  492. [[[[[]]]]]
  493. """
  494. r = []
  495. for i in range(u):
  496. r = [r]
  497. return r
  498. def dmp_one_p(f, u, K):
  499. """
  500. Return ``True`` if ``f`` is one in ``K[X]``.
  501. Examples
  502. ========
  503. >>> from sympy.polys.domains import ZZ
  504. >>> from sympy.polys.densebasic import dmp_one_p
  505. >>> dmp_one_p([[[ZZ(1)]]], 2, ZZ)
  506. True
  507. """
  508. return dmp_ground_p(f, K.one, u)
  509. def dmp_one(u, K):
  510. """
  511. Return a multivariate one over ``K``.
  512. Examples
  513. ========
  514. >>> from sympy.polys.domains import ZZ
  515. >>> from sympy.polys.densebasic import dmp_one
  516. >>> dmp_one(2, ZZ)
  517. [[[1]]]
  518. """
  519. return dmp_ground(K.one, u)
  520. def dmp_ground_p(f, c, u):
  521. """
  522. Return True if ``f`` is constant in ``K[X]``.
  523. Examples
  524. ========
  525. >>> from sympy.polys.densebasic import dmp_ground_p
  526. >>> dmp_ground_p([[[3]]], 3, 2)
  527. True
  528. >>> dmp_ground_p([[[4]]], None, 2)
  529. True
  530. """
  531. if c is not None and not c:
  532. return dmp_zero_p(f, u)
  533. while u:
  534. if len(f) != 1:
  535. return False
  536. f = f[0]
  537. u -= 1
  538. if c is None:
  539. return len(f) <= 1
  540. else:
  541. return f == [c]
  542. def dmp_ground(c, u):
  543. """
  544. Return a multivariate constant.
  545. Examples
  546. ========
  547. >>> from sympy.polys.densebasic import dmp_ground
  548. >>> dmp_ground(3, 5)
  549. [[[[[[3]]]]]]
  550. >>> dmp_ground(1, -1)
  551. 1
  552. """
  553. if not c:
  554. return dmp_zero(u)
  555. for i in range(u + 1):
  556. c = [c]
  557. return c
  558. def dmp_zeros(n, u, K):
  559. """
  560. Return a list of multivariate zeros.
  561. Examples
  562. ========
  563. >>> from sympy.polys.domains import ZZ
  564. >>> from sympy.polys.densebasic import dmp_zeros
  565. >>> dmp_zeros(3, 2, ZZ)
  566. [[[[]]], [[[]]], [[[]]]]
  567. >>> dmp_zeros(3, -1, ZZ)
  568. [0, 0, 0]
  569. """
  570. if not n:
  571. return []
  572. if u < 0:
  573. return [K.zero]*n
  574. else:
  575. return [ dmp_zero(u) for i in range(n) ]
  576. def dmp_grounds(c, n, u):
  577. """
  578. Return a list of multivariate constants.
  579. Examples
  580. ========
  581. >>> from sympy.polys.domains import ZZ
  582. >>> from sympy.polys.densebasic import dmp_grounds
  583. >>> dmp_grounds(ZZ(4), 3, 2)
  584. [[[[4]]], [[[4]]], [[[4]]]]
  585. >>> dmp_grounds(ZZ(4), 3, -1)
  586. [4, 4, 4]
  587. """
  588. if not n:
  589. return []
  590. if u < 0:
  591. return [c]*n
  592. else:
  593. return [ dmp_ground(c, u) for i in range(n) ]
  594. def dmp_negative_p(f, u, K):
  595. """
  596. Return ``True`` if ``LC(f)`` is negative.
  597. Examples
  598. ========
  599. >>> from sympy.polys.domains import ZZ
  600. >>> from sympy.polys.densebasic import dmp_negative_p
  601. >>> dmp_negative_p([[ZZ(1)], [-ZZ(1)]], 1, ZZ)
  602. False
  603. >>> dmp_negative_p([[-ZZ(1)], [ZZ(1)]], 1, ZZ)
  604. True
  605. """
  606. return K.is_negative(dmp_ground_LC(f, u, K))
  607. def dmp_positive_p(f, u, K):
  608. """
  609. Return ``True`` if ``LC(f)`` is positive.
  610. Examples
  611. ========
  612. >>> from sympy.polys.domains import ZZ
  613. >>> from sympy.polys.densebasic import dmp_positive_p
  614. >>> dmp_positive_p([[ZZ(1)], [-ZZ(1)]], 1, ZZ)
  615. True
  616. >>> dmp_positive_p([[-ZZ(1)], [ZZ(1)]], 1, ZZ)
  617. False
  618. """
  619. return K.is_positive(dmp_ground_LC(f, u, K))
  620. def dup_from_dict(f, K):
  621. """
  622. Create a ``K[x]`` polynomial from a ``dict``.
  623. Examples
  624. ========
  625. >>> from sympy.polys.domains import ZZ
  626. >>> from sympy.polys.densebasic import dup_from_dict
  627. >>> dup_from_dict({(0,): ZZ(7), (2,): ZZ(5), (4,): ZZ(1)}, ZZ)
  628. [1, 0, 5, 0, 7]
  629. >>> dup_from_dict({}, ZZ)
  630. []
  631. """
  632. if not f:
  633. return []
  634. n, h = max(f.keys()), []
  635. if isinstance(n, int):
  636. for k in range(n, -1, -1):
  637. h.append(f.get(k, K.zero))
  638. else:
  639. (n,) = n
  640. for k in range(n, -1, -1):
  641. h.append(f.get((k,), K.zero))
  642. return dup_strip(h)
  643. def dup_from_raw_dict(f, K):
  644. """
  645. Create a ``K[x]`` polynomial from a raw ``dict``.
  646. Examples
  647. ========
  648. >>> from sympy.polys.domains import ZZ
  649. >>> from sympy.polys.densebasic import dup_from_raw_dict
  650. >>> dup_from_raw_dict({0: ZZ(7), 2: ZZ(5), 4: ZZ(1)}, ZZ)
  651. [1, 0, 5, 0, 7]
  652. """
  653. if not f:
  654. return []
  655. n, h = max(f.keys()), []
  656. for k in range(n, -1, -1):
  657. h.append(f.get(k, K.zero))
  658. return dup_strip(h)
  659. def dmp_from_dict(f, u, K):
  660. """
  661. Create a ``K[X]`` polynomial from a ``dict``.
  662. Examples
  663. ========
  664. >>> from sympy.polys.domains import ZZ
  665. >>> from sympy.polys.densebasic import dmp_from_dict
  666. >>> dmp_from_dict({(0, 0): ZZ(3), (0, 1): ZZ(2), (2, 1): ZZ(1)}, 1, ZZ)
  667. [[1, 0], [], [2, 3]]
  668. >>> dmp_from_dict({}, 0, ZZ)
  669. []
  670. """
  671. if not u:
  672. return dup_from_dict(f, K)
  673. if not f:
  674. return dmp_zero(u)
  675. coeffs = {}
  676. for monom, coeff in f.items():
  677. head, tail = monom[0], monom[1:]
  678. if head in coeffs:
  679. coeffs[head][tail] = coeff
  680. else:
  681. coeffs[head] = { tail: coeff }
  682. n, v, h = max(coeffs.keys()), u - 1, []
  683. for k in range(n, -1, -1):
  684. coeff = coeffs.get(k)
  685. if coeff is not None:
  686. h.append(dmp_from_dict(coeff, v, K))
  687. else:
  688. h.append(dmp_zero(v))
  689. return dmp_strip(h, u)
  690. def dup_to_dict(f, K=None, zero=False):
  691. """
  692. Convert ``K[x]`` polynomial to a ``dict``.
  693. Examples
  694. ========
  695. >>> from sympy.polys.densebasic import dup_to_dict
  696. >>> dup_to_dict([1, 0, 5, 0, 7])
  697. {(0,): 7, (2,): 5, (4,): 1}
  698. >>> dup_to_dict([])
  699. {}
  700. """
  701. if not f and zero:
  702. return {(0,): K.zero}
  703. n, result = len(f) - 1, {}
  704. for k in range(0, n + 1):
  705. if f[n - k]:
  706. result[(k,)] = f[n - k]
  707. return result
  708. def dup_to_raw_dict(f, K=None, zero=False):
  709. """
  710. Convert a ``K[x]`` polynomial to a raw ``dict``.
  711. Examples
  712. ========
  713. >>> from sympy.polys.densebasic import dup_to_raw_dict
  714. >>> dup_to_raw_dict([1, 0, 5, 0, 7])
  715. {0: 7, 2: 5, 4: 1}
  716. """
  717. if not f and zero:
  718. return {0: K.zero}
  719. n, result = len(f) - 1, {}
  720. for k in range(0, n + 1):
  721. if f[n - k]:
  722. result[k] = f[n - k]
  723. return result
  724. def dmp_to_dict(f, u, K=None, zero=False):
  725. """
  726. Convert a ``K[X]`` polynomial to a ``dict````.
  727. Examples
  728. ========
  729. >>> from sympy.polys.densebasic import dmp_to_dict
  730. >>> dmp_to_dict([[1, 0], [], [2, 3]], 1)
  731. {(0, 0): 3, (0, 1): 2, (2, 1): 1}
  732. >>> dmp_to_dict([], 0)
  733. {}
  734. """
  735. if not u:
  736. return dup_to_dict(f, K, zero=zero)
  737. if dmp_zero_p(f, u) and zero:
  738. return {(0,)*(u + 1): K.zero}
  739. n, v, result = dmp_degree(f, u), u - 1, {}
  740. if n == -oo:
  741. n = -1
  742. for k in range(0, n + 1):
  743. h = dmp_to_dict(f[n - k], v)
  744. for exp, coeff in h.items():
  745. result[(k,) + exp] = coeff
  746. return result
  747. def dmp_swap(f, i, j, u, K):
  748. """
  749. Transform ``K[..x_i..x_j..]`` to ``K[..x_j..x_i..]``.
  750. Examples
  751. ========
  752. >>> from sympy.polys.domains import ZZ
  753. >>> from sympy.polys.densebasic import dmp_swap
  754. >>> f = ZZ.map([[[2], [1, 0]], []])
  755. >>> dmp_swap(f, 0, 1, 2, ZZ)
  756. [[[2], []], [[1, 0], []]]
  757. >>> dmp_swap(f, 1, 2, 2, ZZ)
  758. [[[1], [2, 0]], [[]]]
  759. >>> dmp_swap(f, 0, 2, 2, ZZ)
  760. [[[1, 0]], [[2, 0], []]]
  761. """
  762. if i < 0 or j < 0 or i > u or j > u:
  763. raise IndexError("0 <= i < j <= %s expected" % u)
  764. elif i == j:
  765. return f
  766. F, H = dmp_to_dict(f, u), {}
  767. for exp, coeff in F.items():
  768. H[exp[:i] + (exp[j],) +
  769. exp[i + 1:j] +
  770. (exp[i],) + exp[j + 1:]] = coeff
  771. return dmp_from_dict(H, u, K)
  772. def dmp_permute(f, P, u, K):
  773. """
  774. Return a polynomial in ``K[x_{P(1)},..,x_{P(n)}]``.
  775. Examples
  776. ========
  777. >>> from sympy.polys.domains import ZZ
  778. >>> from sympy.polys.densebasic import dmp_permute
  779. >>> f = ZZ.map([[[2], [1, 0]], []])
  780. >>> dmp_permute(f, [1, 0, 2], 2, ZZ)
  781. [[[2], []], [[1, 0], []]]
  782. >>> dmp_permute(f, [1, 2, 0], 2, ZZ)
  783. [[[1], []], [[2, 0], []]]
  784. """
  785. F, H = dmp_to_dict(f, u), {}
  786. for exp, coeff in F.items():
  787. new_exp = [0]*len(exp)
  788. for e, p in zip(exp, P):
  789. new_exp[p] = e
  790. H[tuple(new_exp)] = coeff
  791. return dmp_from_dict(H, u, K)
  792. def dmp_nest(f, l, K):
  793. """
  794. Return a multivariate value nested ``l``-levels.
  795. Examples
  796. ========
  797. >>> from sympy.polys.domains import ZZ
  798. >>> from sympy.polys.densebasic import dmp_nest
  799. >>> dmp_nest([[ZZ(1)]], 2, ZZ)
  800. [[[[1]]]]
  801. """
  802. if not isinstance(f, list):
  803. return dmp_ground(f, l)
  804. for i in range(l):
  805. f = [f]
  806. return f
  807. def dmp_raise(f, l, u, K):
  808. """
  809. Return a multivariate polynomial raised ``l``-levels.
  810. Examples
  811. ========
  812. >>> from sympy.polys.domains import ZZ
  813. >>> from sympy.polys.densebasic import dmp_raise
  814. >>> f = ZZ.map([[], [1, 2]])
  815. >>> dmp_raise(f, 2, 1, ZZ)
  816. [[[[]]], [[[1]], [[2]]]]
  817. """
  818. if not l:
  819. return f
  820. if not u:
  821. if not f:
  822. return dmp_zero(l)
  823. k = l - 1
  824. return [ dmp_ground(c, k) for c in f ]
  825. v = u - 1
  826. return [ dmp_raise(c, l, v, K) for c in f ]
  827. def dup_deflate(f, K):
  828. """
  829. Map ``x**m`` to ``y`` in a polynomial in ``K[x]``.
  830. Examples
  831. ========
  832. >>> from sympy.polys.domains import ZZ
  833. >>> from sympy.polys.densebasic import dup_deflate
  834. >>> f = ZZ.map([1, 0, 0, 1, 0, 0, 1])
  835. >>> dup_deflate(f, ZZ)
  836. (3, [1, 1, 1])
  837. """
  838. if dup_degree(f) <= 0:
  839. return 1, f
  840. g = 0
  841. for i in range(len(f)):
  842. if not f[-i - 1]:
  843. continue
  844. g = igcd(g, i)
  845. if g == 1:
  846. return 1, f
  847. return g, f[::g]
  848. def dmp_deflate(f, u, K):
  849. """
  850. Map ``x_i**m_i`` to ``y_i`` in a polynomial in ``K[X]``.
  851. Examples
  852. ========
  853. >>> from sympy.polys.domains import ZZ
  854. >>> from sympy.polys.densebasic import dmp_deflate
  855. >>> f = ZZ.map([[1, 0, 0, 2], [], [3, 0, 0, 4]])
  856. >>> dmp_deflate(f, 1, ZZ)
  857. ((2, 3), [[1, 2], [3, 4]])
  858. """
  859. if dmp_zero_p(f, u):
  860. return (1,)*(u + 1), f
  861. F = dmp_to_dict(f, u)
  862. B = [0]*(u + 1)
  863. for M in F.keys():
  864. for i, m in enumerate(M):
  865. B[i] = igcd(B[i], m)
  866. for i, b in enumerate(B):
  867. if not b:
  868. B[i] = 1
  869. B = tuple(B)
  870. if all(b == 1 for b in B):
  871. return B, f
  872. H = {}
  873. for A, coeff in F.items():
  874. N = [ a // b for a, b in zip(A, B) ]
  875. H[tuple(N)] = coeff
  876. return B, dmp_from_dict(H, u, K)
  877. def dup_multi_deflate(polys, K):
  878. """
  879. Map ``x**m`` to ``y`` in a set of polynomials in ``K[x]``.
  880. Examples
  881. ========
  882. >>> from sympy.polys.domains import ZZ
  883. >>> from sympy.polys.densebasic import dup_multi_deflate
  884. >>> f = ZZ.map([1, 0, 2, 0, 3])
  885. >>> g = ZZ.map([4, 0, 0])
  886. >>> dup_multi_deflate((f, g), ZZ)
  887. (2, ([1, 2, 3], [4, 0]))
  888. """
  889. G = 0
  890. for p in polys:
  891. if dup_degree(p) <= 0:
  892. return 1, polys
  893. g = 0
  894. for i in range(len(p)):
  895. if not p[-i - 1]:
  896. continue
  897. g = igcd(g, i)
  898. if g == 1:
  899. return 1, polys
  900. G = igcd(G, g)
  901. return G, tuple([ p[::G] for p in polys ])
  902. def dmp_multi_deflate(polys, u, K):
  903. """
  904. Map ``x_i**m_i`` to ``y_i`` in a set of polynomials in ``K[X]``.
  905. Examples
  906. ========
  907. >>> from sympy.polys.domains import ZZ
  908. >>> from sympy.polys.densebasic import dmp_multi_deflate
  909. >>> f = ZZ.map([[1, 0, 0, 2], [], [3, 0, 0, 4]])
  910. >>> g = ZZ.map([[1, 0, 2], [], [3, 0, 4]])
  911. >>> dmp_multi_deflate((f, g), 1, ZZ)
  912. ((2, 1), ([[1, 0, 0, 2], [3, 0, 0, 4]], [[1, 0, 2], [3, 0, 4]]))
  913. """
  914. if not u:
  915. M, H = dup_multi_deflate(polys, K)
  916. return (M,), H
  917. F, B = [], [0]*(u + 1)
  918. for p in polys:
  919. f = dmp_to_dict(p, u)
  920. if not dmp_zero_p(p, u):
  921. for M in f.keys():
  922. for i, m in enumerate(M):
  923. B[i] = igcd(B[i], m)
  924. F.append(f)
  925. for i, b in enumerate(B):
  926. if not b:
  927. B[i] = 1
  928. B = tuple(B)
  929. if all(b == 1 for b in B):
  930. return B, polys
  931. H = []
  932. for f in F:
  933. h = {}
  934. for A, coeff in f.items():
  935. N = [ a // b for a, b in zip(A, B) ]
  936. h[tuple(N)] = coeff
  937. H.append(dmp_from_dict(h, u, K))
  938. return B, tuple(H)
  939. def dup_inflate(f, m, K):
  940. """
  941. Map ``y`` to ``x**m`` in a polynomial in ``K[x]``.
  942. Examples
  943. ========
  944. >>> from sympy.polys.domains import ZZ
  945. >>> from sympy.polys.densebasic import dup_inflate
  946. >>> f = ZZ.map([1, 1, 1])
  947. >>> dup_inflate(f, 3, ZZ)
  948. [1, 0, 0, 1, 0, 0, 1]
  949. """
  950. if m <= 0:
  951. raise IndexError("'m' must be positive, got %s" % m)
  952. if m == 1 or not f:
  953. return f
  954. result = [f[0]]
  955. for coeff in f[1:]:
  956. result.extend([K.zero]*(m - 1))
  957. result.append(coeff)
  958. return result
  959. def _rec_inflate(g, M, v, i, K):
  960. """Recursive helper for :func:`dmp_inflate`."""
  961. if not v:
  962. return dup_inflate(g, M[i], K)
  963. if M[i] <= 0:
  964. raise IndexError("all M[i] must be positive, got %s" % M[i])
  965. w, j = v - 1, i + 1
  966. g = [ _rec_inflate(c, M, w, j, K) for c in g ]
  967. result = [g[0]]
  968. for coeff in g[1:]:
  969. for _ in range(1, M[i]):
  970. result.append(dmp_zero(w))
  971. result.append(coeff)
  972. return result
  973. def dmp_inflate(f, M, u, K):
  974. """
  975. Map ``y_i`` to ``x_i**k_i`` in a polynomial in ``K[X]``.
  976. Examples
  977. ========
  978. >>> from sympy.polys.domains import ZZ
  979. >>> from sympy.polys.densebasic import dmp_inflate
  980. >>> f = ZZ.map([[1, 2], [3, 4]])
  981. >>> dmp_inflate(f, (2, 3), 1, ZZ)
  982. [[1, 0, 0, 2], [], [3, 0, 0, 4]]
  983. """
  984. if not u:
  985. return dup_inflate(f, M[0], K)
  986. if all(m == 1 for m in M):
  987. return f
  988. else:
  989. return _rec_inflate(f, M, u, 0, K)
  990. def dmp_exclude(f, u, K):
  991. """
  992. Exclude useless levels from ``f``.
  993. Return the levels excluded, the new excluded ``f``, and the new ``u``.
  994. Examples
  995. ========
  996. >>> from sympy.polys.domains import ZZ
  997. >>> from sympy.polys.densebasic import dmp_exclude
  998. >>> f = ZZ.map([[[1]], [[1], [2]]])
  999. >>> dmp_exclude(f, 2, ZZ)
  1000. ([2], [[1], [1, 2]], 1)
  1001. """
  1002. if not u or dmp_ground_p(f, None, u):
  1003. return [], f, u
  1004. J, F = [], dmp_to_dict(f, u)
  1005. for j in range(0, u + 1):
  1006. for monom in F.keys():
  1007. if monom[j]:
  1008. break
  1009. else:
  1010. J.append(j)
  1011. if not J:
  1012. return [], f, u
  1013. f = {}
  1014. for monom, coeff in F.items():
  1015. monom = list(monom)
  1016. for j in reversed(J):
  1017. del monom[j]
  1018. f[tuple(monom)] = coeff
  1019. u -= len(J)
  1020. return J, dmp_from_dict(f, u, K), u
  1021. def dmp_include(f, J, u, K):
  1022. """
  1023. Include useless levels in ``f``.
  1024. Examples
  1025. ========
  1026. >>> from sympy.polys.domains import ZZ
  1027. >>> from sympy.polys.densebasic import dmp_include
  1028. >>> f = ZZ.map([[1], [1, 2]])
  1029. >>> dmp_include(f, [2], 1, ZZ)
  1030. [[[1]], [[1], [2]]]
  1031. """
  1032. if not J:
  1033. return f
  1034. F, f = dmp_to_dict(f, u), {}
  1035. for monom, coeff in F.items():
  1036. monom = list(monom)
  1037. for j in J:
  1038. monom.insert(j, 0)
  1039. f[tuple(monom)] = coeff
  1040. u += len(J)
  1041. return dmp_from_dict(f, u, K)
  1042. def dmp_inject(f, u, K, front=False):
  1043. """
  1044. Convert ``f`` from ``K[X][Y]`` to ``K[X,Y]``.
  1045. Examples
  1046. ========
  1047. >>> from sympy.polys.rings import ring
  1048. >>> from sympy.polys.domains import ZZ
  1049. >>> from sympy.polys.densebasic import dmp_inject
  1050. >>> R, x,y = ring("x,y", ZZ)
  1051. >>> dmp_inject([R(1), x + 2], 0, R.to_domain())
  1052. ([[[1]], [[1], [2]]], 2)
  1053. >>> dmp_inject([R(1), x + 2], 0, R.to_domain(), front=True)
  1054. ([[[1]], [[1, 2]]], 2)
  1055. """
  1056. f, h = dmp_to_dict(f, u), {}
  1057. v = K.ngens - 1
  1058. for f_monom, g in f.items():
  1059. g = g.to_dict()
  1060. for g_monom, c in g.items():
  1061. if front:
  1062. h[g_monom + f_monom] = c
  1063. else:
  1064. h[f_monom + g_monom] = c
  1065. w = u + v + 1
  1066. return dmp_from_dict(h, w, K.dom), w
  1067. def dmp_eject(f, u, K, front=False):
  1068. """
  1069. Convert ``f`` from ``K[X,Y]`` to ``K[X][Y]``.
  1070. Examples
  1071. ========
  1072. >>> from sympy.polys.domains import ZZ
  1073. >>> from sympy.polys.densebasic import dmp_eject
  1074. >>> dmp_eject([[[1]], [[1], [2]]], 2, ZZ['x', 'y'])
  1075. [1, x + 2]
  1076. """
  1077. f, h = dmp_to_dict(f, u), {}
  1078. n = K.ngens
  1079. v = u - K.ngens + 1
  1080. for monom, c in f.items():
  1081. if front:
  1082. g_monom, f_monom = monom[:n], monom[n:]
  1083. else:
  1084. g_monom, f_monom = monom[-n:], monom[:-n]
  1085. if f_monom in h:
  1086. h[f_monom][g_monom] = c
  1087. else:
  1088. h[f_monom] = {g_monom: c}
  1089. for monom, c in h.items():
  1090. h[monom] = K(c)
  1091. return dmp_from_dict(h, v - 1, K)
  1092. def dup_terms_gcd(f, K):
  1093. """
  1094. Remove GCD of terms from ``f`` in ``K[x]``.
  1095. Examples
  1096. ========
  1097. >>> from sympy.polys.domains import ZZ
  1098. >>> from sympy.polys.densebasic import dup_terms_gcd
  1099. >>> f = ZZ.map([1, 0, 1, 0, 0])
  1100. >>> dup_terms_gcd(f, ZZ)
  1101. (2, [1, 0, 1])
  1102. """
  1103. if dup_TC(f, K) or not f:
  1104. return 0, f
  1105. i = 0
  1106. for c in reversed(f):
  1107. if not c:
  1108. i += 1
  1109. else:
  1110. break
  1111. return i, f[:-i]
  1112. def dmp_terms_gcd(f, u, K):
  1113. """
  1114. Remove GCD of terms from ``f`` in ``K[X]``.
  1115. Examples
  1116. ========
  1117. >>> from sympy.polys.domains import ZZ
  1118. >>> from sympy.polys.densebasic import dmp_terms_gcd
  1119. >>> f = ZZ.map([[1, 0], [1, 0, 0], [], []])
  1120. >>> dmp_terms_gcd(f, 1, ZZ)
  1121. ((2, 1), [[1], [1, 0]])
  1122. """
  1123. if dmp_ground_TC(f, u, K) or dmp_zero_p(f, u):
  1124. return (0,)*(u + 1), f
  1125. F = dmp_to_dict(f, u)
  1126. G = monomial_min(*list(F.keys()))
  1127. if all(g == 0 for g in G):
  1128. return G, f
  1129. f = {}
  1130. for monom, coeff in F.items():
  1131. f[monomial_div(monom, G)] = coeff
  1132. return G, dmp_from_dict(f, u, K)
  1133. def _rec_list_terms(g, v, monom):
  1134. """Recursive helper for :func:`dmp_list_terms`."""
  1135. d, terms = dmp_degree(g, v), []
  1136. if not v:
  1137. for i, c in enumerate(g):
  1138. if not c:
  1139. continue
  1140. terms.append((monom + (d - i,), c))
  1141. else:
  1142. w = v - 1
  1143. for i, c in enumerate(g):
  1144. terms.extend(_rec_list_terms(c, w, monom + (d - i,)))
  1145. return terms
  1146. def dmp_list_terms(f, u, K, order=None):
  1147. """
  1148. List all non-zero terms from ``f`` in the given order ``order``.
  1149. Examples
  1150. ========
  1151. >>> from sympy.polys.domains import ZZ
  1152. >>> from sympy.polys.densebasic import dmp_list_terms
  1153. >>> f = ZZ.map([[1, 1], [2, 3]])
  1154. >>> dmp_list_terms(f, 1, ZZ)
  1155. [((1, 1), 1), ((1, 0), 1), ((0, 1), 2), ((0, 0), 3)]
  1156. >>> dmp_list_terms(f, 1, ZZ, order='grevlex')
  1157. [((1, 1), 1), ((1, 0), 1), ((0, 1), 2), ((0, 0), 3)]
  1158. """
  1159. def sort(terms, O):
  1160. return sorted(terms, key=lambda term: O(term[0]), reverse=True)
  1161. terms = _rec_list_terms(f, u, ())
  1162. if not terms:
  1163. return [((0,)*(u + 1), K.zero)]
  1164. if order is None:
  1165. return terms
  1166. else:
  1167. return sort(terms, monomial_key(order))
  1168. def dup_apply_pairs(f, g, h, args, K):
  1169. """
  1170. Apply ``h`` to pairs of coefficients of ``f`` and ``g``.
  1171. Examples
  1172. ========
  1173. >>> from sympy.polys.domains import ZZ
  1174. >>> from sympy.polys.densebasic import dup_apply_pairs
  1175. >>> h = lambda x, y, z: 2*x + y - z
  1176. >>> dup_apply_pairs([1, 2, 3], [3, 2, 1], h, (1,), ZZ)
  1177. [4, 5, 6]
  1178. """
  1179. n, m = len(f), len(g)
  1180. if n != m:
  1181. if n > m:
  1182. g = [K.zero]*(n - m) + g
  1183. else:
  1184. f = [K.zero]*(m - n) + f
  1185. result = []
  1186. for a, b in zip(f, g):
  1187. result.append(h(a, b, *args))
  1188. return dup_strip(result)
  1189. def dmp_apply_pairs(f, g, h, args, u, K):
  1190. """
  1191. Apply ``h`` to pairs of coefficients of ``f`` and ``g``.
  1192. Examples
  1193. ========
  1194. >>> from sympy.polys.domains import ZZ
  1195. >>> from sympy.polys.densebasic import dmp_apply_pairs
  1196. >>> h = lambda x, y, z: 2*x + y - z
  1197. >>> dmp_apply_pairs([[1], [2, 3]], [[3], [2, 1]], h, (1,), 1, ZZ)
  1198. [[4], [5, 6]]
  1199. """
  1200. if not u:
  1201. return dup_apply_pairs(f, g, h, args, K)
  1202. n, m, v = len(f), len(g), u - 1
  1203. if n != m:
  1204. if n > m:
  1205. g = dmp_zeros(n - m, v, K) + g
  1206. else:
  1207. f = dmp_zeros(m - n, v, K) + f
  1208. result = []
  1209. for a, b in zip(f, g):
  1210. result.append(dmp_apply_pairs(a, b, h, args, v, K))
  1211. return dmp_strip(result, u)
  1212. def dup_slice(f, m, n, K):
  1213. """Take a continuous subsequence of terms of ``f`` in ``K[x]``. """
  1214. k = len(f)
  1215. if k >= m:
  1216. M = k - m
  1217. else:
  1218. M = 0
  1219. if k >= n:
  1220. N = k - n
  1221. else:
  1222. N = 0
  1223. f = f[N:M]
  1224. if not f:
  1225. return []
  1226. else:
  1227. return f + [K.zero]*m
  1228. def dmp_slice(f, m, n, u, K):
  1229. """Take a continuous subsequence of terms of ``f`` in ``K[X]``. """
  1230. return dmp_slice_in(f, m, n, 0, u, K)
  1231. def dmp_slice_in(f, m, n, j, u, K):
  1232. """Take a continuous subsequence of terms of ``f`` in ``x_j`` in ``K[X]``. """
  1233. if j < 0 or j > u:
  1234. raise IndexError("-%s <= j < %s expected, got %s" % (u, u, j))
  1235. if not u:
  1236. return dup_slice(f, m, n, K)
  1237. f, g = dmp_to_dict(f, u), {}
  1238. for monom, coeff in f.items():
  1239. k = monom[j]
  1240. if k < m or k >= n:
  1241. monom = monom[:j] + (0,) + monom[j + 1:]
  1242. if monom in g:
  1243. g[monom] += coeff
  1244. else:
  1245. g[monom] = coeff
  1246. return dmp_from_dict(g, u, K)
  1247. def dup_random(n, a, b, K):
  1248. """
  1249. Return a polynomial of degree ``n`` with coefficients in ``[a, b]``.
  1250. Examples
  1251. ========
  1252. >>> from sympy.polys.domains import ZZ
  1253. >>> from sympy.polys.densebasic import dup_random
  1254. >>> dup_random(3, -10, 10, ZZ) #doctest: +SKIP
  1255. [-2, -8, 9, -4]
  1256. """
  1257. f = [ K.convert(random.randint(a, b)) for _ in range(0, n + 1) ]
  1258. while not f[0]:
  1259. f[0] = K.convert(random.randint(a, b))
  1260. return f