def test_polynomial_sums(): assert summation(n**2, (n, 3, 8)) == 199 assert summation(n, (n, a, b)).equals((a + b) * (b - a + 1) / 2) assert summation(n**2, (n, 1, b)).equals((2 * b**3 + 3 * b**2 + b) / 6) assert summation(n**3, (n, 1, b)).equals((b**4 + 2 * b**3 + b**2) / 4) assert summation(n**6, (n, 1, b)).equals( (6 * b**7 + 21 * b**6 + 21 * b**5 - 7 * b**3 + b) / 42)
def test_matplotlib_advanced(): """Examples from the 'advanced' notebook.""" try: name = 'test' tmp_file = TmpFileManager.tmp_file s = summation(1/x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p = plot(summation(1/x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) ### # Test expressions that can not be translated to np and # generate complex results. ### plot(sin(x) + I*cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) # Characteristic function of a StudentT distribution with nu=10 plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I*pi)/2) + meijerg(((1/2,), ()), ((5, 0, 1/2), ()), 5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) finally: TmpFileManager.cleanup()
def test_other_sums(): f = m**2 + m * exp(m) g = 3 * exp(Rational(3, 2)) / 2 + exp(Rational(1, 2)) / 2 - exp( -Rational(1, 2)) / 2 - 3 * exp(-Rational(3, 2)) / 2 + 5 assert summation(f, (m, -Rational(3, 2), Rational(3, 2))).expand() == g assert summation(f, (m, -1.5, 1.5)).evalf().epsilon_eq(g.evalf(), 1e-10)
def test_sympyissue_21557(): f1 = 2**(-k) * exp(I * k) assert summation(f1, (k, 1, oo)).equals(E**I / (2 - E**I)) f2 = 2**(k * (I - 1)) assert summation(f2, (k, 1, oo)).equals(2**I / (2 - 2**I)) assert summation(f2, (k, 1, x)).equals(2**(-x + I) * (-2**x + 2**(I * x)) / (-2 + 2**I))
def test_matplotlib_advanced(): """Examples from the 'advanced' notebook.""" try: name = 'test' tmp_file = TmpFileManager.tmp_file s = summation(1 / x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p = plot(summation(1 / x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) ### # Test expressions that can not be translated to np and # generate complex results. ### plot(sin(x) + I * cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) # Characteristic function of a StudentT distribution with nu=10 plot((meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) finally: TmpFileManager.cleanup()
def test_polynomial_sums(): assert summation(n**2, (n, 3, 8)) == 199 assert summation(n, (n, a, b)) == \ ((a + b)*(b - a + 1)/2).expand() assert summation(n**2, (n, 1, b)) == \ ((2*b**3 + 3*b**2 + b)/6).expand() assert summation(n**3, (n, 1, b)) == \ ((b**4 + 2*b**3 + b**2)/4).expand() assert summation(n**6, (n, 1, b)) == \ ((6*b**7 + 21*b**6 + 21*b**5 - 7*b**3 + b)/42).expand()
def test_other_sums(): f = m**2 + m * exp(m) g = E + 14 + 2 * E**2 + 3 * E**3 assert summation(f, (m, 0, 3)) == g assert summation(f, (m, 0, 3)).evalf().epsilon_eq(g.evalf(), 1e-10) assert summation(n**x, (n, 1, oo)) == Sum(n**x, (n, 1, oo)) f = Function('f') assert summation(f(n) * f(k), (k, m, n)) == Sum(f(n) * f(k), (k, m, n))
def test_other_sums(): f = m**2 + m*exp(m) g = 3*exp(Rational(3, 2))/2 + exp(Rational(1, 2))/2 - exp(-Rational(1, 2))/2 - 3*exp(-Rational(3, 2))/2 + 5 assert summation(f, (m, -Rational(3, 2), Rational(3, 2))).expand() == g assert summation(f, (m, -Rational(3, 2), Rational(3, 2))).evalf().epsilon_eq(g.evalf(), 1e-10) assert summation(n**x, (n, 1, oo)) == Sum(n**x, (n, 1, oo)) f = Function('f') assert summation(f(n)*f(k), (k, m, n)) == Sum(f(n)*f(k), (k, m, n))
def test_arithmetic_sums(): assert summation(1, (n, a, b)) == b - a + 1 assert Sum(nan, (n, a, b)) is nan assert Sum(x, (n, a, a)).doit() == x assert Sum(x, (x, a, a)).doit() == a assert Sum(x, (n, 1, a)).doit() == a * x lo, hi = 1, 2 s1 = Sum(n, (n, lo, hi)) s2 = Sum(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == 3 and s2.doit() == 0 lo, hi = x, x + 1 s1 = Sum(n, (n, lo, hi)) s2 = Sum(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == 2 * x + 1 and s2.doit() == 0 assert Sum(Integral(x, (x, 1, y)) + x, (x, 1, 2)).doit() == \ y**2 + 2 assert summation(1, (n, 1, 10)) == 10 assert summation(2 * n, (n, 0, 10**10)) == 100000000010000000000 assert summation(4*n*m, (n, a, 1), (m, 1, d)).expand() == \ 2*d + 2*d**2 + a*d + a*d**2 - d*a**2 - a**2*d**2 assert summation(cos(n), (n, -2, 1)) == cos(-2) + cos(-1) + cos(0) + cos(1) assert summation(cos(n), (n, x, x + 2)) == cos(x) + cos(x + 1) + cos(x + 2) assert isinstance(summation(cos(n), (n, x, x + Rational(1, 2))), Sum) assert summation(k, (k, 0, oo)) == oo
def test_arithmetic_sums(): assert summation(1, (n, a, b)) == b - a + 1 assert Sum(nan, (n, a, b)) is nan assert Sum(x, (n, a, a)).doit() == x assert Sum(x, (x, a, a)).doit() == a assert Sum(x, (n, 1, a)).doit() == a*x lo, hi = 1, 2 s1 = Sum(n, (n, lo, hi)) s2 = Sum(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == 3 and s2.doit() == 0 lo, hi = x, x + 1 s1 = Sum(n, (n, lo, hi)) s2 = Sum(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == 2*x + 1 and s2.doit() == 0 assert Sum(Integral(x, (x, 1, y)) + x, (x, 1, 2)).doit() == \ y**2 + 2 assert summation(1, (n, 1, 10)) == 10 assert summation(2*n, (n, 0, 10**10)) == 100000000010000000000 assert summation(4*n*m, (n, a, 1), (m, 1, d)).expand() == \ 2*d + 2*d**2 + a*d + a*d**2 - d*a**2 - a**2*d**2 assert summation(cos(n), (n, -2, 1)) == cos(-2) + cos(-1) + cos(0) + cos(1) assert summation(cos(n), (n, x, x + 2)) == cos(x) + cos(x + 1) + cos(x + 2) assert isinstance(summation(cos(n), (n, x, x + Rational(1, 2))), Sum) assert summation(k, (k, 0, oo)) == oo
def test_sympyissue_8016(): n, m = symbols('n, m', integer=True, positive=True) k = symbols('k', integer=True) assert summation(binomial(m, k) * binomial(m, n - k) * (-1)**k, (k, 0, n)).equals( cos(pi * n / 2) * gamma(m + 1) / gamma(n / 2 + 1) / gamma(m - n / 2 + 1))
def test_Sum_doit(): f = Function('f') assert Sum(n*Integral(a**2), (n, 0, 2)).doit() == a**3 assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \ 3*Integral(a**2) assert summation(n*Integral(a**2), (n, 0, 2)) == 3*Integral(a**2) # test nested sum evaluation s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n)) assert 0 == (s.doit() - n*(n + 1)*(n - 1)).factor() assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3 assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \ 3*Piecewise((1, And(Integer(1) <= k, k <= 3)), (0, True)) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \ f(1) + f(2) + f(3) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \ Sum(Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo)) l = Symbol('l', integer=True, positive=True) assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \ Sum(f(l), (l, 1, oo)) # issue sympy/sympy#2597 nmax = symbols('N', integer=True, positive=True) pw = Piecewise((1, And(Integer(1) <= n, n <= nmax)), (0, True)) assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
def compute_solution_for_recurrence(recurr_coeff: Expr, inhom_part_solution: Expr, initial_value: Expr): """ Computes the (unique) solution to the recurrence relation: f(0) = initial_value; f(n+1) = recurr_coeff * f(n) + inhom_part_solution """ log( f"Start compute solution for recurrence, { recurr_coeff }, { inhom_part_solution }, { initial_value }", LOG_VERBOSE) n = symbols('n', integer=True, positive=True) if recurr_coeff.is_zero: return expand(inhom_part_solution.xreplace({n: n - 1})) hom_solution = (recurr_coeff**n) * initial_value k = symbols('_k', integer=True, positive=True) summand = simplify( (recurr_coeff**k) * inhom_part_solution.xreplace({n: (n - 1) - k})) particular_solution = summation(summand, (k, 0, (n - 1))) particular_solution = without_piecewise(particular_solution) solution = simplify(hom_solution + particular_solution) log( f"End compute solution for recurrence, { recurr_coeff }, { inhom_part_solution }, { initial_value }", LOG_VERBOSE) return solution
def test_Sum_doit(): f = Function('f') assert Sum(n * Integral(a**2), (n, 0, 2)).doit() == a**3 assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \ 3*Integral(a**2) assert summation(n * Integral(a**2), (n, 0, 2)) == 3 * Integral(a**2) # test nested sum evaluation s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n)) assert 0 == (s.doit() - n * (n + 1) * (n - 1)).factor() assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3 assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \ 3*Piecewise((1, And(Integer(1) <= k, k <= 3)), (0, True)) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \ f(1) + f(2) + f(3) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \ Sum(Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo)) l = Symbol('l', integer=True, positive=True) assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \ Sum(f(l), (l, 1, oo)) # issue sympy/sympy#2597 nmax = symbols('N', integer=True, positive=True) pw = Piecewise((1, And(Integer(1) <= n, n <= nmax)), (0, True)) assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
def test_telescopic_sums(): # checks also input 2 of comment 1 issue sympy/sympy#4127 assert Sum(1/k - 1/(k + 1), (k, 1, n)).doit() == 1 - 1/(1 + n) f = Function('f') assert Sum( f(k) - f(k + 2), (k, m, n)).doit() == -f(1 + n) - f(2 + n) + f(m) + f(1 + m) assert Sum(cos(k) - cos(k + 3), (k, 1, n)).doit() == -cos(1 + n) - \ cos(2 + n) - cos(3 + n) + cos(1) + cos(2) + cos(3) # dummy variable shouldn't matter assert telescopic(1/m, -m/(1 + m), (m, n - 1, n)) == \ telescopic(1/k, -k/(1 + k), (k, n - 1, n)) assert Sum(1/x/(x - 1), (x, a, b)).doit().simplify() == ((b - a + 1)/(b*(a - 1))) assert summation(f(k) - f(k - 1), (k, m, n)) == f(n) - f(m - 1) assert isinstance(summation(f(k) - f(k - x), (k, m, n)), Sum)
def test_the_sum(l, n, m): # Summand s = i**3 # First sum a = l b = n - 1 S1 = summation(s, (i, a, b)) # Second sum a = l b = m - 1 S2 = summation(s, (i, a, b)) # Third sum a = m b = n - 1 S3 = summation(s, (i, a, b)) # Test if S1 = S2 + S3 as required assert S1 - (S2 + S3) == 0
def test_telescopic_sums(): # checks also input 2 of comment 1 issue sympy/sympy#4127 assert Sum(1/k - 1/(k + 1), (k, 1, n)).doit() == 1 - 1/(1 + n) f = Function("f") assert Sum( f(k) - f(k + 2), (k, m, n)).doit() == -f(1 + n) - f(2 + n) + f(m) + f(1 + m) assert Sum(cos(k) - cos(k + 3), (k, 1, n)).doit() == -cos(1 + n) - \ cos(2 + n) - cos(3 + n) + cos(1) + cos(2) + cos(3) # dummy variable shouldn't matter assert telescopic(1/m, -m/(1 + m), (m, n - 1, n)) == \ telescopic(1/k, -k/(1 + k), (k, n - 1, n)) assert Sum(1/x/(x - 1), (x, a, b)).doit().simplify() == ((b - a + 1)/(b*(a - 1))) assert summation(f(k) - f(k - 1), (k, m, n)) == f(n) - f(m - 1) assert isinstance(summation(f(k) - f(k - x), (k, m, n)), Sum)
def test_composite_sums(): f = Rational(1, 2) * (7 - 6 * n + Rational(1, 7) * n**3) s = summation(f, (n, a, b)) assert not isinstance(s, Sum) A = 0 for i in range(-3, 5): A += f.subs({n: i}) B = s.subs({a: -3, b: 4}) assert A == B
def expectation(self, expr, var, evaluate=True, **kwargs): """ Expectation of expression over distribution """ # TODO: support discrete sets with non integer stepsizes if evaluate: return summation(expr * self.pdf(var), (var, self.set.inf, self.set.sup), **kwargs) else: return Sum(expr * self.pdf(var), (var, self.set.inf, self.set.sup), **kwargs)
def test_sympyissue_2787(): n, k = symbols('n k', positive=True, integer=True) p = symbols('p', positive=True) binomial_dist = binomial(n, k) * p**k * (1 - p)**(n - k) s = summation(binomial_dist * k, (k, 0, n)) assert s.simplify() == Piecewise( (n * p, And(Or(-n + 1 < 0, Ne(p / (p - 1), 1)), p / abs(p - 1) <= 1)), (Sum(k * p**k * (-p + 1)**(-k) * (-p + 1)**n * binomial(n, k), (k, 0, n)), True))
def test_composite_sums(): f = Rational(1, 2)*(7 - 6*n + Rational(1, 7)*n**3) s = summation(f, (n, a, b)) assert not isinstance(s, Sum) A = 0 for i in range(-3, 5): A += f.subs({n: i}) B = s.subs({a: -3, b: 4}) assert A == B
def test_Sum_interface(): assert isinstance(Sum(0, (n, 0, 2)), Sum) assert Sum(nan, (n, 0, 2)) is nan assert Sum(nan, (n, 0, oo)) is nan assert Sum(0, (n, 0, 2)).doit() == 0 assert isinstance(Sum(0, (n, 0, oo)), Sum) assert Sum(0, (n, 0, oo)).doit() == 0 pytest.raises(ValueError, lambda: Sum(1)) pytest.raises(ValueError, lambda: summation(1)) pytest.raises(ValueError, lambda: Sum(x, (x, 1, 2, 3)))
def test_the_sum(m, n): # g g = i**3 + 2 * i**2 - 3 * i # f = Delta g f = simplify(g.subs({i: i + 1}) - g) # The sum a = m b = n - 1 S = summation(f, (i, a, b)) # Test if Sum_{m <= i < n} f(i) = g(n) - g(m) assert simplify(S - (g.subs({i: n}) - g.subs({i: m}))) == 0
def compute_cdf(self, **kwargs): """ Compute the CDF from the PDF Returns a Lambda """ x, z = symbols('x, z', integer=True, finite=True, cls=Dummy) left_bound = self.set.inf # CDF is integral of PDF from left bound to z pdf = self.pdf(x) cdf = summation(pdf, (x, left_bound, z), **kwargs) # CDF Ensure that CDF left of left_bound is zero cdf = Piecewise((cdf, z >= left_bound), (0, True)) return Lambda(z, cdf)
def test_totient(): assert [totient(k) for k in range(1, 12)] == \ [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10] assert totient(5005) == 2880 assert totient(5006) == 2502 assert totient(5009) == 5008 assert totient(2**100) == 2**99 m = Symbol("m", integer=True) assert totient(m) assert totient(m).subs({m: 3**10}) == 3**10 - 3**9 assert summation(totient(m), (m, 1, 11)) == 42 n = Symbol("n", integer=True, positive=True) assert totient(n).is_integer assert totient(m).is_integer is None
def test_Sum_interface(): assert isinstance(Sum(0, (n, 0, 2)), Sum) assert Sum(nan, (n, 0, 2)) is nan assert Sum(nan, (n, 0, oo)) is nan assert Sum(0, (n, 0, 2)).doit() == 0 assert isinstance(Sum(0, (n, 0, oo)), Sum) assert Sum(0, (n, 0, oo)).doit() == 0 pytest.raises(ValueError, lambda: Sum(1)) pytest.raises(ValueError, lambda: summation(1)) pytest.raises(ValueError, lambda: Sum(x, (x, 1, 2, 3))) # issue sympy/sympy#21888 pytest.raises(ValueError, lambda: Sum(-1, (x, I, 5))) # issue sympy/sympy#19745 pytest.raises(ValueError, lambda: Sum(x, (x, 1, Rational(3, 2))))
def test_hypersum(): assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x) assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x) assert simplify(summation((-1)**n*x**(2*n + 1) / factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120 assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3) assert summation(1/n**4, (n, 1, oo)) == pi**4/90 s = summation(x**n*n, (n, -oo, 0)) assert s.is_Piecewise assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2) assert s.args[0].args[1] == (abs(1/x) < 1) m = Symbol('n', integer=True, positive=True) assert summation(binomial(m, k), (k, 0, m)) == 2**m # issue sympy/sympy#8822 assert summation(1/((k+3.5)*(k+8)), (k, 1, n)) is not nan
def test_hypersum(): assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x) assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x) assert simplify(summation((-1)**n*x**(2*n + 1) / factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120 assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3) assert summation(1/n**4, (n, 1, oo)) == pi**4/90 s = summation(x**n*n, (n, -oo, 0)) assert s.is_Piecewise assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2) assert s.args[0].args[1] == (abs(1/x) < 1) m = Symbol('n', integer=True, positive=True) assert summation(binomial(m, k), (k, 0, m)) == 2**m
def test_divisor_sigma(): assert [divisor_sigma(k) for k in range(1, 12)] == \ [1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12] assert [divisor_sigma(k, 2) for k in range(1, 12)] == \ [1, 5, 10, 21, 26, 50, 50, 85, 91, 130, 122] assert divisor_sigma(23450) == 50592 assert divisor_sigma(23450, 0) == 24 assert divisor_sigma(23450, 1) == 50592 assert divisor_sigma(23450, 2) == 730747500 assert divisor_sigma(23450, 3) == 14666785333344 m = Symbol("m", integer=True) k = Symbol("k", integer=True) assert divisor_sigma(m) assert divisor_sigma(m, k) assert divisor_sigma(m).subs({m: 3**10}) == 88573 assert divisor_sigma(m, k).subs({m: 3**10, k: 3}) == 213810021790597 assert summation(divisor_sigma(m), (m, 1, 11)) == 99 pytest.raises(ValueError, lambda: divisor_sigma(-4))
def test_sympyissue_7097(): assert sum(x**n/n for n in range(1, 401)) == summation(x**n/n, (n, 1, 400))
def test_sympyissue_4171(): assert summation(factorial(2*k + 1)/factorial(2*k), (k, 0, oo)) == oo assert summation(2*k + 1, (k, 0, oo)) == oo
def test_sympyissue_4170(): assert summation(1/factorial(k), (k, 0, oo)) == E
def test_diofantissue_236(): assert summation(n/((n + 2)*(n + 4)*(n + 8)), (n, 1, oo)) == Rational(4, 35)
def test_sympyissue_4668(): assert summation(1 / n, (n, 2, oo)) == oo
def test_hypergeometric_sums(): assert summation( binomial(2*k, k)/4**k, (k, 0, n)) == (1 + 2*n)*binomial(2*n, n)/4**n
def test_sympyissue_8822(): assert summation(1 / ((k + 3.5) * (k + 8)), (k, 1, n)) is not nan
def test_harmonic_sums(): assert summation(1/k, (k, 0, n)) == Sum(1/k, (k, 0, n)) assert summation(1/k, (k, 1, n)) == harmonic(n) assert summation(n/k, (k, 1, n)) == n*harmonic(n) assert summation(1/k, (k, 5, n)) == harmonic(n) - harmonic(4)
def test_geometric_sums(): assert summation(pi**n, (n, 0, b)) == (1 - pi**(b + 1)) / (1 - pi) assert summation(2 * 3**n, (n, 0, b)) == 3**(b + 1) - 1 assert summation(Rational(1, 2)**n, (n, 1, oo)) == 1 assert summation(2**n, (n, 0, b)) == 2**(b + 1) - 1 assert summation(2**n, (n, 1, oo)) == oo assert summation(2**(-n), (n, 1, oo)) == 1 assert summation(3**(-n), (n, 4, oo)) == Rational(1, 54) assert summation(2**(-4*n + 3), (n, 1, oo)) == Rational(8, 15) assert summation(2**(n + 1), (n, 1, b)).expand() == 4*(2**b - 1) # issue sympy/sympy#6664: assert summation(x**n, (n, 0, oo)) == \ Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**n, (n, 0, oo)), True)) assert summation(-2**n, (n, 0, oo)) == -oo assert summation(I**n, (n, 0, oo)) == Sum(I**n, (n, 0, oo)) # issue sympy/sympy#6802: assert summation((-1)**(2*x + 2), (x, 0, n)) == n + 1 assert summation((-2)**(2*x + 2), (x, 0, n)) == 4*4**(n + 1)/3 - Rational(4, 3) assert summation((-1)**x, (x, 0, n)) == -(-1)**(n + 1)/2 + Rational(1, 2) assert summation(y**x, (x, a, b)) == \ Piecewise((-a + b + 1, Eq(y, 1)), ((y**a - y**(b + 1))/(-y + 1), True)) assert summation((-2)**(y*x + 2), (x, 0, n)) == \ 4*Piecewise((n + 1, Eq((-2)**y, 1)), ((-(-2)**(y*(n + 1)) + 1)/(-(-2)**y + 1), True)) # issue sympy/sympy#8251: assert summation((1/(n + 1)**2)*n**2, (n, 0, oo)) == oo # issue sympy/sympy#9908: assert Sum(1/(n**3 - 1), (n, -oo, -2)).doit() == summation(1/(n**3 - 1), (n, -oo, -2))
def test_sympyissue_4668(): assert summation(1/n, (n, 2, oo)) == oo
def test_sympyissue_8822(): assert summation(1/((k+3.5)*(k+8)), (k, 1, n)) is not nan
def test_sympyissue_4171(): assert summation(factorial(2 * k + 1) / factorial(2 * k), (k, 0, oo)) == oo assert summation(2 * k + 1, (k, 0, oo)) == oo
def test_Sum(): assert str(summation(cos(3 * z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))" assert str(Sum(x*y**2, (x, -2, 2), (y, -5, 5))) == \ "Sum(x*y**2, (x, -2, 2), (y, -5, 5))"
def test_Sum(): assert str(summation(cos(3*z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))" assert str(Sum(x*y**2, (x, -2, 2), (y, -5, 5))) == \ "Sum(x*y**2, (x, -2, 2), (y, -5, 5))"
def test_sympyissue_4170(): assert summation(1 / factorial(k), (k, 0, oo)) == E
def test_diofantissue_236(): assert summation(n / ((n + 2) * (n + 4) * (n + 8)), (n, 1, oo)) == Rational(4, 35)
def test_sympyissue_7097(): assert sum(x**n / n for n in range(1, 401)) == summation(x**n / n, (n, 1, 400))
def test_hypergeometric_sums(): assert summation(binomial(2 * k, k) / 4**k, (k, 0, n)) == (1 + 2 * n) * binomial(2 * n, n) / 4**n