Take a nonlinear equation such as
exp = (x + 3)/4 + Exp[x] + 1 + (c + x)
Note that this is not a polynomial. Now, I want to extract the coefficients of this. A few are easy:
Coefficient[exp, x ] (* Correctly gives 5/4*)
Coefficient[exp, Exp[x] ] (* Correctly gives 1*)
But how can I extract the coefficient on the constant term?
I can't figure out how to write the "form" for the coefficient function to extract it. Note that treating it as a polyomial and asking for the 0th order will not work (e.g. Coefficient[exp, x,0]
is not correct)
Answer
You can use CoefficientList
:
exp = (x + 3)/4 + Exp[x] + 1 + (c + x);
CoefficientList[exp, {x, Exp[x]}]
(* {{7/4 + c, 1}, {5/4, 0}} *)
Whether that is a convenient way depends on what you want to do with it. Following extracts the parts:
{{const, ExpC}, {xC, xExpC}} = %
Comments
Post a Comment