def test_multiple_sums(): s = Sum(i * x + j, (i, a, b), (j, c, d)) l = lambdarepr(s) assert l == "(builtins.sum(i*x + j for i in range(a, b+1) for j in range(c, d+1)))" assert (lambdify((x, a, b, c, d), s)(2, 3, 4, 5, 6) == s.subs([(x, 2), (a, 3), (b, 4), (c, 5), (d, 6)]).doit())
def test_sum__2(): s = Sum(i * x, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(i*x for i in range(a, b+1)))" args = x, a, b f = lambdify(args, s) v = 2, 3, 8 assert f(*v) == s.subs(zip(args, v)).doit()
def test_sum(): # In each case, test eval() the lambdarepr() to make sure that # it evaluates to the same results as the symbolic expression s = Sum(x**i, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(x**i for i in range(a, b+1)))" assert (lambdify((x, a, b), s)(2, 3, 8) == s.subs([(x, 2), (a, 3), (b, 8)]).doit()) s = Sum(i * x, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(i*x for i in range(a, b+1)))" assert (lambdify((x, a, b), s)(2, 3, 8) == s.subs([(x, 2), (a, 3), (b, 8)]).doit())
def test_sum(): # In each case, test eval() the lambdarepr() to make sure that # it evaluates to the same results as the symbolic expression s = Sum(x ** i, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(x**i for i in range(a, b+1)))" assert (lambdify((x, a, b), s)(2, 3, 8) == s.subs([(x, 2), (a, 3), (b, 8)]).doit()) s = Sum(i * x, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(i*x for i in range(a, b+1)))" assert (lambdify((x, a, b), s)(2, 3, 8) == s.subs([(x, 2), (a, 3), (b, 8)]).doit())
def test_sum__1(): # In each case, test eval() the lambdarepr() to make sure that # it evaluates to the same results as the symbolic expression s = Sum(x**i, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(x**i for i in range(a, b+1)))" args = x, a, b f = lambdify(args, s) v = 2, 3, 8 assert f(*v) == s.subs(zip(args, v)).doit()
def test_sum__1(): # In each case, test eval() the lambdarepr() to make sure that # it evaluates to the same results as the symbolic expression s = Sum(x ** i, (i, a, b)) l = lambdarepr(s) assert l == "(builtins.sum(x**i for i in range(a, b+1)))" args = x, a, b f = lambdify(args, s) v = 2, 3, 8 assert f(*v) == s.subs(zip(args, v)).doit()
def test_multiple_sums(): s = Sum(i * x + j, (i, a, b), (j, c, d)) l = lambdarepr(s) assert l == "(builtins.sum(i*x + j for i in range(a, b+1) for j in range(c, d+1)))" args = x, a, b, c, d f = lambdify(args, s) vals = 2, 3, 4, 5, 6 f_ref = s.subs(zip(args, vals)).doit() f_res = f(*vals) assert f_res == f_ref
def test_issue_14640(): i, n = symbols("i n", integer=True) a, b, c = symbols("a b c") assert Sum(a**-i/(a - b), (i, 0, n)).doit() == Sum( 1/(a*a**i - a**i*b), (i, 0, n)).doit() == Piecewise( (n + 1, Eq(1/a, 1)), ((-a**(-n - 1) + 1)/(1 - 1/a), True))/(a - b) assert Sum((b*a**i - c*a**i)**-2, (i, 0, n)).doit() == Piecewise( (n + 1, Eq(a**(-2), 1)), ((-a**(-2*n - 2) + 1)/(1 - 1/a**2), True))/(b - c)**2 s = Sum(i*(a**(n - i) - b**(n - i))/(a - b), (i, 0, n)).doit() assert not s.has(Sum) assert s.subs({a: 2, b: 3, n: 5}) == 122
def generate_coupling_expansions(obj, verbose=True): """ generate expansions for coupling. """ if verbose: print('* Generating coupling expansions...') i = sym.symbols('i_sym') # summation index psi = obj.psi eps = obj.eps ruleA = {'psi': obj.pA['expand']} ruleB = {'psi': obj.pB['expand']} rule_trunc = {} for k in range(obj.miter, obj.miter + 200): rule_trunc.update({obj.eps**k: 0}) for key in obj.var_names: if verbose: print('key in coupling expand', key) gA = Sum(psi**i * Indexed('g' + key + 'A', i), (i, 1, obj.miter)).doit() gB = Sum(psi**i * Indexed('g' + key + 'B', i), (i, 1, obj.miter)).doit() iA = Sum(psi**i * Indexed('i' + key + 'A', i), (i, 0, obj.miter)).doit() iB = Sum(psi**i * Indexed('i' + key + 'B', i), (i, 0, obj.miter)).doit() tmp = gA.subs(ruleA) tmp = sym.expand(tmp, basic=False, deep=True, power_base=False, power_exp=False, mul=False, log=False, multinomial=True) tmp = tmp.subs(rule_trunc) gA_collected = collect(expand(tmp).subs(rule_trunc), eps) if verbose: print('completed gA collected') tmp = gB.subs(ruleB) tmp = sym.expand(tmp, basic=False, deep=True, power_base=False, power_exp=False, mul=False, log=False, multinomial=True) tmp = tmp.subs(rule_trunc) gB_collected = collect(expand(tmp).subs(rule_trunc), eps) if verbose: print('completed gB collected') tmp = iA.subs(ruleA) tmp = sym.expand(tmp, basic=False, deep=True, power_base=False, power_exp=False, mul=False, log=False, multinomial=True) tmp = tmp.subs(rule_trunc) iA_collected = collect(expand(tmp).subs(rule_trunc), eps) if verbose: print('completed iA collected') tmp = iB.subs(ruleB) tmp = sym.expand(tmp, basic=False, deep=True, power_base=False, power_exp=False, mul=False, log=False, multinomial=True) tmp = tmp.subs(rule_trunc) iB_collected = collect(expand(tmp).subs(rule_trunc), eps) if verbose: print('completed iB collected') obj.g[key + '_epsA'] = 0 obj.g[key + '_epsB'] = 0 obj.i[key + '_epsA'] = 0 obj.i[key + '_epsB'] = 0 for j in range(obj.miter): obj.g[key + '_epsA'] += eps**j * gA_collected.coeff(eps, j) obj.g[key + '_epsB'] += eps**j * gB_collected.coeff(eps, j) obj.i[key + '_epsA'] += eps**j * iA_collected.coeff(eps, j) obj.i[key + '_epsB'] += eps**j * iB_collected.coeff(eps, j) return obj.g, obj.i
## cf. Wilf and Zeilberger (1992) x, y = symbols("x y") f2 = (x + y) ** 2 f2.expand() f3 = (x + y) ** 3 f3.expand() n = Symbol("n", positive=True, integer=True) k = Symbol("k", integer=True) # binomialpoly = implemented_function(Function('(x+y)^n'), lambda n: (x+y)**n) # binomialpoly_n = lambdify(n, binomialpoly(n)) binomialpoly = (x + y) ** n binomialpolyexpand = Sum(binomial(n, k) * x ** k * y ** (n - k), (k, 0, n)) binomialpoly.subs(n, 1) == binomialpolyexpand.subs(n, 1).doit() # True binomialpoly.subs(n, 2).expand() == binomialpolyexpand.subs(n, 2).doit() # True binomialpoly.subs(n, 3).expand() == binomialpolyexpand.subs(n, 3).doit() # True binomialpoly.subs(n, 4).expand() == binomialpolyexpand.subs(n, 4).doit() # True # rising factorial a, q = symbols("a q") # a_n = Product(a+k,(k,0,n-1)) a_n = rf(a, n) aq_n = Product(Rat(1) - a * q ** k, (k, 0, n - 1)) # e.g. aq_n.subs(n,3).doit() # Hermite polynomials HermiteF = factorial(n) * (Rat(-1)) ** k * (Rat(2) * x) ** (n - Rat(2) * k) / (factorial(n - Rat(2) * k) * factorial(k)) HermiteP = Sum(HermiteF, (k, 0, floor(n / 2)))
q = Sum(i**k * (1-z)**i, (i, 0, n-1)) u = s * (z-1)**(k+1) - n**k*z**n*(z-1)**k ''' t = ans.euler_maclaurin() (0**k/2 + z**(n - 1)*(n - 1)**k/2 + Integral(_x**k*z**_x, (_x, 0, n - 1)), Abs(-oo*0**k*k - 0**k*log(z)/12 + k*z**(n - 1)*(n - 1)**k/(12*(n - 1)) + z**(n - 1)*(n - 1)**k*log(z)/12)) ''' if 0: t = lambda _k = 0: ans.subs(k, _k).doit() print(ans) for i in range(5): print('S(k={k}, z, n) = {s}'.format(k=i, s=t(i))) elif 0: t = lambda _k = 0: q.subs(k, _k).doit() print(q) for i in range(5): print('S(k={k}, 1-z, n) = {s}'.format(k=i, s=t(i))) elif 0: #T d z = sum z**i C(i,d) {~i} ## test = k ## ans = test.subs(k,k-1) ## print(ans) ## assert ans == k-1 T = Sum(ff(i,k) * z**i, (i,0,n)) K = T - ff(n,k)*z**n + k*z* T.subs(k,k-1) #ans = K.doit() fail for i in range(1,5): a = K.subs(n,i).doit().expand(force=True) print(a)