Ask Your Question

# Setting relations between variables

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

## 2 Answers

Sort by » oldest newest most voted

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

more

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)]

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)]


## Your Answer

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

Add Answer

## Stats

Asked: 2018-09-03 02:50:25 +0200

Seen: 113 times

Last updated: Sep 03 '18