Symbolics and Polynomial

Is there a function that lets you know if a given expression is a polynomial?

The polynomial_coeffs function can be used for this.

1 Like

This seems to work:

function isSingleVariablePoly(espr)
    variables = Symbolics.get_variables(espr)
    if !isone(length(variables))
        return false
    end
    x = variables[1]
    Symbolics._iszero(Symbolics.polynomial_coeffs(espr, [x])[2]) ? true : false
end
1 Like

That should work. It might be more flexible to allow the user to pass the variables in, such as:

function is_poly(expr, vars)
       p,r = polynomial_coeffs(expr, vars)
       length(intersect(Symbolics.get_variables(r), vars)) == 0
end
2 Likes