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.Sun, 15 Mar 2015 02:31:02 +0100Conversion of Differential Forms to a manipulable symbolic expressionhttps://ask.sagemath.org/question/10090/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
Sat, 04 May 2013 23:04:16 +0200https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/Answer by burcin for <p>[Sage 5.4.1] Hi, I got the following code that does total differentiation off a Sage blog:</p>
<pre><code>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
</code></pre>
<p>How do I convert <code>g</code> to a symbolic form where <code>dx</code>, <code>dy</code>, and <code>dz</code> are also symbolic variables? I need to assign values to all variables via a for loop. Also, is it possible for <code>g</code> to be a 3x3 matrix? Thanks much, mahlon</p>
https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?answer=14884#post-id-14884It'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)
Sun, 05 May 2013 09:36:02 +0200https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?answer=14884#post-id-14884Comment by nbruin for <p>It's a terrible hack, but perhaps this will help you as a workaround:</p>
<pre><code>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
</code></pre>
<p>That's what you have already. Now, we convert <code>g</code> to a string and parse it as a new symbolic expression:</p>
<pre><code>sage: t = SR(str(g))
sage: t.operands()
[2*dx*x, dz*cos(z), dy]
sage: t.variables()
(dx, dy, dz, x, z)
</code></pre>
https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?comment=26197#post-id-26197Better 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".Sun, 15 Mar 2015 02:31:02 +0100https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?comment=26197#post-id-26197Answer by Dionysus for <p>[Sage 5.4.1] Hi, I got the following code that does total differentiation off a Sage blog:</p>
<pre><code>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
</code></pre>
<p>How do I convert <code>g</code> to a symbolic form where <code>dx</code>, <code>dy</code>, and <code>dz</code> are also symbolic variables? I need to assign values to all variables via a for loop. Also, is it possible for <code>g</code> to be a 3x3 matrix? Thanks much, mahlon</p>
https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?answer=14887#post-id-14887Thanks 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]
Tue, 07 May 2013 02:08:09 +0200https://ask.sagemath.org/question/10090/conversion-of-differential-forms-to-a-manipulable-symbolic-expression/?answer=14887#post-id-14887