ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 03 Sep 2018 10:07:08 +0200Setting relations between variableshttps://ask.sagemath.org/question/43559/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?Mon, 03 Sep 2018 02:50:25 +0200https://ask.sagemath.org/question/43559/setting-relations-between-variables/Answer by Sébastien for <p>I have a question about doing this in SAGE.</p>
<p>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:</p>
<p>S_ijkl = S_jikl = S_ijlk = S_jilk,</p>
<p>how can I go about doing this?</p>
https://ask.sagemath.org/question/43559/setting-relations-between-variables/?answer=43562#post-id-43562You 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)]
Mon, 03 Sep 2018 09:15:55 +0200https://ask.sagemath.org/question/43559/setting-relations-between-variables/?answer=43562#post-id-43562Comment by slelievre for <p>You may create a dictionary of variables:</p>
<pre><code>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))
</code></pre>
<p>which then allows you to create a loop to set your relations.</p>
<p>Alternatively, you may create only the variable <code>S_ijkl</code> in the loop above and add a line for the others:</p>
<pre><code>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)]
</code></pre>
https://ask.sagemath.org/question/43559/setting-relations-between-variables/?comment=43563#post-id-43563Nice 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)]Mon, 03 Sep 2018 10:07:08 +0200https://ask.sagemath.org/question/43559/setting-relations-between-variables/?comment=43563#post-id-43563Answer by slelievre for <p>I have a question about doing this in SAGE.</p>
<p>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:</p>
<p>S_ijkl = S_jikl = S_ijlk = S_jilk,</p>
<p>how can I go about doing this?</p>
https://ask.sagemath.org/question/43559/setting-relations-between-variables/?answer=43561#post-id-43561One 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
Mon, 03 Sep 2018 09:15:13 +0200https://ask.sagemath.org/question/43559/setting-relations-between-variables/?answer=43561#post-id-43561