ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 19 Jun 2019 13:24:37 -0500How to make formal variables with more than one index?https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/I'd like to make formal variables that can be multiplied, added etc. but have them labelled by two indices.
Here:
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/
it's done for variables with one index, and I asked a similar question about defining differential forms with multi-labels here:
https://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/
I see no clear way of merging these two approaches. Wed, 19 Jun 2019 08:47:11 -0500https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/Answer by Juanjo for <p>I'd like to make formal variables that can be multiplied, added etc. but have them labelled by two indices. <br>Here:
<a href="https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/">https://ask.sagemath.org/question/792...</a>
it's done for variables with one index, and I asked a similar question about defining differential forms with multi-labels here:
<a href="https://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/">https://ask.sagemath.org/question/468...</a>
I see no clear way of merging these two approaches. </p>
https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/?answer=46944#post-id-46944<p>Consider the following code:</p>
<pre><code>sage: m, n = 3, 2
sage: g = matrix(m+1, n+1, [var("g_{}{}".format(i,j),
....: latex_name="g_{{{}{}}}".format(i,j))
....: for i in [0..m] for j in [0..n]])
sage: g
[g_00 g_01 g_02]
[g_10 g_11 g_12]
[g_20 g_21 g_22]
[g_30 g_31 g_32]
</code></pre>
<p>It creates a matrix <code>g</code> of symbolic variables <code>g_00</code>, <code>g_01</code>, etc. You can access each variable either by <code>g_ij</code> or <code>g[i,j]</code>. </p>
**Edit**. To cope with a more general case you can use a dictionary, for example:
sage: m, n, p = 3, 2, 2
sage: g = {(i,j,k): var("g_{}{}{}".format(i,j,k),
....: latex_name="g_{{{}{}{}}}".format(i,j,k))
....: for i in [0..m] for j in [0..n] for k in [0..p]}
sage: g
{(0, 0, 0): g_000,
(0, 0, 1): g_001,
(0, 0, 2): g_002,
(0, 1, 0): g_010,
(0, 1, 1): g_011,
.................................
(3, 2, 0): g_320,
(3, 2, 1): g_321,
(3, 2, 2): g_322}
Variables can be accessed by `g_ijk` or `g[(i,j,k)]`.Wed, 19 Jun 2019 12:40:59 -0500https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/?answer=46944#post-id-46944Comment by sum8tion for <p>Consider the following code:</p>
<pre><code>sage: m, n = 3, 2
sage: g = matrix(m+1, n+1, [var("g_{}{}".format(i,j),
....: latex_name="g_{{{}{}}}".format(i,j))
....: for i in [0..m] for j in [0..n]])
sage: g
[g_00 g_01 g_02]
[g_10 g_11 g_12]
[g_20 g_21 g_22]
[g_30 g_31 g_32]
</code></pre>
<p>It creates a matrix <code>g</code> of symbolic variables <code>g_00</code>, <code>g_01</code>, etc. You can access each variable either by <code>g_ij</code> or <code>g[i,j]</code>. </p>
<p><strong>Edit</strong>. To cope with a more general case you can use a dictionary, for example:</p>
<pre><code>sage: m, n, p = 3, 2, 2
sage: g = {(i,j,k): var("g_{}{}{}".format(i,j,k),
....: latex_name="g_{{{}{}{}}}".format(i,j,k))
....: for i in [0..m] for j in [0..n] for k in [0..p]}
sage: g
{(0, 0, 0): g_000,
(0, 0, 1): g_001,
(0, 0, 2): g_002,
(0, 1, 0): g_010,
(0, 1, 1): g_011,
.................................
(3, 2, 0): g_320,
(3, 2, 1): g_321,
(3, 2, 2): g_322}
</code></pre>
<p>Variables can be accessed by <code>g_ijk</code> or <code>g[(i,j,k)]</code>.</p>
https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/?comment=46946#post-id-46946Thanks! What if I wanted to make variables with 3 indices? It doesn't seem like the matrix approach would work in that case.Wed, 19 Jun 2019 13:24:37 -0500https://ask.sagemath.org/question/46942/how-to-make-formal-variables-with-more-than-one-index/?comment=46946#post-id-46946