pg_utils.numerics.symparser.powers_of

pg_utils.numerics.symparser.powers_of(expr: Expr, *args: Symbol, return_expr: bool = False)[source]

Retrieve the power of symbols in a given expression.

Parameters:
  • expr (sympy.Symbol) – symbolic expression

  • expr – symbols whose powers are to be estimated

  • return_expr (bool) – whether to return the expressions

Returns:

list of powers, optionally with the respective terms (if return_expr)

Usage:

Assume we have symbols defined as p, q, a, b, n, m = sympy.symbols("p q a b m n"). We can calculate the powers in a monomial:

>>> sample_monomial = p**2*q**(n + 2*m)*jacobi(n, a, b, p**2 + 1)
>>> powers_of(sample_monomial, p, q)
[2*n + 2, 2*m + n]

Or we can calculate the powers inccurred in a polynomial:

>>> sample_polynomial = q**2*chebyshev(n, p*q) + p**(n + m)*sp.jacobi(n, a, b, p**2*q**3)
>>> powers_of(sample_polynomial, p, q)
[[m + 4*n, 2*n], [n, n + 2]]

Warning

This is a very intricate method, and must be used with care, with sanitized input.

If a special function is present in the expression, it will be interpreted as a polynomial, whose first argument is the degree of the polynomial. This at least works for Jacobi polynomials and their special types.