2
$\begingroup$

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

Thanks,

$\endgroup$
3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Can we get the corresponding i as well? $\endgroup$ – Loi.Luu Nov 13 '13 at 18:39
  • 1
    $\begingroup$ @Loi.Luu. Yes we can. I'll add that now $\endgroup$ – RunnyKine Nov 13 '13 at 19:09
  • $\begingroup$ yeah, take your time :) $\endgroup$ – Loi.Luu Nov 13 '13 at 19:21
  • $\begingroup$ Have you tried with F(x) = x yet? It gives {} instead $\endgroup$ – Loi.Luu Nov 13 '13 at 19:58
  • $\begingroup$ @Loi.Luu Please see my update. $\endgroup$ – RunnyKine Nov 13 '13 at 21:08
2
$\begingroup$

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

First[Select[CoefficientList[polynomial, x], Positive]]
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – muzzlator Nov 13 '13 at 7:31
  • $\begingroup$ can we get the index i also? $\endgroup$ – Loi.Luu Nov 13 '13 at 18:50
2
$\begingroup$

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

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"})
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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