Cadabra

Variational derivatives

This notebook shows how to do variational derivatives of an action for a scalar field, in order to compute equations of motion, also introducing how to do integration by parts. This is of course a trivial example but the same logic applies to more complicated situations.
x::Coordinate; \phi::Depends(x); \delta{\phi}::Depends(x); \partial{#}::PartialDerivative; ex:= -1/2 \int{\partial_{\mu}{\phi} \partial^{\mu}{\phi} + m**2 \phi**2}{x};
\(\displaystyle{}\text{Property Coordinate attached to }x.\)
\(\displaystyle{}\text{Property Depends attached to }\phi.\)
\(\displaystyle{}\text{Property Depends attached to }\delta\left(\phi\right).\)
\(\displaystyle{}\text{Property PartialDerivative attached to }\backslash\texttt{partial}\{\#\}.\)
\(\displaystyle{} - \frac{1}{2}\,\int \left(\partial_{\mu}{\phi} \partial^{\mu}{\phi} +{m}^{2\,} {\phi}^{2\,}\right)\,\,{\rm d}x\)
vary(_, $\phi -> \delta{\phi}$);
\(\displaystyle{} - \frac{1}{2}\,\int \left(\partial_{\mu}{\delta\left(\phi\right)} \partial^{\mu}{\phi} +\partial_{\mu}{\phi} \partial^{\mu}{\delta\left(\phi\right)} +2\,{m}^{2\,} \phi \delta\left(\phi\right)\right)\,\,{\rm d}x\)
integrate_by_parts(_, $\delta{\phi}$);
\(\displaystyle{} - \frac{1}{2}\,\int \left(-\,\delta\left(\phi\right) \partial_{\mu}\,^{\mu}{\phi} -\,\partial^{\mu}\,_{\mu}{\phi} \delta\left(\phi\right) +2\,{m}^{2\,} \phi \delta\left(\phi\right)\right)\,\,{\rm d}x\)
sort_product(_);
\(\displaystyle{} - \frac{1}{2}\,\int \left(-\,\delta\left(\phi\right) \partial_{\mu}\,^{\mu}{\phi} -\,\delta\left(\phi\right) \partial^{\mu}\,_{\mu}{\phi} +2\,\delta\left(\phi\right) \phi {m}^{2\,}\right)\,\,{\rm d}x\)
canonicalise(_);
\(\displaystyle{} - \frac{1}{2}\,\int \left(-2\,\delta\left(\phi\right) \partial^{\mu}\,_{\mu}{\phi} +2\,\delta\left(\phi\right) \phi {m}^{2\,}\right)\,\,{\rm d}x\)
factor_out(_, $\delta{\phi}$);
\(\displaystyle{} - \frac{1}{2}\,\int \delta\left(\phi\right) \left(-2\,\partial^{\mu}\,_{\mu}{\phi} +2\,\phi {m}^{2\,}\right)\,\,{\rm d}x\)