ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 18 Jun 2019 10:13:08 -0500How to define multiple objects with a for loop?http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/ While this question can be answered in greater generality, I'm specifically writing a program in which I need to systematically define and label a bunch of differential forms.
Suppose I have the set up
M = manifold(2, 'M')
U = M.open_subset('U')
c_xy.<x,y> = U.chart()
eU = c_xy.frame()
Now I want to have a bunch of differential forms indexed by 2 numbers (as they will be going into a matrix).
Instead of writing them all out by hand as
a_00 = M.diff_form(1, name='a_00')
a_01 = M.diff_form(1, name='a_01')
...
a_33 = M.diff_form(1, name='a_33')
Is there any way I can write a for loop to build these for me? I've seen a similar question asked here:
https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/
but I'm not sure how to extend to the 2-index case. Also, the linked example works for symbols, but I don't know if this can be made to work for defining a list of objects like differential forms. Thu, 06 Jun 2019 19:50:09 -0500http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/Answer by eric_g for <p>While this question can be answered in greater generality, I'm specifically writing a program in which I need to systematically define and label a bunch of differential forms. <br>
Suppose I have the set up</p>
<pre><code>M = manifold(2, 'M')
U = M.open_subset('U')
c_xy.<x,y> = U.chart()
eU = c_xy.frame()
</code></pre>
<p>Now I want to have a bunch of differential forms indexed by 2 numbers (as they will be going into a matrix). <br>
Instead of writing them all out by hand as</p>
<pre><code>a_00 = M.diff_form(1, name='a_00')
a_01 = M.diff_form(1, name='a_01')
...
a_33 = M.diff_form(1, name='a_33')
</code></pre>
<p>Is there any way I can write a for loop to build these for me? I've seen a similar question asked here:
<a href="https://ask.sagemath.org/question/7925/a-list-of-symbolic-variables/">https://ask.sagemath.org/question/792...</a>
but I'm not sure how to extend to the 2-index case. Also, the linked example works for symbols, but I don't know if this can be made to work for defining a list of objects like differential forms. </p>
http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/?answer=46845#post-id-46845I would advise to create a nested list of objects:
a = [[M.diff_form(1, name='a_{}{}'.format(i, j),
latex_name='a_{{{}{}}}'.format(i, j))
for j in range(4)] for i in range(4)]
Then you access to `a_ij` via `a[i][j]`. For instance (first line is input, second line is output):
a[1][3]
1-form a_13 on the 2-dimensional differentiable manifold M
and
latex(a[1][3])
a_{13}
It is possible to dynamically inject names like `a_13` in the global namespace, by adding new entries in the dictionary `globals()`, but this is generally not recommended.Fri, 07 Jun 2019 01:25:30 -0500http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/?answer=46845#post-id-46845Comment by sum8tion for <p>I would advise to create a nested list of objects:</p>
<pre><code>a = [[M.diff_form(1, name='a_{}{}'.format(i, j),
latex_name='a_{{{}{}}}'.format(i, j))
for j in range(4)] for i in range(4)]
</code></pre>
<p>Then you access to <code>a_ij</code> via <code>a[i][j]</code>. For instance (first line is input, second line is output):</p>
<pre><code>a[1][3]
1-form a_13 on the 2-dimensional differentiable manifold M
</code></pre>
<p>and </p>
<pre><code>latex(a[1][3])
a_{13}
</code></pre>
<p>It is possible to dynamically inject names like <code>a_13</code> in the global namespace, by adding new entries in the dictionary <code>globals()</code>, but this is generally not recommended.</p>
http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/?comment=46936#post-id-46936and what if I also wanted to define a list of variables as in the question I linked to above, but having tow indices?
That is, generating a collection of formal variables g[0][0], g[0][1], ..., g[2][1], g[2][2]?Tue, 18 Jun 2019 10:13:08 -0500http://ask.sagemath.org/question/46844/how-to-define-multiple-objects-with-a-for-loop/?comment=46936#post-id-46936