# List of multi indices and their sums

If I define:

def p(t):
return SR.var(('p' + '_{}'*len(t)).format(*[str(i) for i in t]))


I can get generic multi indices like

p((2, 4, 1, 3,9,8,0,9,6,1))
p_2_4_1_3_9_8_0_9_6_1


So far, I would like to know if I can get something like

p_24139_80961


and also how can I sum the variables "p" in the second block of indices, for example for j in [1..10] I would like to sum all

p_24139_809j1


Any suggestions would be great.

edit retag close merge delete

Sort by ยป oldest newest most voted

There is not much to adapt to get what you want.

Regarding the underscores, just use a different string to format.

Also, no need to use str(...) with .format string formatting, it comes for free.

If you know you always expect 10 entries from 0 to 9 in some order:

def p(t):
return SR.var('p_{}{}{}{}{}_{}{}{}{}{}'.format(*t))


Usage:

sage: p((2, 4, 1, 3, 9, 8, 0, 9, 6, 1))
p_24139_80961


The double parentheses can be avoided using *args.

def p(*t):
return SR.var('p_{}{}{}{}{}_{}{}{}{}{}'.format(*t))


Usage:

sage: p(2, 4, 1, 3, 9, 8, 0, 9, 6, 1)
p_24139_80961


Summing:

sage: sum(p(2, 4, 1, 3, 9, 8, 0, 9, j, 1) for j in (0 .. 9))
p_24139_80901 + p_24139_80911 + p_24139_80921 + p_24139_80931
+ p_24139_80941 + p_24139_80951 + p_24139_80961
+ p_24139_80971 + p_24139_80981 + p_24139_80991

more

Many thanks.

( 2021-01-07 02:31:58 +0200 )edit