test_convolutions.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. from sympy.core.numbers import (E, Rational, pi)
  2. from sympy.functions.elementary.exponential import exp
  3. from sympy.functions.elementary.miscellaneous import sqrt
  4. from sympy.core import S, symbols, I
  5. from sympy.discrete.convolutions import (
  6. convolution, convolution_fft, convolution_ntt, convolution_fwht,
  7. convolution_subset, covering_product, intersecting_product,
  8. convolution_int)
  9. from sympy.testing.pytest import raises
  10. from sympy.abc import x, y
  11. def test_convolution():
  12. # fft
  13. a = [1, Rational(5, 3), sqrt(3), Rational(7, 5)]
  14. b = [9, 5, 5, 4, 3, 2]
  15. c = [3, 5, 3, 7, 8]
  16. d = [1422, 6572, 3213, 5552]
  17. e = [-1, Rational(5, 3), Rational(7, 5)]
  18. assert convolution(a, b) == convolution_fft(a, b)
  19. assert convolution(a, b, dps=9) == convolution_fft(a, b, dps=9)
  20. assert convolution(a, d, dps=7) == convolution_fft(d, a, dps=7)
  21. assert convolution(a, d[1:], dps=3) == convolution_fft(d[1:], a, dps=3)
  22. # prime moduli of the form (m*2**k + 1), sequence length
  23. # should be a divisor of 2**k
  24. p = 7*17*2**23 + 1
  25. q = 19*2**10 + 1
  26. # ntt
  27. assert convolution(d, b, prime=q) == convolution_ntt(b, d, prime=q)
  28. assert convolution(c, b, prime=p) == convolution_ntt(b, c, prime=p)
  29. assert convolution(d, c, prime=p) == convolution_ntt(c, d, prime=p)
  30. raises(TypeError, lambda: convolution(b, d, dps=5, prime=q))
  31. raises(TypeError, lambda: convolution(b, d, dps=6, prime=q))
  32. # fwht
  33. assert convolution(a, b, dyadic=True) == convolution_fwht(a, b)
  34. assert convolution(a, b, dyadic=False) == convolution(a, b)
  35. raises(TypeError, lambda: convolution(b, d, dps=2, dyadic=True))
  36. raises(TypeError, lambda: convolution(b, d, prime=p, dyadic=True))
  37. raises(TypeError, lambda: convolution(a, b, dps=2, dyadic=True))
  38. raises(TypeError, lambda: convolution(b, c, prime=p, dyadic=True))
  39. # subset
  40. assert convolution(a, b, subset=True) == convolution_subset(a, b) == \
  41. convolution(a, b, subset=True, dyadic=False) == \
  42. convolution(a, b, subset=True)
  43. assert convolution(a, b, subset=False) == convolution(a, b)
  44. raises(TypeError, lambda: convolution(a, b, subset=True, dyadic=True))
  45. raises(TypeError, lambda: convolution(c, d, subset=True, dps=6))
  46. raises(TypeError, lambda: convolution(a, c, subset=True, prime=q))
  47. # integer
  48. assert convolution([0], [0]) == convolution_int([0], [0])
  49. assert convolution(b, c) == convolution_int(b, c)
  50. # rational
  51. assert convolution([Rational(1,2)], [Rational(1,2)]) == [Rational(1, 4)]
  52. assert convolution(b, e) == [-9, 10, Rational(239, 15), Rational(34, 3),
  53. Rational(32, 3), Rational(43, 5), Rational(113, 15),
  54. Rational(14, 5)]
  55. def test_cyclic_convolution():
  56. # fft
  57. a = [1, Rational(5, 3), sqrt(3), Rational(7, 5)]
  58. b = [9, 5, 5, 4, 3, 2]
  59. assert convolution([1, 2, 3], [4, 5, 6], cycle=0) == \
  60. convolution([1, 2, 3], [4, 5, 6], cycle=5) == \
  61. convolution([1, 2, 3], [4, 5, 6])
  62. assert convolution([1, 2, 3], [4, 5, 6], cycle=3) == [31, 31, 28]
  63. a = [Rational(1, 3), Rational(7, 3), Rational(5, 9), Rational(2, 7), Rational(5, 8)]
  64. b = [Rational(3, 5), Rational(4, 7), Rational(7, 8), Rational(8, 9)]
  65. assert convolution(a, b, cycle=0) == \
  66. convolution(a, b, cycle=len(a) + len(b) - 1)
  67. assert convolution(a, b, cycle=4) == [Rational(87277, 26460), Rational(30521, 11340),
  68. Rational(11125, 4032), Rational(3653, 1080)]
  69. assert convolution(a, b, cycle=6) == [Rational(20177, 20160), Rational(676, 315), Rational(47, 24),
  70. Rational(3053, 1080), Rational(16397, 5292), Rational(2497, 2268)]
  71. assert convolution(a, b, cycle=9) == \
  72. convolution(a, b, cycle=0) + [S.Zero]
  73. # ntt
  74. a = [2313, 5323532, S(3232), 42142, 42242421]
  75. b = [S(33456), 56757, 45754, 432423]
  76. assert convolution(a, b, prime=19*2**10 + 1, cycle=0) == \
  77. convolution(a, b, prime=19*2**10 + 1, cycle=8) == \
  78. convolution(a, b, prime=19*2**10 + 1)
  79. assert convolution(a, b, prime=19*2**10 + 1, cycle=5) == [96, 17146, 2664,
  80. 15534, 3517]
  81. assert convolution(a, b, prime=19*2**10 + 1, cycle=7) == [4643, 3458, 1260,
  82. 15534, 3517, 16314, 13688]
  83. assert convolution(a, b, prime=19*2**10 + 1, cycle=9) == \
  84. convolution(a, b, prime=19*2**10 + 1) + [0]
  85. # fwht
  86. u, v, w, x, y = symbols('u v w x y')
  87. p, q, r, s, t = symbols('p q r s t')
  88. c = [u, v, w, x, y]
  89. d = [p, q, r, s, t]
  90. assert convolution(a, b, dyadic=True, cycle=3) == \
  91. [2499522285783, 19861417974796, 4702176579021]
  92. assert convolution(a, b, dyadic=True, cycle=5) == [2718149225143,
  93. 2114320852171, 20571217906407, 246166418903, 1413262436976]
  94. assert convolution(c, d, dyadic=True, cycle=4) == \
  95. [p*u + p*y + q*v + r*w + s*x + t*u + t*y,
  96. p*v + q*u + q*y + r*x + s*w + t*v,
  97. p*w + q*x + r*u + r*y + s*v + t*w,
  98. p*x + q*w + r*v + s*u + s*y + t*x]
  99. assert convolution(c, d, dyadic=True, cycle=6) == \
  100. [p*u + q*v + r*w + r*y + s*x + t*w + t*y,
  101. p*v + q*u + r*x + s*w + s*y + t*x,
  102. p*w + q*x + r*u + s*v,
  103. p*x + q*w + r*v + s*u,
  104. p*y + t*u,
  105. q*y + t*v]
  106. # subset
  107. assert convolution(a, b, subset=True, cycle=7) == [18266671799811,
  108. 178235365533, 213958794, 246166418903, 1413262436976,
  109. 2397553088697, 1932759730434]
  110. assert convolution(a[1:], b, subset=True, cycle=4) == \
  111. [178104086592, 302255835516, 244982785880, 3717819845434]
  112. assert convolution(a, b[:-1], subset=True, cycle=6) == [1932837114162,
  113. 178235365533, 213958794, 245166224504, 1413262436976, 2397553088697]
  114. assert convolution(c, d, subset=True, cycle=3) == \
  115. [p*u + p*x + q*w + r*v + r*y + s*u + t*w,
  116. p*v + p*y + q*u + s*y + t*u + t*x,
  117. p*w + q*y + r*u + t*v]
  118. assert convolution(c, d, subset=True, cycle=5) == \
  119. [p*u + q*y + t*v,
  120. p*v + q*u + r*y + t*w,
  121. p*w + r*u + s*y + t*x,
  122. p*x + q*w + r*v + s*u,
  123. p*y + t*u]
  124. raises(ValueError, lambda: convolution([1, 2, 3], [4, 5, 6], cycle=-1))
  125. def test_convolution_fft():
  126. assert all(convolution_fft([], x, dps=y) == [] for x in ([], [1]) for y in (None, 3))
  127. assert convolution_fft([1, 2, 3], [4, 5, 6]) == [4, 13, 28, 27, 18]
  128. assert convolution_fft([1], [5, 6, 7]) == [5, 6, 7]
  129. assert convolution_fft([1, 3], [5, 6, 7]) == [5, 21, 25, 21]
  130. assert convolution_fft([1 + 2*I], [2 + 3*I]) == [-4 + 7*I]
  131. assert convolution_fft([1 + 2*I, 3 + 4*I, 5 + 3*I/5], [Rational(2, 5) + 4*I/7]) == \
  132. [Rational(-26, 35) + I*48/35, Rational(-38, 35) + I*116/35, Rational(58, 35) + I*542/175]
  133. assert convolution_fft([Rational(3, 4), Rational(5, 6)], [Rational(7, 8), Rational(1, 3), Rational(2, 5)]) == \
  134. [Rational(21, 32), Rational(47, 48), Rational(26, 45), Rational(1, 3)]
  135. assert convolution_fft([Rational(1, 9), Rational(2, 3), Rational(3, 5)], [Rational(2, 5), Rational(3, 7), Rational(4, 9)]) == \
  136. [Rational(2, 45), Rational(11, 35), Rational(8152, 14175), Rational(523, 945), Rational(4, 15)]
  137. assert convolution_fft([pi, E, sqrt(2)], [sqrt(3), 1/pi, 1/E]) == \
  138. [sqrt(3)*pi, 1 + sqrt(3)*E, E/pi + pi*exp(-1) + sqrt(6),
  139. sqrt(2)/pi + 1, sqrt(2)*exp(-1)]
  140. assert convolution_fft([2321, 33123], [5321, 6321, 71323]) == \
  141. [12350041, 190918524, 374911166, 2362431729]
  142. assert convolution_fft([312313, 31278232], [32139631, 319631]) == \
  143. [10037624576503, 1005370659728895, 9997492572392]
  144. raises(TypeError, lambda: convolution_fft(x, y))
  145. raises(ValueError, lambda: convolution_fft([x, y], [y, x]))
  146. def test_convolution_ntt():
  147. # prime moduli of the form (m*2**k + 1), sequence length
  148. # should be a divisor of 2**k
  149. p = 7*17*2**23 + 1
  150. q = 19*2**10 + 1
  151. r = 2*500000003 + 1 # only for sequences of length 1 or 2
  152. # s = 2*3*5*7 # composite modulus
  153. assert all(convolution_ntt([], x, prime=y) == [] for x in ([], [1]) for y in (p, q, r))
  154. assert convolution_ntt([2], [3], r) == [6]
  155. assert convolution_ntt([2, 3], [4], r) == [8, 12]
  156. assert convolution_ntt([32121, 42144, 4214, 4241], [32132, 3232, 87242], p) == [33867619,
  157. 459741727, 79180879, 831885249, 381344700, 369993322]
  158. assert convolution_ntt([121913, 3171831, 31888131, 12], [17882, 21292, 29921, 312], q) == \
  159. [8158, 3065, 3682, 7090, 1239, 2232, 3744]
  160. assert convolution_ntt([12, 19, 21, 98, 67], [2, 6, 7, 8, 9], p) == \
  161. convolution_ntt([12, 19, 21, 98, 67], [2, 6, 7, 8, 9], q)
  162. assert convolution_ntt([12, 19, 21, 98, 67], [21, 76, 17, 78, 69], p) == \
  163. convolution_ntt([12, 19, 21, 98, 67], [21, 76, 17, 78, 69], q)
  164. raises(ValueError, lambda: convolution_ntt([2, 3], [4, 5], r))
  165. raises(ValueError, lambda: convolution_ntt([x, y], [y, x], q))
  166. raises(TypeError, lambda: convolution_ntt(x, y, p))
  167. def test_convolution_fwht():
  168. assert convolution_fwht([], []) == []
  169. assert convolution_fwht([], [1]) == []
  170. assert convolution_fwht([1, 2, 3], [4, 5, 6]) == [32, 13, 18, 27]
  171. assert convolution_fwht([Rational(5, 7), Rational(6, 8), Rational(7, 3)], [2, 4, Rational(6, 7)]) == \
  172. [Rational(45, 7), Rational(61, 14), Rational(776, 147), Rational(419, 42)]
  173. a = [1, Rational(5, 3), sqrt(3), Rational(7, 5), 4 + 5*I]
  174. b = [94, 51, 53, 45, 31, 27, 13]
  175. c = [3 + 4*I, 5 + 7*I, 3, Rational(7, 6), 8]
  176. assert convolution_fwht(a, b) == [53*sqrt(3) + 366 + 155*I,
  177. 45*sqrt(3) + Rational(5848, 15) + 135*I,
  178. 94*sqrt(3) + Rational(1257, 5) + 65*I,
  179. 51*sqrt(3) + Rational(3974, 15),
  180. 13*sqrt(3) + 452 + 470*I,
  181. Rational(4513, 15) + 255*I,
  182. 31*sqrt(3) + Rational(1314, 5) + 265*I,
  183. 27*sqrt(3) + Rational(3676, 15) + 225*I]
  184. assert convolution_fwht(b, c) == [Rational(1993, 2) + 733*I, Rational(6215, 6) + 862*I,
  185. Rational(1659, 2) + 527*I, Rational(1988, 3) + 551*I, 1019 + 313*I, Rational(3955, 6) + 325*I,
  186. Rational(1175, 2) + 52*I, Rational(3253, 6) + 91*I]
  187. assert convolution_fwht(a[3:], c) == [Rational(-54, 5) + I*293/5, -1 + I*204/5,
  188. Rational(133, 15) + I*35/6, Rational(409, 30) + 15*I, Rational(56, 5), 32 + 40*I, 0, 0]
  189. u, v, w, x, y, z = symbols('u v w x y z')
  190. assert convolution_fwht([u, v], [x, y]) == [u*x + v*y, u*y + v*x]
  191. assert convolution_fwht([u, v, w], [x, y]) == \
  192. [u*x + v*y, u*y + v*x, w*x, w*y]
  193. assert convolution_fwht([u, v, w], [x, y, z]) == \
  194. [u*x + v*y + w*z, u*y + v*x, u*z + w*x, v*z + w*y]
  195. raises(TypeError, lambda: convolution_fwht(x, y))
  196. raises(TypeError, lambda: convolution_fwht(x*y, u + v))
  197. def test_convolution_subset():
  198. assert convolution_subset([], []) == []
  199. assert convolution_subset([], [Rational(1, 3)]) == []
  200. assert convolution_subset([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
  201. a = [1, Rational(5, 3), sqrt(3), 4 + 5*I]
  202. b = [64, 71, 55, 47, 33, 29, 15]
  203. c = [3 + I*2/3, 5 + 7*I, 7, Rational(7, 5), 9]
  204. assert convolution_subset(a, b) == [64, Rational(533, 3), 55 + 64*sqrt(3),
  205. 71*sqrt(3) + Rational(1184, 3) + 320*I, 33, 84,
  206. 15 + 33*sqrt(3), 29*sqrt(3) + 157 + 165*I]
  207. assert convolution_subset(b, c) == [192 + I*128/3, 533 + I*1486/3,
  208. 613 + I*110/3, Rational(5013, 5) + I*1249/3,
  209. 675 + 22*I, 891 + I*751/3,
  210. 771 + 10*I, Rational(3736, 5) + 105*I]
  211. assert convolution_subset(a, c) == convolution_subset(c, a)
  212. assert convolution_subset(a[:2], b) == \
  213. [64, Rational(533, 3), 55, Rational(416, 3), 33, 84, 15, 25]
  214. assert convolution_subset(a[:2], c) == \
  215. [3 + I*2/3, 10 + I*73/9, 7, Rational(196, 15), 9, 15, 0, 0]
  216. u, v, w, x, y, z = symbols('u v w x y z')
  217. assert convolution_subset([u, v, w], [x, y]) == [u*x, u*y + v*x, w*x, w*y]
  218. assert convolution_subset([u, v, w, x], [y, z]) == \
  219. [u*y, u*z + v*y, w*y, w*z + x*y]
  220. assert convolution_subset([u, v], [x, y, z]) == \
  221. convolution_subset([x, y, z], [u, v])
  222. raises(TypeError, lambda: convolution_subset(x, z))
  223. raises(TypeError, lambda: convolution_subset(Rational(7, 3), u))
  224. def test_covering_product():
  225. assert covering_product([], []) == []
  226. assert covering_product([], [Rational(1, 3)]) == []
  227. assert covering_product([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
  228. a = [1, Rational(5, 8), sqrt(7), 4 + 9*I]
  229. b = [66, 81, 95, 49, 37, 89, 17]
  230. c = [3 + I*2/3, 51 + 72*I, 7, Rational(7, 15), 91]
  231. assert covering_product(a, b) == [66, Rational(1383, 8), 95 + 161*sqrt(7),
  232. 130*sqrt(7) + 1303 + 2619*I, 37,
  233. Rational(671, 4), 17 + 54*sqrt(7),
  234. 89*sqrt(7) + Rational(4661, 8) + 1287*I]
  235. assert covering_product(b, c) == [198 + 44*I, 7740 + 10638*I,
  236. 1412 + I*190/3, Rational(42684, 5) + I*31202/3,
  237. 9484 + I*74/3, 22163 + I*27394/3,
  238. 10621 + I*34/3, Rational(90236, 15) + 1224*I]
  239. assert covering_product(a, c) == covering_product(c, a)
  240. assert covering_product(b, c[:-1]) == [198 + 44*I, 7740 + 10638*I,
  241. 1412 + I*190/3, Rational(42684, 5) + I*31202/3,
  242. 111 + I*74/3, 6693 + I*27394/3,
  243. 429 + I*34/3, Rational(23351, 15) + 1224*I]
  244. assert covering_product(a, c[:-1]) == [3 + I*2/3,
  245. Rational(339, 4) + I*1409/12, 7 + 10*sqrt(7) + 2*sqrt(7)*I/3,
  246. -403 + 772*sqrt(7)/15 + 72*sqrt(7)*I + I*12658/15]
  247. u, v, w, x, y, z = symbols('u v w x y z')
  248. assert covering_product([u, v, w], [x, y]) == \
  249. [u*x, u*y + v*x + v*y, w*x, w*y]
  250. assert covering_product([u, v, w, x], [y, z]) == \
  251. [u*y, u*z + v*y + v*z, w*y, w*z + x*y + x*z]
  252. assert covering_product([u, v], [x, y, z]) == \
  253. covering_product([x, y, z], [u, v])
  254. raises(TypeError, lambda: covering_product(x, z))
  255. raises(TypeError, lambda: covering_product(Rational(7, 3), u))
  256. def test_intersecting_product():
  257. assert intersecting_product([], []) == []
  258. assert intersecting_product([], [Rational(1, 3)]) == []
  259. assert intersecting_product([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
  260. a = [1, sqrt(5), Rational(3, 8) + 5*I, 4 + 7*I]
  261. b = [67, 51, 65, 48, 36, 79, 27]
  262. c = [3 + I*2/5, 5 + 9*I, 7, Rational(7, 19), 13]
  263. assert intersecting_product(a, b) == [195*sqrt(5) + Rational(6979, 8) + 1886*I,
  264. 178*sqrt(5) + 520 + 910*I, Rational(841, 2) + 1344*I,
  265. 192 + 336*I, 0, 0, 0, 0]
  266. assert intersecting_product(b, c) == [Rational(128553, 19) + I*9521/5,
  267. Rational(17820, 19) + 1602*I, Rational(19264, 19), Rational(336, 19), 1846, 0, 0, 0]
  268. assert intersecting_product(a, c) == intersecting_product(c, a)
  269. assert intersecting_product(b[1:], c[:-1]) == [Rational(64788, 19) + I*8622/5,
  270. Rational(12804, 19) + 1152*I, Rational(11508, 19), Rational(252, 19), 0, 0, 0, 0]
  271. assert intersecting_product(a, c[:-2]) == \
  272. [Rational(-99, 5) + 10*sqrt(5) + 2*sqrt(5)*I/5 + I*3021/40,
  273. -43 + 5*sqrt(5) + 9*sqrt(5)*I + 71*I, Rational(245, 8) + 84*I, 0]
  274. u, v, w, x, y, z = symbols('u v w x y z')
  275. assert intersecting_product([u, v, w], [x, y]) == \
  276. [u*x + u*y + v*x + w*x + w*y, v*y, 0, 0]
  277. assert intersecting_product([u, v, w, x], [y, z]) == \
  278. [u*y + u*z + v*y + w*y + w*z + x*y, v*z + x*z, 0, 0]
  279. assert intersecting_product([u, v], [x, y, z]) == \
  280. intersecting_product([x, y, z], [u, v])
  281. raises(TypeError, lambda: intersecting_product(x, z))
  282. raises(TypeError, lambda: intersecting_product(u, Rational(8, 3)))
  283. def test_convolution_int():
  284. assert convolution_int([1], [1]) == [1]
  285. assert convolution_int([1, 1], [0]) == [0]
  286. assert convolution_int([1, 2, 3], [4, 5, 6]) == [4, 13, 28, 27, 18]
  287. assert convolution_int([1], [5, 6, 7]) == [5, 6, 7]
  288. assert convolution_int([1, 3], [5, 6, 7]) == [5, 21, 25, 21]
  289. assert convolution_int([10, -5, 1, 3], [-5, 6, 7]) == [-50, 85, 35, -44, 25, 21]
  290. assert convolution_int([0, 1, 0, -1], [1, 0, -1, 0]) == [0, 1, 0, -2, 0, 1]
  291. assert convolution_int(
  292. [-341, -5, 1, 3, -71, -99, 43, 87],
  293. [5, 6, 7, 12, 345, 21, -78, -7, -89]
  294. ) == [-1705, -2071, -2412, -4106, -118035, -9774, 25998, 2981, 5509,
  295. -34317, 19228, 38870, 5485, 1724, -4436, -7743]