#!/usr/local/bin/cadabra2
{\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);
\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.
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?} } );
todo:= 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(_);
substitute(_, $\partial_{4}{A??} -> 0$, repeat=True)
substitute(_, $\partial_{4 m?}{A??} -> 0$, repeat=True)
substitute(_, $\partial_{m? 4}{A??} -> 0$, repeat=True);
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(_);
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}$ );
converge(todo):
    distribute(_)
    product_rule(_)
    canonicalise(_)
;
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(_)
;
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(_);
    
tst:= - 1/4 * \partial_{m}{\phi} * \partial_{n}{\phi} * \phi**(-1) + 1/4 * \partial_{p}{\phi} * \partial_{n}{h_{m q}} * h^{p q} - 1/2 * \partial_{m n}{\phi} + 1/4 * F_{m p} * F_{n q} * \phi**3 * h^{p q} + 1/4 * \partial_{p}{\phi} * \partial_{q}{\phi} * \phi**(-1) * h_{m n} * h^{p q} - 1/4 * \partial_{p}{\phi} * \partial_{q}{h_{m n}} * h^{p q} + 1/4 * \partial_{p}{\phi} * \partial_{m}{h_{n q}} * h^{p q} - @(todo);

assert(tst==0)

