# Picking out terms according to proportionality in expressions

I am trying to figure out how to pick terms proportional to $x$ (or any other expression).

I have the following problem:

I want to pick out $(1+2y) x$ from a polynomial of the form $(1+2y)x + e^xx + y^2$ (i.e., terms proportional to $x$).

I tried the command Coefficient, i.e. Coefficient[(1+2y)x + e^x x + y^2,x] x, but the output is $((1+2y)+e^x)x$. So the expression has $e^x$ in it and it is not what I am looking for.

Is there a way to pick out only the terms linear wrt $x$ from the polynomial above? I.e. constant (for x) coefficients of $x^1$

Thanks a lot.

Ps. I am only using the above polynomial as an example. In a real situation I'd expect to see polynomials with many special functions.

• what does terms proportional to x mean? In what sense is (1+2 y) proportional to x Commented Apr 3, 2018 at 7:29
• Sorry, I omitted the x term accidentally since that was what Coefficient was returning. I've edited the question. Thanks for pointing this out.
– OTH
Commented Apr 3, 2018 at 15:09
• Perhaps Cases[Expand@expr, Longest[a_.] x /; FreeQ[a, x] :> a] works. Commented Apr 3, 2018 at 16:43
• You mean the constant (for $x$) coefficients of $x^1$? Commented Apr 5, 2018 at 2:51
• @march That seems to work! Thanks a lot! Feel free to post as an answer
– OTH
Commented Apr 6, 2018 at 7:44

prop[exp_] :=
Module[{c = Coefficient[exp, x]},
c =!= 0 && Cases[c, x, Infinity] == {}]
Select[ List @@ ((1 + 2 y) x + e^x x + y^2) , prop ]


{x (1 + 2 y)}

note this only finds terms in the form given..

Select[ List @@ ((1 + 2 y + Sin[x]) x + e^x x + y^2) , prop ]


{}

• I see; thanks. However, I was looking for something a bit more general; it seems like March in comments section was giving the more general case. However, for a particular case this is very helpful, so thanks.
– OTH
Commented Apr 9, 2018 at 3:52