# 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?

edit retag close merge delete

Sort by » oldest newest most voted

There is a problem with your approach because the $c_i$ commute while $D_i$ anticommute.

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.

more

Thanks, 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?

( 2019-08-06 09:53:48 -0600 )edit

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

( 2019-08-06 10:03:16 -0600 )edit