Cadabra
a field-theory motivated approach to computer algebra

cdb.core.manip

Manipulating equations, inequalities and similar expressions
This package contains various helper functions to manipulate expressions in elementary ways, e.g. moving terms around or multiplying sides of an expression by a factor. In order to use this package, import it with the standard Python import statement: if you do import cdb.core.manip as manip then e.g. the isolate function is available as manip.isolate.

eq_to_subrule(ex: Ex) -> Ex

Create substitution rule from an equation.
Creates a substitution rule from an equation Ex.
eq_to_subrule($a = b + c + d$);
\(\displaystyle{}b+c+d \rightarrow a\)
b + c + d -> a

multiply_through(ex: Ex, factor: Ex, auto_distribute=False) -> Ex

Multiply all terms by the same factor.
Multiplies all terms in ex by factor. If auto_distribute is True, distribute is called on the resulting expression.
multiply_through($a+b < c$, $k$);
\(\displaystyle{}k \left(a+b\right) < k c\)
k (a + b) < k c

add_through(ex: Ex, factor: Ex) -> Ex

Add a factor to both sides of an equation.
add_through($A_{\mu}B_{\nu} = C_{\mu \nu}$, $D_{\mu \nu}$);
\(\displaystyle{}A_{\mu} B_{\nu}+D_{\mu \nu} = C_{\mu \nu}+D_{\mu \nu}\)
A_{\mu} B_{\nu} + D_{\mu \nu} = C_{\mu \nu} + D_{\mu \nu}

apply_through(ex: Ex, op: Ex) -> Ex

Apply an operator through both sides of an equation.
apply_through($\partial{x_{\mu}} = 0$, $\partial{#}$);
\(\displaystyle{}\partial\left(\partial\left(x_{\mu}\right)\right) = \partial\left(0\right)\)
\partial(\partial(x_{\mu})) = \partial(0)

get_lhs(ex: Ex) -> Ex

Return left-hand side of an expression
Returns the left hand side of an equation, inequality or expression whose top node contains two children.
get_lhs($a=b$);
\(\displaystyle{}a\)
a

get_rhs(ex: Ex) -> Ex

Return right-hand side of an expression
Returns the right-hand side of an equation, inequality or expression whose top node contains two children.
get_rhs($a=b$);
\(\displaystyle{}b\)
b

swap_sides(ex: Ex) -> Ex

Swap the left and right-hand sides of an expression.
Swaps the two sides of an equation, inequality or expression whose top node contains two children. Inequalities will flip as appropriate.

get_factor(node: ExNode, term: Ex) -> Ex

Get all multiplicative factors of term in node
get_factor($3/2 R$.top(), $R$);
\(\displaystyle{}\frac{3}{2}\)
3/2

get_basis_component(ex: Ex, basis: Ex) -> Ex

Returns the multiplicative coefficients of \texttt{basis
in \texttt{ex}}
test := A_{\mu\nu} \exp(i*k_\mu*x^{\mu})+B_{\mu\nu} \exp(-i*k_\mu*x^{\mu}); get_basis_component(test, $\exp(-i*k_\mu*x^{\mu})$); get_basis_component(test, $A_{\mu\nu}$); get_basis_component(test, $k_{\mu}$); get_basis_component(test, $C_{\mu\nu}$);
\(\displaystyle{}A_{\mu \nu} \exp\left(i k_{\mu} x^{\mu}\right)+B_{\mu \nu} \exp\left(-i k_{\mu} x^{\mu}\right)\)
A_{\mu \nu} \exp(i k_{\mu} x^{\mu}) + B_{\mu \nu} \exp(-i k_{\mu} x^{\mu})
\(\displaystyle{}B_{\mu \nu}\)
B_{\mu \nu}
\(\displaystyle{}\exp\left(i k_{\mu} x^{\mu}\right)\)
\exp(i k_{\mu} x^{\mu})
\(\displaystyle{}A_{\mu \nu} \exp\left(i x^{\mu}\right)+B_{\mu \nu} \exp\left(-i x^{\mu}\right)\)
A_{\mu \nu} \exp(i x^{\mu}) + B_{\mu \nu} \exp(-i x^{\mu})
\(\displaystyle{}0\)
0

to_rhs(ex: Ex, *parts: Ex..., match_type: str = "all") -> Ex

Move the indicated term of the expression to the right-hand side.
If ex is an equation or inequality which contains part on its left hand side, then moves all parts to the right hand side. match_type can be either all which will move all terms which contain each part as a factor, numeric which will move terms up to a numeric prefactor or exact which will only move the term if the subtree matches exactly.
to_rhs($2a+b+j*b+k*b = c+d$);
\(\displaystyle{}0 = c+d-2a-b-j b-k b\)
0 = c + d-2a-b-j b-k b
\partial{#}::Derivative. to_rhs($-\partial_{\alpha}{b_{\beta}} - \partial_{\beta}{a_{\alpha}} = 0$, $\partial_{\alpha}{b_{\beta}}$, match_type="exact");
\(\displaystyle{}-\partial_{\alpha}{b_{\beta}}-\partial_{\beta}{a_{\alpha}} = 0\)
-\partial_{\alpha}(b_{\beta})-\partial_{\beta}(a_{\alpha}) = 0

to_lhs(ex: Ex, *parts: Ex..., match_type: str = "all") -> Ex

Move the indicated term of the expression to the left-hand side.
Complement of to_rhs.

isolate()

Isolates a term on the left hand side of an equation or inequality
ex := 2a + 1/2 a * k - b = c * a + b * k + a - 4; isolate(ex, $a$, True); ex2 := -3/2 R + 5\Lambda = T\kappa; isolate(ex2, $R$);
\(\displaystyle{}2a+\frac{1}{2}a k-b = c a+b k+a-4\)
2a + 1/2 a k-b = c a + b k + a-4
\(\displaystyle{}a = \left(b k-4+b\right) {\left(1-c\right)}^{-1}\)
a = (b k-4 + b) (1-c)**(-1)
\(\displaystyle{} - \frac{3}{2}R+5\Lambda = T \kappa\)
- 3/2 R + 5\Lambda = T \kappa
\(\displaystyle{}R = - \frac{2}{3}T \kappa+\frac{10}{3}\Lambda\)
R = - 2/3 T \kappa + 10/3 \Lambda
Copyright © 2001-2021 Kasper Peeters
Questions? info@cadabra.science