I would like to write a program to find the leading coefficient of a given polynomial. (Sounds easy, right?) I'd like to be able to handle the format x^2-x+7
(with any formal variable x
) as well as the format #^2-#+7&
. I would also like to be able to reject/error on inputs which are not univariate polynomials.
I thought the function Variables
would make this easy, but it turns out that
Variables[#^2-#+7&]
gives
{#1^2 - #1 + 7 &}
which isn't helpful. My current plan:
- Check if the input is a function; if so, substitute
f[\[FormalX]]
forf
. - Check if
Variables[f]
has length other than 1; if so, fail. - Define
x
asVariables[f][[1]]
and returnCoefficient[f,x,Exponent[f,x]]
.
But I'm stuck on the first step. Is there a good way to do this? Is there a better approach to what I'm trying to do?
yourFunction[x_Function]:= do something to x.
? $\endgroup$Variables[#^2 - # + 7]
will work, tho. $\endgroup$Variables@First@Function[#^2 - # + 7]
$\endgroup$PolynomialQ[]
. $\endgroup$