简体   繁体   English

如何计算写成乘积的数学家的幂和?

[英]How to calculate Mathologer's power sums written as products?

I'm a beginning Python programmer with no experience with sympy - just for fun.我是一个没有 sympy 经验的 Python 程序员 - 只是为了好玩。 I'm trying to generate the formulas for sums of powers of integers as described in the latest Mathologer video (Power Sums).我正在尝试生成整数幂和的公式,如最新的数学家视频(Power Sums)中所述。 The following program generates the set of equations that follow.以下程序生成以下方程组。 While correct, I would like the polynomials in the numerator to be factored where appropriate and individual terms to actually be summed (ie, a common denominator).虽然是正确的,但我希望在适当的情况下将分子中的多项式考虑在内,并将各个项实际相加(即公分母)。 I've tried a lot of "simplifiers", but nothing seems to work.我尝试了很多“简化器”,但似乎没有任何效果。 Any ideas?有任何想法吗?

Program :程序

from sympy import S, Matrix, Symbol, simplify, pprint
import math

def bn(n, k):

    sign = n%2+k%2  # used to negate odd (0 based) diagonals
    n = n+1         # shift triangle up and left
    bc = math.factorial(n)//math.factorial(k)//math.factorial(n-k)
    if sign == 1: bc = -bc
    return bc


if __name__ == "__main__": 

    N = 5

    n = Symbol('n')
    v = Matrix([n**i for i in range(1,N+1)])

    M = [[bn(i,k) for k in range(0,i+1)]+(N-i-1)*[S(0)] for i in range(0,N)]
    eqs = simplify(Matrix(M).inv()*v)
    print(eqs.__repr__())
    # pprint(eqs, use_unicode=True)      

Output (note; had to replace double *'s with ^): Output (注意;必须用 ^ 替换双 *):

Matrix([[                           n],
        [                 n*(n + 1)/2],
        [       n*(2*n^2 + 3*n + 1)/6],
        [       n^2*(n^2 + 2*n + 1)/4],
        [n^5/5 + n^4/2 + n^3/3 - n/30]])

Does this do what you want?这是做你想做的吗?

In [8]: eqs.applyfunc(factor)                                                                                                                                 
Out[8]: 
⎡                 n                  ⎤
⎢                                    ⎥
⎢             n⋅(n + 1)              ⎥
⎢             ─────────              ⎥
⎢                 2                  ⎥
⎢                                    ⎥
⎢        n⋅(n + 1)⋅(2⋅n + 1)         ⎥
⎢        ───────────────────         ⎥
⎢                 6                  ⎥
⎢                                    ⎥
⎢             2        2             ⎥
⎢            n ⋅(n + 1)              ⎥
⎢            ───────────             ⎥
⎢                 4                  ⎥
⎢                                    ⎥
⎢                    ⎛   2          ⎞⎥
⎢n⋅(n + 1)⋅(2⋅n + 1)⋅⎝3⋅n  + 3⋅n - 1⎠⎥
⎢────────────────────────────────────⎥
⎣                 30                 ⎦

Note that as an alternative, and to check the Mathologer formulas, you may also calculate these sums as:请注意,作为替代方案,要检查 Mathologer 公式,您还可以将这些总和计算为:

from sympy import symbols, Sum, factor, expand, Poly

j, n = symbols("j n", integer=True, positive=True)
for k in range(11):
    print(k, ":", factor(Sum(j**k, (j, 1, n)).doit()))

giving:给予:

 0 : n
 1 : n*(n + 1)/2
 2 : n*(n + 1)*(2*n + 1)/6
 3 : n**2*(n + 1)**2/4
 4 : n*(n + 1)*(2*n + 1)*(3*n**2 + 3*n - 1)/30
 5 : n**2*(n + 1)**2*(2*n**2 + 2*n - 1)/12
 6 : n*(n + 1)*(2*n + 1)*(3*n**4 + 6*n**3 - 3*n + 1)/42
 7 : n**2*(n + 1)**2*(3*n**4 + 6*n**3 - n**2 - 4*n + 2)/24
 8 : n*(n + 1)*(2*n + 1)*(5*n**6 + 15*n**5 + 5*n**4 - 15*n**3 - n**2 + 9*n - 3)/90
 9 : n**2*(n + 1)**2*(n**2 + n - 1)*(2*n**4 + 4*n**3 - n**2 - 3*n + 3)/20
10 : n*(n + 1)*(2*n + 1)*(n**2 + n - 1)*(3*n**6 + 9*n**5 + 2*n**4 - 11*n**3 + 3*n**2 + 10*n - 5)/66

If you substitute factor() by expand() , you get all the powers separated:如果将factor()替换为expand() ,则将所有权力分开:

 0 : n
 1 : n**2/2 + n/2
 2 : n**3/3 + n**2/2 + n/6
 3 : n**4/4 + n**3/2 + n**2/4
 4 : n**5/5 + n**4/2 + n**3/3 - n/30
 5 : n**6/6 + n**5/2 + 5*n**4/12 - n**2/12
 6 : n**7/7 + n**6/2 + n**5/2 - n**3/6 + n/42
 7 : n**8/8 + n**7/2 + 7*n**6/12 - 7*n**4/24 + n**2/12
 8 : n**9/9 + n**8/2 + 2*n**7/3 - 7*n**5/15 + 2*n**3/9 - n/30
 9 : n**10/10 + n**9/2 + 3*n**8/4 - 7*n**6/10 + n**4/2 - 3*n**2/20
10 : n**11/11 + n**10/2 + 5*n**9/6 - n**7 + n**5 - n**3/2 + 5*n/66

With Poly(Sum(j**k, (j, 1, n)).doit()).all_coeffs() you can get just the coefficients:使用Poly(Sum(j**k, (j, 1, n)).doit()).all_coeffs()您可以获得系数:

 0 : [1, 0]
 1 : [1/2, 1/2, 0]
 2 : [1/3, 1/2, 1/6, 0]
 3 : [1/4, 1/2, 1/4, 0, 0]
 4 : [1/5, 1/2, 1/3, 0, -1/30, 0]
 5 : [1/6, 1/2, 5/12, 0, -1/12, 0, 0]
 6 : [1/7, 1/2, 1/2, 0, -1/6, 0, 1/42, 0]
 7 : [1/8, 1/2, 7/12, 0, -7/24, 0, 1/12, 0, 0]
 8 : [1/9, 1/2, 2/3, 0, -7/15, 0, 2/9, 0, -1/30, 0]
 9 : [1/10, 1/2, 3/4, 0, -7/10, 0, 1/2, 0, -3/20, 0, 0]
10 : [1/11, 1/2, 5/6, 0, -1, 0, 1, 0, -1/2, 0, 5/66, 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM