1
$\begingroup$

After doing some preceding work I end up with a polynomial equation that should look something like this:

Eq = c + b x + a x^2 - d x^2 == k

I would like to convert this equation to

Eq = c + b x + a x^2 - d x^2 - k == 0

then extract the LHS into a variable:

Poly = c + b x + a x^2 - d x^2 - k

and proceed with my analysis to do things like

Collect[Poly,x]

which would return

c - k + b x + (a - d) x^2

Is there a way to automate this procedure?

$\endgroup$
3
$\begingroup$
eq = c + b x + a x^2 - d x^2 == k;

Collect[Subtract @@ eq, x]

or

Collect[eq /. Equal -> Subtract, x]

both give

(* c - k + b x + (a - d) x^2 *)
$\endgroup$
  • $\begingroup$ Subtract @@ eq works perfectly! Can you explain how this works? I understand that @@ applies eq as argument to Subtract, but nowhere in the documentation of subtract does it mention how it treats the == $\endgroup$ – Miguel Feb 11 '15 at 20:25
  • $\begingroup$ @Miguel, see Apply (@@) : Apply[f,expr] or f@@expr replaces the head of expr by f. The head of eq is Equal (you can see that by checking FullForm[eq] and/or Head[eq]) and Apply[Subtract, eq] (or Subtract@@ eq) replaces Equal with Subtract so that Equal[something] becomes Subtract[something]. Btw thank you for the accept. $\endgroup$ – kglr Feb 11 '15 at 20:33
2
$\begingroup$

one way

Clear[c, b, x, a, d, k, lhs, rhs];
eq = c + b x + a x^2 - d x^2 == k;
lhs = eq /. (lhs_) == (rhs_) -> lhs;
rhs = eq /. (lhs_) == (rhs_) -> rhs;
poly = lhs - rhs == 0;
Collect[poly, x]

Mathematica graphics

$\endgroup$

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.