Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

Thanks,

share|improve this question
add comment

4 Answers 4

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

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

share|improve this answer
    
Can we get the corresponding i as well? –  Loi.Luu Nov 13 '13 at 18:39
1  
@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
show 1 more comment

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

First[Select[CoefficientList[polynomial, x], Positive]]
share|improve this answer
1  
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
add comment

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
add comment

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"})
share|improve this answer
add comment

Your Answer

 
discard

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.