densearith.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. """Arithmetics for dense recursive polynomials in ``K[x]`` or ``K[X]``. """
  2. from sympy.polys.densebasic import (
  3. dup_slice,
  4. dup_LC, dmp_LC,
  5. dup_degree, dmp_degree,
  6. dup_strip, dmp_strip,
  7. dmp_zero_p, dmp_zero,
  8. dmp_one_p, dmp_one,
  9. dmp_ground, dmp_zeros)
  10. from sympy.polys.polyerrors import (ExactQuotientFailed, PolynomialDivisionFailed)
  11. def dup_add_term(f, c, i, K):
  12. """
  13. Add ``c*x**i`` to ``f`` in ``K[x]``.
  14. Examples
  15. ========
  16. >>> from sympy.polys import ring, ZZ
  17. >>> R, x = ring("x", ZZ)
  18. >>> R.dup_add_term(x**2 - 1, ZZ(2), 4)
  19. 2*x**4 + x**2 - 1
  20. """
  21. if not c:
  22. return f
  23. n = len(f)
  24. m = n - i - 1
  25. if i == n - 1:
  26. return dup_strip([f[0] + c] + f[1:])
  27. else:
  28. if i >= n:
  29. return [c] + [K.zero]*(i - n) + f
  30. else:
  31. return f[:m] + [f[m] + c] + f[m + 1:]
  32. def dmp_add_term(f, c, i, u, K):
  33. """
  34. Add ``c(x_2..x_u)*x_0**i`` to ``f`` in ``K[X]``.
  35. Examples
  36. ========
  37. >>> from sympy.polys import ring, ZZ
  38. >>> R, x,y = ring("x,y", ZZ)
  39. >>> R.dmp_add_term(x*y + 1, 2, 2)
  40. 2*x**2 + x*y + 1
  41. """
  42. if not u:
  43. return dup_add_term(f, c, i, K)
  44. v = u - 1
  45. if dmp_zero_p(c, v):
  46. return f
  47. n = len(f)
  48. m = n - i - 1
  49. if i == n - 1:
  50. return dmp_strip([dmp_add(f[0], c, v, K)] + f[1:], u)
  51. else:
  52. if i >= n:
  53. return [c] + dmp_zeros(i - n, v, K) + f
  54. else:
  55. return f[:m] + [dmp_add(f[m], c, v, K)] + f[m + 1:]
  56. def dup_sub_term(f, c, i, K):
  57. """
  58. Subtract ``c*x**i`` from ``f`` in ``K[x]``.
  59. Examples
  60. ========
  61. >>> from sympy.polys import ring, ZZ
  62. >>> R, x = ring("x", ZZ)
  63. >>> R.dup_sub_term(2*x**4 + x**2 - 1, ZZ(2), 4)
  64. x**2 - 1
  65. """
  66. if not c:
  67. return f
  68. n = len(f)
  69. m = n - i - 1
  70. if i == n - 1:
  71. return dup_strip([f[0] - c] + f[1:])
  72. else:
  73. if i >= n:
  74. return [-c] + [K.zero]*(i - n) + f
  75. else:
  76. return f[:m] + [f[m] - c] + f[m + 1:]
  77. def dmp_sub_term(f, c, i, u, K):
  78. """
  79. Subtract ``c(x_2..x_u)*x_0**i`` from ``f`` in ``K[X]``.
  80. Examples
  81. ========
  82. >>> from sympy.polys import ring, ZZ
  83. >>> R, x,y = ring("x,y", ZZ)
  84. >>> R.dmp_sub_term(2*x**2 + x*y + 1, 2, 2)
  85. x*y + 1
  86. """
  87. if not u:
  88. return dup_add_term(f, -c, i, K)
  89. v = u - 1
  90. if dmp_zero_p(c, v):
  91. return f
  92. n = len(f)
  93. m = n - i - 1
  94. if i == n - 1:
  95. return dmp_strip([dmp_sub(f[0], c, v, K)] + f[1:], u)
  96. else:
  97. if i >= n:
  98. return [dmp_neg(c, v, K)] + dmp_zeros(i - n, v, K) + f
  99. else:
  100. return f[:m] + [dmp_sub(f[m], c, v, K)] + f[m + 1:]
  101. def dup_mul_term(f, c, i, K):
  102. """
  103. Multiply ``f`` by ``c*x**i`` in ``K[x]``.
  104. Examples
  105. ========
  106. >>> from sympy.polys import ring, ZZ
  107. >>> R, x = ring("x", ZZ)
  108. >>> R.dup_mul_term(x**2 - 1, ZZ(3), 2)
  109. 3*x**4 - 3*x**2
  110. """
  111. if not c or not f:
  112. return []
  113. else:
  114. return [ cf * c for cf in f ] + [K.zero]*i
  115. def dmp_mul_term(f, c, i, u, K):
  116. """
  117. Multiply ``f`` by ``c(x_2..x_u)*x_0**i`` in ``K[X]``.
  118. Examples
  119. ========
  120. >>> from sympy.polys import ring, ZZ
  121. >>> R, x,y = ring("x,y", ZZ)
  122. >>> R.dmp_mul_term(x**2*y + x, 3*y, 2)
  123. 3*x**4*y**2 + 3*x**3*y
  124. """
  125. if not u:
  126. return dup_mul_term(f, c, i, K)
  127. v = u - 1
  128. if dmp_zero_p(f, u):
  129. return f
  130. if dmp_zero_p(c, v):
  131. return dmp_zero(u)
  132. else:
  133. return [ dmp_mul(cf, c, v, K) for cf in f ] + dmp_zeros(i, v, K)
  134. def dup_add_ground(f, c, K):
  135. """
  136. Add an element of the ground domain to ``f``.
  137. Examples
  138. ========
  139. >>> from sympy.polys import ring, ZZ
  140. >>> R, x = ring("x", ZZ)
  141. >>> R.dup_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
  142. x**3 + 2*x**2 + 3*x + 8
  143. """
  144. return dup_add_term(f, c, 0, K)
  145. def dmp_add_ground(f, c, u, K):
  146. """
  147. Add an element of the ground domain to ``f``.
  148. Examples
  149. ========
  150. >>> from sympy.polys import ring, ZZ
  151. >>> R, x,y = ring("x,y", ZZ)
  152. >>> R.dmp_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
  153. x**3 + 2*x**2 + 3*x + 8
  154. """
  155. return dmp_add_term(f, dmp_ground(c, u - 1), 0, u, K)
  156. def dup_sub_ground(f, c, K):
  157. """
  158. Subtract an element of the ground domain from ``f``.
  159. Examples
  160. ========
  161. >>> from sympy.polys import ring, ZZ
  162. >>> R, x = ring("x", ZZ)
  163. >>> R.dup_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
  164. x**3 + 2*x**2 + 3*x
  165. """
  166. return dup_sub_term(f, c, 0, K)
  167. def dmp_sub_ground(f, c, u, K):
  168. """
  169. Subtract an element of the ground domain from ``f``.
  170. Examples
  171. ========
  172. >>> from sympy.polys import ring, ZZ
  173. >>> R, x,y = ring("x,y", ZZ)
  174. >>> R.dmp_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
  175. x**3 + 2*x**2 + 3*x
  176. """
  177. return dmp_sub_term(f, dmp_ground(c, u - 1), 0, u, K)
  178. def dup_mul_ground(f, c, K):
  179. """
  180. Multiply ``f`` by a constant value in ``K[x]``.
  181. Examples
  182. ========
  183. >>> from sympy.polys import ring, ZZ
  184. >>> R, x = ring("x", ZZ)
  185. >>> R.dup_mul_ground(x**2 + 2*x - 1, ZZ(3))
  186. 3*x**2 + 6*x - 3
  187. """
  188. if not c or not f:
  189. return []
  190. else:
  191. return [ cf * c for cf in f ]
  192. def dmp_mul_ground(f, c, u, K):
  193. """
  194. Multiply ``f`` by a constant value in ``K[X]``.
  195. Examples
  196. ========
  197. >>> from sympy.polys import ring, ZZ
  198. >>> R, x,y = ring("x,y", ZZ)
  199. >>> R.dmp_mul_ground(2*x + 2*y, ZZ(3))
  200. 6*x + 6*y
  201. """
  202. if not u:
  203. return dup_mul_ground(f, c, K)
  204. v = u - 1
  205. return [ dmp_mul_ground(cf, c, v, K) for cf in f ]
  206. def dup_quo_ground(f, c, K):
  207. """
  208. Quotient by a constant in ``K[x]``.
  209. Examples
  210. ========
  211. >>> from sympy.polys import ring, ZZ, QQ
  212. >>> R, x = ring("x", ZZ)
  213. >>> R.dup_quo_ground(3*x**2 + 2, ZZ(2))
  214. x**2 + 1
  215. >>> R, x = ring("x", QQ)
  216. >>> R.dup_quo_ground(3*x**2 + 2, QQ(2))
  217. 3/2*x**2 + 1
  218. """
  219. if not c:
  220. raise ZeroDivisionError('polynomial division')
  221. if not f:
  222. return f
  223. if K.is_Field:
  224. return [ K.quo(cf, c) for cf in f ]
  225. else:
  226. return [ cf // c for cf in f ]
  227. def dmp_quo_ground(f, c, u, K):
  228. """
  229. Quotient by a constant in ``K[X]``.
  230. Examples
  231. ========
  232. >>> from sympy.polys import ring, ZZ, QQ
  233. >>> R, x,y = ring("x,y", ZZ)
  234. >>> R.dmp_quo_ground(2*x**2*y + 3*x, ZZ(2))
  235. x**2*y + x
  236. >>> R, x,y = ring("x,y", QQ)
  237. >>> R.dmp_quo_ground(2*x**2*y + 3*x, QQ(2))
  238. x**2*y + 3/2*x
  239. """
  240. if not u:
  241. return dup_quo_ground(f, c, K)
  242. v = u - 1
  243. return [ dmp_quo_ground(cf, c, v, K) for cf in f ]
  244. def dup_exquo_ground(f, c, K):
  245. """
  246. Exact quotient by a constant in ``K[x]``.
  247. Examples
  248. ========
  249. >>> from sympy.polys import ring, QQ
  250. >>> R, x = ring("x", QQ)
  251. >>> R.dup_exquo_ground(x**2 + 2, QQ(2))
  252. 1/2*x**2 + 1
  253. """
  254. if not c:
  255. raise ZeroDivisionError('polynomial division')
  256. if not f:
  257. return f
  258. return [ K.exquo(cf, c) for cf in f ]
  259. def dmp_exquo_ground(f, c, u, K):
  260. """
  261. Exact quotient by a constant in ``K[X]``.
  262. Examples
  263. ========
  264. >>> from sympy.polys import ring, QQ
  265. >>> R, x,y = ring("x,y", QQ)
  266. >>> R.dmp_exquo_ground(x**2*y + 2*x, QQ(2))
  267. 1/2*x**2*y + x
  268. """
  269. if not u:
  270. return dup_exquo_ground(f, c, K)
  271. v = u - 1
  272. return [ dmp_exquo_ground(cf, c, v, K) for cf in f ]
  273. def dup_lshift(f, n, K):
  274. """
  275. Efficiently multiply ``f`` by ``x**n`` in ``K[x]``.
  276. Examples
  277. ========
  278. >>> from sympy.polys import ring, ZZ
  279. >>> R, x = ring("x", ZZ)
  280. >>> R.dup_lshift(x**2 + 1, 2)
  281. x**4 + x**2
  282. """
  283. if not f:
  284. return f
  285. else:
  286. return f + [K.zero]*n
  287. def dup_rshift(f, n, K):
  288. """
  289. Efficiently divide ``f`` by ``x**n`` in ``K[x]``.
  290. Examples
  291. ========
  292. >>> from sympy.polys import ring, ZZ
  293. >>> R, x = ring("x", ZZ)
  294. >>> R.dup_rshift(x**4 + x**2, 2)
  295. x**2 + 1
  296. >>> R.dup_rshift(x**4 + x**2 + 2, 2)
  297. x**2 + 1
  298. """
  299. return f[:-n]
  300. def dup_abs(f, K):
  301. """
  302. Make all coefficients positive in ``K[x]``.
  303. Examples
  304. ========
  305. >>> from sympy.polys import ring, ZZ
  306. >>> R, x = ring("x", ZZ)
  307. >>> R.dup_abs(x**2 - 1)
  308. x**2 + 1
  309. """
  310. return [ K.abs(coeff) for coeff in f ]
  311. def dmp_abs(f, u, K):
  312. """
  313. Make all coefficients positive in ``K[X]``.
  314. Examples
  315. ========
  316. >>> from sympy.polys import ring, ZZ
  317. >>> R, x,y = ring("x,y", ZZ)
  318. >>> R.dmp_abs(x**2*y - x)
  319. x**2*y + x
  320. """
  321. if not u:
  322. return dup_abs(f, K)
  323. v = u - 1
  324. return [ dmp_abs(cf, v, K) for cf in f ]
  325. def dup_neg(f, K):
  326. """
  327. Negate a polynomial in ``K[x]``.
  328. Examples
  329. ========
  330. >>> from sympy.polys import ring, ZZ
  331. >>> R, x = ring("x", ZZ)
  332. >>> R.dup_neg(x**2 - 1)
  333. -x**2 + 1
  334. """
  335. return [ -coeff for coeff in f ]
  336. def dmp_neg(f, u, K):
  337. """
  338. Negate a polynomial in ``K[X]``.
  339. Examples
  340. ========
  341. >>> from sympy.polys import ring, ZZ
  342. >>> R, x,y = ring("x,y", ZZ)
  343. >>> R.dmp_neg(x**2*y - x)
  344. -x**2*y + x
  345. """
  346. if not u:
  347. return dup_neg(f, K)
  348. v = u - 1
  349. return [ dmp_neg(cf, v, K) for cf in f ]
  350. def dup_add(f, g, K):
  351. """
  352. Add dense polynomials in ``K[x]``.
  353. Examples
  354. ========
  355. >>> from sympy.polys import ring, ZZ
  356. >>> R, x = ring("x", ZZ)
  357. >>> R.dup_add(x**2 - 1, x - 2)
  358. x**2 + x - 3
  359. """
  360. if not f:
  361. return g
  362. if not g:
  363. return f
  364. df = dup_degree(f)
  365. dg = dup_degree(g)
  366. if df == dg:
  367. return dup_strip([ a + b for a, b in zip(f, g) ])
  368. else:
  369. k = abs(df - dg)
  370. if df > dg:
  371. h, f = f[:k], f[k:]
  372. else:
  373. h, g = g[:k], g[k:]
  374. return h + [ a + b for a, b in zip(f, g) ]
  375. def dmp_add(f, g, u, K):
  376. """
  377. Add dense polynomials in ``K[X]``.
  378. Examples
  379. ========
  380. >>> from sympy.polys import ring, ZZ
  381. >>> R, x,y = ring("x,y", ZZ)
  382. >>> R.dmp_add(x**2 + y, x**2*y + x)
  383. x**2*y + x**2 + x + y
  384. """
  385. if not u:
  386. return dup_add(f, g, K)
  387. df = dmp_degree(f, u)
  388. if df < 0:
  389. return g
  390. dg = dmp_degree(g, u)
  391. if dg < 0:
  392. return f
  393. v = u - 1
  394. if df == dg:
  395. return dmp_strip([ dmp_add(a, b, v, K) for a, b in zip(f, g) ], u)
  396. else:
  397. k = abs(df - dg)
  398. if df > dg:
  399. h, f = f[:k], f[k:]
  400. else:
  401. h, g = g[:k], g[k:]
  402. return h + [ dmp_add(a, b, v, K) for a, b in zip(f, g) ]
  403. def dup_sub(f, g, K):
  404. """
  405. Subtract dense polynomials in ``K[x]``.
  406. Examples
  407. ========
  408. >>> from sympy.polys import ring, ZZ
  409. >>> R, x = ring("x", ZZ)
  410. >>> R.dup_sub(x**2 - 1, x - 2)
  411. x**2 - x + 1
  412. """
  413. if not f:
  414. return dup_neg(g, K)
  415. if not g:
  416. return f
  417. df = dup_degree(f)
  418. dg = dup_degree(g)
  419. if df == dg:
  420. return dup_strip([ a - b for a, b in zip(f, g) ])
  421. else:
  422. k = abs(df - dg)
  423. if df > dg:
  424. h, f = f[:k], f[k:]
  425. else:
  426. h, g = dup_neg(g[:k], K), g[k:]
  427. return h + [ a - b for a, b in zip(f, g) ]
  428. def dmp_sub(f, g, u, K):
  429. """
  430. Subtract dense polynomials in ``K[X]``.
  431. Examples
  432. ========
  433. >>> from sympy.polys import ring, ZZ
  434. >>> R, x,y = ring("x,y", ZZ)
  435. >>> R.dmp_sub(x**2 + y, x**2*y + x)
  436. -x**2*y + x**2 - x + y
  437. """
  438. if not u:
  439. return dup_sub(f, g, K)
  440. df = dmp_degree(f, u)
  441. if df < 0:
  442. return dmp_neg(g, u, K)
  443. dg = dmp_degree(g, u)
  444. if dg < 0:
  445. return f
  446. v = u - 1
  447. if df == dg:
  448. return dmp_strip([ dmp_sub(a, b, v, K) for a, b in zip(f, g) ], u)
  449. else:
  450. k = abs(df - dg)
  451. if df > dg:
  452. h, f = f[:k], f[k:]
  453. else:
  454. h, g = dmp_neg(g[:k], u, K), g[k:]
  455. return h + [ dmp_sub(a, b, v, K) for a, b in zip(f, g) ]
  456. def dup_add_mul(f, g, h, K):
  457. """
  458. Returns ``f + g*h`` where ``f, g, h`` are in ``K[x]``.
  459. Examples
  460. ========
  461. >>> from sympy.polys import ring, ZZ
  462. >>> R, x = ring("x", ZZ)
  463. >>> R.dup_add_mul(x**2 - 1, x - 2, x + 2)
  464. 2*x**2 - 5
  465. """
  466. return dup_add(f, dup_mul(g, h, K), K)
  467. def dmp_add_mul(f, g, h, u, K):
  468. """
  469. Returns ``f + g*h`` where ``f, g, h`` are in ``K[X]``.
  470. Examples
  471. ========
  472. >>> from sympy.polys import ring, ZZ
  473. >>> R, x,y = ring("x,y", ZZ)
  474. >>> R.dmp_add_mul(x**2 + y, x, x + 2)
  475. 2*x**2 + 2*x + y
  476. """
  477. return dmp_add(f, dmp_mul(g, h, u, K), u, K)
  478. def dup_sub_mul(f, g, h, K):
  479. """
  480. Returns ``f - g*h`` where ``f, g, h`` are in ``K[x]``.
  481. Examples
  482. ========
  483. >>> from sympy.polys import ring, ZZ
  484. >>> R, x = ring("x", ZZ)
  485. >>> R.dup_sub_mul(x**2 - 1, x - 2, x + 2)
  486. 3
  487. """
  488. return dup_sub(f, dup_mul(g, h, K), K)
  489. def dmp_sub_mul(f, g, h, u, K):
  490. """
  491. Returns ``f - g*h`` where ``f, g, h`` are in ``K[X]``.
  492. Examples
  493. ========
  494. >>> from sympy.polys import ring, ZZ
  495. >>> R, x,y = ring("x,y", ZZ)
  496. >>> R.dmp_sub_mul(x**2 + y, x, x + 2)
  497. -2*x + y
  498. """
  499. return dmp_sub(f, dmp_mul(g, h, u, K), u, K)
  500. def dup_mul(f, g, K):
  501. """
  502. Multiply dense polynomials in ``K[x]``.
  503. Examples
  504. ========
  505. >>> from sympy.polys import ring, ZZ
  506. >>> R, x = ring("x", ZZ)
  507. >>> R.dup_mul(x - 2, x + 2)
  508. x**2 - 4
  509. """
  510. if f == g:
  511. return dup_sqr(f, K)
  512. if not (f and g):
  513. return []
  514. df = dup_degree(f)
  515. dg = dup_degree(g)
  516. n = max(df, dg) + 1
  517. if n < 100:
  518. h = []
  519. for i in range(0, df + dg + 1):
  520. coeff = K.zero
  521. for j in range(max(0, i - dg), min(df, i) + 1):
  522. coeff += f[j]*g[i - j]
  523. h.append(coeff)
  524. return dup_strip(h)
  525. else:
  526. # Use Karatsuba's algorithm (divide and conquer), see e.g.:
  527. # Joris van der Hoeven, Relax But Don't Be Too Lazy,
  528. # J. Symbolic Computation, 11 (2002), section 3.1.1.
  529. n2 = n//2
  530. fl, gl = dup_slice(f, 0, n2, K), dup_slice(g, 0, n2, K)
  531. fh = dup_rshift(dup_slice(f, n2, n, K), n2, K)
  532. gh = dup_rshift(dup_slice(g, n2, n, K), n2, K)
  533. lo, hi = dup_mul(fl, gl, K), dup_mul(fh, gh, K)
  534. mid = dup_mul(dup_add(fl, fh, K), dup_add(gl, gh, K), K)
  535. mid = dup_sub(mid, dup_add(lo, hi, K), K)
  536. return dup_add(dup_add(lo, dup_lshift(mid, n2, K), K),
  537. dup_lshift(hi, 2*n2, K), K)
  538. def dmp_mul(f, g, u, K):
  539. """
  540. Multiply dense polynomials in ``K[X]``.
  541. Examples
  542. ========
  543. >>> from sympy.polys import ring, ZZ
  544. >>> R, x,y = ring("x,y", ZZ)
  545. >>> R.dmp_mul(x*y + 1, x)
  546. x**2*y + x
  547. """
  548. if not u:
  549. return dup_mul(f, g, K)
  550. if f == g:
  551. return dmp_sqr(f, u, K)
  552. df = dmp_degree(f, u)
  553. if df < 0:
  554. return f
  555. dg = dmp_degree(g, u)
  556. if dg < 0:
  557. return g
  558. h, v = [], u - 1
  559. for i in range(0, df + dg + 1):
  560. coeff = dmp_zero(v)
  561. for j in range(max(0, i - dg), min(df, i) + 1):
  562. coeff = dmp_add(coeff, dmp_mul(f[j], g[i - j], v, K), v, K)
  563. h.append(coeff)
  564. return dmp_strip(h, u)
  565. def dup_sqr(f, K):
  566. """
  567. Square dense polynomials in ``K[x]``.
  568. Examples
  569. ========
  570. >>> from sympy.polys import ring, ZZ
  571. >>> R, x = ring("x", ZZ)
  572. >>> R.dup_sqr(x**2 + 1)
  573. x**4 + 2*x**2 + 1
  574. """
  575. df, h = len(f) - 1, []
  576. for i in range(0, 2*df + 1):
  577. c = K.zero
  578. jmin = max(0, i - df)
  579. jmax = min(i, df)
  580. n = jmax - jmin + 1
  581. jmax = jmin + n // 2 - 1
  582. for j in range(jmin, jmax + 1):
  583. c += f[j]*f[i - j]
  584. c += c
  585. if n & 1:
  586. elem = f[jmax + 1]
  587. c += elem**2
  588. h.append(c)
  589. return dup_strip(h)
  590. def dmp_sqr(f, u, K):
  591. """
  592. Square dense polynomials in ``K[X]``.
  593. Examples
  594. ========
  595. >>> from sympy.polys import ring, ZZ
  596. >>> R, x,y = ring("x,y", ZZ)
  597. >>> R.dmp_sqr(x**2 + x*y + y**2)
  598. x**4 + 2*x**3*y + 3*x**2*y**2 + 2*x*y**3 + y**4
  599. """
  600. if not u:
  601. return dup_sqr(f, K)
  602. df = dmp_degree(f, u)
  603. if df < 0:
  604. return f
  605. h, v = [], u - 1
  606. for i in range(0, 2*df + 1):
  607. c = dmp_zero(v)
  608. jmin = max(0, i - df)
  609. jmax = min(i, df)
  610. n = jmax - jmin + 1
  611. jmax = jmin + n // 2 - 1
  612. for j in range(jmin, jmax + 1):
  613. c = dmp_add(c, dmp_mul(f[j], f[i - j], v, K), v, K)
  614. c = dmp_mul_ground(c, K(2), v, K)
  615. if n & 1:
  616. elem = dmp_sqr(f[jmax + 1], v, K)
  617. c = dmp_add(c, elem, v, K)
  618. h.append(c)
  619. return dmp_strip(h, u)
  620. def dup_pow(f, n, K):
  621. """
  622. Raise ``f`` to the ``n``-th power in ``K[x]``.
  623. Examples
  624. ========
  625. >>> from sympy.polys import ring, ZZ
  626. >>> R, x = ring("x", ZZ)
  627. >>> R.dup_pow(x - 2, 3)
  628. x**3 - 6*x**2 + 12*x - 8
  629. """
  630. if not n:
  631. return [K.one]
  632. if n < 0:
  633. raise ValueError("Cannot raise polynomial to a negative power")
  634. if n == 1 or not f or f == [K.one]:
  635. return f
  636. g = [K.one]
  637. while True:
  638. n, m = n//2, n
  639. if m % 2:
  640. g = dup_mul(g, f, K)
  641. if not n:
  642. break
  643. f = dup_sqr(f, K)
  644. return g
  645. def dmp_pow(f, n, u, K):
  646. """
  647. Raise ``f`` to the ``n``-th power in ``K[X]``.
  648. Examples
  649. ========
  650. >>> from sympy.polys import ring, ZZ
  651. >>> R, x,y = ring("x,y", ZZ)
  652. >>> R.dmp_pow(x*y + 1, 3)
  653. x**3*y**3 + 3*x**2*y**2 + 3*x*y + 1
  654. """
  655. if not u:
  656. return dup_pow(f, n, K)
  657. if not n:
  658. return dmp_one(u, K)
  659. if n < 0:
  660. raise ValueError("Cannot raise polynomial to a negative power")
  661. if n == 1 or dmp_zero_p(f, u) or dmp_one_p(f, u, K):
  662. return f
  663. g = dmp_one(u, K)
  664. while True:
  665. n, m = n//2, n
  666. if m & 1:
  667. g = dmp_mul(g, f, u, K)
  668. if not n:
  669. break
  670. f = dmp_sqr(f, u, K)
  671. return g
  672. def dup_pdiv(f, g, K):
  673. """
  674. Polynomial pseudo-division in ``K[x]``.
  675. Examples
  676. ========
  677. >>> from sympy.polys import ring, ZZ
  678. >>> R, x = ring("x", ZZ)
  679. >>> R.dup_pdiv(x**2 + 1, 2*x - 4)
  680. (2*x + 4, 20)
  681. """
  682. df = dup_degree(f)
  683. dg = dup_degree(g)
  684. q, r, dr = [], f, df
  685. if not g:
  686. raise ZeroDivisionError("polynomial division")
  687. elif df < dg:
  688. return q, r
  689. N = df - dg + 1
  690. lc_g = dup_LC(g, K)
  691. while True:
  692. lc_r = dup_LC(r, K)
  693. j, N = dr - dg, N - 1
  694. Q = dup_mul_ground(q, lc_g, K)
  695. q = dup_add_term(Q, lc_r, j, K)
  696. R = dup_mul_ground(r, lc_g, K)
  697. G = dup_mul_term(g, lc_r, j, K)
  698. r = dup_sub(R, G, K)
  699. _dr, dr = dr, dup_degree(r)
  700. if dr < dg:
  701. break
  702. elif not (dr < _dr):
  703. raise PolynomialDivisionFailed(f, g, K)
  704. c = lc_g**N
  705. q = dup_mul_ground(q, c, K)
  706. r = dup_mul_ground(r, c, K)
  707. return q, r
  708. def dup_prem(f, g, K):
  709. """
  710. Polynomial pseudo-remainder in ``K[x]``.
  711. Examples
  712. ========
  713. >>> from sympy.polys import ring, ZZ
  714. >>> R, x = ring("x", ZZ)
  715. >>> R.dup_prem(x**2 + 1, 2*x - 4)
  716. 20
  717. """
  718. df = dup_degree(f)
  719. dg = dup_degree(g)
  720. r, dr = f, df
  721. if not g:
  722. raise ZeroDivisionError("polynomial division")
  723. elif df < dg:
  724. return r
  725. N = df - dg + 1
  726. lc_g = dup_LC(g, K)
  727. while True:
  728. lc_r = dup_LC(r, K)
  729. j, N = dr - dg, N - 1
  730. R = dup_mul_ground(r, lc_g, K)
  731. G = dup_mul_term(g, lc_r, j, K)
  732. r = dup_sub(R, G, K)
  733. _dr, dr = dr, dup_degree(r)
  734. if dr < dg:
  735. break
  736. elif not (dr < _dr):
  737. raise PolynomialDivisionFailed(f, g, K)
  738. return dup_mul_ground(r, lc_g**N, K)
  739. def dup_pquo(f, g, K):
  740. """
  741. Polynomial exact pseudo-quotient in ``K[X]``.
  742. Examples
  743. ========
  744. >>> from sympy.polys import ring, ZZ
  745. >>> R, x = ring("x", ZZ)
  746. >>> R.dup_pquo(x**2 - 1, 2*x - 2)
  747. 2*x + 2
  748. >>> R.dup_pquo(x**2 + 1, 2*x - 4)
  749. 2*x + 4
  750. """
  751. return dup_pdiv(f, g, K)[0]
  752. def dup_pexquo(f, g, K):
  753. """
  754. Polynomial pseudo-quotient in ``K[x]``.
  755. Examples
  756. ========
  757. >>> from sympy.polys import ring, ZZ
  758. >>> R, x = ring("x", ZZ)
  759. >>> R.dup_pexquo(x**2 - 1, 2*x - 2)
  760. 2*x + 2
  761. >>> R.dup_pexquo(x**2 + 1, 2*x - 4)
  762. Traceback (most recent call last):
  763. ...
  764. ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]
  765. """
  766. q, r = dup_pdiv(f, g, K)
  767. if not r:
  768. return q
  769. else:
  770. raise ExactQuotientFailed(f, g)
  771. def dmp_pdiv(f, g, u, K):
  772. """
  773. Polynomial pseudo-division in ``K[X]``.
  774. Examples
  775. ========
  776. >>> from sympy.polys import ring, ZZ
  777. >>> R, x,y = ring("x,y", ZZ)
  778. >>> R.dmp_pdiv(x**2 + x*y, 2*x + 2)
  779. (2*x + 2*y - 2, -4*y + 4)
  780. """
  781. if not u:
  782. return dup_pdiv(f, g, K)
  783. df = dmp_degree(f, u)
  784. dg = dmp_degree(g, u)
  785. if dg < 0:
  786. raise ZeroDivisionError("polynomial division")
  787. q, r, dr = dmp_zero(u), f, df
  788. if df < dg:
  789. return q, r
  790. N = df - dg + 1
  791. lc_g = dmp_LC(g, K)
  792. while True:
  793. lc_r = dmp_LC(r, K)
  794. j, N = dr - dg, N - 1
  795. Q = dmp_mul_term(q, lc_g, 0, u, K)
  796. q = dmp_add_term(Q, lc_r, j, u, K)
  797. R = dmp_mul_term(r, lc_g, 0, u, K)
  798. G = dmp_mul_term(g, lc_r, j, u, K)
  799. r = dmp_sub(R, G, u, K)
  800. _dr, dr = dr, dmp_degree(r, u)
  801. if dr < dg:
  802. break
  803. elif not (dr < _dr):
  804. raise PolynomialDivisionFailed(f, g, K)
  805. c = dmp_pow(lc_g, N, u - 1, K)
  806. q = dmp_mul_term(q, c, 0, u, K)
  807. r = dmp_mul_term(r, c, 0, u, K)
  808. return q, r
  809. def dmp_prem(f, g, u, K):
  810. """
  811. Polynomial pseudo-remainder in ``K[X]``.
  812. Examples
  813. ========
  814. >>> from sympy.polys import ring, ZZ
  815. >>> R, x,y = ring("x,y", ZZ)
  816. >>> R.dmp_prem(x**2 + x*y, 2*x + 2)
  817. -4*y + 4
  818. """
  819. if not u:
  820. return dup_prem(f, g, K)
  821. df = dmp_degree(f, u)
  822. dg = dmp_degree(g, u)
  823. if dg < 0:
  824. raise ZeroDivisionError("polynomial division")
  825. r, dr = f, df
  826. if df < dg:
  827. return r
  828. N = df - dg + 1
  829. lc_g = dmp_LC(g, K)
  830. while True:
  831. lc_r = dmp_LC(r, K)
  832. j, N = dr - dg, N - 1
  833. R = dmp_mul_term(r, lc_g, 0, u, K)
  834. G = dmp_mul_term(g, lc_r, j, u, K)
  835. r = dmp_sub(R, G, u, K)
  836. _dr, dr = dr, dmp_degree(r, u)
  837. if dr < dg:
  838. break
  839. elif not (dr < _dr):
  840. raise PolynomialDivisionFailed(f, g, K)
  841. c = dmp_pow(lc_g, N, u - 1, K)
  842. return dmp_mul_term(r, c, 0, u, K)
  843. def dmp_pquo(f, g, u, K):
  844. """
  845. Polynomial exact pseudo-quotient in ``K[X]``.
  846. Examples
  847. ========
  848. >>> from sympy.polys import ring, ZZ
  849. >>> R, x,y = ring("x,y", ZZ)
  850. >>> f = x**2 + x*y
  851. >>> g = 2*x + 2*y
  852. >>> h = 2*x + 2
  853. >>> R.dmp_pquo(f, g)
  854. 2*x
  855. >>> R.dmp_pquo(f, h)
  856. 2*x + 2*y - 2
  857. """
  858. return dmp_pdiv(f, g, u, K)[0]
  859. def dmp_pexquo(f, g, u, K):
  860. """
  861. Polynomial pseudo-quotient in ``K[X]``.
  862. Examples
  863. ========
  864. >>> from sympy.polys import ring, ZZ
  865. >>> R, x,y = ring("x,y", ZZ)
  866. >>> f = x**2 + x*y
  867. >>> g = 2*x + 2*y
  868. >>> h = 2*x + 2
  869. >>> R.dmp_pexquo(f, g)
  870. 2*x
  871. >>> R.dmp_pexquo(f, h)
  872. Traceback (most recent call last):
  873. ...
  874. ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]
  875. """
  876. q, r = dmp_pdiv(f, g, u, K)
  877. if dmp_zero_p(r, u):
  878. return q
  879. else:
  880. raise ExactQuotientFailed(f, g)
  881. def dup_rr_div(f, g, K):
  882. """
  883. Univariate division with remainder over a ring.
  884. Examples
  885. ========
  886. >>> from sympy.polys import ring, ZZ
  887. >>> R, x = ring("x", ZZ)
  888. >>> R.dup_rr_div(x**2 + 1, 2*x - 4)
  889. (0, x**2 + 1)
  890. """
  891. df = dup_degree(f)
  892. dg = dup_degree(g)
  893. q, r, dr = [], f, df
  894. if not g:
  895. raise ZeroDivisionError("polynomial division")
  896. elif df < dg:
  897. return q, r
  898. lc_g = dup_LC(g, K)
  899. while True:
  900. lc_r = dup_LC(r, K)
  901. if lc_r % lc_g:
  902. break
  903. c = K.exquo(lc_r, lc_g)
  904. j = dr - dg
  905. q = dup_add_term(q, c, j, K)
  906. h = dup_mul_term(g, c, j, K)
  907. r = dup_sub(r, h, K)
  908. _dr, dr = dr, dup_degree(r)
  909. if dr < dg:
  910. break
  911. elif not (dr < _dr):
  912. raise PolynomialDivisionFailed(f, g, K)
  913. return q, r
  914. def dmp_rr_div(f, g, u, K):
  915. """
  916. Multivariate division with remainder over a ring.
  917. Examples
  918. ========
  919. >>> from sympy.polys import ring, ZZ
  920. >>> R, x,y = ring("x,y", ZZ)
  921. >>> R.dmp_rr_div(x**2 + x*y, 2*x + 2)
  922. (0, x**2 + x*y)
  923. """
  924. if not u:
  925. return dup_rr_div(f, g, K)
  926. df = dmp_degree(f, u)
  927. dg = dmp_degree(g, u)
  928. if dg < 0:
  929. raise ZeroDivisionError("polynomial division")
  930. q, r, dr = dmp_zero(u), f, df
  931. if df < dg:
  932. return q, r
  933. lc_g, v = dmp_LC(g, K), u - 1
  934. while True:
  935. lc_r = dmp_LC(r, K)
  936. c, R = dmp_rr_div(lc_r, lc_g, v, K)
  937. if not dmp_zero_p(R, v):
  938. break
  939. j = dr - dg
  940. q = dmp_add_term(q, c, j, u, K)
  941. h = dmp_mul_term(g, c, j, u, K)
  942. r = dmp_sub(r, h, u, K)
  943. _dr, dr = dr, dmp_degree(r, u)
  944. if dr < dg:
  945. break
  946. elif not (dr < _dr):
  947. raise PolynomialDivisionFailed(f, g, K)
  948. return q, r
  949. def dup_ff_div(f, g, K):
  950. """
  951. Polynomial division with remainder over a field.
  952. Examples
  953. ========
  954. >>> from sympy.polys import ring, QQ
  955. >>> R, x = ring("x", QQ)
  956. >>> R.dup_ff_div(x**2 + 1, 2*x - 4)
  957. (1/2*x + 1, 5)
  958. """
  959. df = dup_degree(f)
  960. dg = dup_degree(g)
  961. q, r, dr = [], f, df
  962. if not g:
  963. raise ZeroDivisionError("polynomial division")
  964. elif df < dg:
  965. return q, r
  966. lc_g = dup_LC(g, K)
  967. while True:
  968. lc_r = dup_LC(r, K)
  969. c = K.exquo(lc_r, lc_g)
  970. j = dr - dg
  971. q = dup_add_term(q, c, j, K)
  972. h = dup_mul_term(g, c, j, K)
  973. r = dup_sub(r, h, K)
  974. _dr, dr = dr, dup_degree(r)
  975. if dr < dg:
  976. break
  977. elif dr == _dr and not K.is_Exact:
  978. # remove leading term created by rounding error
  979. r = dup_strip(r[1:])
  980. dr = dup_degree(r)
  981. if dr < dg:
  982. break
  983. elif not (dr < _dr):
  984. raise PolynomialDivisionFailed(f, g, K)
  985. return q, r
  986. def dmp_ff_div(f, g, u, K):
  987. """
  988. Polynomial division with remainder over a field.
  989. Examples
  990. ========
  991. >>> from sympy.polys import ring, QQ
  992. >>> R, x,y = ring("x,y", QQ)
  993. >>> R.dmp_ff_div(x**2 + x*y, 2*x + 2)
  994. (1/2*x + 1/2*y - 1/2, -y + 1)
  995. """
  996. if not u:
  997. return dup_ff_div(f, g, K)
  998. df = dmp_degree(f, u)
  999. dg = dmp_degree(g, u)
  1000. if dg < 0:
  1001. raise ZeroDivisionError("polynomial division")
  1002. q, r, dr = dmp_zero(u), f, df
  1003. if df < dg:
  1004. return q, r
  1005. lc_g, v = dmp_LC(g, K), u - 1
  1006. while True:
  1007. lc_r = dmp_LC(r, K)
  1008. c, R = dmp_ff_div(lc_r, lc_g, v, K)
  1009. if not dmp_zero_p(R, v):
  1010. break
  1011. j = dr - dg
  1012. q = dmp_add_term(q, c, j, u, K)
  1013. h = dmp_mul_term(g, c, j, u, K)
  1014. r = dmp_sub(r, h, u, K)
  1015. _dr, dr = dr, dmp_degree(r, u)
  1016. if dr < dg:
  1017. break
  1018. elif not (dr < _dr):
  1019. raise PolynomialDivisionFailed(f, g, K)
  1020. return q, r
  1021. def dup_div(f, g, K):
  1022. """
  1023. Polynomial division with remainder in ``K[x]``.
  1024. Examples
  1025. ========
  1026. >>> from sympy.polys import ring, ZZ, QQ
  1027. >>> R, x = ring("x", ZZ)
  1028. >>> R.dup_div(x**2 + 1, 2*x - 4)
  1029. (0, x**2 + 1)
  1030. >>> R, x = ring("x", QQ)
  1031. >>> R.dup_div(x**2 + 1, 2*x - 4)
  1032. (1/2*x + 1, 5)
  1033. """
  1034. if K.is_Field:
  1035. return dup_ff_div(f, g, K)
  1036. else:
  1037. return dup_rr_div(f, g, K)
  1038. def dup_rem(f, g, K):
  1039. """
  1040. Returns polynomial remainder in ``K[x]``.
  1041. Examples
  1042. ========
  1043. >>> from sympy.polys import ring, ZZ, QQ
  1044. >>> R, x = ring("x", ZZ)
  1045. >>> R.dup_rem(x**2 + 1, 2*x - 4)
  1046. x**2 + 1
  1047. >>> R, x = ring("x", QQ)
  1048. >>> R.dup_rem(x**2 + 1, 2*x - 4)
  1049. 5
  1050. """
  1051. return dup_div(f, g, K)[1]
  1052. def dup_quo(f, g, K):
  1053. """
  1054. Returns exact polynomial quotient in ``K[x]``.
  1055. Examples
  1056. ========
  1057. >>> from sympy.polys import ring, ZZ, QQ
  1058. >>> R, x = ring("x", ZZ)
  1059. >>> R.dup_quo(x**2 + 1, 2*x - 4)
  1060. 0
  1061. >>> R, x = ring("x", QQ)
  1062. >>> R.dup_quo(x**2 + 1, 2*x - 4)
  1063. 1/2*x + 1
  1064. """
  1065. return dup_div(f, g, K)[0]
  1066. def dup_exquo(f, g, K):
  1067. """
  1068. Returns polynomial quotient in ``K[x]``.
  1069. Examples
  1070. ========
  1071. >>> from sympy.polys import ring, ZZ
  1072. >>> R, x = ring("x", ZZ)
  1073. >>> R.dup_exquo(x**2 - 1, x - 1)
  1074. x + 1
  1075. >>> R.dup_exquo(x**2 + 1, 2*x - 4)
  1076. Traceback (most recent call last):
  1077. ...
  1078. ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]
  1079. """
  1080. q, r = dup_div(f, g, K)
  1081. if not r:
  1082. return q
  1083. else:
  1084. raise ExactQuotientFailed(f, g)
  1085. def dmp_div(f, g, u, K):
  1086. """
  1087. Polynomial division with remainder in ``K[X]``.
  1088. Examples
  1089. ========
  1090. >>> from sympy.polys import ring, ZZ, QQ
  1091. >>> R, x,y = ring("x,y", ZZ)
  1092. >>> R.dmp_div(x**2 + x*y, 2*x + 2)
  1093. (0, x**2 + x*y)
  1094. >>> R, x,y = ring("x,y", QQ)
  1095. >>> R.dmp_div(x**2 + x*y, 2*x + 2)
  1096. (1/2*x + 1/2*y - 1/2, -y + 1)
  1097. """
  1098. if K.is_Field:
  1099. return dmp_ff_div(f, g, u, K)
  1100. else:
  1101. return dmp_rr_div(f, g, u, K)
  1102. def dmp_rem(f, g, u, K):
  1103. """
  1104. Returns polynomial remainder in ``K[X]``.
  1105. Examples
  1106. ========
  1107. >>> from sympy.polys import ring, ZZ, QQ
  1108. >>> R, x,y = ring("x,y", ZZ)
  1109. >>> R.dmp_rem(x**2 + x*y, 2*x + 2)
  1110. x**2 + x*y
  1111. >>> R, x,y = ring("x,y", QQ)
  1112. >>> R.dmp_rem(x**2 + x*y, 2*x + 2)
  1113. -y + 1
  1114. """
  1115. return dmp_div(f, g, u, K)[1]
  1116. def dmp_quo(f, g, u, K):
  1117. """
  1118. Returns exact polynomial quotient in ``K[X]``.
  1119. Examples
  1120. ========
  1121. >>> from sympy.polys import ring, ZZ, QQ
  1122. >>> R, x,y = ring("x,y", ZZ)
  1123. >>> R.dmp_quo(x**2 + x*y, 2*x + 2)
  1124. 0
  1125. >>> R, x,y = ring("x,y", QQ)
  1126. >>> R.dmp_quo(x**2 + x*y, 2*x + 2)
  1127. 1/2*x + 1/2*y - 1/2
  1128. """
  1129. return dmp_div(f, g, u, K)[0]
  1130. def dmp_exquo(f, g, u, K):
  1131. """
  1132. Returns polynomial quotient in ``K[X]``.
  1133. Examples
  1134. ========
  1135. >>> from sympy.polys import ring, ZZ
  1136. >>> R, x,y = ring("x,y", ZZ)
  1137. >>> f = x**2 + x*y
  1138. >>> g = x + y
  1139. >>> h = 2*x + 2
  1140. >>> R.dmp_exquo(f, g)
  1141. x
  1142. >>> R.dmp_exquo(f, h)
  1143. Traceback (most recent call last):
  1144. ...
  1145. ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]
  1146. """
  1147. q, r = dmp_div(f, g, u, K)
  1148. if dmp_zero_p(r, u):
  1149. return q
  1150. else:
  1151. raise ExactQuotientFailed(f, g)
  1152. def dup_max_norm(f, K):
  1153. """
  1154. Returns maximum norm of a polynomial in ``K[x]``.
  1155. Examples
  1156. ========
  1157. >>> from sympy.polys import ring, ZZ
  1158. >>> R, x = ring("x", ZZ)
  1159. >>> R.dup_max_norm(-x**2 + 2*x - 3)
  1160. 3
  1161. """
  1162. if not f:
  1163. return K.zero
  1164. else:
  1165. return max(dup_abs(f, K))
  1166. def dmp_max_norm(f, u, K):
  1167. """
  1168. Returns maximum norm of a polynomial in ``K[X]``.
  1169. Examples
  1170. ========
  1171. >>> from sympy.polys import ring, ZZ
  1172. >>> R, x,y = ring("x,y", ZZ)
  1173. >>> R.dmp_max_norm(2*x*y - x - 3)
  1174. 3
  1175. """
  1176. if not u:
  1177. return dup_max_norm(f, K)
  1178. v = u - 1
  1179. return max([ dmp_max_norm(c, v, K) for c in f ])
  1180. def dup_l1_norm(f, K):
  1181. """
  1182. Returns l1 norm of a polynomial in ``K[x]``.
  1183. Examples
  1184. ========
  1185. >>> from sympy.polys import ring, ZZ
  1186. >>> R, x = ring("x", ZZ)
  1187. >>> R.dup_l1_norm(2*x**3 - 3*x**2 + 1)
  1188. 6
  1189. """
  1190. if not f:
  1191. return K.zero
  1192. else:
  1193. return sum(dup_abs(f, K))
  1194. def dmp_l1_norm(f, u, K):
  1195. """
  1196. Returns l1 norm of a polynomial in ``K[X]``.
  1197. Examples
  1198. ========
  1199. >>> from sympy.polys import ring, ZZ
  1200. >>> R, x,y = ring("x,y", ZZ)
  1201. >>> R.dmp_l1_norm(2*x*y - x - 3)
  1202. 6
  1203. """
  1204. if not u:
  1205. return dup_l1_norm(f, K)
  1206. v = u - 1
  1207. return sum([ dmp_l1_norm(c, v, K) for c in f ])
  1208. def dup_l2_norm_squared(f, K):
  1209. """
  1210. Returns squared l2 norm of a polynomial in ``K[x]``.
  1211. Examples
  1212. ========
  1213. >>> from sympy.polys import ring, ZZ
  1214. >>> R, x = ring("x", ZZ)
  1215. >>> R.dup_l2_norm_squared(2*x**3 - 3*x**2 + 1)
  1216. 14
  1217. """
  1218. return sum([coeff**2 for coeff in f], K.zero)
  1219. def dmp_l2_norm_squared(f, u, K):
  1220. """
  1221. Returns squared l2 norm of a polynomial in ``K[X]``.
  1222. Examples
  1223. ========
  1224. >>> from sympy.polys import ring, ZZ
  1225. >>> R, x,y = ring("x,y", ZZ)
  1226. >>> R.dmp_l2_norm_squared(2*x*y - x - 3)
  1227. 14
  1228. """
  1229. if not u:
  1230. return dup_l2_norm_squared(f, K)
  1231. v = u - 1
  1232. return sum([ dmp_l2_norm_squared(c, v, K) for c in f ])
  1233. def dup_expand(polys, K):
  1234. """
  1235. Multiply together several polynomials in ``K[x]``.
  1236. Examples
  1237. ========
  1238. >>> from sympy.polys import ring, ZZ
  1239. >>> R, x = ring("x", ZZ)
  1240. >>> R.dup_expand([x**2 - 1, x, 2])
  1241. 2*x**3 - 2*x
  1242. """
  1243. if not polys:
  1244. return [K.one]
  1245. f = polys[0]
  1246. for g in polys[1:]:
  1247. f = dup_mul(f, g, K)
  1248. return f
  1249. def dmp_expand(polys, u, K):
  1250. """
  1251. Multiply together several polynomials in ``K[X]``.
  1252. Examples
  1253. ========
  1254. >>> from sympy.polys import ring, ZZ
  1255. >>> R, x,y = ring("x,y", ZZ)
  1256. >>> R.dmp_expand([x**2 + y**2, x + 1])
  1257. x**3 + x**2 + x*y**2 + y**2
  1258. """
  1259. if not polys:
  1260. return dmp_one(u, K)
  1261. f = polys[0]
  1262. for g in polys[1:]:
  1263. f = dmp_mul(f, g, u, K)
  1264. return f