# Get the first positive coefficient in polynomial?

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

Thanks,

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


Usage:

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

• 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
• 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
• @Loi.Luu Please see my update. – RunnyKine Nov 13 '13 at 21:08

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

First[Select[CoefficientList[polynomial, x], Positive]]

• 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]


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

Cases[CoefficientList[pol,x],_?Positive,1,1];


If exponent required,

Cases[Reverse@CoefficientRules[pol],Rule[a_,b_?Positive]->(a->b),1,1]


Assuming (in x):

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


Or if exponent and coefficient required

Last@(Cases[CoefficientRules[pol],
Rule[{e_}, x_?Positive] -> {e -> x}]/.{} -> {"No positive coefficient"})