I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,392 @@
from sympy.core.numbers import (E, Rational, pi)
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.core import S, symbols, I
from sympy.discrete.convolutions import (
convolution, convolution_fft, convolution_ntt, convolution_fwht,
convolution_subset, covering_product, intersecting_product,
convolution_int)
from sympy.testing.pytest import raises
from sympy.abc import x, y
def test_convolution():
# fft
a = [1, Rational(5, 3), sqrt(3), Rational(7, 5)]
b = [9, 5, 5, 4, 3, 2]
c = [3, 5, 3, 7, 8]
d = [1422, 6572, 3213, 5552]
e = [-1, Rational(5, 3), Rational(7, 5)]
assert convolution(a, b) == convolution_fft(a, b)
assert convolution(a, b, dps=9) == convolution_fft(a, b, dps=9)
assert convolution(a, d, dps=7) == convolution_fft(d, a, dps=7)
assert convolution(a, d[1:], dps=3) == convolution_fft(d[1:], a, dps=3)
# prime moduli of the form (m*2**k + 1), sequence length
# should be a divisor of 2**k
p = 7*17*2**23 + 1
q = 19*2**10 + 1
# ntt
assert convolution(d, b, prime=q) == convolution_ntt(b, d, prime=q)
assert convolution(c, b, prime=p) == convolution_ntt(b, c, prime=p)
assert convolution(d, c, prime=p) == convolution_ntt(c, d, prime=p)
raises(TypeError, lambda: convolution(b, d, dps=5, prime=q))
raises(TypeError, lambda: convolution(b, d, dps=6, prime=q))
# fwht
assert convolution(a, b, dyadic=True) == convolution_fwht(a, b)
assert convolution(a, b, dyadic=False) == convolution(a, b)
raises(TypeError, lambda: convolution(b, d, dps=2, dyadic=True))
raises(TypeError, lambda: convolution(b, d, prime=p, dyadic=True))
raises(TypeError, lambda: convolution(a, b, dps=2, dyadic=True))
raises(TypeError, lambda: convolution(b, c, prime=p, dyadic=True))
# subset
assert convolution(a, b, subset=True) == convolution_subset(a, b) == \
convolution(a, b, subset=True, dyadic=False) == \
convolution(a, b, subset=True)
assert convolution(a, b, subset=False) == convolution(a, b)
raises(TypeError, lambda: convolution(a, b, subset=True, dyadic=True))
raises(TypeError, lambda: convolution(c, d, subset=True, dps=6))
raises(TypeError, lambda: convolution(a, c, subset=True, prime=q))
# integer
assert convolution([0], [0]) == convolution_int([0], [0])
assert convolution(b, c) == convolution_int(b, c)
# rational
assert convolution([Rational(1,2)], [Rational(1,2)]) == [Rational(1, 4)]
assert convolution(b, e) == [-9, 10, Rational(239, 15), Rational(34, 3),
Rational(32, 3), Rational(43, 5), Rational(113, 15),
Rational(14, 5)]
def test_cyclic_convolution():
# fft
a = [1, Rational(5, 3), sqrt(3), Rational(7, 5)]
b = [9, 5, 5, 4, 3, 2]
assert convolution([1, 2, 3], [4, 5, 6], cycle=0) == \
convolution([1, 2, 3], [4, 5, 6], cycle=5) == \
convolution([1, 2, 3], [4, 5, 6])
assert convolution([1, 2, 3], [4, 5, 6], cycle=3) == [31, 31, 28]
a = [Rational(1, 3), Rational(7, 3), Rational(5, 9), Rational(2, 7), Rational(5, 8)]
b = [Rational(3, 5), Rational(4, 7), Rational(7, 8), Rational(8, 9)]
assert convolution(a, b, cycle=0) == \
convolution(a, b, cycle=len(a) + len(b) - 1)
assert convolution(a, b, cycle=4) == [Rational(87277, 26460), Rational(30521, 11340),
Rational(11125, 4032), Rational(3653, 1080)]
assert convolution(a, b, cycle=6) == [Rational(20177, 20160), Rational(676, 315), Rational(47, 24),
Rational(3053, 1080), Rational(16397, 5292), Rational(2497, 2268)]
assert convolution(a, b, cycle=9) == \
convolution(a, b, cycle=0) + [S.Zero]
# ntt
a = [2313, 5323532, S(3232), 42142, 42242421]
b = [S(33456), 56757, 45754, 432423]
assert convolution(a, b, prime=19*2**10 + 1, cycle=0) == \
convolution(a, b, prime=19*2**10 + 1, cycle=8) == \
convolution(a, b, prime=19*2**10 + 1)
assert convolution(a, b, prime=19*2**10 + 1, cycle=5) == [96, 17146, 2664,
15534, 3517]
assert convolution(a, b, prime=19*2**10 + 1, cycle=7) == [4643, 3458, 1260,
15534, 3517, 16314, 13688]
assert convolution(a, b, prime=19*2**10 + 1, cycle=9) == \
convolution(a, b, prime=19*2**10 + 1) + [0]
# fwht
u, v, w, x, y = symbols('u v w x y')
p, q, r, s, t = symbols('p q r s t')
c = [u, v, w, x, y]
d = [p, q, r, s, t]
assert convolution(a, b, dyadic=True, cycle=3) == \
[2499522285783, 19861417974796, 4702176579021]
assert convolution(a, b, dyadic=True, cycle=5) == [2718149225143,
2114320852171, 20571217906407, 246166418903, 1413262436976]
assert convolution(c, d, dyadic=True, cycle=4) == \
[p*u + p*y + q*v + r*w + s*x + t*u + t*y,
p*v + q*u + q*y + r*x + s*w + t*v,
p*w + q*x + r*u + r*y + s*v + t*w,
p*x + q*w + r*v + s*u + s*y + t*x]
assert convolution(c, d, dyadic=True, cycle=6) == \
[p*u + q*v + r*w + r*y + s*x + t*w + t*y,
p*v + q*u + r*x + s*w + s*y + t*x,
p*w + q*x + r*u + s*v,
p*x + q*w + r*v + s*u,
p*y + t*u,
q*y + t*v]
# subset
assert convolution(a, b, subset=True, cycle=7) == [18266671799811,
178235365533, 213958794, 246166418903, 1413262436976,
2397553088697, 1932759730434]
assert convolution(a[1:], b, subset=True, cycle=4) == \
[178104086592, 302255835516, 244982785880, 3717819845434]
assert convolution(a, b[:-1], subset=True, cycle=6) == [1932837114162,
178235365533, 213958794, 245166224504, 1413262436976, 2397553088697]
assert convolution(c, d, subset=True, cycle=3) == \
[p*u + p*x + q*w + r*v + r*y + s*u + t*w,
p*v + p*y + q*u + s*y + t*u + t*x,
p*w + q*y + r*u + t*v]
assert convolution(c, d, subset=True, cycle=5) == \
[p*u + q*y + t*v,
p*v + q*u + r*y + t*w,
p*w + r*u + s*y + t*x,
p*x + q*w + r*v + s*u,
p*y + t*u]
raises(ValueError, lambda: convolution([1, 2, 3], [4, 5, 6], cycle=-1))
def test_convolution_fft():
assert all(convolution_fft([], x, dps=y) == [] for x in ([], [1]) for y in (None, 3))
assert convolution_fft([1, 2, 3], [4, 5, 6]) == [4, 13, 28, 27, 18]
assert convolution_fft([1], [5, 6, 7]) == [5, 6, 7]
assert convolution_fft([1, 3], [5, 6, 7]) == [5, 21, 25, 21]
assert convolution_fft([1 + 2*I], [2 + 3*I]) == [-4 + 7*I]
assert convolution_fft([1 + 2*I, 3 + 4*I, 5 + 3*I/5], [Rational(2, 5) + 4*I/7]) == \
[Rational(-26, 35) + I*48/35, Rational(-38, 35) + I*116/35, Rational(58, 35) + I*542/175]
assert convolution_fft([Rational(3, 4), Rational(5, 6)], [Rational(7, 8), Rational(1, 3), Rational(2, 5)]) == \
[Rational(21, 32), Rational(47, 48), Rational(26, 45), Rational(1, 3)]
assert convolution_fft([Rational(1, 9), Rational(2, 3), Rational(3, 5)], [Rational(2, 5), Rational(3, 7), Rational(4, 9)]) == \
[Rational(2, 45), Rational(11, 35), Rational(8152, 14175), Rational(523, 945), Rational(4, 15)]
assert convolution_fft([pi, E, sqrt(2)], [sqrt(3), 1/pi, 1/E]) == \
[sqrt(3)*pi, 1 + sqrt(3)*E, E/pi + pi*exp(-1) + sqrt(6),
sqrt(2)/pi + 1, sqrt(2)*exp(-1)]
assert convolution_fft([2321, 33123], [5321, 6321, 71323]) == \
[12350041, 190918524, 374911166, 2362431729]
assert convolution_fft([312313, 31278232], [32139631, 319631]) == \
[10037624576503, 1005370659728895, 9997492572392]
raises(TypeError, lambda: convolution_fft(x, y))
raises(ValueError, lambda: convolution_fft([x, y], [y, x]))
def test_convolution_ntt():
# prime moduli of the form (m*2**k + 1), sequence length
# should be a divisor of 2**k
p = 7*17*2**23 + 1
q = 19*2**10 + 1
r = 2*500000003 + 1 # only for sequences of length 1 or 2
# s = 2*3*5*7 # composite modulus
assert all(convolution_ntt([], x, prime=y) == [] for x in ([], [1]) for y in (p, q, r))
assert convolution_ntt([2], [3], r) == [6]
assert convolution_ntt([2, 3], [4], r) == [8, 12]
assert convolution_ntt([32121, 42144, 4214, 4241], [32132, 3232, 87242], p) == [33867619,
459741727, 79180879, 831885249, 381344700, 369993322]
assert convolution_ntt([121913, 3171831, 31888131, 12], [17882, 21292, 29921, 312], q) == \
[8158, 3065, 3682, 7090, 1239, 2232, 3744]
assert convolution_ntt([12, 19, 21, 98, 67], [2, 6, 7, 8, 9], p) == \
convolution_ntt([12, 19, 21, 98, 67], [2, 6, 7, 8, 9], q)
assert convolution_ntt([12, 19, 21, 98, 67], [21, 76, 17, 78, 69], p) == \
convolution_ntt([12, 19, 21, 98, 67], [21, 76, 17, 78, 69], q)
raises(ValueError, lambda: convolution_ntt([2, 3], [4, 5], r))
raises(ValueError, lambda: convolution_ntt([x, y], [y, x], q))
raises(TypeError, lambda: convolution_ntt(x, y, p))
def test_convolution_fwht():
assert convolution_fwht([], []) == []
assert convolution_fwht([], [1]) == []
assert convolution_fwht([1, 2, 3], [4, 5, 6]) == [32, 13, 18, 27]
assert convolution_fwht([Rational(5, 7), Rational(6, 8), Rational(7, 3)], [2, 4, Rational(6, 7)]) == \
[Rational(45, 7), Rational(61, 14), Rational(776, 147), Rational(419, 42)]
a = [1, Rational(5, 3), sqrt(3), Rational(7, 5), 4 + 5*I]
b = [94, 51, 53, 45, 31, 27, 13]
c = [3 + 4*I, 5 + 7*I, 3, Rational(7, 6), 8]
assert convolution_fwht(a, b) == [53*sqrt(3) + 366 + 155*I,
45*sqrt(3) + Rational(5848, 15) + 135*I,
94*sqrt(3) + Rational(1257, 5) + 65*I,
51*sqrt(3) + Rational(3974, 15),
13*sqrt(3) + 452 + 470*I,
Rational(4513, 15) + 255*I,
31*sqrt(3) + Rational(1314, 5) + 265*I,
27*sqrt(3) + Rational(3676, 15) + 225*I]
assert convolution_fwht(b, c) == [Rational(1993, 2) + 733*I, Rational(6215, 6) + 862*I,
Rational(1659, 2) + 527*I, Rational(1988, 3) + 551*I, 1019 + 313*I, Rational(3955, 6) + 325*I,
Rational(1175, 2) + 52*I, Rational(3253, 6) + 91*I]
assert convolution_fwht(a[3:], c) == [Rational(-54, 5) + I*293/5, -1 + I*204/5,
Rational(133, 15) + I*35/6, Rational(409, 30) + 15*I, Rational(56, 5), 32 + 40*I, 0, 0]
u, v, w, x, y, z = symbols('u v w x y z')
assert convolution_fwht([u, v], [x, y]) == [u*x + v*y, u*y + v*x]
assert convolution_fwht([u, v, w], [x, y]) == \
[u*x + v*y, u*y + v*x, w*x, w*y]
assert convolution_fwht([u, v, w], [x, y, z]) == \
[u*x + v*y + w*z, u*y + v*x, u*z + w*x, v*z + w*y]
raises(TypeError, lambda: convolution_fwht(x, y))
raises(TypeError, lambda: convolution_fwht(x*y, u + v))
def test_convolution_subset():
assert convolution_subset([], []) == []
assert convolution_subset([], [Rational(1, 3)]) == []
assert convolution_subset([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
a = [1, Rational(5, 3), sqrt(3), 4 + 5*I]
b = [64, 71, 55, 47, 33, 29, 15]
c = [3 + I*2/3, 5 + 7*I, 7, Rational(7, 5), 9]
assert convolution_subset(a, b) == [64, Rational(533, 3), 55 + 64*sqrt(3),
71*sqrt(3) + Rational(1184, 3) + 320*I, 33, 84,
15 + 33*sqrt(3), 29*sqrt(3) + 157 + 165*I]
assert convolution_subset(b, c) == [192 + I*128/3, 533 + I*1486/3,
613 + I*110/3, Rational(5013, 5) + I*1249/3,
675 + 22*I, 891 + I*751/3,
771 + 10*I, Rational(3736, 5) + 105*I]
assert convolution_subset(a, c) == convolution_subset(c, a)
assert convolution_subset(a[:2], b) == \
[64, Rational(533, 3), 55, Rational(416, 3), 33, 84, 15, 25]
assert convolution_subset(a[:2], c) == \
[3 + I*2/3, 10 + I*73/9, 7, Rational(196, 15), 9, 15, 0, 0]
u, v, w, x, y, z = symbols('u v w x y z')
assert convolution_subset([u, v, w], [x, y]) == [u*x, u*y + v*x, w*x, w*y]
assert convolution_subset([u, v, w, x], [y, z]) == \
[u*y, u*z + v*y, w*y, w*z + x*y]
assert convolution_subset([u, v], [x, y, z]) == \
convolution_subset([x, y, z], [u, v])
raises(TypeError, lambda: convolution_subset(x, z))
raises(TypeError, lambda: convolution_subset(Rational(7, 3), u))
def test_covering_product():
assert covering_product([], []) == []
assert covering_product([], [Rational(1, 3)]) == []
assert covering_product([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
a = [1, Rational(5, 8), sqrt(7), 4 + 9*I]
b = [66, 81, 95, 49, 37, 89, 17]
c = [3 + I*2/3, 51 + 72*I, 7, Rational(7, 15), 91]
assert covering_product(a, b) == [66, Rational(1383, 8), 95 + 161*sqrt(7),
130*sqrt(7) + 1303 + 2619*I, 37,
Rational(671, 4), 17 + 54*sqrt(7),
89*sqrt(7) + Rational(4661, 8) + 1287*I]
assert covering_product(b, c) == [198 + 44*I, 7740 + 10638*I,
1412 + I*190/3, Rational(42684, 5) + I*31202/3,
9484 + I*74/3, 22163 + I*27394/3,
10621 + I*34/3, Rational(90236, 15) + 1224*I]
assert covering_product(a, c) == covering_product(c, a)
assert covering_product(b, c[:-1]) == [198 + 44*I, 7740 + 10638*I,
1412 + I*190/3, Rational(42684, 5) + I*31202/3,
111 + I*74/3, 6693 + I*27394/3,
429 + I*34/3, Rational(23351, 15) + 1224*I]
assert covering_product(a, c[:-1]) == [3 + I*2/3,
Rational(339, 4) + I*1409/12, 7 + 10*sqrt(7) + 2*sqrt(7)*I/3,
-403 + 772*sqrt(7)/15 + 72*sqrt(7)*I + I*12658/15]
u, v, w, x, y, z = symbols('u v w x y z')
assert covering_product([u, v, w], [x, y]) == \
[u*x, u*y + v*x + v*y, w*x, w*y]
assert covering_product([u, v, w, x], [y, z]) == \
[u*y, u*z + v*y + v*z, w*y, w*z + x*y + x*z]
assert covering_product([u, v], [x, y, z]) == \
covering_product([x, y, z], [u, v])
raises(TypeError, lambda: covering_product(x, z))
raises(TypeError, lambda: covering_product(Rational(7, 3), u))
def test_intersecting_product():
assert intersecting_product([], []) == []
assert intersecting_product([], [Rational(1, 3)]) == []
assert intersecting_product([6 + I*3/7], [Rational(2, 3)]) == [4 + I*2/7]
a = [1, sqrt(5), Rational(3, 8) + 5*I, 4 + 7*I]
b = [67, 51, 65, 48, 36, 79, 27]
c = [3 + I*2/5, 5 + 9*I, 7, Rational(7, 19), 13]
assert intersecting_product(a, b) == [195*sqrt(5) + Rational(6979, 8) + 1886*I,
178*sqrt(5) + 520 + 910*I, Rational(841, 2) + 1344*I,
192 + 336*I, 0, 0, 0, 0]
assert intersecting_product(b, c) == [Rational(128553, 19) + I*9521/5,
Rational(17820, 19) + 1602*I, Rational(19264, 19), Rational(336, 19), 1846, 0, 0, 0]
assert intersecting_product(a, c) == intersecting_product(c, a)
assert intersecting_product(b[1:], c[:-1]) == [Rational(64788, 19) + I*8622/5,
Rational(12804, 19) + 1152*I, Rational(11508, 19), Rational(252, 19), 0, 0, 0, 0]
assert intersecting_product(a, c[:-2]) == \
[Rational(-99, 5) + 10*sqrt(5) + 2*sqrt(5)*I/5 + I*3021/40,
-43 + 5*sqrt(5) + 9*sqrt(5)*I + 71*I, Rational(245, 8) + 84*I, 0]
u, v, w, x, y, z = symbols('u v w x y z')
assert intersecting_product([u, v, w], [x, y]) == \
[u*x + u*y + v*x + w*x + w*y, v*y, 0, 0]
assert intersecting_product([u, v, w, x], [y, z]) == \
[u*y + u*z + v*y + w*y + w*z + x*y, v*z + x*z, 0, 0]
assert intersecting_product([u, v], [x, y, z]) == \
intersecting_product([x, y, z], [u, v])
raises(TypeError, lambda: intersecting_product(x, z))
raises(TypeError, lambda: intersecting_product(u, Rational(8, 3)))
def test_convolution_int():
assert convolution_int([1], [1]) == [1]
assert convolution_int([1, 1], [0]) == [0]
assert convolution_int([1, 2, 3], [4, 5, 6]) == [4, 13, 28, 27, 18]
assert convolution_int([1], [5, 6, 7]) == [5, 6, 7]
assert convolution_int([1, 3], [5, 6, 7]) == [5, 21, 25, 21]
assert convolution_int([10, -5, 1, 3], [-5, 6, 7]) == [-50, 85, 35, -44, 25, 21]
assert convolution_int([0, 1, 0, -1], [1, 0, -1, 0]) == [0, 1, 0, -2, 0, 1]
assert convolution_int(
[-341, -5, 1, 3, -71, -99, 43, 87],
[5, 6, 7, 12, 345, 21, -78, -7, -89]
) == [-1705, -2071, -2412, -4106, -118035, -9774, 25998, 2981, 5509,
-34317, 19228, 38870, 5485, 1724, -4436, -7743]

View File

@ -0,0 +1,59 @@
from sympy.core.numbers import Rational
from sympy.functions.combinatorial.numbers import fibonacci
from sympy.core import S, symbols
from sympy.testing.pytest import raises
from sympy.discrete.recurrences import linrec
def test_linrec():
assert linrec(coeffs=[1, 1], init=[1, 1], n=20) == 10946
assert linrec(coeffs=[1, 2, 3, 4, 5], init=[1, 1, 0, 2], n=10) == 1040
assert linrec(coeffs=[0, 0, 11, 13], init=[23, 27], n=25) == 59628567384
assert linrec(coeffs=[0, 0, 1, 1, 2], init=[1, 5, 3], n=15) == 165
assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=70) == \
56889923441670659718376223533331214868804815612050381493741233489928913241
assert linrec(coeffs=[0]*55 + [1, 1, 2, 3], init=[0]*50 + [1, 2, 3], n=4000) == \
702633573874937994980598979769135096432444135301118916539
assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**4)
assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**5)
assert all(linrec(coeffs=[1, 1], init=[0, 1], n=n) == fibonacci(n)
for n in range(95, 115))
assert all(linrec(coeffs=[1, 1], init=[1, 1], n=n) == fibonacci(n + 1)
for n in range(595, 615))
a = [S.Half, Rational(3, 4), Rational(5, 6), 7, Rational(8, 9), Rational(3, 5)]
b = [1, 2, 8, Rational(5, 7), Rational(3, 7), Rational(2, 9), 6]
x, y, z = symbols('x y z')
assert linrec(coeffs=a[:5], init=b[:4], n=80) == \
Rational(1726244235456268979436592226626304376013002142588105090705187189,
1960143456748895967474334873705475211264)
assert linrec(coeffs=a[:4], init=b[:4], n=50) == \
Rational(368949940033050147080268092104304441, 504857282956046106624)
assert linrec(coeffs=a[3:], init=b[:3], n=35) == \
Rational(97409272177295731943657945116791049305244422833125109,
814315512679031689453125)
assert linrec(coeffs=[0]*60 + [Rational(2, 3), Rational(4, 5)], init=b, n=3000) == \
Rational(26777668739896791448594650497024, 48084516708184142230517578125)
raises(TypeError, lambda: linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4, 5], n=1))
raises(TypeError, lambda: linrec(coeffs=a[:4], init=b[:5], n=10000))
raises(ValueError, lambda: linrec(coeffs=a[:4], init=b[:4], n=-10000))
raises(TypeError, lambda: linrec(x, b, n=10000))
raises(TypeError, lambda: linrec(a, y, n=10000))
assert linrec(coeffs=[x, y, z], init=[1, 1, 1], n=4) == \
x**2 + x*y + x*z + y + z
assert linrec(coeffs=[1, 2, 1], init=[x, y, z], n=20) == \
269542*x + 664575*y + 578949*z
assert linrec(coeffs=[0, 3, 1, 2], init=[x, y], n=30) == \
58516436*x + 56372788*y
assert linrec(coeffs=[0]*50 + [1, 2, 3], init=[x, y, z], n=1000) == \
11477135884896*x + 25999077948732*y + 41975630244216*z
assert linrec(coeffs=[], init=[1, 1], n=20) == 0
assert linrec(coeffs=[x, y, z], init=[1, 2, 3], n=2) == 3

