Ask Your Question
1

List of multi indices and their sums

asked 2021-01-06 21:44:52 +0100

anonymous user

Anonymous

updated 2021-01-06 21:45:32 +0100

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-01-06 23:33:19 +0100

slelievre gravatar image

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
edit flag offensive delete link more

Comments

Many thanks.

phcosta gravatar imagephcosta ( 2021-01-07 02:31:58 +0100 )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: 2021-01-06 21:44:52 +0100

Seen: 170 times

Last updated: Jan 06 '21