def test_arithmetic_sums(): assert sum(1, (n, a, b)) == b - a + 1 assert sum(1, (n, 1, 10)) == 10 assert sum(2 * n, (n, 0, 10**10)) == 100000000010000000000 assert sum(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 sum(cos(n), (n, -2, 1)) == cos(-2) + cos(-1) + cos(0) + cos(1)
def test_arithmetic_sums(): assert sum(1, (n, a, b)) == b-a+1 assert sum(1, (n, 1, 10)) == 10 assert sum(2*n, (n, 0, 10**10)) == 100000000010000000000 assert sum(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 sum(cos(n), (n, -2, 1)) == cos(-2)+cos(-1)+cos(0)+cos(1)
def test_polynomial_sums(): assert sum(n**2, (n, 3, 8)) == 199 assert sum(n, (n, a, b)) == \ ((a+b)*(b-a+1)/2).expand() assert sum(n**2, (n, 1, b)) == \ ((2*b**3+3*b**2+b)/6).expand() assert sum(n**3, (n, 1, b)) == \ ((b**4+2*b**3+b**2)/4).expand() assert sum(n**6, (n, 1, b)) == \ ((6*b**7+21*b**6+21*b**5-7*b**3+b)/42).expand()
def test_geometric_sums(): assert sum(pi**n, (n, 0, b)) == (1-pi**(b+1)) / (1-pi) assert sum(2 * 3**n, (n, 0, b)) == 3**(b+1) - 1 assert sum(Rational(1,2)**n, (n, 1, oo)) == 1 assert sum(2**n, (n, 0, b)) == 2**(b+1) - 1 assert sum(2**n, (n, 1, oo)) == oo assert sum(2**(-n), (n, 1, oo)) == 1 assert sum(3**(-n), (n, 4, oo)) == Rational(1,54) assert sum(2**(-4*n+3), (n, 1, oo)) == Rational(8,15) assert sum(2**(n+1), (n, 1, b)).expand() == 4*(2**b-1)
def test_geometric_sums(): assert sum(pi**n, (n, 0, b)) == (1 - pi**(b + 1)) / (1 - pi) assert sum(2 * 3**n, (n, 0, b)) == 3**(b + 1) - 1 assert sum(Rational(1, 2)**n, (n, 1, oo)) == 1 assert sum(2**n, (n, 0, b)) == 2**(b + 1) - 1 assert sum(2**n, (n, 1, oo)) == oo assert sum(2**(-n), (n, 1, oo)) == 1 assert sum(3**(-n), (n, 4, oo)) == Rational(1, 54) assert sum(2**(-4 * n + 3), (n, 1, oo)) == Rational(8, 15) assert sum(2**(n + 1), (n, 1, b)).expand() == 4 * (2**b - 1)
def test_composite_sums(): f = Rational(1,2)*(7 - 6*n + Rational(1,7)*n**3) s = sum(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).subs(b,4) assert A == B
def test_composite_sums(): f = Rational(1, 2) * (7 - 6 * n + Rational(1, 7) * n**3) s = sum(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).subs(b, 4) assert A == B
def _eval_product(self, a, n, term): from sympy import sum, Sum k = self.index if not term.has(k): return term**(n-a+1) elif term.is_polynomial(k): poly = term.as_poly(k) A = B = Q = S.One C_= poly.LC all_roots = roots(poly, multiple=True) for r in all_roots: A *= C.RisingFactorial(a-r, n-a+1) Q *= n - r if len(all_roots) < poly.degree: B = Product(quo(poly, Q.as_poly(k)), (k, a, n)) return poly.LC**(n-a+1) * A * B elif term.is_Add: p, q = term.as_numer_denom() p = self._eval_product(a, n, p) q = self._eval_product(a, n, q) return p / q elif term.is_Mul: exclude, include = [], [] for t in term.args: p = self._eval_product(a, n, t) if p is not None: exclude.append(p) else: include.append(t) if not exclude: return None else: A, B = Mul(*exclude), Mul(*include) return A * Product(B, (k, a, n)) elif term.is_Pow: if not term.base.has(k): s = sum(term.exp, (k, a, n)) if not isinstance(s, Sum): return term.base**s elif not term.exp.has(k): p = self._eval_product(a, n, term.base) if p is not None: return p**term.exp
def _eval_product(self, a, n, term): from sympy import sum, Sum k = self.index if not term.has(k): return term**(n - a + 1) elif term.is_polynomial(k): poly = term.as_poly(k) A = B = Q = S.One C_ = poly.LC() all_roots = roots(poly, multiple=True) for r in all_roots: A *= C.RisingFactorial(a - r, n - a + 1) Q *= n - r if len(all_roots) < poly.degree(): B = Product(quo(poly, Q.as_poly(k)), (k, a, n)) return poly.LC()**(n - a + 1) * A * B elif term.is_Add: p, q = term.as_numer_denom() p = self._eval_product(a, n, p) q = self._eval_product(a, n, q) return p / q elif term.is_Mul: exclude, include = [], [] for t in term.args: p = self._eval_product(a, n, t) if p is not None: exclude.append(p) else: include.append(t) if not exclude: return None else: A, B = Mul(*exclude), Mul(*include) return A * Product(B, (k, a, n)) elif term.is_Pow: if not term.base.has(k): s = sum(term.exp, (k, a, n)) if not isinstance(s, Sum): return term.base**s elif not term.exp.has(k): p = self._eval_product(a, n, term.base) if p is not None: return p**term.exp
def test_Sum(): assert str(sum(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(sum(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(sum(cos(3*z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))"
def test_Sum(): assert str(sum(cos(3 * z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))"
def test_Sum_doit(): 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 sum(n * Integral(a**2), (n, 0, 2)) == 3 * Integral(a**2)
def test_Sum_doit(): 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 sum(n*Integral(a**2), (n, 0, 2)) == 3*Integral(a**2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-69-5b80e95b5264> in <module>() ----> 1 power(x,2) TypeError: 'module' object is not callable power Out[70]: <module 'sympy.core.power' from '/home/alexander/py_virt_envs/base/local/lib/python2.7/site-packages/sympy/core/power.pyc'> x**(0.5). File "<ipython-input-73-ad48eaeec398>", line 1; print(';'.join(get_ipython().Completer.all_completions('f.e'))) #PYTHON-MODE SILENT f.eval(2) f sympy.diff(f, x) sympy.exp(2) sympy.exp(x) sympy.sum(x) sympy.Sum(x) sum(x) ImportError Traceback (most recent call last);ImportError: No module named mpmath;ImportError Traceback (most recent call last);ImportError: No module named mpmath;ImportError Traceback (most recent call last);ImportError: No module named mpmath;ImportError Traceback (most recent call last);ImportError: No module named mpmath;ImportError Traceback (most recent call last);ImportError: No module named mpmath;ImportError Traceback (most recent call last) <ipython-input-63-8855bbdd7472> in <module>() ----> 1 from sympy import pow ImportError: cannot import name pow from sympy import power power(x,2) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-65-5b80e95b5264> in <module>() ----> 1 power(x,2) TypeError: 'module' object is not callable x Out[66]: x