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 fdiff(self, argindex=4): from diofant import Sum if argindex == 1: # Diff wrt n raise ArgumentIndexError(self, argindex) elif argindex == 2: # Diff wrt a n, a, b, x = self.args k = Dummy("k") f1 = 1 / (a + b + n + k + 1) f2 = ((a + b + 2*k + 1) * RisingFactorial(b + k + 1, n - k) / ((n - k) * RisingFactorial(a + b + k + 1, n - k))) return Sum(f1 * (jacobi(n, a, b, x) + f2*jacobi(k, a, b, x)), (k, 0, n - 1)) elif argindex == 3: # Diff wrt b n, a, b, x = self.args k = Dummy("k") f1 = 1 / (a + b + n + k + 1) f2 = (-1)**(n - k) * ((a + b + 2*k + 1) * RisingFactorial(a + k + 1, n - k) / ((n - k) * RisingFactorial(a + b + k + 1, n - k))) return Sum(f1 * (jacobi(n, a, b, x) + f2*jacobi(k, a, b, x)), (k, 0, n - 1)) elif argindex == 4: # Diff wrt x n, a, b, x = self.args return S.Half * (a + b + n + 1) * jacobi(n - 1, a + 1, b + 1, x) else: raise ArgumentIndexError(self, argindex)
def test_Sum(): assert mathematica_code(Sum(sin(x), (x, 0, 10))) == 'Hold[Sum[Sin[x], {x, 0, 10}]]' assert mathematica_code(Sum(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))) == \ 'Hold[Sum[E^(-x^2 - y^2), {x, -Infinity, Infinity}, ' \ '{y, -Infinity, Infinity}]]'
def test_harmonic_rewrite_sum_fail(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def sum_add(self, other, method=0): """Helper function for Sum simplification""" from diofant.concrete.summations import Sum if type(self) == type(other): if method == 0: if self.limits == other.limits: return Sum(self.function + other.function, *self.limits) elif method == 1: if simplify(self.function - other.function) == 0: if len(self.limits) == len(other.limits) == 1: i = self.limits[0][0] x1 = self.limits[0][1] y1 = self.limits[0][2] j = other.limits[0][0] x2 = other.limits[0][1] y2 = other.limits[0][2] if i == j: if x2 == y1 + 1: return Sum(self.function, (i, x1, y2)) elif x1 == y2 + 1: return Sum(self.function, (i, x2, y1)) return Add(self, other)
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_distribution_over_equality(): f = Function('f') assert Product(Eq(x * 2, f(x)), (x, 1, 3)).doit() == Eq(48, f(1) * f(2) * f(3)) assert Sum(Eq(f(x), x**2), (x, 0, y)) == \ Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
def test_harmonic_rewrite_sum_fail(): n = Symbol('n') m = Symbol('m') _k = Dummy('k') assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def _eval_expand_func(self, **hints): from diofant import Sum n = self.args[0] m = self.args[1] if len(self.args) == 2 else 1 if m == S.One: if n.is_Add: off = n.args[0] nnew = n - off if off.is_Integer and off.is_positive: result = [S.One / (nnew + i) for i in range(off, 0, -1)] + [harmonic(nnew)] return Add(*result) elif off.is_Integer and off.is_negative: result = [-S.One / (nnew + i) for i in range(0, off, -1)] + [harmonic(nnew)] return Add(*result) if n.is_Rational: # Expansions for harmonic numbers at general rational arguments (u + p/q) # Split n as u + p/q with p < q p, q = n.as_numer_denom() u = p // q p = p - u * q if u.is_nonnegative and p.is_positive and q.is_positive and p < q: k = Dummy("k") t1 = q * Sum(1 / (q * k + p), (k, 0, u)) t2 = 2 * Sum( cos((2 * pi * p * k) / q) * log(sin((pi * k) / q)), (k, 1, floor((q - 1) / Integer(2)))) t3 = (pi / 2) * cot((pi * p) / q) + log(2 * q) return t1 + t2 - t3 return self
def test_sympyissue_21651(): a = Sum(floor(2 * 2**-n), (n, 1, 2)) b = floor(2 * 2**-1) + floor(2 * 2**-2) assert a.doit() == b.doit() c = Sum(ceiling(2 * 2**-n), (n, 1, 2)) d = ceiling(2 * 2**-1) + ceiling(2 * 2**-2) assert c.doit() == d.doit()
def test_eval_derivative(): assert Sum(x, (x, 1, 2)).diff(x) == 0 assert Sum(x*y, (x, 1, 2)).diff(x) == 0 assert Sum(x*y, (y, 1, 2)).diff(x) == Sum(y, (y, 1, 2)) e = Sum(x*y, (x, 1, a)) assert e.diff(a) == Derivative(e, a) assert Sum(x*y, (x, 1, 3), (a, 2, 5)).diff(y) == \ Sum(x*y, (x, 1, 3), (a, 2, 5)).doit().diff(y) == 24
def test_basic1(): assert limit(x, x, oo) == oo assert limit(x, x, -oo) == -oo assert limit(-x, x, oo) == -oo assert limit(x**2, x, -oo) == oo assert limit(-x**2, x, oo) == -oo assert limit(x*log(x), x, 0, dir="+") == 0 assert limit(1/x, x, oo) == 0 assert limit(exp(x), x, oo) == oo assert limit(-exp(x), x, oo) == -oo assert limit(exp(x)/x, x, oo) == oo assert limit(1/x - exp(-x), x, oo) == 0 assert limit(x + 1/x, x, oo) == oo assert limit(x - x**2, x, oo) == -oo assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1 assert limit((1 + x)**oo, x, 0) == oo assert limit((1 + x)**oo, x, 0, dir='-') == 0 assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**(oo) assert limit(y/x/log(x), x, 0) == -oo*sign(y) assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo limit(Sum(1/x, (x, 1, y)) - log(y), y, oo) limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo) assert limit(gamma(1/x + 3), x, oo) == 2 assert limit(S.NaN, x, -oo) == S.NaN assert limit(O(2)*x, x, S.NaN) == S.NaN assert limit(sin(O(x)), x, 0) == 0 assert limit(1/(x - 1), x, 1, dir="+") == oo assert limit(1/(x - 1), x, 1, dir="-") == -oo assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo assert limit(1/(5 - x)**3, x, 5, dir="-") == oo assert limit(1/sin(x), x, pi, dir="+") == -oo assert limit(1/sin(x), x, pi, dir="-") == oo assert limit(1/cos(x), x, pi/2, dir="+") == -oo assert limit(1/cos(x), x, pi/2, dir="-") == oo assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") == oo assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") == -oo assert limit(1/cot(x)**3, x, (3*pi/2), dir="+") == -oo assert limit(1/cot(x)**3, x, (3*pi/2), dir="-") == oo # approaching 0 # from dir="+" assert limit(1 + 1/x, x, 0) == oo # from dir='-' # Add assert limit(1 + 1/x, x, 0, dir='-') == -oo # Pow assert limit(x**(-2), x, 0, dir='-') == oo assert limit(x**(-3), x, 0, dir='-') == -oo assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I assert limit(x**2, x, 0, dir='-') == 0 assert limit(sqrt(x), x, 0, dir='-') == 0 assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi)) assert limit((1 + cos(x))**oo, x, 0) == oo assert limit(x**2, x, 0, dir='real') == 0 assert limit(exp(x), x, 0, dir='real') == 1 pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
def test_harmonic_rewrite_sum(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert replace_dummy(harmonic(n).rewrite(Sum), _k) == Sum(1 / _k, (_k, 1, n)) assert replace_dummy(harmonic(n, m).rewrite(Sum), _k) == Sum(_k**(-m), (_k, 1, n))
def test_harmonic_rewrite_sum(): n = Symbol('n') m = Symbol('m') _k = Dummy('k') assert replace_dummy(harmonic(n).rewrite(Sum), _k) == Sum(1 / _k, (_k, 1, n)) assert replace_dummy(harmonic(n, m).rewrite(Sum), _k) == Sum(_k**(-m), (_k, 1, n))
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 = Sum(binomial_dist * k, (k, 0, n)) res = s.doit().simplify() assert res == 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_function_subs(): f = Function("f") S = Sum(x * f(y), (x, 0, oo), (y, 0, oo)) assert S.subs(f(y), y) == Sum(x * y, (x, 0, oo), (y, 0, oo)) assert S.subs(f(x), x) == S pytest.raises(ValueError, lambda: S.subs(f(y), x + y)) S = Sum(x * log(y), (x, 0, oo), (y, 0, oo)) assert S.subs(log(y), y) == S S = Sum(x * f(y), (x, 0, oo), (y, 0, oo)) assert S.subs(f(y), y) == Sum(x * y, (x, 0, oo), (y, 0, oo))
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 = Sum(binomial_dist*k, (k, 0, n)) res = s.doit().simplify() assert res == 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_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_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 sum_simplify(s): """Main function for Sum simplification""" from diofant.concrete.summations import Sum terms = Add.make_args(s) s_t = [] # Sum Terms o_t = [] # Other Terms for term in terms: if isinstance(term, Mul): constant = 1 other = 1 s = 0 n_sum_terms = 0 for j in range(len(term.args)): if isinstance(term.args[j], Sum): s = term.args[j] n_sum_terms = n_sum_terms + 1 elif term.args[j].is_number: constant = constant * term.args[j] else: other = other * term.args[j] if other == 1 and n_sum_terms == 1: # Insert the constant inside the Sum s_t.append(Sum(constant * s.function, *s.limits)) elif other != 1 and n_sum_terms == 1: o_t.append(other * Sum(constant * s.function, *s.limits)) else: o_t.append(term) elif isinstance(term, Sum): s_t.append(term) else: o_t.append(term) used = [False] * len(s_t) for method in range(2): for i, s_term1 in enumerate(s_t): if not used[i]: for j, s_term2 in enumerate(s_t): if not used[j] and i != j: temp = sum_add(s_term1, s_term2, method) if isinstance(temp, Sum): s_t[i] = temp s_term1 = s_t[i] used[j] = True result = Add(*o_t) for i, s_term in enumerate(s_t): if not used[i]: result = Add(result, s_term) return result
def test_evalf_fast_series_sympyissue_4021(): # Catalan's constant assert NS(Sum((-1)**(n - 1)*2**(8*n)*(40*n**2 - 24*n + 3)*fac(2*n)**3 * fac(n)**2/n**3/(2*n - 1)/fac(4*n)**2, (n, 1, oo))/64, 100, strict=False) == NS(Catalan, 100) astr = NS(zeta(3), 100) assert NS(5*Sum( (-1)**(n - 1)*fac(n)**2 / n**3 / fac(2*n), (n, 1, oo))/2, 100) == astr assert NS(Sum((-1)**(n - 1)*(56*n**2 - 32*n + 5) / (2*n - 1)**2 * fac(n - 1) ** 3 / fac(3*n), (n, 1, oo))/4, 100, strict=False) == astr
def test_eval_derivative(): assert Sum(x, (x, 1, 2)).diff(x) == 0 assert Sum(x * y, (x, 1, 2)).diff(x) == 0 assert Sum(x * y, (y, 1, 2)).diff(x) == Sum(y, (y, 1, 2)) e = Sum(x * y, (x, 1, a)) assert e.diff(a) == Derivative(e, a) assert Sum(x*y, (x, 1, 3), (a, 2, 5)).diff(y) == \ Sum(x*y, (x, 1, 3), (a, 2, 5)).doit().diff(y) == 24
def test_jacobi(): assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1) assert (jacobi(2, a, b, x) == a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 + x**2*(a**2/8 + a*b/4 + 7*a/8 + b**2/8 + 7*b/8 + Rational(3, 2)) + x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - Rational(1, 2)) assert jacobi(n, a, a, x) == RisingFactorial( a + 1, n)*gegenbauer(n, a + Rational(1, 2), x)/RisingFactorial(2*a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x) * factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x) * gamma(-b + n + 1)/gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, Rational(1, 2), Rational(1, 2), x) == RisingFactorial( Rational(3, 2), n)*chebyshevu(n, x)/factorial(n + 1) assert jacobi(n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial( Rational(1, 2), n)*chebyshevt(n, x)/factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper( (-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n) m = Symbol('m', positive=True) assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m) assert jacobi(n, a, b, oo) == jacobi(n, a, b, oo, evaluate=False) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), x) == \ (a + b + n + 1)*jacobi(n - 1, a + 1, b + 1, x)/2 assert (jacobi(n, a, b, x).diff(a) == Sum((jacobi(n, a, b, x) + (a + b + 2*k + 1)*RisingFactorial(b + k + 1, n - k) * jacobi(k, a, b, x)/((n - k)*RisingFactorial(a + b + k + 1, n - k)))/(a + b + n + k + 1), (k, 0, n - 1))) assert (jacobi(n, a, b, x).diff(b) == Sum(((-1)**(n - k)*(a + b + 2*k + 1)*RisingFactorial(a + k + 1, n - k) * jacobi(k, a, b, x)/((n - k)*RisingFactorial(a + b + k + 1, n - k)) + jacobi(n, a, b, x))/(a + b + n + k + 1), (k, 0, n - 1))) assert jacobi_normalized(n, a, b, x) == \ (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1) / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))) pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x)) pytest.raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo)) pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def _eval_rewrite_as_Sum(self, arg): from diofant import Sum if arg.is_even: k = Dummy("k", integer=True) j = Dummy("j", integer=True) n = self.args[0] / 2 Em = (S.ImaginaryUnit * Sum( Sum( binomial(k, j) * ((-1)**j * (k - 2 * j)**(2 * n + 1)) / (2**k * S.ImaginaryUnit**k * k), (j, 0, k)), (k, 1, 2 * n + 1))) return Em
def test_evalf_divergent_series(): pytest.raises(ValueError, lambda: Sum(1/n, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum(n/(n**2 + 1), (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum(n**2, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum(2**n, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum((-2)**n, (n, 1, oo)).evalf()) pytest.raises(ValueError, lambda: Sum((2*n + 3)/(3*n**2 + 4), (n, 0, oo)).evalf()) pytest.raises(ValueError, lambda: Sum((0.5*n**3)/(n**4 + 1), (n, 0, oo)).evalf())
def test_findrecur(): pytest.raises(ValueError, lambda: Sum(x*y, (x, 0, 2), (y, 0, 2)).findrecur()) pytest.raises(ValueError, lambda: Sum(x*y, (x, 0, 2)).findrecur()) pytest.raises(ValueError, lambda: Sum(x, (x, 0, oo)).findrecur()) pytest.raises(ValueError, lambda: Sum(sin(x*y)/(x**2 + 1), (x, 0, oo)).findrecur()) n, k = symbols("n, k", integer=True) F = symbols("F", cls=Function) f = Sum(factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo)) g = Sum(k*factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo)) fa = Sum((-1)**k*binomial(n, k)*binomial(2*n - 2*k, n + a), (k, 0, oo)) fb = Sum(binomial(x, k)*binomial(y, n - k), (k, 0, oo)) assert f.findrecur(F) == -F(n, k) + F(n - 1, k) + F(n - 1, k - 1) assert (Sum(binomial(n, k), (k, 0, oo)).findrecur(F) == -F(n, k) + F(n - 1, k) + F(n - 1, k - 1)) assert (g.findrecur(F) == (-1 + 1/n)*F(n, k) + F(n - 1, k) + F(n - 1, k - 1)) assert (fa.findrecur(F, n) == F(n - 2, k - 1) - (2*n - 1)*F(n - 1, k)/(2*n - 2) - (a**2 - n**2)*F(n, k)/(4*n*(n - 1))) assert (fb.findrecur(F, n) == -n*F(n, k)/(-n + x + y + 2) + (-n + x + 1)*F(n - 1, k - 1)/(-n + x + y + 2) + (-n + y + 1)*F(n - 1, k)/(-n + x + y + 2) + F(n - 2, k - 1))
def test_evalf_sum(): assert Sum(n, (n, 1, 2)).evalf() == 3. assert Sum(I*n, (n, 1, 2)).evalf() == 3.*I assert Sum(n, (n, 1, 2)).doit().evalf() == 3. # the next test should return instantly assert Sum(1/n, (n, 1, 2)).evalf() == 1.5 # issue sympy/sympy#8219 assert Sum(E/factorial(n), (n, 0, oo)).evalf() == (E*E).evalf() # issue sympy/sympy#8254 assert Sum(2**n*n/factorial(n), (n, 0, oo)).evalf() == (2*E*E).evalf() # issue sympy/sympy#8411 s = Sum(1/x**2, (x, 100, oo)) assert s.evalf() == s.doit().evalf()
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_telescopic_sums(): # checks also input 2 of comment 1 issue 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)))
def test_is_number(): assert Integral(x).is_number is False assert Integral(1, x).is_number is False assert Integral(1, (x, 1)).is_number is True assert Integral(1, (x, 1, 2)).is_number is True assert Integral(1, (x, 1, y)).is_number is False assert Integral(1, (x, y)).is_number is False assert Integral(x, y).is_number is False assert Integral(x, (y, 1, x)).is_number is False assert Integral(x, (y, 1, 2)).is_number is False assert Integral(x, (x, 1, 2)).is_number is True # `foo.is_number` should always be eqivalent to `not foo.free_symbols` # in each of these cases, there are pseudo-free symbols i = Integral(x, (y, 1, 1)) assert i.is_number is False and i.evalf() == 0 i = Integral(x, (y, z, z)) assert i.is_number is False and i.evalf() == 0 i = Integral(1, (y, z, z + 2)) assert i.is_number is False and i.evalf() == 2 assert Integral(x * y, (x, 1, 2), (y, 1, 3)).is_number is True assert Integral(x * y, (x, 1, 2), (y, 1, z)).is_number is False assert Integral(x, (x, 1)).is_number is True assert Integral(x, (x, 1, Integral(y, (y, 1, 2)))).is_number is True assert Integral(Sum(z, (z, 1, 2)), (x, 1, 2)).is_number is True # it is possible to get a false negative if the integrand is # actually an unsimplified zero, but this is true of is_number in general. assert Integral(sin(x)**2 + cos(x)**2 - 1, x).is_number is False assert Integral(f(x), (x, 0, 1)).is_number is True
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_nsimplify(): assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5))/2 assert nsimplify((1 + sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2 assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == Rational(1, 2) - sqrt(3)*I/2 assert nsimplify(sin(3*pi/5, evaluate=False)) == sqrt(sqrt(5)/8 + Rational(5, 8)) assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2*atan('1/4')*I)) == Rational(49, 17) + 8*I/17 assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == cbrt(2) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).evalf(), rational=True) == Rational(109861228866811, 100000000000000) assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).evalf(3), [pi, log(2)]) == \ -pi/4 - log(2) + Rational(7, 4) assert nsimplify(x/7.0) == x/7 assert nsimplify(pi/1e2) == pi/100 assert nsimplify(pi/1e2, rational=False) == pi/100.0 assert nsimplify(pi/1e-7) == 10000000*pi assert not nsimplify( factor(-3.0*z**2*(z**2)**(-2.5) + 3*(z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == 33 assert nsimplify(33.33, tolerance=10, rational=True) == 30 assert nsimplify(37.76, tolerance=10, rational=True) == 40 assert nsimplify(-203.1) == -Rational(2031, 10) assert nsimplify(+.2, tolerance=0) == Rational(+1, 5) assert nsimplify(-.2, tolerance=0) == Rational(-1, 5) assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == -Rational(1111, 5000) # issue sympy/sympy#7211, PR sympy/sympy#4112 assert nsimplify(Float(2e-8)) == Rational(1, 50000000) # issue sympy/sympy#7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue sympy/sympy#10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i)*oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(Sum(1/n**2, (n, 1, oo)), [pi]) == pi**2/6
def test_interval_arguments(): assert Interval(0, oo).right_open is false assert Interval(-oo, 0).left_open is false assert Interval(oo, -oo) == S.EmptySet assert isinstance(Interval(1, 1), FiniteSet) e = Sum(x, (x, 1, 3)) assert isinstance(Interval(e, e), FiniteSet) assert Interval(1, 0) == S.EmptySet assert Interval(1, 1).measure == 0 assert Interval(1, 1, False, True) == S.EmptySet assert Interval(1, 1, True, False) == S.EmptySet assert Interval(1, 1, True, True) == S.EmptySet assert isinstance(Interval(0, Symbol('a')), Interval) assert Interval(Symbol('a', extended_real=True, positive=True), 0) == S.EmptySet pytest.raises(ValueError, lambda: Interval(0, I)) pytest.raises(ValueError, lambda: Interval(0, Symbol('z', extended_real=False))) pytest.raises(NotImplementedError, lambda: Interval(0, 1, And(x, y))) pytest.raises(NotImplementedError, lambda: Interval(0, 1, False, And(x, y))) pytest.raises(NotImplementedError, lambda: Interval(0, 1, z, And(x, y)))
def test_hyper_rewrite_sum(): _k = Dummy("k") assert replace_dummy(hyper((1, 2), (1, 3), x).rewrite(Sum), _k) == \ Sum(x**_k / factorial(_k) * RisingFactorial(2, _k) / RisingFactorial(3, _k), (_k, 0, oo)) assert hyper((1, 2, 3), (-1, 3), z).rewrite(Sum) == \ hyper((1, 2, 3), (-1, 3), z)
def test_uniformsum_d(): n = Symbol('n', integer=True) k = Symbol('k') X = UniformSum('x', n) d = density(X)(x) assert d == 1/factorial(n - 1)*Sum((-1)**k*(x - k)**(n - 1) * binomial(n, k), (k, 0, floor(x)))
def test_change_index(): b, u, v = symbols('b, u, v', integer=True) assert Sum(x, (x, a, b)).change_index(x, x + 1, y) == \ Sum(y - 1, (y, a + 1, b + 1)) assert Sum(x**2, (x, a, b)).change_index( x, x - 1) == \ Sum((x+1)**2, (x, a - 1, b - 1)) assert Sum(x**2, (x, a, b)).change_index( x, -x, y) == \ Sum((-y)**2, (y, -b, -a)) assert Sum(x, (x, a, b)).change_index( x, -x - 1) == \ Sum(-x - 1, (x, -b - 1, -a - 1)) assert Sum(x*y, (x, a, b), (y, c, d)).change_index( x, x - 1, z) == \ Sum((z + 1)*y, (z, a - 1, b - 1), (y, c, d)) assert Sum(x, (x, a, b)).change_index( x, x + v) == \ Sum(-v + x, (x, a + v, b + v)) assert Sum(x, (x, a, b)).change_index( x, -x - v) == \ Sum(-v - x, (x, -b - v, -a - v)) S = Sum(x, (x, a, b)) assert S.change_index(x, u*x+v, y) == Sum((-v + y)/u, (y, b*u + v, a*u + v))
def test_function_subs(): f = Function("f") S = Sum(x*f(y), (x, 0, oo), (y, 0, oo)) assert S.subs({f(y): y}) == Sum(x*y, (x, 0, oo), (y, 0, oo)) assert S.subs({f(x): x}) == S pytest.raises(ValueError, lambda: S.subs({f(y): x + y})) S = Sum(x*log(y), (x, 0, oo), (y, 0, oo)) assert S.subs({log(y): y}) == S S = Sum(x*f(y), (x, 0, oo), (y, 0, oo)) assert S.subs({f(y): y}) == Sum(x*y, (x, 0, oo), (y, 0, oo))
def test_euler_maclaurin(): pytest.raises(ValueError, lambda: Sum(x - y, (x, 0, 2), (y, 0, 2)).euler_maclaurin()) # Exact polynomial sums with E-M def check_exact(f, a, b, m, n): A = Sum(f, (k, a, b)) s, e = A.euler_maclaurin(m, n) assert (e == 0) and (s.expand() == A.doit()) check_exact(k**4, a, b, 0, 2) check_exact(k**4 + 2*k, a, b, 1, 2) check_exact(k**4 + k**2, a, b, 1, 5) check_exact(k**5, 2, 6, 1, 2) check_exact(k**5, 2, 6, 1, 3) assert Sum(x-1, (x, 0, 2)).euler_maclaurin(m=30, n=30, eps=2**-15) == (0, 0) # Not exact assert Sum(k**6, (k, a, b)).euler_maclaurin(0, 2)[1] != 0 # Numerical test for m, n in [(2, 4), (2, 20), (10, 20), (18, 20)]: A = Sum(1/k**3, (k, 1, oo)) s, e = A.euler_maclaurin(m, n) assert abs((s - zeta(3)).evalf()) < e.evalf()
def test_conjugate_transpose(): A, B = symbols("A B", commutative=False) p = Sum(A*B**n, (n, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose()
def test_karr_convention(): # Test the Karr summation convention that we want to hold. # See his paper "Summation in Finite Terms" for a detailed # reasoning why we really want exactly this definition. # The convention is described on page 309 and essentially # in section 1.4, definition 3: # # \sum_{m <= i < n} f(i) 'has the obvious meaning' for m < n # \sum_{m <= i < n} f(i) = 0 for m = n # \sum_{m <= i < n} f(i) = - \sum_{n <= i < m} f(i) for m > n # # It is important to note that he defines all sums with # the upper limit being *exclusive*. # In contrast, diofant and the usual mathematical notation has: # # sum_{i = a}^b f(i) = f(a) + f(a+1) + ... + f(b-1) + f(b) # # with the upper limit *inclusive*. So translating between # the two we find that: # # \sum_{m <= i < n} f(i) = \sum_{i = m}^{n-1} f(i) # # where we intentionally used two different ways to typeset the # sum and its limits. i = Symbol("i", integer=True) k = Symbol("k", integer=True) j = Symbol("j", integer=True) # A simple example with a concrete summand and symbolic limits. # The normal sum: m = k and n = k + j and therefore m < n: m = k n = k + j a = m b = n - 1 S1 = Sum(i**2, (i, a, b)).doit() # The reversed sum: m = k + j and n = k and therefore m > n: m = k + j n = k a = m b = n - 1 S2 = Sum(i**2, (i, a, b)).doit() assert simplify(S1 + S2) == 0 # Test the empty sum: m = k and n = k and therefore m = n: m = k n = k a = m b = n - 1 Sz = Sum(i**2, (i, a, b)).doit() assert Sz == 0 # Another example this time with an unspecified summand and # numeric limits. (We can not do both tests in the same example.) f = Function("f") # The normal sum with m < n: m = 2 n = 11 a = m b = n - 1 S1 = Sum(f(i), (i, a, b)).doit() # The reversed sum with m > n: m = 11 n = 2 a = m b = n - 1 S2 = Sum(f(i), (i, a, b)).doit() assert simplify(S1 + S2) == 0 # Test the empty sum with m = n: m = 5 n = 5 a = m b = n - 1 Sz = Sum(f(i), (i, a, b)).doit() assert Sz == 0 e = Piecewise((exp(-i), Mod(i, 2) > 0), (0, True)) s = Sum(e, (i, 0, 11)) assert s.evalf(3) == s.doit().evalf(3)
def test_sympyissue_8016(): n, m = symbols('n, m', integer=True, positive=True) k = symbols('k', integer=True) s = Sum(binomial(m, k)*binomial(m, n-k)*(-1)**k, (k, 0, n)) assert s.doit().simplify() == \ cos(pi*n/2)*gamma(m + 1)/gamma(n/2 + 1)/gamma(m - n/2 + 1)
def check_exact(f, a, b, m, n): A = Sum(f, (k, a, b)) s, e = A.euler_maclaurin(m, n) assert (e == 0) and (s.expand() == A.doit())
def test_evalf_symbolic(): f, g = symbols('f g', cls=Function) # issue sympy/sympy#6328 expr = Sum(f(x), (x, 1, 3)) + Sum(g(x), (x, 1, 3)) assert expr.evalf(strict=False) == expr