Ask Your Question
0

Conversion of Differential Forms to a manipulable symbolic expression

asked 2013-05-04 16:04:16 -0500

Dionysus gravatar image

updated 2013-06-09 01:12:22 -0500

[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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-05-05 02:36:02 -0500

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)
edit flag offensive delete link more

Comments

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".

nbruin gravatar imagenbruin ( 2015-03-14 20:31:02 -0500 )edit
0

answered 2013-05-06 19:08:09 -0500

Dionysus gravatar image

updated 2013-06-09 01:19:43 -0500

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]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-05-04 16:04:16 -0500

Seen: 450 times

Last updated: Jun 09 '13