1
$\begingroup$

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.

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

1 Answer 1

1
$\begingroup$
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 ]

{}

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Commented Apr 9, 2018 at 3:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.