Summary
I want to extract linear/quadratic/nonlinear terms from nonlinear expressions.
Description
I first tried expressions.args and check the polynomial_degree() of each subexpressions. However, if the subexpression is a constant. An error will be reported since int and float have no polynomial_degree().
Then I tried first transforming expressions into repn and then obtaining the linear/quadratic/nonlinear terms. I got stuck in the second step. The nonlinear_expr is stored in the repn, but linear_expr and quadratic_expr are not stored.
I know that linear_coefs, linear_vars, quadratic_coefs and quadratic_vars are provided. linear_expr and quadratic_expr can be obtained by SUMPRODUCT.
Is there an existing function to do this? I think to_expression might be the best candidate. Can we add some args, like linear_only, quadratic_only for this function?