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}}
With my limited Mathematica knowledge, I would do something like:
First[Select[CoefficientList[polynomial, x], Positive]]
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"})