def test_sequence(): form = SeqFormula(n**2, (n, 0, 5)) per = SeqPer((1, 2, 3), (n, 0, 5)) inter = SeqFormula(n**2) assert sequence(n**2, (n, 0, 5)) == form assert sequence((1, 2, 3), (n, 0, 5)) == per assert sequence(n**2) == inter
def test_mul__coeff_mul(): assert SeqPer((1, 2), (n, 0, oo)).coeff_mul(2) == SeqPer((2, 4), (n, 0, oo)) assert SeqFormula(n**2).coeff_mul(2) == SeqFormula(2*n**2) assert S.EmptySequence.coeff_mul(100) == S.EmptySequence assert SeqPer((1, 2), (n, 0, oo)) * (SeqPer((2, 3))) == \ SeqPer((2, 6), (n, 0, oo)) assert SeqFormula(n**2) * SeqFormula(n**3) == SeqFormula(n**5) assert S.EmptySequence * SeqFormula(n**2) == S.EmptySequence assert SeqFormula(n**2) * S.EmptySequence == S.EmptySequence raises(TypeError, lambda: sequence(n**2) * n) raises(TypeError, lambda: n * sequence(n**2))
#!/usr/bin/python from sympy import summation, sequence, pprint from sympy.abc import x s = sequence(x, (x, 1, 10)) print(s) pprint(s) print(list(s)) print(s.length) print(summation(s.formula, (x, s.start, s.stop))) # print(sum(list(s)))
def test_find_linear_recurrence(): assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \ (n, 0, 10)).find_linear_recurrence(11) == [1, 1] assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \ 1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11] assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \ == [4, -6, 4, -1] assert sequence(x**n, (n, 0, 20)).find_linear_recurrence(21) == [x] assert sequence((1, 2, 3)).find_linear_recurrence(10, 5) == [0, 0, 1] assert sequence(((1 + sqrt(5))/2)**n + \ (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1] assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \ (n,0,oo)).find_linear_recurrence(10) == [1, 1] assert sequence((1, 2, 3, 4, 6), (n, 0, 4)).find_linear_recurrence(5) == [] assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([], None) assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([Rational(19, 2), -20, Rational(27, 2)], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2)) assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \ == ([1, 1], -x/(x**2 + x - 1)) assert sequence(tribonacci(n)).find_linear_recurrence(30,gfvar=x) \ == ([1, 1, 1], -x/(x**3 + x**2 + x - 1))
def test_find_linear_recurrence(): assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \ (n, 0, 10)).find_linear_recurrence(11) == [1, 1] assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \ 1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11] assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \ == [4, -6, 4, -1] assert sequence(x**n, (n,0,20)).find_linear_recurrence(21) == [x] assert sequence((1,2,3)).find_linear_recurrence(10, 5) == [0, 0, 1] assert sequence(((1 + sqrt(5))/2)**n + \ (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1] assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \ (n,0,oo)).find_linear_recurrence(10) == [1, 1] assert sequence((1,2,3,4,6),(n, 0, 4)).find_linear_recurrence(5) == [] assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([], None) assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \ == ([19/2, -20, 27/2], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2)) assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \ == ([1, 1], -x/(x**2 + x - 1))
k, i = sp.symbols('k, i', integer=True) xy = x * y yx = y * x # Not including x(yx)^k=0 because it's a Mul, not a Pow. # I'm not handling those yet. my_relations = { x**2: y * xy**(k - 1) + c * xy**k, y**2: d * xy**k, yx**k: xy**k } basis = [ S.One, sp.sequence(y * xy**i, (i, 0, k - 1)), sp.sequence(x * yx**i, (i, 0, k - 1)), sp.sequence(xy**i, (i, 1, k - 1)), sp.sequence(yx**i, (i, 1, k - 1)), xy**k ] zeros_mul = { '(x*y)**(k - 1)*y', 'x*(x*y)**(k - 1)', 'y*(y*x)**(k - 1)', 'x**2*y', 'x*(x*y)**k', '(x*y)**k*y', 'y*(x*y)**k', '(y*x)**(k - 1)*x', # TODO: get rid at least of these
# from tool import * import sympy from sympy import * # from sympy import sequence,SeqFormula,SeqPer from sympy import sequence, SeqPer, SeqFormula from sympy.abc import n a = sequence(n**2, (n, 0, 5)) print(a) # SeqFormula(n**2, (n, 0, 5)) b = sequence((1, 2, 3, 4), (n, 0, 4)) # SeqPer((1, 2, 3), (n, 0, 5)) print(b) print(a.coeff(3)) # 在自变量取特定值时的值 print(a[:]) print(b[:]) print(b.periodical) # 周期序列 print(b.period) # 周期序列长度 k = sympy.symbols('k') c = SeqPer((k, k**2, k**3), (k, 0, oo)) print(c.periodical) print(c[0:10]) print(c.coeff(4)) d = SeqFormula(n**3, (n, -oo, 0)) # 不能写0,-oo print(d[0:6]) print(SeqAdd(SeqPer((1, 2), (n, 0, oo)), S.EmptySequence))