I wonder that which command can rearrange one side of a equation into 0, e.g. "ax==by" into "ax-by==0".
5 Answers
"ax==by" into "ax+by==0".
I assume you meant a x - b y ==0 in the above.
One way (out of many I am sure) is
ClearAll[x,y,a,b,lhs,rhs];
eq=a x==b y;
lhs=eq/.(lhs_==rhs_)-> lhs;
rhs=eq/.(lhs_==rhs_)-> rhs;
eq=lhs-rhs==0

- 143,286
- 11
- 154
- 359
- 130,679
- 6
- 243
- 355
-
Thanks for your reply! However, the actual case is a bit more complicated, where both sides is not konwn, and your solution might be not so easy to carry out. – Charles6 Nov 29 '17 at 06:53
Here are a couple of ways:
#1 - #2 == 0 & @@ (a x == b y)
(* a x - b y == 0 *)
#[[1]] - #[[2]] == 0 &@ (a x == b y)
(* a x - b y == 0 *)
As far as I can tell, they're going to work on any equation, with no modification. And it's straightforward to define
lhsequals0 = #[[1]] - #[[2]] == 0 &
so that, for example
lhsequals0[3 x^2 + 2 y - 4 == 6 xy^2 - 4 x^2 + Cos[Sqrt[y]]]
(* -4 + 7 x^2 - 6 xy^2 + 2 y - Cos[Sqrt[y]] == 0 *)
Just to give a sense of how and why this works, look at the FullForm for your equation:
FullForm[a x == b y]
(* Equal[Times[a, x], Times[b, y]] *)
An equation will always have the head Equal, with the first argument (#[[1]] or #1, depending) being the lhs and the second (#[[2]] or #2) being the rhs. So all both of the above functions are doing is subtracting the rhs from the lhs and setting Equal to zero.
Do see @kglr's link in the comments for some more in-depth answers.
- 5,424
- 1
- 11
- 22
Since V 12.3 we have SubtractSides and other related functions.
f = a x == b y;
SubtractSides[f, b y]
a x - b y == 0
- 67,911
- 5
- 60
- 168
A minor rephrasing of the existing answers:
f1 = a x == b y;
f2 = 3 x^2 + 2 y - 4 == 6 xy^2 - 4 x^2 + Cos[Sqrt[y]];
Subtract @@ # == 0 & /@ {f1, f2}
{a x - b y == 0, -4 + 7 x^2 - 6 xy^2 + 2 y - Cos[Sqrt[y]] == 0}
- 52,495
- 4
- 30
- 85
# - eqn[[-1]] & /@ eqn– Bob Hanlon Nov 29 '17 at 03:31