Cadabra
a field-theory motivated approach to computer algebra

cdb.utils.indices

Additional functionality to deal with indices

all_index_positions(ex: ExNode) -> Generator[Ex]

Generate all possible combinations of covariant and contravariant indices
ex := A_{\mu \nu \rho}. for var in all_index_positions(ex): var;
\(\displaystyle{}A_{\mu \nu \rho}\)
\(\displaystyle{}A_{\mu \nu}\,^{\rho}\)
\(\displaystyle{}A_{\mu}\,^{\nu}\,_{\rho}\)
\(\displaystyle{}A_{\mu}\,^{\nu \rho}\)
\(\displaystyle{}A^{\mu}\,_{\nu \rho}\)
\(\displaystyle{}A^{\mu}\,_{\nu}\,^{\rho}\)
\(\displaystyle{}A^{\mu \nu}\,_{\rho}\)
\(\displaystyle{}A^{\mu \nu \rho}\)

replace_index(ex: Ex, top_name: str, old_index: str, new_index: str) -> Ex

Rename indices
Renames with \texttt{new_index} all occurences of indices named \texttt{old_index} in any subtree whose top node is named \texttt{top_name} \textbf{Note:} This is useful for avoiding triple index-repeated errors due to the presence of tensor indices in the argument of the exponential function i.e. $\exp(\pm i k_\lambda x^\lambda)$
test := h_{\mu\nu} = A_{\mu\nu} \exp(i*k_\mu*x^{\mu}); test = replace_index(test,r"\exp",r'\mu',r'\alpha');
\(\displaystyle{}h_{\mu \nu} = A_{\mu \nu} \exp\left(i k_{\mu} x^{\mu}\right)\)
\(\displaystyle{}h_{\mu \nu} = A_{\mu \nu} \exp\left(i k_{\alpha} x^{\alpha}\right)\)

get_indices(node: ExNode) -> List[Ex]

Return a list of all indices inside the given node
\partial{#}::Derivative. ex := 0 + a_{\mu} \partial_{\sigma}{b_{\mu}b_{\lambda}} \delta{p_{\rho}p^{\rho}} + g_{\sigma\lambda}g_{\kappa}g^{\kappa} = p_{\sigma}p_{\lambda}; try: get_indices(ex); except AssertionError: "Caught error"; get_indices(ex[1]);
\(\displaystyle{}a_{\mu} \partial_{\sigma}\left(b_{\mu} b_{\lambda}\right) \delta\left(p_{\rho} p^{\rho}\right)+g_{\sigma \lambda} g_{\kappa} g^{\kappa} = p_{\sigma} p_{\lambda}\)
Caught error
{}$\big[$$\sigma$, $\lambda$$\big]$

get_dummy_indices(node: ExNode) -> List[Ex]

Return a list of all dummy indices inside the given node
\partial{#}::Derivative. ex := a_{\mu} \partial_{\sigma}{b_{\mu}b_{\lambda}} \delta{p_{\rho}p^{\rho}}; get_dummy_indices(ex);
\(\displaystyle{}a_{\mu} \partial_{\sigma}\left(b_{\mu} b_{\lambda}\right) \delta\left(p_{\rho} p^{\rho}\right)\)
{}$\big[$$\mu$$\big]$

get_free_indices(node: ExNode) -> List[Ex]

Return a list of all free indices inside the given node
\partial{#}::Derivative. ex := 0 + a_{\mu} \partial_{\sigma}{b_{\mu}b_{\lambda}} \delta{p_{\rho}p^{\rho}} + g_{\sigma\lambda}g_{\kappa}g^{\kappa} = p_{\sigma}p_{\lambda}; get_free_indices(ex);
\(\displaystyle{}a_{\mu} \partial_{\sigma}\left(b_{\mu} b_{\lambda}\right) \delta\left(p_{\rho} p^{\rho}\right)+g_{\sigma \lambda} g_{\kappa} g^{\kappa} = p_{\sigma} p_{\lambda}\)
{}$\big[$$\sigma$, $\lambda$$\big]$

symmetrise_slots(ex: Ex, slots: list[int], antisym: bool = False)

Symmetrise or antisymmetrise an expression in the given slots
Given an expression, iterates over all top level terms and symmetrises the indices in the given \texttt{slots}, so that e.g. symmetrise_slots($A_{a b c}$, [0,1]) $\rightarrow$ $A_{a b c} + A_{b a c}$. For an expression containing a sum of terms, all terms in the expression are symmetrised.
ex1 := R_{a b c d}: symmetrise_slots(ex1, [0,1], True) symmetrise_slots(ex1, [2,3], True) symmetrise_slots(ex1, [0,2]) symmetrise_slots(ex1, [1,3]) sort_sum(ex1); R_{a b c d}::RiemannTensor. ex2 := 12 R_{a b c d}: young_project_tensor(ex2) sort_sum(ex2); assert ex1 == ex2
\(\displaystyle{}R_{a b c d}-R_{a b d c}-R_{a c d b}+R_{a d c b}-R_{b a c d}+R_{b a d c}+R_{b c d a}-R_{b d c a}-R_{c a b d}+R_{c b a d}+R_{c d a b}-R_{c d b a}+R_{d a b c}-R_{d b a c}-R_{d c a b}+R_{d c b a}\)
\(\displaystyle{}R_{a b c d}-R_{a b d c}-R_{a c d b}+R_{a d c b}-R_{b a c d}+R_{b a d c}+R_{b c d a}-R_{b d c a}-R_{c a b d}+R_{c b a d}+R_{c d a b}-R_{c d b a}+R_{d a b c}-R_{d b a c}-R_{d c a b}+R_{d c b a}\)
Copyright © 2001-2025 Kasper Peeters
Questions? info@cadabra.science