Cadabra
a field-theory motivated approach to computer algebra

keep_weight

Keep terms with indicated weight
Keep only those terms for which a product has the indicated weight. Weights are computed by making use of the Weight property of symbols. This algorithm does the opposite of drop_weight. As an example, consider the simple case in which we want to keep all terms with 3 fields. This is done using
{A,B}::Weight(label=field); ex:=A B B + A A A + A B + B; keep_weight(_, $field=3$);
\(\displaystyle{}\text{Attached property Weight to }\left[A, B\right].\)
\(\displaystyle{}A B B+A A A+A B+B\)
\(\displaystyle{}A B B+A A A\)
However, you can also do more complicated things by assigning non-unit weights to symbols, as in the example below,
{A,B}::Weight(label=field); C::Weight(label=field, value=2); ex:= A B B + A A A + A B + A C + B;
\(\displaystyle{}\text{Attached property Weight to }\left[A, B\right].\)
\(\displaystyle{}\text{Attached property Weight to }C.\)
\(\displaystyle{}A B B+A A A+A B+A C+B\)
keep_weight(_, $field=3$);
\(\displaystyle{}A B B+A A A+A C\)
Weights also apply to tensorial expressions. Consider e.g. a situation in which we have a polynomial of the type
ex:=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};
\(\displaystyle{}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}\)
and we want to keep only the quadratic term. This can be done using
x^{a}::Weight(label=crd, value=1); c^{#}::Weight(label=crd, value=0);
\(\displaystyle{}\text{Attached property Weight to }x^{a}.\)
\(\displaystyle{}\text{Attached property Weight to }c^{\#}.\)
keep_weight(ex, $crd=2$);
\(\displaystyle{}c^{a}\,_{b c} x^{b} x^{c}\)
Weights can be "inherited" by operators by using the WeightInherit property. Here is an example using partial derivatives,
{\phi,\chi}::Weight(label=small, value=1); \partial{#}::PartialDerivative; \partial{#}::WeightInherit(label=all, type=multiplicative); ex:=\phi \partial_{0}{\phi} + \partial_{0}{\lambda} + \lambda \partial_{3}{\chi}; keep_weight(_, $small=1$);
\(\displaystyle{}\text{Attached property Weight to }\left[\phi, \chi\right].\)
\(\displaystyle{}\text{Attached property PartialDerivative to }\partial{\#}.\)
\(\displaystyle{}\text{Attached property WeightInherit to }\partial{\#}.\)
\(\displaystyle{}\phi \partial_{0}{\phi}+\partial_{0}{\lambda}+\lambda \partial_{3}{\chi}\)
\(\displaystyle{}\lambda \partial_{3}{\chi}\)
If you want to use weights for dimension counting, in which operators can also carry a dimension themselves (e.g. derivatives), then use the self attribute,
reset();
{\phi,\chi}::Weight(label=length, value=1); x::Coordinate; \partial{#}::PartialDerivative; \partial{#}::WeightInherit(label=length, type=multiplicative, self=-1); ex:=\phi \partial_{x}{\phi} + \phi\chi + \partial_{x}{ \phi \chi**2 };
\(\displaystyle{}\text{Attached property Weight to }\left[\phi, \chi\right].\)
\(\displaystyle{}\text{Attached property Coordinate to }x.\)
\(\displaystyle{}\text{Attached property PartialDerivative to }\partial{\#}.\)
\(\displaystyle{}\text{Attached property WeightInherit to }\partial{\#}.\)
\(\displaystyle{}\phi \partial_{x}{\phi}+\phi \chi+\partial_{x}\left(\phi {\chi}^{2}\right)\)
keep_weight(_, $length=1$);
\(\displaystyle{}\phi \partial_{x}{\phi}\)
Copyright © 2001-2024 Kasper Peeters
Questions? info@cadabra.science