def test_hyper(): assert rsolve((n**2 - 2) * f(n) - (2 * n + 1) * f(n + 1) + f(n + 2)) == [{ f: Lambda(n, C0 * rf(-sqrt(2), n) + C1 * rf(+sqrt(2), n)) }] assert rsolve((n**2 - k) * f(n) - (2 * n + 1) * f(n + 1) + f(n + 2)) == [{ f: Lambda(n, C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n)) }] assert rsolve(2 * n * (n + 1) * f(n) - (n**2 + 3 * n - 2) * f(n + 1) + (n - 1) * f(n + 2)) == [{ f: Lambda(n, C1 * factorial(n) + C0 * 2**n) }] assert rsolve_hyper( [n + 2, -(2 * n + 3) * (17 * n**2 + 51 * n + 39), n + 1], 0, n) == (0, []) assert rsolve_hyper([-n - 1, -1, 1], 0, n) == (0, []) assert rsolve(-n - f(n) + f(n + 1)) == [{ f: Lambda(n, C0 + n * (n - 1) / 2) }] assert rsolve(-1 - n - f(n) + f(n + 1)) == [{ f: Lambda(n, C0 + n * (n + 1) / 2) }] assert rsolve(-3 * (n + n**2) - f(n) + f(n + 1)) == [{ f: Lambda(n, C0 + n**3 - n) }] assert rsolve(-n - factorial(n) - f(n) + f(n + 1)) is None assert rsolve(-a * f(n) + f(n + 1)) == [{f: Lambda(n, C0 * a**n)}] assert rsolve(-a * f(n) + f(n + 2)) == [{ f: Lambda(n, a**(n / 2) * ((-1)**n * C1 + C0)) }] assert (rsolve(f(n) + f(n + 1) + f(n + 2)) == [{ f: Lambda( n, 2**-n * (C0 * (-1 - sqrt(3) * I)**n + C1 * (-1 + sqrt(3) * I)**n)) }]) assert rsolve_hyper([1, -2 * n / a - 2 / a, 1], 0, n) == (0, []) assert rsolve_hyper([1, 1], sqrt(n), n) is None assert rsolve_hyper([1, 1], n + sqrt(n), n) is None
def test_rsolve_bulk(): """Some bulk-generated tests.""" funcs = [n, n + 1, n**2, n**3, n**4, n + n**2, 27*n + 52*n**2 - 3*n**3 + 12*n**4 - 52*n**5] coeffs = [[-2, 1], [-2, -1, 1], [-1, 1, 1, -1, 1], [-n, 1], [n**2 - n + 12, 1] ] for p in funcs: # compute difference for c in coeffs: q = recurrence_term(c, p) if p.is_polynomial(n): assert rsolve_poly(c, q, n) == p if p.is_hypergeometric(n) and len(c) <= 3: assert rsolve_hyper(c, q, n).subs(zip(symbols('C:3'), [0, 0, 0])).expand() == p
def test_rsolve_bulk(): """Some bulk-generated tests.""" funcs = [ n, n + 1, n**2, n**3, n**4, n + n**2, 27 * n + 52 * n**2 - 3 * n**3 + 12 * n**4 - 52 * n**5 ] coeffs = [[-2, 1], [-2, -1, 1], [-1, 1, 1, -1, 1], [-n, 1], [n**2 - n + 12, 1]] for p in funcs: # compute difference for c in coeffs: q = recurrence_term(c, p) if p.is_polynomial(n): assert rsolve_poly(c, q, n) == p if p.is_hypergeometric(n) and len(c) <= 3: assert rsolve_hyper(c, q, n).subs(zip(symbols('C:3'), [0, 0, 0])).expand() == p
def test_bulk(): funcs = [ n, n + 1, n**2, n**3, n**4, n + n**2, 27 * n + 52 * n**2 - 3 * n**3 + 12 * n**4 - 52 * n**5 ] coeffs = [[-2, 1], [-2, -1, 1], [-1, 1, 1, -1, 1], [-n, 1], [n**2 - n + 12, 1]] for p in funcs: for c in coeffs: q = sum(c[i] * p.subs({n: n + i}) for i in range(len(c))) if p.is_polynomial(n): assert rsolve_poly(c, q, n)[0] == p if p.is_hypergeometric(n) and len(c) <= 3: assert rsolve_hyper(c, q, n)[0].subs({ C0: 0, C1: 0, C2: 0 }).expand() == p
def test_rsolve_hyper(): assert rsolve_hyper([-1, -1, 1], 0, n) in [ C0 * (Rational(1, 2) - sqrt(5) / 2)**n + C1 * (Rational(1, 2) + sqrt(5) / 2)**n, C1 * (Rational(1, 2) - sqrt(5) / 2)**n + C0 * (Rational(1, 2) + sqrt(5) / 2)**n, ] assert rsolve_hyper([n**2 - 2, -2 * n - 1, 1], 0, n) in [ C0 * rf(sqrt(2), n) + C1 * rf(-sqrt(2), n), C1 * rf(sqrt(2), n) + C0 * rf(-sqrt(2), n), ] assert rsolve_hyper([n**2 - k, -2 * n - 1, 1], 0, n) in [ C0 * rf(sqrt(k), n) + C1 * rf(-sqrt(k), n), C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n), ] assert rsolve_hyper([2 * n * (n + 1), -n**2 - 3 * n + 2, n - 1], 0, n) == C1 * factorial(n) + C0 * 2**n assert rsolve_hyper( [n + 2, -(2 * n + 3) * (17 * n**2 + 51 * n + 39), n + 1], 0, n) == 0 assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0 assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2 / 2 - n / 2 assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2 / 2 + n / 2 assert rsolve_hyper([-1, 1], 3 * (n + n**2), n).expand() == C0 + n**3 - n assert rsolve_hyper([-1, 1], n + factorial(n), n) is None assert rsolve_hyper([-a, 1], 0, n).expand() == C0 * a**n assert rsolve_hyper( [-a, 0, 1], 0, n).expand() == (-1)**n * C1 * a**(n / 2) + C0 * a**(n / 2) assert rsolve_hyper([1, 1, 1], 0, n).expand() == \ C0*(-Rational(1, 2) - sqrt(3)*I/2)**n + C1*(-Rational(1, 2) + sqrt(3)*I/2)**n assert rsolve_hyper([1, -2 * n / a - 2 / a, 1], 0, n) == 0 assert rsolve_hyper([1, 1], sqrt(n), n) is None assert rsolve_hyper([1, 1], n + sqrt(n), n) is None
def test_rsolve_hyper(): assert rsolve_hyper([-1, -1, 1], 0, n) in [ C0*(Rational(1, 2) - sqrt(5)/2)**n + C1*(Rational(1, 2) + sqrt(5)/2)**n, C1*(Rational(1, 2) - sqrt(5)/2)**n + C0*(Rational(1, 2) + sqrt(5)/2)**n, ] assert rsolve_hyper([n**2 - 2, -2*n - 1, 1], 0, n) in [ C0*rf(sqrt(2), n) + C1*rf(-sqrt(2), n), C1*rf(sqrt(2), n) + C0*rf(-sqrt(2), n), ] assert rsolve_hyper([n**2 - k, -2*n - 1, 1], 0, n) in [ C0*rf(sqrt(k), n) + C1*rf(-sqrt(k), n), C1*rf(sqrt(k), n) + C0*rf(-sqrt(k), n), ] assert rsolve_hyper( [2*n*(n + 1), -n**2 - 3*n + 2, n - 1], 0, n) == C1*factorial(n) + C0*2**n assert rsolve_hyper( [n + 2, -(2*n + 3)*(17*n**2 + 51*n + 39), n + 1], 0, n) == 0 assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0 assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2/2 - n/2 assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2/2 + n/2 assert rsolve_hyper([-1, 1], 3*(n + n**2), n).expand() == C0 + n**3 - n assert rsolve_hyper([-1, 1], n + factorial(n), n) is None assert rsolve_hyper([-a, 1], 0, n).expand() == C0*a**n assert rsolve_hyper([-a, 0, 1], 0, n).expand() == (-1)**n*C1*a**(n/2) + C0*a**(n/2) assert rsolve_hyper([1, 1, 1], 0, n).expand() == \ C0*(-Rational(1, 2) - sqrt(3)*I/2)**n + C1*(-Rational(1, 2) + sqrt(3)*I/2)**n assert rsolve_hyper([1, -2*n/a - 2/a, 1], 0, n) == 0 assert rsolve_hyper([1, 1], sqrt(n), n) is None assert rsolve_hyper([1, 1], n + sqrt(n), n) is None