vary
Generic variation algorithm for functional derivatives.
Generic variation command. Takes a rule or a set of rules
according to which the terms in a sum should be varied. In every term,
apply the rule once to every factor.ex:= A B + A C;
vary(_, $A -> \epsilon D, B -> \epsilon C, C -> \epsilon A - \epsilon B$ );
\(\displaystyle{}A B%
+A C\)
\(\displaystyle{}\epsilon D B%
+A \epsilon C%
+\epsilon D C%
+A \left(\epsilon A%
-\epsilon B\right)\)
It also works when acting on powers, in which case it will use the
product rule to expand them.
ex:= A**3;
vary(_, $A -> \delta{A}$);
\(\displaystyle{}A^{3}\)
\(\displaystyle{}3A^{2} \delta\left(A\right)\)
This algorithm is thus mostly intended for variational derivatives.
\partial{#}::PartialDerivative;
ex:= -\int{\partial_{\mu}{\phi} \partial^{\mu}{\phi} + m**2 \phi**2}{x};
\(\displaystyle{}\text{Attached property PartialDerivative to }\partial{\#}.\)
\(\displaystyle{}-\int{}\left(\partial_{\mu}{\phi} \partial^{\mu}{\phi}%
+m^{2} \phi^{2}\right)\, {\rm d}x\)
vary(ex, $\phi -> \delta{\phi}$);
\(\displaystyle{}-\int{}\left(\partial_{\mu}{\delta\left(\phi\right)} \partial^{\mu}{\phi}%
+\partial_{\mu}{\phi} \partial^{\mu}{\delta\left(\phi\right)}%
+2m^{2} \phi \delta\left(\phi\right)\right)\, {\rm d}x\)
integrate_by_parts(ex, $\delta{\phi}$);
\(\displaystyle{}-\int{}\left(-\delta\left(\phi\right) \partial_{\mu}\,^{\mu}{\phi}%
-\partial^{\mu}\,_{\mu}{\phi} \delta\left(\phi\right)%
+2m^{2} \phi \delta\left(\phi\right)\right)\, {\rm d}x\)
canonicalise(_)
sort_product(_);
\(\displaystyle{}-\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{}-\int{}\delta\left(\phi\right) \left(-2\partial^{\mu}\,_{\mu}{\phi}%
+2\phi m^{2}\right)\, {\rm d}x\)