Sometimes when we have some equations we wish to add a constant to both sides or multiply by a factor.
If you naively try:
eq1 = a == b;
eq2 = c == d;
eq1 + 1 (* Out: 1 + (a == b) *)
eq1 + eq2 (* Out: (a == b) + (c == d) *)
2*eq1 (* Out: 2*(a == b) *)
To augment the functionality of Equal, we need to change built-in functions (not a smart idea).
To do so we'll use the following lines of code:
SetEqual = (Unprotect@Equal;
(* HoldPattern is needed to avoid missinterpretation of the pattern *)
Equal /: HoldPattern@Plus[Equal[a_, b_], c : Except[Equal[__], _]] := a + c == b + c;
Equal /: HoldPattern@Plus[Equal[a_, b_], Equal[c_, d_]] := a + c == b + d;
Equal /: HoldPattern@Times[Equal[a_, b_], c : Except[Equal[__], _]] := a*c == b*c;
Equal /: HoldPattern@Power[Equal[a_, b_], c : Except[0, _]] := a^c == b^c;
HoldPattern@Equal[L : List[a_, b__], c_] := (# == c & /@ L);
Protect@Equal;)
(* Undo built-in modifications *)
ClearEqual := (Unprotect@Equal; Clear@Equal; Protect@Equal;)
(* Make Simplify not change sides *)
SimplifySides[Equal[a_, b_]] := Simplify@a == Simplify@b
SimplifySides[L_List, args___] := SimplifySides[#, args] & /@ L
With this (after calling SetEqual
) we have:
eq1 + 1 (* Out: 1 + a == 1 + b *)
eq1 + eq2 (* Out: a + c == b + d *)
2*eq1 (* Out: 2*a == 2*b *)
And after we can just call ClearEqual
to avoid any mishaps.
With this we can complete the square:
*Where the use of HoldForm is needed otherwise Simplify is not smart enough. Also, List is needed because of the Plus or Minus square roots of the equation.