Cadabra
a field-theory motivated approach to computer algebra

take_match

Select a subset of terms in a sum for further computations.
The take_match and replace_match algorithms enable you to temporarily work only on a part of an expression. You select the terms that you want to work on with take_match. When you are done, put the result back into the larger expression with replace_match. A simple example shows how this works:
ex:=A C + B D G + C D A;
\(\displaystyle{}A C+B D G+C D A\)
take_match(_, $D Q??$);
\(\displaystyle{}B D G+C D A\)
substitute(_, $C -> Q$);
\(\displaystyle{}B D G+Q D A\)
replace_match(_);
\(\displaystyle{}A C+B D G+Q D A\)
As you can see here, the replacement $C\rightarrow Q$ was only done on the 2nd and 3rd term of the original expression, to which we restricted all manipulations using the take_match command. This of course also works with more complicated, tensorial expressions, as the example below shows.
ex:= A_{m n} \chi B^{m}_{p} + \psi A_{n p};
\(\displaystyle{}A_{m n} \chi B^{m}\,_{p}+\psi A_{n p}\)
take_match(_, $\chi Q??$);
\(\displaystyle{}A_{m n} \chi B^{m}\,_{p}\)
substitute(_, $A_{m n} -> C_{m n}$);
\(\displaystyle{}C_{m n} \chi B^{m}\,_{p}\)
replace_match(_);
\(\displaystyle{}C_{m n} \chi B^{m}\,_{p}+\psi A_{n p}\)
When you are working on a part of an expression, you can restrict attention further by applying take_match again. The replace_match then puts sub-expressions back into the larger expression in reverse order:
ex:=A B + C B + C D B;
\(\displaystyle{}A B+C B+C D B\)
take_match(_, $C Q??$);
\(\displaystyle{}C B+C D B\)
substitute(_, $C -> Q$);
\(\displaystyle{}Q B+Q D B\)
take_match(_, $D Q??$);
\(\displaystyle{}Q D B\)
substitute(_, $B -> R$);
\(\displaystyle{}Q D R\)
replace_match(_);
\(\displaystyle{}Q B\)
replace_match(_);
\(\displaystyle{}A B+Q B\)
Copyright © 2001-2024 Kasper Peeters
Questions? info@cadabra.science