0
$\begingroup$

I wonder that which command can rearrange one side of a equation into 0, e.g. "ax==by" into "ax-by==0".

$\endgroup$
1
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks aardvark2012! your way seems elegant. $\endgroup$ – HC6 Nov 29 '17 at 7:00
  • $\begingroup$ @HC6 Realised I was needlessly complicating it. Fixed now. $\endgroup$ – aardvark2012 Nov 29 '17 at 7:11
1
$\begingroup$

"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

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Oh, silly me! I do mean "ax-by==0" $\endgroup$ – HC6 Nov 29 '17 at 6:47
1
$\begingroup$

You can use Thread on Equal:

eqn = a x == b y;

Thread[eqn - b y, Equal]

a x - b y == 0

|improve this answer|||||
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – HC6 Nov 29 '17 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.