a field-theory motivated approach to computer algebra

# The Kaluza-Klein example from section 2.5 of hep-th/0701238

This example shows how to use split_index in a somewhat more complicated setting. We first declare the indices that we will use.
{\mu,\nu,\rho,\sigma,\kappa,\lambda,\eta,\chi#}::Indices(full, position=independent); {m,n,p,q,r,s,t,u,v,w,x,y,z,m#}::Indices(subspace, position=independent, parent=full);
$$\displaystyle{}\text{Attached property Indices(position=independent) to }\left[\mu, \nu, \rho, \sigma, \kappa, \lambda, \eta, \chi\#\right].$$
$$\displaystyle{}\text{Attached property Indices(position=independent) to }\left[m, n, p, q, r, s, t, u, v, w, x, y, z, m\#\right].$$
Note the appearance of parent=full. This indicates that the indices in the second set span a subspace of the indices in the first set. Also note that we have declared the indices as position=independent, to prevent Cadabra from automatically raising or lowering them when canonicalising (as this does not really help us here). The remaining declarations are standard,
\partial{#}::PartialDerivative. g_{\mu\nu}::Metric. g^{\mu\nu}::InverseMetric. g_{\mu? \nu?}::Symmetric. g^{\mu? \nu?}::Symmetric. h_{m n}::Metric. h^{m n}::InverseMetric. \delta^{\mu?}_{\nu?}::KroneckerDelta. \delta_{\mu?}^{\nu?}::KroneckerDelta. F_{m n}::AntiSymmetric.
We will want to expand the Riemann tensor in terms of the metric. The following two substitution rules do the conversion from Riemann tensor to Christoffel symbol and from Christoffel symbol to metric (a library with common substitution rules like these is in preparation).
RtoG:= R^{\lambda?}_{\mu?\nu?\kappa?} -> - \partial_{\kappa?}{ \Gamma^{\lambda?}_{\mu?\nu?} } + \partial_{\nu?}{ \Gamma^{\lambda?}_{\mu?\kappa?} } - \Gamma^{\eta}_{\mu?\nu?} \Gamma^{\lambda?}_{\kappa?\eta} + \Gamma^{\eta}_{\mu?\kappa?} \Gamma^{\lambda?}_{\nu?\eta}; Gtog:= \Gamma^{\lambda?}_{\mu?\nu?} -> (1/2) * g^{\lambda?\kappa} ( \partial_{\nu?}{ g_{\kappa\mu?} } + \partial_{\mu?}{ g_{\kappa\nu?} } - \partial_{\kappa}{ g_{\mu?\nu?} } );
$$\displaystyle{}R^{\lambda?}\,_{\mu? \nu? \kappa?} \rightarrow -\partial_{\kappa?}{\Gamma^{\lambda?}\,_{\mu? \nu?}}+\partial_{\nu?}{\Gamma^{\lambda?}\,_{\mu? \kappa?}}-\Gamma^{\eta}\,_{\mu? \nu?} \Gamma^{\lambda?}\,_{\kappa? \eta}+\Gamma^{\eta}\,_{\mu? \kappa?} \Gamma^{\lambda?}\,_{\nu? \eta}$$
$$\displaystyle{}\Gamma^{\lambda?}\,_{\mu? \nu?} \rightarrow \frac{1}{2}g^{\lambda? \kappa} \left(\partial_{\nu?}{g_{\kappa \mu?}}+\partial_{\mu?}{g_{\kappa \nu?}}-\partial_{\kappa}{g_{\mu? \nu?}}\right)$$
In this example we want to compute the Kaluza-Klein reduction of the $R_{m 4 n 4}$ component of the Riemann tensor. So we enter this component and do the substitution that takes the Riemann tensor to metrics. After each substitution, we distribute products over sums. We also apply the product rule to distribute derivatives over factors in a product.
todo:= g_{m1 m} R^{m1}_{4 n 4} + g_{4 m} R^{4}_{4 n 4};
$$\displaystyle{}g_{m1 m} R^{m1}\,_{4 n 4}+g_{4 m} R^{4}\,_{4 n 4}$$
substitute(_, RtoG) substitute(_, Gtog) distribute(_) product_rule(_) distribute(_) sort_product(_);
$$\displaystyle{} - \frac{1}{2}\partial_{n}{g_{\kappa 4}} \partial_{4}{g^{m1 \kappa}} g_{m1 m} - \frac{1}{2}\partial_{4 n}{g_{\kappa 4}} g_{m1 m} g^{m1 \kappa} - \frac{1}{2}\partial_{4}{g^{m1 \kappa}} \partial_{4}{g_{\kappa n}} g_{m1 m} - \frac{1}{2}\partial_{4 4}{g_{\kappa n}} g_{m1 m} g^{m1 \kappa}+\frac{1}{2}\partial_{\kappa}{g_{4 n}} \partial_{4}{g^{m1 \kappa}} g_{m1 m}+\frac{1}{2}\partial_{4 \kappa}{g_{4 n}} g_{m1 m} g^{m1 \kappa}+\partial_{n}{g^{m1 \kappa}} \partial_{4}{g_{\kappa 4}} g_{m1 m}+\partial_{n 4}{g_{\kappa 4}} g_{m1 m} g^{m1 \kappa} - \frac{1}{2}\partial_{\kappa}{g_{4 4}} \partial_{n}{g^{m1 \kappa}} g_{m1 m} - \frac{1}{2}\partial_{n \kappa}{g_{4 4}} g_{m1 m} g^{m1 \kappa} - \frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{n}{g_{\kappa 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{n}{g_{\kappa 4}} \partial_{4}{g_{\mu \eta}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{n}{g_{\kappa 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{4}{g_{\kappa n}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{4}{g_{\mu \eta}} \partial_{4}{g_{\kappa n}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{4}{g_{\kappa n}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{\kappa}{g_{4 n}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{4}\partial_{\kappa}{g_{4 n}} \partial_{4}{g_{\mu \eta}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{\kappa}{g_{4 n}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}% +\frac{1}{2}\partial_{\eta}{g_{\mu n}} \partial_{4}{g_{\kappa 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{2}\partial_{n}{g_{\mu \eta}} \partial_{4}{g_{\kappa 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{2}\partial_{\mu}{g_{n \eta}} \partial_{4}{g_{\kappa 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{\eta}{g_{\mu n}} \partial_{\kappa}{g_{4 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{4}\partial_{\kappa}{g_{4 4}} \partial_{n}{g_{\mu \eta}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu}+\frac{1}{4}\partial_{\mu}{g_{n \eta}} \partial_{\kappa}{g_{4 4}} g_{m1 m} g^{\eta \kappa} g^{m1 \mu} - \frac{1}{2}\partial_{n}{g_{\kappa 4}} \partial_{4}{g^{4 \kappa}} g_{4 m} - \frac{1}{2}\partial_{4 n}{g_{\kappa 4}} g_{4 m} g^{4 \kappa} - \frac{1}{2}\partial_{4}{g^{4 \kappa}} \partial_{4}{g_{\kappa n}} g_{4 m} - \frac{1}{2}\partial_{4 4}{g_{\kappa n}} g_{4 m} g^{4 \kappa}+\frac{1}{2}\partial_{\kappa}{g_{4 n}} \partial_{4}{g^{4 \kappa}} g_{4 m}+\frac{1}{2}\partial_{4 \kappa}{g_{4 n}} g_{4 m} g^{4 \kappa}+\partial_{n}{g^{4 \kappa}} \partial_{4}{g_{\kappa 4}} g_{4 m}+\partial_{n 4}{g_{\kappa 4}} g_{4 m} g^{4 \kappa} - \frac{1}{2}\partial_{\kappa}{g_{4 4}} \partial_{n}{g^{4 \kappa}} g_{4 m} - \frac{1}{2}\partial_{n \kappa}{g_{4 4}} g_{4 m} g^{4 \kappa} - \frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{n}{g_{\kappa 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{4}\partial_{n}{g_{\kappa 4}} \partial_{4}{g_{\mu \eta}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{n}{g_{\kappa 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{4}{g_{\kappa n}} g_{4 m} g^{\eta \kappa} g^{4 \mu}% - \frac{1}{4}\partial_{4}{g_{\mu \eta}} \partial_{4}{g_{\kappa n}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{4}{g_{\kappa n}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{4}\partial_{\eta}{g_{\mu 4}} \partial_{\kappa}{g_{4 n}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{4}\partial_{\kappa}{g_{4 n}} \partial_{4}{g_{\mu \eta}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{4}\partial_{\mu}{g_{4 \eta}} \partial_{\kappa}{g_{4 n}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{2}\partial_{\eta}{g_{\mu n}} \partial_{4}{g_{\kappa 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{2}\partial_{n}{g_{\mu \eta}} \partial_{4}{g_{\kappa 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{2}\partial_{\mu}{g_{n \eta}} \partial_{4}{g_{\kappa 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{4}\partial_{\eta}{g_{\mu n}} \partial_{\kappa}{g_{4 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu} - \frac{1}{4}\partial_{\kappa}{g_{4 4}} \partial_{n}{g_{\mu \eta}} g_{4 m} g^{\eta \kappa} g^{4 \mu}+\frac{1}{4}\partial_{\mu}{g_{n \eta}} \partial_{\kappa}{g_{4 4}} g_{4 m} g^{\eta \kappa} g^{4 \mu}$$
We then split the greek indices into roman indices and the 4-th direction. Notice the use of repeat=True which will keep doing this substitution until all greek indices have been split. After that we again have to set all derivatives in the 4-th direction to zero.
split_index(_, $\mu, m1, 4$, repeat=True) substitute(_, $\partial_{4}{A??} -> 0$, repeat=True) substitute(_, $\partial_{4 m?}{A??} -> 0$, repeat=True) substitute(_, $\partial_{m? 4}{A??} -> 0$, repeat=True) canonicalise(_);
$$\displaystyle{} - \frac{1}{2}\partial_{m1}{g_{4 4}} \partial_{n}{g^{m1 p}} g_{m p} - \frac{1}{2}\partial_{n m1}{g_{4 4}} g_{m p} g^{m1 p} - \frac{1}{4}\partial_{n}{g_{4 m1}} \partial_{p}{g_{4 q}} g_{m r} g^{m1 p} g^{q r} - \frac{1}{2}\partial_{m1}{g_{4 4}} \partial_{n}{g_{4 p}} g_{m q} g^{m1 p} g^{4 q} - \frac{1}{4}\partial_{m1}{g_{4 p}} \partial_{n}{g_{4 4}} g_{m q} g^{4 m1} g^{p q} - \frac{1}{2}\partial_{n}{g_{4 4}} \partial_{m1}{g_{4 4}} g_{m p} g^{4 m1} g^{4 p}+\frac{1}{4}\partial_{m1}{g_{4 p}} \partial_{n}{g_{4 q}} g_{m r} g^{m1 r} g^{p q}+\frac{1}{4}\partial_{m1}{g_{4 p}} \partial_{n}{g_{4 4}} g_{m q} g^{4 p} g^{m1 q}+\frac{1}{4}\partial_{m1}{g_{4 4}} \partial_{n}{g_{4 p}} g_{m q} g^{4 p} g^{m1 q}+\frac{1}{4}\partial_{n}{g_{4 4}} \partial_{m1}{g_{4 4}} g_{m p} g^{4 4} g^{m1 p}+\frac{1}{4}\partial_{m1}{g_{4 p}} \partial_{q}{g_{n 4}} g_{m r} g^{m1 q} g^{p r}+\frac{1}{4}\partial_{m1}{g_{4 4}} \partial_{p}{g_{n 4}} g_{m q} g^{m1 p} g^{4 q} - \frac{1}{4}\partial_{m1}{g_{n 4}} \partial_{p}{g_{4 q}} g_{m r} g^{m1 q} g^{p r} - \frac{1}{4}\partial_{m1}{g_{4 4}} \partial_{p}{g_{n 4}} g_{m q} g^{4 p} g^{m1 q} - \frac{1}{4}\partial_{m1}{g_{n p}} \partial_{q}{g_{4 4}} g_{m r} g^{m1 q} g^{p r} - \frac{1}{4}\partial_{m1}{g_{n 4}} \partial_{p}{g_{4 4}} g_{m q} g^{m1 p} g^{4 q} - \frac{1}{4}\partial_{m1}{g_{4 4}} \partial_{n}{g_{p q}} g_{m r} g^{m1 p} g^{q r} - \frac{1}{4}\partial_{m1}{g_{4 4}} \partial_{n}{g_{4 p}} g_{m q} g^{4 m1} g^{p q}+\frac{1}{4}\partial_{m1}{g_{n p}} \partial_{q}{g_{4 4}} g_{m r} g^{m1 r} g^{p q}% +\frac{1}{4}\partial_{m1}{g_{n 4}} \partial_{p}{g_{4 4}} g_{m q} g^{4 p} g^{m1 q} - \frac{1}{2}\partial_{p}{g_{4 4}} \partial_{n}{g^{4 p}} g_{m 4} - \frac{1}{2}\partial_{n p}{g_{4 4}} g_{m 4} g^{4 p} - \frac{1}{4}\partial_{n}{g_{4 p}} \partial_{q}{g_{4 r}} g_{m 4} g^{p q} g^{4 r} - \frac{1}{2}\partial_{p}{g_{4 4}} \partial_{n}{g_{4 q}} g_{m 4} g^{p q} g^{4 4} - \frac{1}{4}\partial_{p}{g_{4 q}} \partial_{n}{g_{4 4}} g_{m 4} g^{4 p} g^{4 q} - \frac{1}{2}\partial_{n}{g_{4 4}} \partial_{p}{g_{4 4}} g_{m 4} g^{4 p} g^{4 4}+\frac{1}{4}\partial_{p}{g_{4 q}} \partial_{n}{g_{4 r}} g_{m 4} g^{q r} g^{4 p}+\frac{1}{4}\partial_{p}{g_{4 q}} \partial_{n}{g_{4 4}} g_{m 4} g^{4 q} g^{4 p}+\frac{1}{4}\partial_{n}{g_{4 4}} \partial_{p}{g_{4 4}} g_{m 4} g^{4 4} g^{4 p}+\frac{1}{4}\partial_{p}{g_{4 q}} \partial_{r}{g_{n 4}} g_{m 4} g^{p r} g^{4 q}+\frac{1}{4}\partial_{p}{g_{4 4}} \partial_{q}{g_{n 4}} g_{m 4} g^{p q} g^{4 4} - \frac{1}{4}\partial_{p}{g_{n 4}} \partial_{q}{g_{4 r}} g_{m 4} g^{p r} g^{4 q} - \frac{1}{4}\partial_{p}{g_{4 4}} \partial_{q}{g_{n 4}} g_{m 4} g^{4 p} g^{4 q} - \frac{1}{4}\partial_{p}{g_{n q}} \partial_{r}{g_{4 4}} g_{m 4} g^{p r} g^{4 q} - \frac{1}{4}\partial_{p}{g_{n 4}} \partial_{q}{g_{4 4}} g_{m 4} g^{p q} g^{4 4} - \frac{1}{4}\partial_{p}{g_{4 4}} \partial_{n}{g_{q r}} g_{m 4} g^{p q} g^{4 r}+\frac{1}{4}\partial_{p}{g_{n q}} \partial_{r}{g_{4 4}} g_{m 4} g^{q r} g^{4 p}+\frac{1}{4}\partial_{p}{g_{n 4}} \partial_{q}{g_{4 4}} g_{m 4} g^{4 p} g^{4 q}$$
Now comes the ansatz of the metric in terms of the Kaluza-Klein gauge field $A_\mu$ and the scalar $\phi$. We substitute this with the lines below. The output is not particularly enlightening...
substitute(_, $g_{4 4} -> \phi$ ) substitute(_, $g_{m 4} -> \phi A_{m}$ ) substitute(_, $g_{4 m} -> \phi A_{m}$ ) substitute(_, $g_{m n} -> \phi**{-1} h_{m n} + \phi A_{m} A_{n}$ ) substitute(_, $g^{4 4} -> \phi**{-1} + \phi A_{m} h^{m n} A_{n}$ ) substitute(_, $g^{m 4} -> - \phi h^{m n} A_{n}$ ) substitute(_, $g^{4 m} -> - \phi h^{m n} A_{n}$ ) substitute(_, $g^{m n} -> \phi h^{m n}$ );
$$\displaystyle{} - \frac{1}{2}\partial_{m1}{\phi} \partial_{n}\left(\phi h^{m1 p}\right) \left({\phi}^{-1} h_{m p}+\phi A_{m} A_{p}\right) - \frac{1}{2}\partial_{n m1}{\phi} \left({\phi}^{-1} h_{m p}+\phi A_{m} A_{p}\right) \phi h^{m1 p} - \frac{1}{4}\partial_{n}\left(\phi A_{m1}\right) \partial_{p}\left(\phi A_{q}\right) \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 p} \phi h^{q r}+\frac{1}{2}\partial_{m1}{\phi} \partial_{n}\left(\phi A_{p}\right) \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{m1 p} \phi h^{q r} A_{r}+\frac{1}{4}\partial_{m1}\left(\phi A_{p}\right) \partial_{n}{\phi} \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{m1 r} A_{r} \phi h^{p q} - \frac{1}{2}\partial_{n}{\phi} \partial_{m1}{\phi} \left({\phi}^{-1} h_{m p}+\phi A_{m} A_{p}\right) \phi h^{m1 q} A_{q} \phi h^{p r} A_{r}+\frac{1}{4}\partial_{m1}\left(\phi A_{p}\right) \partial_{n}\left(\phi A_{q}\right) \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 r} \phi h^{p q} - \frac{1}{4}\partial_{m1}\left(\phi A_{p}\right) \partial_{n}{\phi} \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{p r} A_{r} \phi h^{m1 q} - \frac{1}{4}\partial_{m1}{\phi} \partial_{n}\left(\phi A_{p}\right) \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{p r} A_{r} \phi h^{m1 q}+\frac{1}{4}\partial_{n}{\phi} \partial_{m1}{\phi} \left({\phi}^{-1} h_{m p}+\phi A_{m} A_{p}\right) \left({\phi}^{-1}+\phi A_{q} h^{q r} A_{r}\right) \phi h^{m1 p}+\frac{1}{4}\partial_{m1}\left(\phi A_{p}\right) \partial_{q}\left(\phi A_{n}\right) \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 q} \phi h^{p r} - \frac{1}{4}\partial_{m1}{\phi} \partial_{p}\left(\phi A_{n}\right) \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{m1 p} \phi h^{q r} A_{r} - \frac{1}{4}\partial_{m1}\left(\phi A_{n}\right) \partial_{p}\left(\phi A_{q}\right) \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 q} \phi h^{p r}+\frac{1}{4}\partial_{m1}{\phi} \partial_{p}\left(\phi A_{n}\right) \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{p r} A_{r} \phi h^{m1 q} - \frac{1}{4}\partial_{m1}\left({\phi}^{-1} h_{n p}+\phi A_{n} A_{p}\right) \partial_{q}{\phi} \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 q} \phi h^{p r}+\frac{1}{4}\partial_{m1}\left(\phi A_{n}\right) \partial_{p}{\phi} \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{m1 p} \phi h^{q r} A_{r} - \frac{1}{4}\partial_{m1}{\phi} \partial_{n}\left({\phi}^{-1} h_{p q}+\phi A_{p} A_{q}\right) \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 p} \phi h^{q r}+\frac{1}{4}\partial_{m1}{\phi} \partial_{n}\left(\phi A_{p}\right) \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{m1 r} A_{r} \phi h^{p q}+\frac{1}{4}\partial_{m1}\left({\phi}^{-1} h_{n p}+\phi A_{n} A_{p}\right) \partial_{q}{\phi} \left({\phi}^{-1} h_{m r}+\phi A_{m} A_{r}\right) \phi h^{m1 r} \phi h^{p q}% - \frac{1}{4}\partial_{m1}\left(\phi A_{n}\right) \partial_{p}{\phi} \left({\phi}^{-1} h_{m q}+\phi A_{m} A_{q}\right) \phi h^{p r} A_{r} \phi h^{m1 q}+\frac{1}{2}\partial_{p}{\phi} \partial_{n}\left(\phi h^{p q} A_{q}\right) \phi A_{m}+\frac{1}{2}\partial_{n p}{\phi} \phi A_{m} \phi h^{p q} A_{q}+\frac{1}{4}\partial_{n}\left(\phi A_{p}\right) \partial_{q}\left(\phi A_{r}\right) \phi A_{m} \phi h^{p q} \phi h^{r s} A_{s} - \frac{1}{2}\partial_{p}{\phi} \partial_{n}\left(\phi A_{q}\right) \phi A_{m} \phi h^{p q} \left({\phi}^{-1}+\phi A_{r} h^{r s} A_{s}\right) - \frac{1}{4}\partial_{p}\left(\phi A_{q}\right) \partial_{n}{\phi} \phi A_{m} \phi h^{p r} A_{r} \phi h^{q s} A_{s}+\frac{1}{2}\partial_{n}{\phi} \partial_{p}{\phi} \phi A_{m} \phi h^{p s} A_{s} \left({\phi}^{-1}+\phi A_{q} h^{q r} A_{r}\right) - \frac{1}{4}\partial_{p}\left(\phi A_{q}\right) \partial_{n}\left(\phi A_{r}\right) \phi A_{m} \phi h^{q r} \phi h^{p s} A_{s}+\frac{1}{4}\partial_{p}\left(\phi A_{q}\right) \partial_{n}{\phi} \phi A_{m} \phi h^{q r} A_{r} \phi h^{p s} A_{s} - \frac{1}{4}\partial_{n}{\phi} \partial_{p}{\phi} \phi A_{m} \left({\phi}^{-1}+\phi A_{q} h^{q r} A_{r}\right) \phi h^{p s} A_{s} - \frac{1}{4}\partial_{p}\left(\phi A_{q}\right) \partial_{r}\left(\phi A_{n}\right) \phi A_{m} \phi h^{p r} \phi h^{q s} A_{s}+\frac{1}{4}\partial_{p}{\phi} \partial_{q}\left(\phi A_{n}\right) \phi A_{m} \phi h^{p q} \left({\phi}^{-1}+\phi A_{r} h^{r s} A_{s}\right)+\frac{1}{4}\partial_{p}\left(\phi A_{n}\right) \partial_{q}\left(\phi A_{r}\right) \phi A_{m} \phi h^{p r} \phi h^{q s} A_{s} - \frac{1}{4}\partial_{p}{\phi} \partial_{q}\left(\phi A_{n}\right) \phi A_{m} \phi h^{p r} A_{r} \phi h^{q s} A_{s}+\frac{1}{4}\partial_{p}\left({\phi}^{-1} h_{n q}+\phi A_{n} A_{q}\right) \partial_{r}{\phi} \phi A_{m} \phi h^{p r} \phi h^{q s} A_{s} - \frac{1}{4}\partial_{p}\left(\phi A_{n}\right) \partial_{q}{\phi} \phi A_{m} \phi h^{p q} \left({\phi}^{-1}+\phi A_{r} h^{r s} A_{s}\right)+\frac{1}{4}\partial_{p}{\phi} \partial_{n}\left({\phi}^{-1} h_{q r}+\phi A_{q} A_{r}\right) \phi A_{m} \phi h^{p q} \phi h^{r s} A_{s} - \frac{1}{4}\partial_{p}\left({\phi}^{-1} h_{n q}+\phi A_{n} A_{q}\right) \partial_{r}{\phi} \phi A_{m} \phi h^{q r} \phi h^{p s} A_{s}+\frac{1}{4}\partial_{p}\left(\phi A_{n}\right) \partial_{q}{\phi} \phi A_{m} \phi h^{p r} A_{r} \phi h^{q s} A_{s}$$
We now need to distribute derivatives over sums and use the product rule. This has to be done a few times, so we wrap this in a converge block which applies the operations until the result no longer changes. The output of those operations is quite large, so we suppress it (by not writing semi-colons after the function calls or after the converge block).
converge(todo): distribute(_) product_rule(_) canonicalise(_)
We then convert derivatives of the inverse metric to derivatives of the metric, and cleanup contractions which lead to Kronecker deltas.
substitute(_, $\partial_{p}{h^{n m}} h_{q m} -> - \partial_{p}{h_{q m}} h^{n m}$ ) collect_factors(_) sort_product(_) converge(todo): substitute(_, $h_{m1 m2} h^{m3 m2} -> \delta_{m1}^{m3}$, repeat=True ) eliminate_kronecker(_) canonicalise(_) ;
$$\displaystyle{} - \frac{1}{4}\partial_{m}{\phi} \partial_{n}{\phi} {\phi}^{-1}+\frac{1}{4}\partial_{m1}{\phi} \partial_{n}{h_{m p}} h^{m1 p} - \frac{3}{4}A_{m} A_{m1} \partial_{n}{\phi} \partial_{p}{\phi} \phi h^{m1 p} - \frac{1}{2}A_{m} A_{m1} \partial_{p}{\phi} \partial_{n}{h^{m1 p}} {\phi}^{2} - \frac{1}{2}\partial_{m n}{\phi} - \frac{1}{2}A_{m} A_{m1} \partial_{n p}{\phi} {\phi}^{2} h^{m1 p} - \frac{1}{4}A_{m1} \partial_{p}{A_{m}} \partial_{n}{\phi} {\phi}^{2} h^{m1 p} - \frac{1}{2}A_{m} \partial_{n}{A_{m1}} \partial_{p}{\phi} {\phi}^{2} h^{m1 p} - \frac{1}{4}\partial_{n}{A_{m1}} \partial_{p}{A_{m}} {\phi}^{3} h^{m1 p} - \frac{1}{4}A_{m} A_{m1} \partial_{n}{A_{p}} \partial_{q}{A_{r}} {\phi}^{5} h^{m1 r} h^{p q}+\frac{3}{4}A_{m} A_{m1} \partial_{n}{\phi} \partial_{q}{\phi} \phi h^{m1 q}+\frac{1}{2}A_{m} \partial_{n}{A_{p}} \partial_{q}{\phi} {\phi}^{2} h^{p q}+\frac{1}{4}A_{m1} \partial_{r}{A_{m}} \partial_{n}{\phi} {\phi}^{2} h^{m1 r}+\frac{1}{4}A_{m1} A_{q} \partial_{m}{\phi} \partial_{n}{\phi} \phi h^{m1 q}+\frac{1}{4}A_{p} \partial_{n}{A_{q}} \partial_{m}{\phi} {\phi}^{2} h^{p q}+\frac{1}{4}A_{p} \partial_{m}{A_{q}} \partial_{n}{\phi} {\phi}^{2} h^{p q}+\frac{1}{4}\partial_{m}{A_{m1}} \partial_{n}{A_{q}} {\phi}^{3} h^{m1 q}+\frac{1}{4}A_{m} A_{m1} \partial_{n}{A_{p}} \partial_{q}{A_{r}} {\phi}^{5} h^{m1 q} h^{p r} - \frac{1}{4}A_{m1} A_{r} \partial_{m}{\phi} \partial_{n}{\phi} \phi h^{m1 r}% - \frac{1}{4}A_{p} \partial_{m}{A_{r}} \partial_{n}{\phi} {\phi}^{2} h^{p r} - \frac{1}{4}A_{p} A_{r} \partial_{m}{\phi} \partial_{n}{\phi} \phi h^{p r} - \frac{1}{4}A_{p} \partial_{n}{A_{r}} \partial_{m}{\phi} {\phi}^{2} h^{p r}+\frac{1}{4}A_{q} A_{r} \partial_{m}{\phi} \partial_{n}{\phi} \phi h^{q r}+\frac{1}{4}A_{m} A_{n} \partial_{m1}{\phi} \partial_{p}{\phi} \phi h^{m1 p}+\frac{1}{4}\partial_{m1}{A_{m}} \partial_{p}{A_{n}} {\phi}^{3} h^{m1 p}+\frac{1}{4}A_{m} A_{m1} \partial_{p}{A_{n}} \partial_{q}{A_{r}} {\phi}^{5} h^{m1 r} h^{p q} - \frac{1}{4}A_{n} A_{m1} \partial_{m}{\phi} \partial_{q}{\phi} \phi h^{m1 q} - \frac{1}{4}A_{n} \partial_{m}{A_{m1}} \partial_{q}{\phi} {\phi}^{2} h^{m1 q} - \frac{1}{4}A_{m1} \partial_{q}{A_{n}} \partial_{m}{\phi} {\phi}^{2} h^{m1 q} - \frac{1}{4}\partial_{m}{A_{m1}} \partial_{q}{A_{n}} {\phi}^{3} h^{m1 q} - \frac{1}{4}A_{m} A_{m1} \partial_{p}{A_{n}} \partial_{q}{A_{r}} {\phi}^{5} h^{m1 q} h^{p r}+\frac{1}{4}A_{p} \partial_{r}{A_{n}} \partial_{m}{\phi} {\phi}^{2} h^{p r}+\frac{1}{4}\partial_{m1}{\phi} \partial_{q}{\phi} {\phi}^{-1} h_{m n} h^{m1 q} - \frac{1}{4}\partial_{m1}{\phi} \partial_{q}{h_{m n}} h^{m1 q}+\frac{1}{4}A_{m} A_{n} \partial_{p}{\phi} \partial_{q}{\phi} \phi h^{p q} - \frac{1}{4}A_{m} A_{m1} \partial_{p}{\phi} \partial_{q}{h_{n r}} {\phi}^{2} h^{m1 r} h^{p q} - \frac{1}{4}A_{m} A_{n} \partial_{m1}{\phi} \partial_{q}{\phi} \phi h^{m1 q} - \frac{1}{4}A_{m} A_{m1} \partial_{p}{\phi} \partial_{n}{h_{q r}} {\phi}^{2} h^{m1 q} h^{p r} - \frac{1}{4}A_{m1} \partial_{n}{A_{m}} \partial_{p}{\phi} {\phi}^{2} h^{m1 p}% +\frac{1}{4}A_{m1} \partial_{n}{A_{m}} \partial_{r}{\phi} {\phi}^{2} h^{m1 r}+\frac{1}{4}\partial_{p}{\phi} \partial_{m}{h_{n q}} h^{p q} - \frac{1}{4}A_{m} A_{q} \partial_{n}{\phi} \partial_{r}{\phi} \phi h^{q r}+\frac{1}{4}A_{m} A_{m1} \partial_{p}{\phi} \partial_{q}{h_{n r}} {\phi}^{2} h^{m1 q} h^{p r}+\frac{1}{4}A_{n} A_{m1} \partial_{m}{\phi} \partial_{p}{\phi} \phi h^{m1 p}+\frac{1}{4}A_{p} \partial_{m}{A_{n}} \partial_{q}{\phi} {\phi}^{2} h^{p q}+\frac{1}{4}A_{n} \partial_{m}{A_{p}} \partial_{q}{\phi} {\phi}^{2} h^{p q} - \frac{1}{4}A_{p} \partial_{m}{A_{n}} \partial_{r}{\phi} {\phi}^{2} h^{p r}+\frac{1}{2}A_{m} A_{p} \partial_{q}{\phi} \partial_{n}{h^{p q}} {\phi}^{2}+\frac{1}{2}A_{m} A_{p} \partial_{n q}{\phi} {\phi}^{2} h^{p q}+\frac{1}{4}A_{m} A_{p} \partial_{n}{A_{q}} \partial_{r}{A_{s}} {\phi}^{5} h^{p s} h^{q r}+\frac{1}{4}A_{m} A_{p} \partial_{n}{\phi} \partial_{s}{\phi} \phi h^{p s} - \frac{1}{4}A_{m} A_{p} \partial_{n}{A_{q}} \partial_{r}{A_{s}} {\phi}^{5} h^{p r} h^{q s} - \frac{1}{4}A_{m} A_{p} \partial_{q}{A_{n}} \partial_{r}{A_{s}} {\phi}^{5} h^{p s} h^{q r}+\frac{1}{4}A_{m} A_{p} \partial_{q}{A_{n}} \partial_{r}{A_{s}} {\phi}^{5} h^{p r} h^{q s} - \frac{1}{4}A_{m} A_{n} \partial_{q}{\phi} \partial_{r}{\phi} \phi h^{q r}+\frac{1}{4}A_{m} A_{p} \partial_{q}{\phi} \partial_{r}{h_{n s}} {\phi}^{2} h^{p s} h^{q r} - \frac{1}{4}A_{m} A_{p} \partial_{n}{\phi} \partial_{r}{\phi} \phi h^{p r}+\frac{1}{4}A_{m} A_{p} \partial_{q}{\phi} \partial_{n}{h_{r s}} {\phi}^{2} h^{p r} h^{q s}+\frac{1}{4}A_{m} A_{r} \partial_{n}{\phi} \partial_{s}{\phi} \phi h^{r s}% - \frac{1}{4}A_{m} A_{p} \partial_{q}{\phi} \partial_{r}{h_{n s}} {\phi}^{2} h^{p r} h^{q s}$$
This is almost the finaly result, but one would normally write it in terms of the field strength, not the gauge potential. This can be done with one further substitution and some simplification, to get the result
substitute(_, $\partial_{n}{A_{m}} -> 1/2*\partial_{n}{A_{m}} + 1/2*F_{n m} + 1/2*\partial_{m}{A_{n}}$ ) distribute(_) sort_product(_) canonicalise(_) rename_dummies(_);
$$\displaystyle{} - \frac{1}{4}\partial_{m}{\phi} \partial_{n}{\phi} {\phi}^{-1}+\frac{1}{4}\partial_{p}{\phi} \partial_{n}{h_{m q}} h^{p q} - \frac{1}{2}\partial_{m n}{\phi}+\frac{1}{4}F_{m p} F_{n q} {\phi}^{3} h^{p q}+\frac{1}{4}\partial_{p}{\phi} \partial_{q}{\phi} {\phi}^{-1} h_{m n} h^{p q} - \frac{1}{4}\partial_{p}{\phi} \partial_{q}{h_{m n}} h^{p q}+\frac{1}{4}\partial_{p}{\phi} \partial_{m}{h_{n q}} h^{p q}$$