a field-theory motivated approach to computer algebra

# Einstein equations from a variational principle

This notebook derives the Einstein equations by starting from the Einstein-Hilbert action, and requiring that its variational derivative with respect to the metric vanishes.
import cdb.core.manip as manip
We first of all need to declare the indices which we will use, as well as the symbols we use for the metric and Kronecker delta.
{\mu,\nu,\rho,\sigma,\alpha,\beta,\gamma,\lambda,\tau,\pi,\xi,\iota,\omega,\kappa,\psi,\chi,\epsilon,\upsilon}::Indices(position=independent); g_{\mu\nu}::Metric. g^{\mu\nu}::InverseMetric. {\delta{#},g_{\mu}^{\nu},g^{\mu}_{\nu}}::KroneckerDelta.
$$\displaystyle{}\text{Attached property Indices(position=independent) to }\left[\mu, \nu, \rho, \sigma, \alpha, \beta, \gamma, \lambda, \tau, \pi, \xi, \iota, \omega, \kappa, \psi, \chi, \epsilon, \upsilon\right].$$
Make various symbols display in a more readable form,
\Lm::LaTeXForm("\mathcal{L}_{\text{mat}}"). \Dg::LaTeXForm("\sqrt{-g}"). \dg{#}::LaTeXForm("\delta g").
\dLm::LaTeXForm("\delta\mathcal{L}_{\text{mat}}"). \d::LaTeXForm("\delta"). \dDg::LaTeXForm("\delta\sqrt{-g}"). \dCn{#}::LaTeXForm("\delta\Gamma"). \dR{#}::LaTeXForm("\delta R").
The Christoffel symbols and their variations are symmetric in the two lower indices,
\Gamma^{\alpha}_{\mu\nu}::TableauSymmetry(shape={2}, indices={1,2}). \dGamma^{\alpha}_{\mu\nu}::TableauSymmetry(shape={2}, indices={1,2}).
We need to declare which symbols to use for derivatives,
{\partial{#}}::PartialDerivative; {\nabla{#},\delta{#}}::Derivative;
$$\displaystyle{}\text{Attached property PartialDerivative to }\partial{\#}.$$
$$\displaystyle{}\text{Attached property Derivative to }\left[\nabla{\#}, \delta{\#}\right].$$
We now need to define the Christoffel connection in terms of the metric, and the Riemann tensor in terms of the Christoffel connection, as well as the Ricci tensor and scalar and Einstein tensor. These are also available in a Cadabra package, but for clarity we spell them out here.
connection := \Gamma^{\mu}_{\nu\rho} = 1/2 g^{\mu\sigma} ( \partial_{\rho}{g_{\nu\sigma}} +\partial_{\nu}{g_{\rho\sigma}} -\partial_{\sigma}{g_{\nu\rho}} ); riemannTensor := R^{\rho}_{\sigma\mu\nu} = \partial_{\mu}{\Gamma^{\rho}_{\nu\sigma}} -\partial_{\nu}{\Gamma^{\rho}_{\mu\sigma}} +\Gamma^{\rho}_{\mu\lambda} \Gamma^{\lambda}_{\nu\sigma} -\Gamma^{\rho}_{\nu\lambda} \Gamma^{\lambda}_{\mu\sigma}; ricciTensor := R_{\sigma\nu} = R^{\rho}_{\sigma\rho\nu}; scalarCurvature := R = R_{\sigma\nu} g^{\sigma\nu}; einsteinTensor := G_{\mu\nu} = R_{\mu\nu} - 1/2 g_{\mu\nu} R;
$$\displaystyle{}\Gamma^{\mu}\,_{\nu \rho} = \frac{1}{2}g^{\mu \sigma} \left(\partial_{\rho}{g_{\nu \sigma}}+\partial_{\nu}{g_{\rho \sigma}}-\partial_{\sigma}{g_{\nu \rho}}\right)$$
\Gamma^{\mu}_{\nu \rho} = 1/2 g^{\mu \sigma} (\partial_{\rho}(g_{\nu \sigma}) + \partial_{\nu}(g_{\rho \sigma})-\partial_{\sigma}(g_{\nu \rho}))
$$\displaystyle{}R^{\rho}\,_{\sigma \mu \nu} = \partial_{\mu}{\Gamma^{\rho}\,_{\nu \sigma}}-\partial_{\nu}{\Gamma^{\rho}\,_{\mu \sigma}}+\Gamma^{\rho}\,_{\mu \lambda} \Gamma^{\lambda}\,_{\nu \sigma}-\Gamma^{\rho}\,_{\nu \lambda} \Gamma^{\lambda}\,_{\mu \sigma}$$
R^{\rho}_{\sigma \mu \nu} = \partial_{\mu}(\Gamma^{\rho}_{\nu \sigma})-\partial_{\nu}(\Gamma^{\rho}_{\mu \sigma}) + \Gamma^{\rho}_{\mu \lambda} \Gamma^{\lambda}_{\nu \sigma}-\Gamma^{\rho}_{\nu \lambda} \Gamma^{\lambda}_{\mu \sigma}
$$\displaystyle{}R_{\sigma \nu} = R^{\rho}\,_{\sigma \rho \nu}$$
R_{\sigma \nu} = R^{\rho}_{\sigma \rho \nu}
$$\displaystyle{}R = R_{\sigma \nu} g^{\sigma \nu}$$
R = R_{\sigma \nu} g^{\sigma \nu}
$$\displaystyle{}G_{\mu \nu} = R_{\mu \nu} - \frac{1}{2}g_{\mu \nu} R$$
G_{\mu \nu} = R_{\mu \nu} - 1/2 g_{\mu \nu} R
We are now ready to address the variational problem. The action we use is the Einstein-Hilbert action, amended with a cosmological constant term.
action := S = \int{\Dg*[1/2*1/\kappa*(R-2\Lambda)+\Lm]}{x}; distribute(_);
$$\displaystyle{}S = \int \sqrt{-g} \left(\frac{1}{2}{\kappa}^{-1} \left(R-2\Lambda\right)+\mathcal{L}_{\text{mat}}\right)\,\,{\rm d}x$$
S = \int{\Dg ( 1/2 (\kappa)**(-1) (R-2\Lambda) + \Lm)}{x}
$$\displaystyle{}S = \int \left(\frac{1}{2}\sqrt{-g} {\kappa}^{-1} R-\sqrt{-g} {\kappa}^{-1} \Lambda+\sqrt{-g} \mathcal{L}_{\text{mat}}\right)\,\,{\rm d}x$$
S = \int{ 1/2 \Dg (\kappa)**(-1) R-\Dg (\kappa)**(-1) \Lambda + \Dg \Lm}{x}
Before we vary this, we need to construct the variation of the various objects in this action step by step. The variation of the metric determinant is given by
deltaMetricDeterminant := \delta(\Dg) = -\frac{1}{2}*\Dg*g_{\mu\nu}\dg^{\mu\nu};
$$\displaystyle{}\delta{\sqrt{-g}} = - \frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu}$$
\delta(\Dg) = - 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu}
By definition the variation of the matter Lagrangian gives the stress tensor; we rewrite it here slightly for later purposes.
energyMomentumTensor := \Dg T_{\mu\nu}\dg^{\mu\nu} = -2\delta(\Dg*\Lm); product_rule(_) distribute(_) substitute(_,deltaMetricDeterminant) matterLagrangianVariation = manip.swap_sides(_);
$$\displaystyle{}\sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu} = -2\delta\left(\sqrt{-g} \mathcal{L}_{\text{mat}}\right)$$
\Dg T_{\mu \nu} \dg^{\mu \nu} = -2\delta(\Dg \Lm)
$$\displaystyle{}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} \mathcal{L}_{\text{mat}}-2\sqrt{-g} \delta{\mathcal{L}_{\text{mat}}} = \sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu}$$
\Dg g_{\mu \nu} \dg^{\mu \nu} \Lm-2\Dg \delta(\Lm) = \Dg T_{\mu \nu} \dg^{\mu \nu}
Next comes the variation of the Christoffel symbols. We then rewrite these in such a way that we isolate the derivative of the variation, as that object will appear once we start varying the Riemann tensor.
covariantDerivativeCn := \nabla_{\sigma}(\dCn^{\mu}_{\nu\rho}) = \partial_{\sigma}(\dCn^{\mu}_{\nu\rho}) +\Gamma^{\mu}_{\sigma\alpha}\dCn^{\alpha}_{\nu\rho}-\Gamma^{\alpha}_{\sigma\nu}\dCn^{\mu}_{\alpha\rho} -\Gamma^{\alpha}_{\sigma\rho}\dCn^{\mu}_{\nu\alpha}; covariantDerivativeCn1 = manip.to_rhs(_, $\nabla_{\sigma}{A??}$); covariantDerivativeCn2 = manip.to_lhs(_, $\partial_{\sigma}{A??}$); partialDerivativeCn = manip.multiply_through(covariantDerivativeCn2, $-1$);
$$\displaystyle{}\nabla_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}} = \partial_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}}+\Gamma^{\mu}\,_{\sigma \alpha} \delta\Gamma^{\alpha}\,_{\nu \rho}-\Gamma^{\alpha}\,_{\sigma \nu} \delta\Gamma^{\mu}\,_{\alpha \rho}-\Gamma^{\alpha}\,_{\sigma \rho} \delta\Gamma^{\mu}\,_{\nu \alpha}$$
\nabla_{\sigma}(\dCn^{\mu}_{\nu \rho}) = \partial_{\sigma}(\dCn^{\mu}_{\nu \rho}) + \Gamma^{\mu}_{\sigma \alpha} \dCn^{\alpha}_{\nu \rho}-\Gamma^{\alpha}_{\sigma \nu} \dCn^{\mu}_{\alpha \rho}-\Gamma^{\alpha}_{\sigma \rho} \dCn^{\mu}_{\nu \alpha}
$$\displaystyle{}0 = \partial_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}}+\Gamma^{\mu}\,_{\sigma \alpha} \delta\Gamma^{\alpha}\,_{\nu \rho}-\Gamma^{\alpha}\,_{\sigma \nu} \delta\Gamma^{\mu}\,_{\alpha \rho}-\Gamma^{\alpha}\,_{\sigma \rho} \delta\Gamma^{\mu}\,_{\nu \alpha}-\nabla_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}}$$
0 = \partial_{\sigma}(\dCn^{\mu}_{\nu \rho}) + \Gamma^{\mu}_{\sigma \alpha} \dCn^{\alpha}_{\nu \rho}-\Gamma^{\alpha}_{\sigma \nu} \dCn^{\mu}_{\alpha \rho}-\Gamma^{\alpha}_{\sigma \rho} \dCn^{\mu}_{\nu \alpha}-\nabla_{\sigma}(\dCn^{\mu}_{\nu \rho})
$$\displaystyle{}-\partial_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}} = \Gamma^{\mu}\,_{\sigma \alpha} \delta\Gamma^{\alpha}\,_{\nu \rho}-\Gamma^{\alpha}\,_{\sigma \nu} \delta\Gamma^{\mu}\,_{\alpha \rho}-\Gamma^{\alpha}\,_{\sigma \rho} \delta\Gamma^{\mu}\,_{\nu \alpha}-\nabla_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}}$$
-\partial_{\sigma}(\dCn^{\mu}_{\nu \rho}) = \Gamma^{\mu}_{\sigma \alpha} \dCn^{\alpha}_{\nu \rho}-\Gamma^{\alpha}_{\sigma \nu} \dCn^{\mu}_{\alpha \rho}-\Gamma^{\alpha}_{\sigma \rho} \dCn^{\mu}_{\nu \alpha}-\nabla_{\sigma}(\dCn^{\mu}_{\nu \rho})
$$\displaystyle{}\partial_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}} = -\Gamma^{\mu}\,_{\sigma \alpha} \delta\Gamma^{\alpha}\,_{\nu \rho}+\Gamma^{\alpha}\,_{\sigma \nu} \delta\Gamma^{\mu}\,_{\alpha \rho}+\Gamma^{\alpha}\,_{\sigma \rho} \delta\Gamma^{\mu}\,_{\nu \alpha}+\nabla_{\sigma}{\delta\Gamma^{\mu}\,_{\nu \rho}}$$
\partial_{\sigma}(\dCn^{\mu}_{\nu \rho}) = -\Gamma^{\mu}_{\sigma \alpha} \dCn^{\alpha}_{\nu \rho} + \Gamma^{\alpha}_{\sigma \nu} \dCn^{\mu}_{\alpha \rho} + \Gamma^{\alpha}_{\sigma \rho} \dCn^{\mu}_{\nu \alpha} + \nabla_{\sigma}(\dCn^{\mu}_{\nu \rho})
The variation of the Riemann tensor comes next. With a few steps this is rewritten to a form which only involves covariant derivatives of the variation of the Christoffel connection.
deltaRiemannTensor = vary(riemannTensor, $\Gamma^{\mu}_{\nu\rho}->\dCn^{\mu}_{\nu\rho}, R^{\rho}_{\sigma\mu\nu} -> \dR^{\rho}_{\sigma\mu\nu}$); sort_product(_); substitute(_,partialDerivativeCn); rename_dummies(_); canonicalise(_);
$$\displaystyle{}\delta R^{\rho}\,_{\sigma \mu \nu} = \partial_{\mu}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\partial_{\nu}{\delta\Gamma^{\rho}\,_{\mu \sigma}}+\delta\Gamma^{\rho}\,_{\mu \lambda} \Gamma^{\lambda}\,_{\nu \sigma}+\Gamma^{\rho}\,_{\mu \lambda} \delta\Gamma^{\lambda}\,_{\nu \sigma}-\delta\Gamma^{\rho}\,_{\nu \lambda} \Gamma^{\lambda}\,_{\mu \sigma}-\Gamma^{\rho}\,_{\nu \lambda} \delta\Gamma^{\lambda}\,_{\mu \sigma}$$
\dR^{\rho}_{\sigma \mu \nu} = \partial_{\mu}(\dCn^{\rho}_{\nu \sigma})-\partial_{\nu}(\dCn^{\rho}_{\mu \sigma}) + \dCn^{\rho}_{\mu \lambda} \Gamma^{\lambda}_{\nu \sigma} + \Gamma^{\rho}_{\mu \lambda} \dCn^{\lambda}_{\nu \sigma}-\dCn^{\rho}_{\nu \lambda} \Gamma^{\lambda}_{\mu \sigma}-\Gamma^{\rho}_{\nu \lambda} \dCn^{\lambda}_{\mu \sigma}
$$\displaystyle{}\delta R^{\rho}\,_{\sigma \mu \nu} = \partial_{\mu}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\partial_{\nu}{\delta\Gamma^{\rho}\,_{\mu \sigma}}+\Gamma^{\lambda}\,_{\nu \sigma} \delta\Gamma^{\rho}\,_{\mu \lambda}+\Gamma^{\rho}\,_{\mu \lambda} \delta\Gamma^{\lambda}\,_{\nu \sigma}-\Gamma^{\lambda}\,_{\mu \sigma} \delta\Gamma^{\rho}\,_{\nu \lambda}-\Gamma^{\rho}\,_{\nu \lambda} \delta\Gamma^{\lambda}\,_{\mu \sigma}$$
\dR^{\rho}_{\sigma \mu \nu} = \partial_{\mu}(\dCn^{\rho}_{\nu \sigma})-\partial_{\nu}(\dCn^{\rho}_{\mu \sigma}) + \Gamma^{\lambda}_{\nu \sigma} \dCn^{\rho}_{\mu \lambda} + \Gamma^{\rho}_{\mu \lambda} \dCn^{\lambda}_{\nu \sigma}-\Gamma^{\lambda}_{\mu \sigma} \dCn^{\rho}_{\nu \lambda}-\Gamma^{\rho}_{\nu \lambda} \dCn^{\lambda}_{\mu \sigma}
$$\displaystyle{}\delta R^{\rho}\,_{\sigma \mu \nu} = -\Gamma^{\rho}\,_{\mu \alpha} \delta\Gamma^{\alpha}\,_{\nu \sigma}+\Gamma^{\alpha}\,_{\mu \nu} \delta\Gamma^{\rho}\,_{\alpha \sigma}+\Gamma^{\alpha}\,_{\mu \sigma} \delta\Gamma^{\rho}\,_{\nu \alpha}+\nabla_{\mu}{\delta\Gamma^{\rho}\,_{\nu \sigma}}+\Gamma^{\rho}\,_{\nu \alpha} \delta\Gamma^{\alpha}\,_{\mu \sigma}-\Gamma^{\alpha}\,_{\nu \mu} \delta\Gamma^{\rho}\,_{\alpha \sigma}-\Gamma^{\alpha}\,_{\nu \sigma} \delta\Gamma^{\rho}\,_{\mu \alpha}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\mu \sigma}}+\Gamma^{\lambda}\,_{\nu \sigma} \delta\Gamma^{\rho}\,_{\mu \lambda}+\Gamma^{\rho}\,_{\mu \lambda} \delta\Gamma^{\lambda}\,_{\nu \sigma}-\Gamma^{\lambda}\,_{\mu \sigma} \delta\Gamma^{\rho}\,_{\nu \lambda}-\Gamma^{\rho}\,_{\nu \lambda} \delta\Gamma^{\lambda}\,_{\mu \sigma}$$
\dR^{\rho}_{\sigma \mu \nu} = -\Gamma^{\rho}_{\mu \alpha} \dCn^{\alpha}_{\nu \sigma} + \Gamma^{\alpha}_{\mu \nu} \dCn^{\rho}_{\alpha \sigma} + \Gamma^{\alpha}_{\mu \sigma} \dCn^{\rho}_{\nu \alpha} + \nabla_{\mu}(\dCn^{\rho}_{\nu \sigma}) + \Gamma^{\rho}_{\nu \alpha} \dCn^{\alpha}_{\mu \sigma}-\Gamma^{\alpha}_{\nu \mu} \dCn^{\rho}_{\alpha \sigma}-\Gamma^{\alpha}_{\nu \sigma} \dCn^{\rho}_{\mu \alpha}-\nabla_{\nu}(\dCn^{\rho}_{\mu \sigma}) + \Gamma^{\lambda}_{\nu \sigma} \dCn^{\rho}_{\mu \lambda} + \Gamma^{\rho}_{\mu \lambda} \dCn^{\lambda}_{\nu \sigma}-\Gamma^{\lambda}_{\mu \sigma} \dCn^{\rho}_{\nu \lambda}-\Gamma^{\rho}_{\nu \lambda} \dCn^{\lambda}_{\mu \sigma}
$$\displaystyle{}\delta R^{\rho}\,_{\sigma \mu \nu} = \Gamma^{\alpha}\,_{\mu \nu} \delta\Gamma^{\rho}\,_{\alpha \sigma}+\nabla_{\mu}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\Gamma^{\alpha}\,_{\nu \mu} \delta\Gamma^{\rho}\,_{\alpha \sigma}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\mu \sigma}}$$
\dR^{\rho}_{\sigma \mu \nu} = \Gamma^{\alpha}_{\mu \nu} \dCn^{\rho}_{\alpha \sigma} + \nabla_{\mu}(\dCn^{\rho}_{\nu \sigma})-\Gamma^{\alpha}_{\nu \mu} \dCn^{\rho}_{\alpha \sigma}-\nabla_{\nu}(\dCn^{\rho}_{\mu \sigma})
$$\displaystyle{}\delta R^{\rho}\,_{\sigma \mu \nu} = \nabla_{\mu}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\mu \sigma}}$$
\dR^{\rho}_{\sigma \mu \nu} = \nabla_{\mu}(\dCn^{\rho}_{\nu \sigma})-\nabla_{\nu}(\dCn^{\rho}_{\mu \sigma})
The variation of the Ricci tensor leads to the so-called Palatini identity:
deltaRicciTensor = vary(ricciTensor, $R^{\rho}_{\sigma\rho\nu}->\dR^{\rho}_{\sigma\rho\nu}, R_{\sigma\nu} -> \dR_{\sigma\nu}$); substitute(_,deltaRiemannTensor);
$$\displaystyle{}\delta R_{\sigma \nu} = \delta R^{\rho}\,_{\sigma \rho \nu}$$
\dR_{\sigma \nu} = \dR^{\rho}_{\sigma \rho \nu}
$$\displaystyle{}\delta R_{\sigma \nu} = \nabla_{\rho}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\rho \sigma}}$$
\dR_{\sigma \nu} = \nabla_{\rho}(\dCn^{\rho}_{\nu \sigma})-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma})
We can now vary the scalar curvature.
deltaScalarCurvature = vary(scalarCurvature, $R-> \dR, R_{\sigma\nu} -> \dR_{\sigma\nu}, g^{\mu\nu}->\dg^{\mu\nu}$); substitute(_,deltaRicciTensor); distribute(_); substitute(_,$\nabla_{\sigma}(\dCn^{\mu}_{\nu\rho})g^{\alpha\beta}->\nabla_{\sigma}(\dCn^{\mu}_{\nu\rho}g^{\alpha\beta})$); canonicalise(_);
$$\displaystyle{}\delta R = \delta R_{\sigma \nu} g^{\sigma \nu}+R_{\sigma \nu} \delta g^{\sigma \nu}$$
\dR = \dR_{\sigma \nu} g^{\sigma \nu} + R_{\sigma \nu} \dg^{\sigma \nu}
$$\displaystyle{}\delta R = \left(\nabla_{\rho}{\delta\Gamma^{\rho}\,_{\nu \sigma}}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\rho \sigma}}\right) g^{\sigma \nu}+R_{\sigma \nu} \delta g^{\sigma \nu}$$
\dR = (\nabla_{\rho}(\dCn^{\rho}_{\nu \sigma})-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma})) g^{\sigma \nu} + R_{\sigma \nu} \dg^{\sigma \nu}
$$\displaystyle{}\delta R = \nabla_{\rho}{\delta\Gamma^{\rho}\,_{\nu \sigma}} g^{\sigma \nu}-\nabla_{\nu}{\delta\Gamma^{\rho}\,_{\rho \sigma}} g^{\sigma \nu}+R_{\sigma \nu} \delta g^{\sigma \nu}$$
\dR = \nabla_{\rho}(\dCn^{\rho}_{\nu \sigma}) g^{\sigma \nu}-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma}) g^{\sigma \nu} + R_{\sigma \nu} \dg^{\sigma \nu}
$$\displaystyle{}\delta R = \nabla_{\rho}\left(\delta\Gamma^{\rho}\,_{\nu \sigma} g^{\sigma \nu}\right)-\nabla_{\nu}\left(\delta\Gamma^{\rho}\,_{\rho \sigma} g^{\sigma \nu}\right)+R_{\sigma \nu} \delta g^{\sigma \nu}$$
\dR = \nabla_{\rho}(\dCn^{\rho}_{\nu \sigma} g^{\sigma \nu})-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma} g^{\sigma \nu}) + R_{\sigma \nu} \dg^{\sigma \nu}
$$\displaystyle{}\delta R = \nabla_{\nu}\left(\delta\Gamma^{\nu}\,_{\rho \sigma} g^{\rho \sigma}\right)-\nabla_{\nu}\left(\delta\Gamma^{\rho}\,_{\rho \sigma} g^{\nu \sigma}\right)+R_{\nu \sigma} \delta g^{\nu \sigma}$$
\dR = \nabla_{\nu}(\dCn^{\nu}_{\rho \sigma} g^{\rho \sigma})-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma} g^{\nu \sigma}) + R_{\nu \sigma} \dg^{\nu \sigma}
With these ingredients we can now vary the action: simply vary all ingredients and then substitute what we have found above for the variations of the metric, connection and curvature tensors.
vary(action, $\Dg->\delta(\Dg), R -> \dR, \Lm->\delta(\Lm), S->\delta(S)$); substitute(action, deltaMetricDeterminant); substitute(action, matterLagrangianVariation); substitute(action, deltaScalarCurvature); distribute(action); rename_dummies(action); factor_out(action, $\dg^{\mu\nu}, \Dg$);
$$\displaystyle{}\delta{S} = \int \left(\frac{1}{2}\delta{\sqrt{-g}} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \delta R-\delta{\sqrt{-g}} {\kappa}^{-1} \Lambda+\delta{\sqrt{-g}} \mathcal{L}_{\text{mat}}+\sqrt{-g} \delta{\mathcal{L}_{\text{mat}}}\right)\,\,{\rm d}x$$
\delta(S) = \int{ 1/2 \delta(\Dg) (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) \dR-\delta(\Dg) (\kappa)**(-1) \Lambda + \delta(\Dg) \Lm + \Dg \delta(\Lm)}{x}
$$\displaystyle{}\delta{S} = \int \left( - \frac{1}{4}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \delta R+\frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} \mathcal{L}_{\text{mat}}+\sqrt{-g} \delta{\mathcal{L}_{\text{mat}}}\right)\,\,{\rm d}x$$
\delta(S) = \int{ - 1/4 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) \dR + 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} \Lm + \Dg \delta(\Lm)}{x}
$$\displaystyle{}\delta{S} = \int \left( - \frac{1}{4}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \delta R+\frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}\sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu}\right)\,\,{\rm d}x$$
\delta(S) = \int{ - 1/4 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) \dR + 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 \Dg T_{\mu \nu} \dg^{\mu \nu}}{x}
$$\displaystyle{}\delta{S} = \int \left( - \frac{1}{4}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \left(\nabla_{\nu}\left(\delta\Gamma^{\nu}\,_{\rho \sigma} g^{\rho \sigma}\right)-\nabla_{\nu}\left(\delta\Gamma^{\rho}\,_{\rho \sigma} g^{\nu \sigma}\right)+R_{\nu \sigma} \delta g^{\nu \sigma}\right)+\frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}\sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu}\right)\,\,{\rm d}x$$
\delta(S) = \int{ - 1/4 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) (\nabla_{\nu}(\dCn^{\nu}_{\rho \sigma} g^{\rho \sigma})-\nabla_{\nu}(\dCn^{\rho}_{\rho \sigma} g^{\nu \sigma}) + R_{\nu \sigma} \dg^{\nu \sigma}) + 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 \Dg T_{\mu \nu} \dg^{\mu \nu}}{x}
$$\displaystyle{}\delta{S} = \int \left( - \frac{1}{4}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \nabla_{\nu}\left(\delta\Gamma^{\nu}\,_{\rho \sigma} g^{\rho \sigma}\right) - \frac{1}{2}\sqrt{-g} {\kappa}^{-1} \nabla_{\nu}\left(\delta\Gamma^{\rho}\,_{\rho \sigma} g^{\nu \sigma}\right)+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} R_{\nu \sigma} \delta g^{\nu \sigma}+\frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}\sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu}\right)\,\,{\rm d}x$$
\delta(S) = \int{ - 1/4 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) \nabla_{\nu}(\dCn^{\nu}_{\rho \sigma} g^{\rho \sigma}) - 1/2 \Dg (\kappa)**(-1) \nabla_{\nu}(\dCn^{\rho}_{\rho \sigma} g^{\nu \sigma}) + 1/2 \Dg (\kappa)**(-1) R_{\nu \sigma} \dg^{\nu \sigma} + 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 \Dg T_{\mu \nu} \dg^{\mu \nu}}{x}
$$\displaystyle{}\delta{S} = \int \left( - \frac{1}{4}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} \nabla_{\mu}\left(\delta\Gamma^{\mu}\,_{\nu \rho} g^{\nu \rho}\right) - \frac{1}{2}\sqrt{-g} {\kappa}^{-1} \nabla_{\rho}\left(\delta\Gamma^{\mu}\,_{\mu \nu} g^{\rho \nu}\right)+\frac{1}{2}\sqrt{-g} {\kappa}^{-1} R_{\mu \nu} \delta g^{\mu \nu}+\frac{1}{2}\sqrt{-g} g_{\mu \nu} \delta g^{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}\sqrt{-g} T_{\mu \nu} \delta g^{\mu \nu}\right)\,\,{\rm d}x$$
\delta(S) = \int{ - 1/4 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) R + 1/2 \Dg (\kappa)**(-1) \nabla_{\mu}(\dCn^{\mu}_{\nu \rho} g^{\nu \rho}) - 1/2 \Dg (\kappa)**(-1) \nabla_{\rho}(\dCn^{\mu}_{\mu \nu} g^{\rho \nu}) + 1/2 \Dg (\kappa)**(-1) R_{\mu \nu} \dg^{\mu \nu} + 1/2 \Dg g_{\mu \nu} \dg^{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 \Dg T_{\mu \nu} \dg^{\mu \nu}}{x}
$$\displaystyle{}\delta{S} = \int \left(\sqrt{-g} \delta g^{\mu \nu} \left( - \frac{1}{4}g_{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}{\kappa}^{-1} R_{\mu \nu}+\frac{1}{2}g_{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}T_{\mu \nu}\right)+\sqrt{-g} \left(\frac{1}{2}{\kappa}^{-1} \nabla_{\mu}\left(\delta\Gamma^{\mu}\,_{\nu \rho} g^{\nu \rho}\right) - \frac{1}{2}{\kappa}^{-1} \nabla_{\rho}\left(\delta\Gamma^{\mu}\,_{\mu \nu} g^{\rho \nu}\right)\right)\right)\,\,{\rm d}x$$
\delta(S) = \int{\Dg \dg^{\mu \nu} ( - 1/4 g_{\mu \nu} (\kappa)**(-1) R + 1/2 (\kappa)**(-1) R_{\mu \nu} + 1/2 g_{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 T_{\mu \nu}) + \Dg ( 1/2 (\kappa)**(-1) \nabla_{\mu}(\dCn^{\mu}_{\nu \rho} g^{\nu \rho}) - 1/2 (\kappa)**(-1) \nabla_{\rho}(\dCn^{\mu}_{\mu \nu} g^{\rho \nu}))}{x}
The terms proportional to $\delta g^{\mu\nu}$ are the Einstein equations, the rest is a total derivative. We can rewrite this a bit further to get the familiar form:
t1 = action[1][0][0][2] eom:= 2\kappa @(t1) = 0; distribute(_) collect_factors(_) manip.to_rhs(_, $- \kappa T_{\mu\nu}$); substitute(_, manip.swap_sides(einsteinTensor));
$$\displaystyle{}2\kappa \left( - \frac{1}{4}g_{\mu \nu} {\kappa}^{-1} R+\frac{1}{2}{\kappa}^{-1} R_{\mu \nu}+\frac{1}{2}g_{\mu \nu} {\kappa}^{-1} \Lambda - \frac{1}{2}T_{\mu \nu}\right) = 0$$
2\kappa ( - 1/4 g_{\mu \nu} (\kappa)**(-1) R + 1/2 (\kappa)**(-1) R_{\mu \nu} + 1/2 g_{\mu \nu} (\kappa)**(-1) \Lambda - 1/2 T_{\mu \nu}) = 0
$$\displaystyle{} - \frac{1}{2}g_{\mu \nu} R+R_{\mu \nu}+g_{\mu \nu} \Lambda = \kappa T_{\mu \nu}$$
- 1/2 g_{\mu \nu} R + R_{\mu \nu} + g_{\mu \nu} \Lambda = \kappa T_{\mu \nu}
$$\displaystyle{}G_{\mu \nu}+g_{\mu \nu} \Lambda = \kappa T_{\mu \nu}$$
G_{\mu \nu} + g_{\mu \nu} \Lambda = \kappa T_{\mu \nu}