1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
\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}
|