Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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[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

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[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. Essentially I want to take a trace of an exponential of a matrix valued in forms. When I try running it with the forms I want, the run time takes far too long, certainly over an hour on my laptop. However, it's much shorter if I use forms with symbolic coefficients. What follows below is my attempt at computing the forms I want with symbols first, and then trying to set the coefficients at the end.
To start, I have

sage: M = Manifold(6, 'M', field='complex')
sage: U = M.open_subset('U')
sage: c_xyzXYZ.<x,y,z,X,Y,Z> = U.chart()
sage: eU = c_xyzXYZ.frame()
sage: var('x,y,z,X,Y,Z')

I create some symbolic differential forms in the following way,

sage: g = {(i, j, k): var("g_{}{}{}".format(i, j, k), latex_name="g_{{{}{}{}}}".format(i,
sage:...  j, k)) for i in range(9) for j in range[3, 4, 5] for k in range(3)}

sage: f = [M.diff_form(2, name='f_{}'.format(i)) for i in range(9)]
sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            f[i][eU, j, k] = g[(i, j, k)]

sage: F = [M.mixed_form(comp=[0, 0, f[i], 0, 0, 0, 0]) for i in range(9)]

Now I create a matrix valued in differential forms (I'm running the current beta version), define an identity matrix of mixed forms, define an exponential function (the usual .exp() for matrices doesn't seem to work when the matrix is valued in mixed forms)

sage: FF = Matrix([[F[0], F[1], F[2]], [F[3], F[4], F[5]], [F[6], F[7], F[8]]])
sage: G = FF.apply_map(lambda f: (I/2*pi)*f)

sage: I_00 = M.mixed_form(comp=[1, 0, 0, 0, 0, 0, 0])
sage: I_01 = M.mixed_form(comp=[0, 0, 0, 0, 0, 0, 0])
Id = matrix([[I_00, I_01, I_01], [I_01, I_00, I_01], [I_01, I_01, I_00]])

sage: EXP = lambda f: Id+f+(f^2).apply_map(lambda g: (1/2)*g)
sage:                            ...+(f^3).apply_map(lambda g: (1/6)*g)
sage:                            ...+(f^4).apply_map(lambda g: (1/24)*g)
sage:                            ...+(f^5).apply_map(lambda g: (1/120)*g)
sage:                            ...+(f^6).apply_map(lambda g: (1/720)*g)

Finally I compute the trace of the exponential of the matrix G,

sage: E = EXP(G)
sage: CH = E.trace()

Now if one trys to display CH in the frame eU, after about 10 minutes, this will print out some massive mixed form with coefficients being the g[(i, j, k)]. I now want to change the components of these forms to explicit forms. For simplicity of the example, lets just assume I want them all to be x^2.

sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            g[(i, j, k)] = x^2

Than I update the forms as suggested below,

sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            f[i][eU, j, k] = g[(i, j, k)]

If you trying displaying any f, it's clear that this has worked. However, when I try to write

sage: CH.display(eU)

all the components are still in terms of g_{}{}{}, they have not been changed to x^2. Is there some way to do this without having to run the computation of CH again? The whole point of this convoluted example was to avoid doing this computation with the functions I'm actually interested in, and only set them at the end.

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[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. Essentially I want to take a trace of an exponential of a matrix valued in forms. When I try running it with the forms I want, the run time takes far too long, certainly over an hour on my laptop. However, it's much shorter if I use forms with symbolic coefficients. What follows below is my attempt at computing the forms I want with symbols first, and then trying to set the coefficients at the end.
To start, I have

sage: M = Manifold(6, 'M', field='complex')
sage: U = M.open_subset('U')
sage: c_xyzXYZ.<x,y,z,X,Y,Z> = U.chart()
sage: eU = c_xyzXYZ.frame()
sage: var('x,y,z,X,Y,Z')

I create some symbolic differential forms in the following way,

sage: g = {(i, j, k): var("g_{}{}{}".format(i, j, k), latex_name="g_{{{}{}{}}}".format(i,
sage:...  j, k)) for i in range(9) for j in range[3, 4, 5] for k in range(3)}

sage: f = [M.diff_form(2, name='f_{}'.format(i)) for i in range(9)]
sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            f[i][eU, j, k] = g[(i, j, k)]

sage: F = [M.mixed_form(comp=[0, 0, f[i], 0, 0, 0, 0]) for i in range(9)]

Now I create a matrix valued in differential forms (I'm running the current beta version), define an identity matrix of mixed forms, define an exponential function (the usual .exp() for matrices doesn't seem to work when the matrix is valued in mixed forms)

sage: FF = Matrix([[F[0], F[1], F[2]], [F[3], F[4], F[5]], [F[6], F[7], F[8]]])
sage: G = FF.apply_map(lambda f: (I/2*pi)*f)

sage: I_00 = M.mixed_form(comp=[1, 0, 0, 0, 0, 0, 0])
sage: I_01 = M.mixed_form(comp=[0, 0, 0, 0, 0, 0, 0])
Id = matrix([[I_00, I_01, I_01], [I_01, I_00, I_01], [I_01, I_01, I_00]])

sage: EXP = lambda f: Id+f+(f^2).apply_map(lambda g: (1/2)*g)
sage:                            ...+(f^3).apply_map(lambda g: (1/6)*g)
sage:                            ...+(f^4).apply_map(lambda g: (1/24)*g)
sage:                            ...+(f^5).apply_map(lambda g: (1/120)*g)
sage:                            ...+(f^6).apply_map(lambda g: (1/720)*g)

Finally I compute the trace of the exponential of the matrix G,

sage: E = EXP(G)
sage: CH = E.trace()

Now if one trys to display CH in the frame eU, after about 10 minutes, this will print out some massive mixed form with coefficients being the g[(i, j, k)]. I now want to change the components of these forms to explicit forms. For simplicity of the example, lets just assume I want them all to be x^2.

sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            g[(i, j, k)] = x^2

Than I update the forms as suggested below,

sage: for i in range(9):
sage:    for j in [3, 4, 5]:
sage:        for k in range(3):
sage:            f[i][eU, j, k] = g[(i, j, k)]

If you trying displaying any f, it's clear that this has worked. However, when I try to write

sage: CH.display(eU)

all the components are still in terms of g_{}{}{}, they have not been changed to x^2. Is there some way to do this without having to run the computation of CH again? The whole point of this convoluted example was to avoid doing this computation with the functions I'm actually interested in, and only set them at the end.here: https://ask.sagemath.org/question/46993/update-how-to-changeset-variables/