Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
Loading...
Searching...
No Matches
sympy.py
1"""
2A bunch of functions and classes extending the sympy library
3"""
4__version__ = '1.0.0'
5
6def categorize(expr:'Expr', unknown:'Symbol'=...) -> list:
7 """ Catagorize `expr` as a function of any of:
8 polynomial
9 rational
10 quadratic
11 root
12 power
13 exponential
14
15 Returns a list of strings of the types it is
16
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.
19
20 `unknown` defaults to Symbol('x')
21 """
22 from sympy import Symbol, Pow, Rational
23
24 if unknown is ...:
25 Symbol('x')
26
27 rtn = []
28 # Polynomial
29 if expr.is_polynomial():
30 rtn.append('polynomial')
31 # Rational
32 if expr.is_rational_function():
33 rtn.append('rational')
34 # Quadratic
35 if (p := expr.as_poly()) is not None and p.degree() == 2:
36 rtn.append('quadratic')
37 # Root
38 if isinstance(expr, Pow) and isinstance(expr.exp, Rational) and int(expr.exp) != expr.exp and expr.base == unknown:
39 rtn.append('root')
40 # Power
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:
43 rtn.append('power')
44 # Exponential
45 if isinstance(expr, Pow) and isinstance(expr.base, Rational) and expr.exp == unknown:
46 rtn.append('exponential')
47 return rtn
list categorize('Expr' expr, 'Symbol' unknown=...)
Catagorize expr as a function of any of: polynomial rational quadratic root power exponential.
Definition: sympy.py:6