Repository: sympy/sympy
File: sympy/polys/tests/test_polytools.py (Python)

Code before:

    raises(PolynomialError, lambda: Poly(1, x).degree(gen=1))
    raises(PolynomialError, lambda: Poly(1, x).degree(gen=y))
    raises(PolynomialError, lambda: Poly(1, x).degree(gen='y'))

    assert Poly(1, x, y).degree() == 0
    assert Poly(2*y, x, y).degree() == 0
    assert Poly(x*y, x, y).degree() == 1

    assert Poly(1, x, y).degree(gen=x) == 0
    assert Poly(2*y, x, y).degree(gen=x) == 0
    assert Poly(x*y, x, y).degree(gen=x) == 1

    assert Poly(1, x, y).degree(gen=y) == 0
    assert Poly(2*y, x, y).degree(gen=y) == 1
    assert Poly(x*y, x, y).degree(gen=y) == 1

    assert degree(0, x) is -oo
    assert degree(1, x) == 0
    assert degree(x, x) == 1

    assert degree(x*y**2, x) == 1
    assert degree(x*y**2, y) == 2
    assert degree(x*y**2, z) == 0

    assert degree(pi) == 1
    raises(TypeError, lambda: degree(y**2 + x**3))
    raises(TypeError, lambda: degree(y**2 + x**3, 1))
    raises(PolynomialError, lambda: degree(x**2/(x**3 + 1), x))

    assert degree(Poly(0,x),z) is -oo
    assert degree(Poly(1,x),z) == 0
    assert degree(Poly(x**2+y**3,y)) == 3
    assert degree(Poly(y**2 + x**3, y, x), 1) == 3
    assert degree(Poly(y**2 + x**3, x), z) == 0
    assert degree(Poly(y**2 + x**3 + z**4, x), z) == 4


    # optimization
    big = 2  # make this number 1000 when full expansion can be avoided

    assert degree((x + 1)**big, x) == big  # Large power (fast-path, no expansion)
    assert degree((x + 1)**big + x**(big-1), x) == big  # Addition without cancellation
    assert degree((x + 1)**2 - x**2, x) == 1  # Addition with cancellation (fallback required)
    assert degree(x*(x + 1)**big, x) == big + 1  # Nested multiplication
    assert degree(y*(x + 1)**big, x) == big  # Generator independence

    # checking 0 detection
    Z = pi*(1/pi + 1) - 1 - pi
    assert degree(Z**big, x) is S.NegativeInfinity
    assert degree(Z**big, pi) is S.NegativeInfinity

Review comment (suggestion):
```suggestion
    assert degree(pi) == 1
    raises(TypeError, lambda: degree(I))  # Poly sees a number but no gen -> needs gen
```

Code after:

    raises(PolynomialError, lambda: Poly(1, x).degree(gen=1))
    raises(PolynomialError, lambda: Poly(1, x).degree(gen=y))
    raises(PolynomialError, lambda: Poly(1, x).degree(gen='y'))

    assert Poly(1, x, y).degree() == 0
    assert Poly(2*y, x, y).degree() == 0
    assert Poly(x*y, x, y).degree() == 1

    assert Poly(1, x, y).degree(gen=x) == 0
    assert Poly(2*y, x, y).degree(gen=x) == 0
    assert Poly(x*y, x, y).degree(gen=x) == 1

    assert Poly(1, x, y).degree(gen=y) == 0
    assert Poly(2*y, x, y).degree(gen=y) == 1
    assert Poly(x*y, x, y).degree(gen=y) == 1

    assert degree(0, x) is -oo
    assert degree(1, x) == 0
    assert degree(x, x) == 1

    assert degree(x*y**2, x) == 1
    assert degree(x*y**2, y) == 2
    assert degree(x*y**2, z) == 0

    assert degree(pi) == 1
    raises(TypeError, lambda: degree(I))  # Poly sees a number but no gen -> needs gen
    raises(TypeError, lambda: degree(y**2 + x**3))
    raises(TypeError, lambda: degree(y**2 + x**3, 1))
    raises(PolynomialError, lambda: degree(x**2/(x**3 + 1), x))

    assert degree(Poly(0,x),z) is -oo
    assert degree(Poly(1,x),z) == 0
    assert degree(Poly(x**2+y**3,y)) == 3
    assert degree(Poly(y**2 + x**3, y, x), 1) == 3
    assert degree(Poly(y**2 + x**3, x), z) == 0
    assert degree(Poly(y**2 + x**3 + z**4, x), z) == 4


    # optimization
    big = 2  # make this number 1000 when full expansion can be avoided

    assert degree((x + 1)**big, x) == big  # Large power (fast-path, no expansion)
    assert degree((x + 1)**big + x**(big-1), x) == big  # Addition without cancellation
    assert degree((x + 1)**2 - x**2, x) == 1  # Addition with cancellation (fallback required)
    assert degree(x*(x + 1)**big, x) == big + 1  # Nested multiplication
    assert degree(y*(x + 1)**big, x) == big  # Generator independence

    # checking 0 detection
    Z = pi*(1/pi + 1) - 1 - pi
    assert degree(Z**big, x) is S.NegativeInfinity