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.Fri, 21 Jun 2019 15:43:31 +0200How to change/set variables?https://ask.sagemath.org/question/46945/how-to-changeset-variables/I created a list of formal variables that I want to work with under addition, multiplication etc. in the following way:
g = list(var('g_%d' % i) for i in rang(4))
Specifically I'm working with differential forms
M = Manifold(4, 'M', field='complex')
U = M.open_subset('U')
c_xyXY<x, y, X, Y>=U.chart()
eU = c_xyXY.frame()
d = [[M.diff_form(2, name='d_{}{}'.format(j, i)) for i in range(2)] for j in range(2)]
d[0][0] [eU, 2, 0] = g[0]
d[0][1] [eU, 3, 0] = g[1]
d[1][0] [eU, 2, 1] = g[2]
d[1][1] [eU, 3, 1] = g[3]
I then want to set the variables to something specific like
g[0] = x^2
but when I try to display this I get
d[0][0].display(eU)
d_00 = -g_0dx/\dX
What I want is
d_00 = -x^2dx/\dX
EDIT: The question as originally asked has been sufficiently answered by eric_g below. However I'm still having difficulty with a more complicated version of the same set-up, which I present here:
https://ask.sagemath.org/question/46993/update-how-to-changeset-variables/Wed, 19 Jun 2019 19:57:49 +0200https://ask.sagemath.org/question/46945/how-to-changeset-variables/Answer by eric_g for <p>I created a list of formal variables that I want to work with under addition, multiplication etc. in the following way:</p>
<pre><code>g = list(var('g_%d' % i) for i in rang(4))
</code></pre>
<p>Specifically I'm working with differential forms</p>
<pre><code>M = Manifold(4, 'M', field='complex')
U = M.open_subset('U')
c_xyXY<x, y, X, Y>=U.chart()
eU = c_xyXY.frame()
d = [[M.diff_form(2, name='d_{}{}'.format(j, i)) for i in range(2)] for j in range(2)]
d[0][0] [eU, 2, 0] = g[0]
d[0][1] [eU, 3, 0] = g[1]
d[1][0] [eU, 2, 1] = g[2]
d[1][1] [eU, 3, 1] = g[3]
</code></pre>
<p>I then want to set the variables to something specific like </p>
<pre><code>g[0] = x^2
</code></pre>
<p>but when I try to display this I get</p>
<pre><code>d[0][0].display(eU)
d_00 = -g_0dx/\dX
</code></pre>
<p>What I want is</p>
<pre><code>d_00 = -x^2dx/\dX
</code></pre>
<p>EDIT: The question as originally asked has been sufficiently answered by eric_g below. However I'm still having difficulty with a more complicated version of the same set-up, which I present here:
<a href="https://ask.sagemath.org/question/46993/update-how-to-changeset-variables/">https://ask.sagemath.org/question/469...</a></p>
https://ask.sagemath.org/question/46945/how-to-changeset-variables/?answer=46954#post-id-46954Well, in Python, when you write
g[0] = x^2
this means that the element 0 in the list `g`, which was initialized to `g_0`, is replaced by `x^2`. The symbolic variable `g_0` remains unchanged; simply it is no longer the element 0 of `g`:
sage: g = list(var('g_%d' % i) for i in range(4))
sage: g
[g_0, g_1, g_2, g_3]
sage: g[0] = x^2
sage: g
[x^2, g_1, g_2, g_3]
sage: g_0
g_0
In particular, `g_0` is still there in `d_00`. If you want to replace it by `x^2`, you have to run `d[0][0] [eU, 2, 0] = g[0]` again:
sage: d[0][0] [eU, 2, 0] = g[0]
sage: d[0][0].display(eU)
d_00 = -x^2 dx/\dXThu, 20 Jun 2019 14:53:04 +0200https://ask.sagemath.org/question/46945/how-to-changeset-variables/?answer=46954#post-id-46954Comment by sum8tion for <p>Well, in Python, when you write</p>
<pre><code>g[0] = x^2
</code></pre>
<p>this means that the element 0 in the list <code>g</code>, which was initialized to <code>g_0</code>, is replaced by <code>x^2</code>. The symbolic variable <code>g_0</code> remains unchanged; simply it is no longer the element 0 of <code>g</code>:</p>
<pre><code>sage: g = list(var('g_%d' % i) for i in range(4))
sage: g
[g_0, g_1, g_2, g_3]
sage: g[0] = x^2
sage: g
[x^2, g_1, g_2, g_3]
sage: g_0
g_0
</code></pre>
<p>In particular, <code>g_0</code> is still there in <code>d_00</code>. If you want to replace it by <code>x^2</code>, you have to run <code>d[0][0] [eU, 2, 0] = g[0]</code> again:</p>
<pre><code>sage: d[0][0] [eU, 2, 0] = g[0]
sage: d[0][0].display(eU)
d_00 = -x^2 dx/\dX
</code></pre>
https://ask.sagemath.org/question/46945/how-to-changeset-variables/?comment=46963#post-id-46963Thanks, this seems to work for this example. However, I'm still having trouble with a more complicated version of the same task. I'll edit the question to add it in.Fri, 21 Jun 2019 15:43:31 +0200https://ask.sagemath.org/question/46945/how-to-changeset-variables/?comment=46963#post-id-46963