Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I tried:

a = var(','.join(f'a{k}' for k in range(10)))

def Z(n):
    if n == 0:
        return 1
    return 1/n * sum([ a[k-1] * Z(n-k) for k in [1..n] ]).expand()

Then:

for n in [0..5]:
    print(f'Z({n}) = {Z(n)}')

delivers:

Z(0) = 1
Z(1) = a0
Z(2) = 1/2*a0^2 + 1/2*a1
Z(3) = 1/6*a0^3 + 1/2*a0*a1 + 1/3*a2
Z(4) = 1/24*a0^4 + 1/4*a0^2*a1 + 1/8*a1^2 + 1/3*a0*a2 + 1/4*a3
Z(5) = 1/120*a0^5 + 1/12*a0^3*a1 + 1/8*a0*a1^2 + 1/6*a0^2*a2 + 1/6*a1*a2 + 1/4*a0*a3 + 1/5*a4

Which are the differences? First, the $a$-variables are a constant w.r.t. the loop, so just put it outside. Then the used sum method is the sum for the elements of a given list. Please compare:

sage: sum([1, 4, 6, 10, 111])
132
sage: var('k,n');
sage: sum(binomial(n, k), k, 1, n)
2^n - 1

The first sum is the sum of a list, known in the moment of applying sum on it. It is a python-specific function. The second sum is a symbolic sum, an invention of sage & CO - and we need the summation to be done w.r.t. specific variables k,n to be introduced as such in advance, and this sum makes sense only for "known symbolic formulas".