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?

=================

Related

– Simon Woods

Feb 9 ’15 at 21:38

=================

2 Answers

2

=================

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 *)

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 ==

– Miguel

Feb 11 ’15 at 20:25

@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.

– kglr

Feb 11 ’15 at 20:33

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]