2A bunch of functions and classes extending the sympy library
6def categorize(expr:
'Expr', unknown:
'Symbol'=...) -> list:
7 """ Catagorize `expr` as a function of any of:
15 Returns a list of strings of the types it is
17 NOTE: `expr` must be a sympy expression,
and only have one unknown
in it.
18 Results
for expressions
with more than 1 unknown
is undefined.
20 `unknown` defaults to Symbol(
'x')
22 from sympy
import Symbol, Pow, Rational
29 if expr.is_polynomial():
30 rtn.append(
'polynomial')
32 if expr.is_rational_function():
33 rtn.append(
'rational')
35 if (p := expr.as_poly())
is not None and p.degree() == 2:
36 rtn.append(
'quadratic')
38 if isinstance(expr, Pow)
and isinstance(expr.exp, Rational)
and int(expr.exp) != expr.exp
and expr.base == unknown:
41 parts = expr.as_coeff_Mul()
42 if parts[0].is_real
and isinstance(parts[1], Pow)
and parts[1].base == unknown
and parts[1].exp.is_real:
45 if isinstance(expr, Pow)
and isinstance(expr.base, Rational)
and expr.exp == unknown:
46 rtn.append(
'exponential')
list categorize('Expr' expr, 'Symbol' unknown=...)
Catagorize expr as a function of any of: polynomial rational quadratic root power exponential.