# How to change/set 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 [eU, 2, 0] = g
d [eU, 3, 0] = g
d [eU, 2, 1] = g
d [eU, 3, 1] = g


I then want to set the variables to something specific like

g = x^2


but when I try to display this I get

d.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/469...

edit retag close merge delete

Sort by » oldest newest most voted

Well, in Python, when you write

g = 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 = 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 [eU, 2, 0] = g again:

sage: d [eU, 2, 0] = g
sage: d.display(eU)
d_00 = -x^2 dx/\dX

more

Thanks, 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.