\cdbalgorithm{vary}{}

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.
\begin{screen}{1,2}
A B + A C;
@vary(%)( A -> \epsilon D,
          B -> \epsilon C,
          C -> \epsilon A - \epsilon B );
\epsilon D B + A \epsilon C + \epsilon D C 
                   + A (\epsilon A - \epsilon B);
\end{screen}
It also works when acting on powers, in which case it will use the
product rule to expand them.
\begin{screen}{1,2}
A**3;
@vary(%)( A -> \delta{A} );
3 A**{2} \delta{A};
\end{screen}
This algorithm is thus mostly intended for variational derivatives
(subsequent partial integrations can be done
using \subscommand{pintegrate}). 

Note: In the examples above, the command is applied only at the top
level (there is no exclamation mark used in the call
of \subscommand{vary}). To understand why this is important, compare
the following two examples. The first one works as expected,
\begin{screen}{1,2}
A B;
@vary(%)( A -> a, B -> b);
a * B + A * b;
\end{screen}
In the second one, we add an exclamation mark,
\begin{screen}{1,2}
A B;
@vary!(%)( A -> a, B -> b);
0;
\end{screen}
The reason why we now get a zero is that in the first step, the vary
command acts in each of the individual factors, producing
\begin{screen}{0}
a b;
\end{screen}
It then acts once more at the level of the product. But now there are
no uppercase symbols left anymore, and the variation produces zero.

\cdbseealgo{pintegrate}
\cdbseealgo{substitute}

