\cdbalgorithm{keep\_weight}{}

Keep only those terms for which a product has the indicated
weight. Weights are computed by making use of the \subsprop{Weight}
property of symbols. This algorithm does the opposite
of \subscommand{drop\_weight}.

As an example, consider the simple case in which we want to keep all
terms with 3~fields. This is done using
\begin{screen}{1,2,3}
{A,B}::Weight(label=field).
A B B + A A A + A B + B:
@keep_weight!(%){field}{3};
A B B + A A A 
\end{screen}

However, you can also do more complicated things by assigning non-unit
weights to symbols, as in the example below,
\begin{screen}{1,2,3,4}
{A,B}::Weight(label=field).
C::Weight(label=field, value=2).
A B B + A A A + A B + A C + B:
@keep_weight!(%){field}{3};
A B B + A A A + A C
\end{screen}

Weights also apply to tensorial expressions. Consider e.g.~a situation
in which we have a polynomial of the type
\begin{equation}
c^{a} + c^{a}_{b} x^{b} + c^{a}_{b c} x^{b} x^{c} + c^{a}_{b c d}
x^{b} x^{c} x^{d};
\end{equation}
and we want to keep only the quadratic term. This can be done using
\begin{screen}{1,2,3,4}
x^{a}::Weight(label=crd, value=1).
c^{#}::Weight(label=crd, value=0).
c^{a} + c^{a}_{b} x^{b} + c^{a}_{b c} x^{b} x^{c} + c^{a}_{b c d} x^{b} x^{c} x^{d};
@keep_weight!(%){crd}{2};
c^{a}_{b c} x^{b} x^{c};
\end{screen}

Weights can be ``inherited'' by operators by using
the \subsprop{WeightInherit} property. Here is an example using
partial derivatives,
\begin{screen}{1,2,3,4,6}
{\phi,\chi}::Weight(label=small, value=1).
\partial{#}::PartialDerivative.
\partial{#}::WeightInherit(label=all, type=Multiplicative).
\phi \partial_{0}{\phi} + \partial_{0}{\lambda} 
                               + \lambda \partial_{3}{\chi}:
@keep_weight!(%){small}{1};
\lambda \partial_{3}{\chi};
\end{screen}

If you want to use weights for dimension counting, in which operators
can also carry a dimension themselves (e.g.~derivatives), then use the
\verb|self| attribute,
\begin{screen}{1,2,3,4,6}
{\phi,\chi}::Weight(label=length, value=1).

x::Coordinate.
\partial{#}::PartialDerivative.
\partial{#}::WeightInherit(label=length, type=Multiplicative, self=-1).

\phi \partial_{x}{\phi} + \phi\chi + \partial_{x}{ \phi \chi**2 };
@keep_weight!(%){length}{1};
\end{screen}
This keeps only the first term.

~
\cdbseealgo{drop_weight}
\cdbseeprop{Weight}
\cdbseeprop{WeightInherit}