View File

@ -0,0 +1,154 @@
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.core import S, Symbol, symbols, I, Rational
from sympy.discrete import (fft, ifft, ntt, intt, fwht, ifwht,
mobius_transform, inverse_mobius_transform)
from sympy.testing.pytest import raises
def test_fft_ifft():
assert all(tf(ls) == ls for tf in (fft, ifft)
for ls in ([], [Rational(5, 3)]))
ls = list(range(6))
fls = [15, -7*sqrt(2)/2 - 4 - sqrt(2)*I/2 + 2*I, 2 + 3*I,
-4 + 7*sqrt(2)/2 - 2*I - sqrt(2)*I/2, -3,
-4 + 7*sqrt(2)/2 + sqrt(2)*I/2 + 2*I,
2 - 3*I, -7*sqrt(2)/2 - 4 - 2*I + sqrt(2)*I/2]
assert fft(ls) == fls
assert ifft(fls) == ls + [S.Zero]*2
ls = [1 + 2*I, 3 + 4*I, 5 + 6*I]
ifls = [Rational(9, 4) + 3*I, I*Rational(-7, 4), Rational(3, 4) + I, -2 - I/4]
assert ifft(ls) == ifls
assert fft(ifls) == ls + [S.Zero]
x = Symbol('x', real=True)
raises(TypeError, lambda: fft(x))
raises(ValueError, lambda: ifft([x, 2*x, 3*x**2, 4*x**3]))
def test_ntt_intt():
# prime moduli of the form (m*2**k + 1), sequence length
# should be a divisor of 2**k
p = 7*17*2**23 + 1
q = 2*500000003 + 1 # only for sequences of length 1 or 2
r = 2*3*5*7 # composite modulus
assert all(tf(ls, p) == ls for tf in (ntt, intt)
for ls in ([], [5]))
ls = list(range(6))
nls = [15, 801133602, 738493201, 334102277, 998244350, 849020224,
259751156, 12232587]
assert ntt(ls, p) == nls
assert intt(nls, p) == ls + [0]*2
ls = [1 + 2*I, 3 + 4*I, 5 + 6*I]
x = Symbol('x', integer=True)
raises(TypeError, lambda: ntt(x, p))
raises(ValueError, lambda: intt([x, 2*x, 3*x**2, 4*x**3], p))
raises(ValueError, lambda: intt(ls, p))
raises(ValueError, lambda: ntt([1.2, 2.1, 3.5], p))
raises(ValueError, lambda: ntt([3, 5, 6], q))
raises(ValueError, lambda: ntt([4, 5, 7], r))
raises(ValueError, lambda: ntt([1.0, 2.0, 3.0], p))
def test_fwht_ifwht():
assert all(tf(ls) == ls for tf in (fwht, ifwht) \
for ls in ([], [Rational(7, 4)]))
ls = [213, 321, 43235, 5325, 312, 53]
fls = [49459, 38061, -47661, -37759, 48729, 37543, -48391, -38277]
assert fwht(ls) == fls
assert ifwht(fls) == ls + [S.Zero]*2
ls = [S.Half + 2*I, Rational(3, 7) + 4*I, Rational(5, 6) + 6*I, Rational(7, 3), Rational(9, 4)]
ifls = [Rational(533, 672) + I*3/2, Rational(23, 224) + I/2, Rational(1, 672), Rational(107, 224) - I,
Rational(155, 672) + I*3/2, Rational(-103, 224) + I/2, Rational(-377, 672), Rational(-19, 224) - I]
assert ifwht(ls) == ifls
assert fwht(ifls) == ls + [S.Zero]*3
x, y = symbols('x y')
raises(TypeError, lambda: fwht(x))
ls = [x, 2*x, 3*x**2, 4*x**3]
ifls = [x**3 + 3*x**2/4 + x*Rational(3, 4),
-x**3 + 3*x**2/4 - x/4,
-x**3 - 3*x**2/4 + x*Rational(3, 4),
x**3 - 3*x**2/4 - x/4]
assert ifwht(ls) == ifls
assert fwht(ifls) == ls
ls = [x, y, x**2, y**2, x*y]
fls = [x**2 + x*y + x + y**2 + y,
x**2 + x*y + x - y**2 - y,
-x**2 + x*y + x - y**2 + y,
-x**2 + x*y + x + y**2 - y,
x**2 - x*y + x + y**2 + y,
x**2 - x*y + x - y**2 - y,
-x**2 - x*y + x - y**2 + y,
-x**2 - x*y + x + y**2 - y]
assert fwht(ls) == fls
assert ifwht(fls) == ls + [S.Zero]*3
ls = list(range(6))
assert fwht(ls) == [x*8 for x in ifwht(ls)]
def test_mobius_transform():
assert all(tf(ls, subset=subset) == ls
for ls in ([], [Rational(7, 4)]) for subset in (True, False)
for tf in (mobius_transform, inverse_mobius_transform))
w, x, y, z = symbols('w x y z')
assert mobius_transform([x, y]) == [x, x + y]
assert inverse_mobius_transform([x, x + y]) == [x, y]
assert mobius_transform([x, y], subset=False) == [x + y, y]
assert inverse_mobius_transform([x + y, y], subset=False) == [x, y]
assert mobius_transform([w, x, y, z]) == [w, w + x, w + y, w + x + y + z]
assert inverse_mobius_transform([w, w + x, w + y, w + x + y + z]) == \
[w, x, y, z]
assert mobius_transform([w, x, y, z], subset=False) == \
[w + x + y + z, x + z, y + z, z]
assert inverse_mobius_transform([w + x + y + z, x + z, y + z, z], subset=False) == \
[w, x, y, z]
ls = [Rational(2, 3), Rational(6, 7), Rational(5, 8), 9, Rational(5, 3) + 7*I]
mls = [Rational(2, 3), Rational(32, 21), Rational(31, 24), Rational(1873, 168),
Rational(7, 3) + 7*I, Rational(67, 21) + 7*I, Rational(71, 24) + 7*I,
Rational(2153, 168) + 7*I]
assert mobius_transform(ls) == mls
assert inverse_mobius_transform(mls) == ls + [S.Zero]*3
mls = [Rational(2153, 168) + 7*I, Rational(69, 7), Rational(77, 8), 9, Rational(5, 3) + 7*I, 0, 0, 0]
assert mobius_transform(ls, subset=False) == mls
assert inverse_mobius_transform(mls, subset=False) == ls + [S.Zero]*3
ls = ls[:-1]
mls = [Rational(2, 3), Rational(32, 21), Rational(31, 24), Rational(1873, 168)]
assert mobius_transform(ls) == mls
assert inverse_mobius_transform(mls) == ls
mls = [Rational(1873, 168), Rational(69, 7), Rational(77, 8), 9]
assert mobius_transform(ls, subset=False) == mls
assert inverse_mobius_transform(mls, subset=False) == ls
raises(TypeError, lambda: mobius_transform(x, subset=True))
raises(TypeError, lambda: inverse_mobius_transform(y, subset=False))