Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature power_list to return all powers of a variable present in f #17043

Closed
wants to merge 14 commits into from
56 changes: 56 additions & 0 deletions sympy/polys/polytools.py
Expand Up @@ -1818,6 +1818,32 @@ def total_degree(f):
else: # pragma: no cover
raise OperationNotSupported(f, 'total_degree')

def power_list(f):
"""
Returns a list of powers of variables in ``f``.

Examples
========

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + y*x + 1, x, y).power_list()
[(2, 1, 0), (1, 0)]
>>> Poly(0, x).power_list()
[(0,)]
>>> Poly(x, x).power_list()
[(1,)]
"""
power = []
for i in range(len(f.gens)):
tmp = []
for j in f.monoms():
tmp.append(j[i])
tmp = reversed(list(set(tmp)))
power.append(tuple(tmp))
return power

def homogenize(f, s):
"""
Returns the homogeneous polynomial of ``f``.
Expand Down Expand Up @@ -4551,6 +4577,36 @@ def degree_list(f, *gens, **args):

return tuple(map(Integer, degrees))

@public
jmig5776 marked this conversation as resolved.
Show resolved Hide resolved
def power_list(f, *gens, **args):
"""
Return a list of powers of ``f`` in all variables.

Examples
========

>>> from sympy import power_list
>>> from sympy.abc import x, y

>>> power_list(x**2 + y*x + 1)
[(2, 1, 0), (1, 0)]
>>> power_list(0, x)
[(0,)]
>>> power_list(x, x)
[(1,)]

jmig5776 marked this conversation as resolved.
Show resolved Hide resolved
"""
options.allowed_flags(args, ['polys'])

try:
F, opt = poly_from_expr(f, *gens, **args)
except PolificationFailed as exc:
raise ComputationFailed('power_list', 1, exc)

powers = F.power_list()

return list(powers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In keeping with degree_list method vs function, the function return values should be sympified, so return sympify(list(powers)) will return Tuples of Integers.

@jksuom , what do you think about adding this function/method? I think the ambiguous output has been resolved and the function will find use at least in the Lambert work that is taking place. It may be of pedagogical interest, too.



@public
def LC(f, *gens, **args):
Expand Down
25 changes: 25 additions & 0 deletions sympy/polys/tests/test_polytools.py
Expand Up @@ -5,6 +5,7 @@
parallel_poly_from_expr,
degree, degree_list,
total_degree,
power_list,
LC, LM, LT,
pdiv, prem, pquo, pexquo,
div, rem, quo, exquo,
Expand Down Expand Up @@ -1220,6 +1221,30 @@ def test_Poly_total_degree():
assert total_degree(Poly(y**2 + x**3 + z**4, x), z) == 4
assert total_degree(Poly(x**9 + x*z*y + x**3*z**2 + z**7,x), z) == 7

def test_Poly_power_list():
assert Poly(0, x).power_list() == [(0,)]
assert Poly(0, x, y).power_list() == [(0,), (0,)]
assert Poly(0, x, y, z).power_list() == [(0,), (0,), (0,)]

assert Poly(1, x).power_list() == [(0,)]
assert Poly(1, x, y).power_list() == [(0,), (0,)]
assert Poly(1, x, y, z).power_list() == [(0,), (0,), (0,)]

assert Poly(x**2*y + x**3*z**2 + 1).power_list() == [(3, 2, 0), (1, 0), (2, 0)]

assert power_list(x, x) == [(1,)]

assert power_list(x*y**2 + x + 1) == [(1, 0), (2, 0)]
assert power_list(x*y**2 + x) == [(1,), (2, 0)] # power of y is zero with x
assert power_list(x*y**2) == [(1,), (2,)]

assert power_list(x**8*(1 - x)**9) == [(17, 16, 15, 14, 13, 12, 11, 10, 9, 8)]
# >>>expand(x**8*(1 - x)**9)
jmig5776 marked this conversation as resolved.
Show resolved Hide resolved
# -x**17 + 9*x**16 - 36*x**15 + 84*x**14 - 126*x**13 + 126*x**12 - 84*x**11
# + 36*x**10 - 9*x**9 + x**8

raises(ComputationFailed, lambda: power_list(1))

def test_Poly_homogenize():
assert Poly(x**2+y).homogenize(z) == Poly(x**2+y*z)
assert Poly(x+y).homogenize(z) == Poly(x+y, x, y, z)
Expand Down