Ask Your Question
0

Recursive computation of a symbolic sum

asked 2025-08-21 22:47:30 +0200

Emm gravatar image

updated 2025-08-22 13:57:20 +0200

Hello,

I would like to program the computation of the following symbolic sum, defined recursively, where n and k are positive integers : $$M_1(n)=n+1,\quad M_k(n)=\sum_{u=0}^nM_{k-1}(n-u) \quad (k>1).$$

I tried the following

   def M(kk,nn) :
    if kk==1:
        SS=nn+1
    else :
        SS=0
        for uz in [0..nn]:
            SS+=M(kk-1,nn-uz)
    return SS

but in return of

n = var('n')
assume(n,'integer')
M(3,n)

I receive a long error message the more interesting par of which seems to be "TypeError: cannot evaluate symbolic expression to a numeric value".

Is there a way to deal with this issue?

NB. This is quite a theoretical general question with a specific example since, with this specific example, the sum can indeed be computed through a closed formula involving Stirling numbers.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2025-08-22 19:04:49 +0200

Max Alekseyev gravatar image

The issue is that the list [0..nn] with symbolic nn is not valid (a list must have an concrete integer not symbolic length). Since in the else clause you want to define SS as the sum with symbolic limits, you best shot would be using symbolic sum function - like this:

def M(kk,nn) :
    if kk==1:
        SS=nn+1
    else :
        uz = var(f'uz{kk}')
        SS = sum(M(kk-1,nn-uz), uz, 0, nn)
    return SS

Note that we give variable uz an index kk to have different summation variables in the recursive sums.

edit flag offensive delete link more

Comments

Great!! Many thanks Max.

Emm gravatar imageEmm ( 2025-08-22 19:43:44 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-08-21 22:47:30 +0200

Seen: 991 times

Last updated: Aug 22