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.Tue, 06 Aug 2019 17:03:16 +0200Substituting symbolic expressions for mixed differential forms.https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/I'm doing some computations with matrices differential forms, and the run times are extremely long. So in an attempt to
keep run times down, I'm attempting to do matrix computations with symbolic terms, and then later replacing them with the mixed forms I want.
However, there's the coercion from mixed forms into the symbolic ring, and this is presenting a problem for me.
As a toy example of what I'm trying to do, I have the set up (say)
Sage: p=1
Sage: q=2
Sage: M = Manifold((2*p*q), 'M', field='complex')
Sage: U = M.open_subset('U')
Sage: x = U.chart(names=tuple('x_%d' % i for i in range(2*p*q)))
Sage: eU = x.frame()
Sage: d = [M.diff_form(1) for i in range(q)]
Sage: for i in range(q):
Sage: d[i][i] = x[i]
Sage: D = [M.mixed_form(comp=([0, d[i], 0, 0, 0])) for i in range(q)]
Sage: c = {(i): var("c_{}".format(i)) for i in range(q)}
Sage: exp = 0
Sage: for i in range(q):
Sage: for j in range(q):
Sage: exp = exp + c[i]*c[j]
Now, in my actual project I will have many of these variables, so it's not feasible to manually type them into one substitution at once, so I need to do it with a for loop, so I try
Sage: for i in range(q):
Sage: exp.subs({c[i] : D[i]})
But this is where I get the coercion error.
As mentioned, the actual computations I'm dealing with involve large matrices of mixed forms, and the run times would be WAY shorter if I can do it all in symbolic matrices, and then transfer the expressions I get out (which come from taking a trace). How might I be able to achieve this? Mon, 05 Aug 2019 17:29:55 +0200https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/Answer by rburing for <p>I'm doing some computations with matrices differential forms, and the run times are extremely long. So in an attempt to
keep run times down, I'm attempting to do matrix computations with symbolic terms, and then later replacing them with the mixed forms I want. <br>
However, there's the coercion from mixed forms into the symbolic ring, and this is presenting a problem for me. <br>
As a toy example of what I'm trying to do, I have the set up (say) </p>
<pre><code>Sage: p=1
Sage: q=2
Sage: M = Manifold((2*p*q), 'M', field='complex')
Sage: U = M.open_subset('U')
Sage: x = U.chart(names=tuple('x_%d' % i for i in range(2*p*q)))
Sage: eU = x.frame()
Sage: d = [M.diff_form(1) for i in range(q)]
Sage: for i in range(q):
Sage: d[i][i] = x[i]
Sage: D = [M.mixed_form(comp=([0, d[i], 0, 0, 0])) for i in range(q)]
Sage: c = {(i): var("c_{}".format(i)) for i in range(q)}
Sage: exp = 0
Sage: for i in range(q):
Sage: for j in range(q):
Sage: exp = exp + c[i]*c[j]
</code></pre>
<p>Now, in my actual project I will have many of these variables, so it's not feasible to manually type them into one substitution at once, so I need to do it with a for loop, so I try</p>
<pre><code>Sage: for i in range(q):
Sage: exp.subs({c[i] : D[i]})
</code></pre>
<p>But this is where I get the coercion error. <br>
As mentioned, the actual computations I'm dealing with involve large matrices of mixed forms, and the run times would be WAY shorter if I can do it all in symbolic matrices, and then transfer the expressions I get out (which come from taking a trace). How might I be able to achieve this? </p>
https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?answer=47400#post-id-47400There is a problem with your approach because the $c_i$ commute while $D_i$ anticommute.
After your definition of `D`, you can use [noncommutative polynomials](http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/plural.html) instead:
A = FreeAlgebra(SR, 2, names='c_')
c = A.gens()
exp = 0
for i in range(q):
for j in range(q):
exp = exp + c[i]*c[j]
print exp
print exp.subs({c[i] : D[i] for i in range(q)}).display(eU)
Output:
c_0^2 + c_0*c_1 + c_1*c_0 + c_1^2
[0] + [0] + [0] + [0] + [0]
The result is zero due to anticommutativity.Mon, 05 Aug 2019 23:22:45 +0200https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?answer=47400#post-id-47400Comment by sum8tion for <p>There is a problem with your approach because the $c_i$ commute while $D_i$ anticommute.</p>
<p>After your definition of <code>D</code>, you can use <a href="http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/plural.html">noncommutative polynomials</a> instead:</p>
<pre><code>A = FreeAlgebra(SR, 2, names='c_')
c = A.gens()
exp = 0
for i in range(q):
for j in range(q):
exp = exp + c[i]*c[j]
print exp
print exp.subs({c[i] : D[i] for i in range(q)}).display(eU)
</code></pre>
<p>Output:</p>
<pre><code>c_0^2 + c_0*c_1 + c_1*c_0 + c_1^2
[0] + [0] + [0] + [0] + [0]
</code></pre>
<p>The result is zero due to anticommutativity.</p>
https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?comment=47412#post-id-47412Thanks, this seems to do the trick. I'm just wondering though, how can I have it so that the c's are indexed by two numbers?Tue, 06 Aug 2019 16:53:48 +0200https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?comment=47412#post-id-47412Comment by rburing for <p>There is a problem with your approach because the $c_i$ commute while $D_i$ anticommute.</p>
<p>After your definition of <code>D</code>, you can use <a href="http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/plural.html">noncommutative polynomials</a> instead:</p>
<pre><code>A = FreeAlgebra(SR, 2, names='c_')
c = A.gens()
exp = 0
for i in range(q):
for j in range(q):
exp = exp + c[i]*c[j]
print exp
print exp.subs({c[i] : D[i] for i in range(q)}).display(eU)
</code></pre>
<p>Output:</p>
<pre><code>c_0^2 + c_0*c_1 + c_1*c_0 + c_1^2
[0] + [0] + [0] + [0] + [0]
</code></pre>
<p>The result is zero due to anticommutativity.</p>
https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?comment=47413#post-id-47413You can do something like this:
indices = [(i,j) for i in range(q) for j in range(q)]
c_names = ['c_{}_{}'.format(i,j) for (i,j) in indices]
A = FreeAlgebra(SR, names=c_names)
c = dict(zip(indices, A.gens()))
Then `c[0,0]` is the generator `c_0_0` of `A`.Tue, 06 Aug 2019 17:03:16 +0200https://ask.sagemath.org/question/47397/substituting-symbolic-expressions-for-mixed-differential-forms/?comment=47413#post-id-47413