# 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. ## 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\)

\less(k (a + b) , k c)

## 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.
swap_sides($a=b$);

\(\displaystyle{}b = a\)

b = a

swap_sides($a>b$);

\(\displaystyle{}b < a\)

\less(b , a)

## to_rhs(ex: Ex, part: Ex) -> 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 `part`

to the right hand side.
`part`

must contain the multiplier of the term to be moved, including minus signs. to_rhs($a+b = c+d$, $b$);

\(\displaystyle{}a = c+d-b\)

a = c + d-b

to_rhs($a-b = c$, $-b$);

\(\displaystyle{}a = c+b\)

a = c + b

## to_lhs(ex: Ex, part: Ex) -> Ex

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

Complement of `to_rhs`

.# Moving terms test
if __name__ == "__main__":
ex0 := a - b = c + d;
to_rhs(ex0, $-b$);
to_rhs(ex0, $a$);
to_lhs(ex0, $d + b$);

\(\displaystyle{}a = c+d+b\)

a = c + d + b

\(\displaystyle{} = c+d+b-a\)

= c + d + b-a

\(\displaystyle{}-d-b = c-a\)

-d-b = c-a

# Equation test
if __name__ == "__main__":
ex1 := a_{\mu}a^{\mu} + b = c;
eq_to_subrule(ex1);
get_lhs(ex1);
get_rhs(ex1);
multiply_through(ex1, $k_{\alpha}$);
swap_sides(ex1);
swap_sides(ex1);

\(\displaystyle{}c \rightarrow a_{\mu} a^{\mu}+b\)

c → a_{\mu} a^{\mu} + b

\(\displaystyle{}a_{\mu} a^{\mu}+b\)

a_{\mu} a^{\mu} + b

\(\displaystyle{}c\)

c

\(\displaystyle{}k_{\alpha} \left(a_{\mu} a^{\mu}+b\right) = k_{\alpha} c\)

k_{\alpha} (a_{\mu} a^{\mu} + b) = k_{\alpha} c

\(\displaystyle{}k_{\alpha} c = k_{\alpha} \left(a_{\mu} a^{\mu}+b\right)\)

k_{\alpha} c = k_{\alpha} (a_{\mu} a^{\mu} + b)

\(\displaystyle{}k_{\alpha} \left(a_{\mu} a^{\mu}+b\right) = k_{\alpha} c\)

k_{\alpha} (a_{\mu} a^{\mu} + b) = k_{\alpha} c

# Inequality test
if __name__ == "__main__":
{f, i}::Symbol.
ex2 := w_{f} - w_{i} > 2 \pi W_{a b}\beta^{a}\beta^{b};
get_lhs(ex2);
get_rhs(ex2);
swap_sides(ex2);
swap_sides(ex2);
multiply_through(ex2, $2n$);

\(\displaystyle{}w_{f}-w_{i}\)

w_{f}-w_{i}

\(\displaystyle{}2\pi W_{a b} \beta^{a} \beta^{b}\)

2\pi W_{a b} \beta^{a} \beta^{b}

\(\displaystyle{}2\pi W_{a b} \beta^{a} \beta^{b} < w_{f}-w_{i}\)

\less(2\pi W_{a b} \beta^{a} \beta^{b} , w_{f}-w_{i})

\(\displaystyle{}w_{f}-w_{i} > 2\pi W_{a b} \beta^{a} \beta^{b}\)

\greater(w_{f}-w_{i} , 2\pi W_{a b} \beta^{a} \beta^{b})

\(\displaystyle{}2n \left(w_{f}-w_{i}\right) > 4n \pi W_{a b} \beta^{a} \beta^{b}\)

\greater(2n (w_{f}-w_{i}) , 4n \pi W_{a b} \beta^{a} \beta^{b})

# Fail tests
if __name__ == "__main__":
ex3 := a, b, c;
try:
eq_to_subrule(ex3)
except Exception as e:
e;
try:
get_lhs(ex3)
except Exception as e:
e;
try:
get_rhs(ex3)
except Exception as e:
e;
try:
swap_sides(ex3)
except Exception as e:
e;
try:
to_rhs(ex3, $a$)
except Exception as e:
e;
try:
to_lhs(ex3, $b$)
except Exception as e:
e;
try:
t::Coordinate.
{i, j}::Indices(values={t}, position=fixed).
ex := a_{i}.
evaluate(ex, ${a_{t} = 1}$)
get_component(ex, $x$)
except Exception as e:
e;

eq_to_subrule called on expression which is not an equation

get_lhs called on expression whose top node contains 3 arguments, 2 required

get_lhs called on expression whose top node contains 3 arguments, 2 required

swap_sides called on expression whose top node contains 3 arguments, 2 required

to_lhs called on expression which is not an equation or inequality

swap_sides called on expression whose top node contains 3 arguments, 2 required

name 'get_component' is not defined