Ask Your Question
1

Setting relations between variables

asked 2018-09-03 02:51:12 +0100

mousestar gravatar image

I have a question about doing this in SAGE.

I have a set of variables S_ijkl for i, j, k l in [1..4] and I would like to set the following relations without having to manually type everything in:

S_ijkl = S_jikl = S_ijlk = S_jilk,

how can I go about doing this?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2018-09-03 09:15:13 +0100

slelievre gravatar image

One way would be to define a function like this:

def S(i, j, k, l):
    return SR.var('S_{}_{}_{}_{}'.format(*(sorted([i, j]) + sorted([k, l]))))

which would return the variables in the order you want.

sage: S(3, 4, 8, 1)
S_3_4_1_8
sage: S(4, 3, 8, 1)
S_3_4_1_8
sage: S(3, 4, 1, 8)
S_3_4_1_8
sage: S(4, 3, 1, 8)
S_3_4_1_8
edit flag offensive delete link more
2

answered 2018-09-03 09:15:55 +0100

Sébastien gravatar image

You may create a dictionary of variables:

import itertools
S = {}
for i,j,k,l in itertools.product([1..4], repeat=4):
    S[(i,j,k,l)] = var('S_{}{}{}{}'.format(i,j,k,l))

which then allows you to create a loop to set your relations.

Alternatively, you may create only the variable S_ijkl in the loop above and add a line for the others:

for i,j,k,l in itertools.product([1..4], repeat=4):
    if j>i or l>k:
        continue
    S[(i,j,k,l)] = var('S_{}{}{}{}'.format(i,j,k,l))
    S[(j,i,k,l)] = S[(i,j,l,k)] = S[(j,i,l,k)]
edit flag offensive delete link more

Comments

Nice solutions! In your second loop, I think the last line needs to refer to the variable defined on the previous line:

for i, j, k, l in itertools.product([1 .. 4], repeat=4):
    if j > i or l > k:
        continue
    S[(i, j, k, l)] = var('S_{}{}{}{}'.format(i, j, k, l))
    S[(j, i, k, l)] = S[(i, j, l, k)] = S[(j, i, l, k)] = S[(i, j, k, l)]
slelievre gravatar imageslelievre ( 2018-09-03 10:07:08 +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: 2018-09-03 02:50:25 +0100

Seen: 418 times

Last updated: Sep 03 '18