Cadabra
a field-theory motivated approach to computer algebra

substitute

Generic substitution algorithm.
Generic substitution algorithm. Takes a rule or a set of rules according to which an expression should be modified. If more than one rule is given, it tries each rule in turn, until the first working one is encountered, after which it continues with the next node.
ex:=G_{\mu \nu \rho} + F_{\mu \nu \rho}; substitute(_, $F_{\mu \nu \rho} -> A_{\mu \nu} B_{\rho}$ );
\(\displaystyle{}G_{\mu \nu \rho}+F_{\mu \nu \rho}\)
\(\displaystyle{}G_{\mu \nu \rho}+A_{\mu \nu} B_{\rho}\)
ex:= A_{\mu \nu} B_{\nu \rho} C_{\rho \sigma}; substitute(_, $A_{m n} C_{p q} -> D_{m q}$ );
\(\displaystyle{}A_{\mu \nu} B_{\nu \rho} C_{\rho \sigma}\)
\(\displaystyle{}D_{\mu \sigma} B_{\nu \rho}\)
This command takes full care of dummy index relabelling, as the following example shows:
{m,n,q,r,s,t,u}::Indices(vector). ex:= a_{m} b_{n};
\(\displaystyle{}a_{m} b_{n}\)
substitute(_, $a_{q} -> c_{m n} d_{m n q}$ );
\(\displaystyle{}c_{q r} d_{q r m} b_{n}\)
By postfixing a name with a question mark, it becomes a pattern. You do not need this for indices (as the examples above show) but it is necessary for other types of function arguments.
ex:= \sin{ x }**2 + 3 + \cos{ x }**2; substitute(ex, $\sin{A?}**2 + \cos{A?}**2 = 1$);
\(\displaystyle{}\sin{x}^{2}+3+\cos{x}^{2}\)
\(\displaystyle{}4\)
Substitute can match sub-products and sub-sums, and you do not have to specify terms or factors in the order in which they appear,
ex:= A + B + C + D; substitute(_, $A+C=Q$);
\(\displaystyle{}A+B+C+D\)
A + B + C + D
\(\displaystyle{}Q+B+D\)
Q + B + D
ex:= A B C D; substitute(_, $B D = Q$);
\(\displaystyle{}A B C D\)
A B C D
\(\displaystyle{}A Q C\)
A Q C
However, you can request that the match is for the full sum or product,
ex:= A B C D + A B C D E F; substitute(_, $A B C D = 1$, partial=False);
\(\displaystyle{}A B C D+A B C D E F\)
A B C D + A B C D E F
\(\displaystyle{}1+A B C D E F\)
1 + A B C D E F
It will respect non-commuting objects and will not match if that would require moving non-commuting objects through each other,
{Q,R,S,T}::NonCommuting; ex:= Q R S T; substitute(_, $S Q = 1$);
\(\displaystyle{}\text{Attached property NonCommuting to }\left[Q, R, S, T\right].\)
\(\displaystyle{}Q R S T\)
Q R S T
\(\displaystyle{}Q R S T\)
Q R S T
Copyright © 2001-2024 Kasper Peeters
Questions? info@cadabra.science