# Conversion of Differential Forms to a manipulable symbolic expression

[Sage 5.4.1] Hi, I got the following code that does total differentiation off a Sage blog:

sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: F = DifferentialForms(U)
sage: f = F(x^2 + y + sin(z)); f
(x^2 + y + sin(z))
sage: g = f.diff(); g
cos(z)*dz + 2*x*dx + dy


How do I convert g to a symbolic form where dx, dy, and dz are also symbolic variables? I need to assign values to all variables via a for loop. Also, is it possible for g to be a 3x3 matrix? Thanks much, mahlon

edit retag close merge delete

Sort by » oldest newest most voted

It's a terrible hack, but perhaps this will help you as a workaround:

sage: x, y, z = var('x, y, z')
sage: U = CoordinatePatch((x, y, z))
sage: F = DifferentialForms(U)
sage: f = F(x^2 + y + sin(z)); f
(x^2 + y + sin(z))
sage: g = f.diff(); g
cos(z)*dz + 2*x*dx + dy


That's what you have already. Now, we convert g to a string and parse it as a new symbolic expression:

sage: t = SR(str(g))
sage: t.operands()
[2*dx*x, dz*cos(z), dy]
sage: t.variables()
(dx, dy, dz, x, z)

more

Better to limit the string conversion to the absolute minimum. The following should be slightly better:

sum([ b*SR.var(''.join([str(F.gen(i)) for i in a])) for a,b in g._components.iteritems()])


Be careful: the dx,dy,dz do NOT commute (they anticommute: they're wedged), so with this code dx/\dy gets encoded as a symbolic variable "dxdy".

( 2015-03-14 20:31:02 -0600 )edit

Thanks much. However, I have found a better way as follows:

sage: t, x3, dx3 = var('t x3 dx3')
sage: a = matrix([[x3+t,2,x3],[t^2,5,x3],[t,x3,x3^2]])
sage: b = derivative(a,t); b
[  1   0   0]
[2*t   0   0]
[  1   0   0]
sage: c = derivative(a,x3); c
[   1    0    1]
[   0    0    1]
[   0    1 2*x3]
sage: d = b+dx3*c; d
[ dx3 + 1        0      dx3]
[     2*t        0      dx3]
[       1      dx3 2*dx3*x3]

more