Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have Polynomial $F(x) = \sum_{i \leq n}{a_ix^i}$. How to get the first $a_i > 0$?


share|improve this question
up vote 3 down vote accepted

suppose our polynomial is

   pol = -6 x^8 + 3 x^2 + 2 x^3 - 4 x^5 + 6 x^4 - 2 x^6 - 2 x^7 - 3 x

We can get the first positive coefficient as follows:

Cases[CoefficientList[pol, x], n_ /; Positive[n], Infinity, 1]

To get both the a and the corresponding i here's an elaborate function that lets you choose how many you want to select:

coeff[{pol_, x_} /; PolynomialQ[pol, x], k_Integer] := 
 With[{ex = Exponent[pol, x, List], cf = DeleteCases[CoefficientList[pol, x], 0]},
  Cases[Transpose[{cf, ex}], {_?Positive, _}, Infinity, k]]


coeff[{pol, x}, 1]

{{3, 2}}

coeff[{pol, x}, 2]    

{{3, 2}, {2, 3}}

This also works if your polynomial is just pol = x

coeff[{x, x}, 1]    

{{1, 1}}

share|improve this answer
Can we get the corresponding i as well? – Loi.Luu Nov 13 '13 at 18:39
@Loi.Luu. Yes we can. I'll add that now – RunnyKine Nov 13 '13 at 19:09
Thank you so much :) – Loi.Luu Nov 13 '13 at 19:10
yeah, take your time :) – Loi.Luu Nov 13 '13 at 19:21
Have you tried with F(x) = x yet? It gives {} instead – Loi.Luu Nov 13 '13 at 19:58

With my limited Mathematica knowledge, I would do something like:

First[Select[CoefficientList[polynomial, x], Positive]]
share|improve this answer
Should mention that this will die if you have no positive coefficients. I suppose add a bit of fidgety stuff to deal with this if you aren't sure about the form of your input. – muzzlator Nov 13 '13 at 7:31
can we get the index i also? – Loi.Luu Nov 13 '13 at 18:50

Function Select has a nice third argument indication how many object that meet your requiremenrts should be taken. So I think one of the veriants to solve the problem is

Select[CoefficientList[pol, x], Positive, 1]
share|improve this answer

Some variants: If empty set ok for single variable power series:


If exponent required,


Assuming (in x):

    CoefficientList[pol, x], _?
     Positive] /. {} -> {"No positive coefficient"})

Or if exponent and coefficient required

  Rule[{e_}, x_?Positive] -> {e -> x}]/.{} -> {"No positive coefficient"})
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.