Transforming both sides of an equation in Mathematica
Today I tried to manually solve some equations when I realized I had made an error somewhere. So I turned to Mathematica to find the error. I wanted to multiply both sides of my equation by some factors, and to add or subtract some constants from both sides. Bascially, I wanted to do "equivalent transforms" ("Äquivalenzumformung" in German) with Mathematica.
Unfortunately, when doing operations on an equation in Mathematica, the operation does not "thread through" to the left hand side and the right hand side. So I defined these simple functions (careful, no error handling, no cases, no nothing)
eAdd[eq_, const_] := (eq[[1]] + const == eq[[2]] + const) ;
eSub[eq_, const_] := (eq[[1]] - const == eq[[2]] - const) ;
eMul[eq_, const_] := (eq[[1]] * const == eq[[2]] * const);
eDiv[eq_, const_] := (eq[[1]] / const == eq[[2]] / const);
ePow[eq_, const_ ] := (eq[[1]]^const == eq[[2]] const);
The first argument is assumed to be an equation (this is crucial, but not actually checked), the second argument the paramter that should be added/subtracted/divided... Now I can do simple equation transforms:
eq = (5 == 10 - a x);
eq = eAdd[eq, a x];
eq = eSub[eq, 5];
eq = eDiv[eq, a]
(*-> x == 5/a *)
Again, there is no error checking, and no warning for edge-cases (for instance, a=0 is not checked). It's no replacement to Solve[eq, x].
Another function that came in handy a few times: Expanding a fraction with some factor:
fracExpandWith[fraction_, factor_] :=
Simplify[Numerator[fraction]*factor]/
Simplify[Denominator[fraction]*factor]
This is helpful to massage fractions which Mathematica has trouble simplifying:
fracExpandWith[(1/x^2 + 5) / (a + 5/x), x^2]
(*-> (1 + 5 x^2)/(x (5 + a x)) *)
Of course, this function also lacks all safety and sanity checks. Wouldn't be fun, otherwise.