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

Code before:
    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

    raises(TypeError, lambda: degree(pi))  # no guessing for non-Number expressions

    raises(TypeError, lambda: degree(y**2 + x**3))
    raises(TypeError, lambda: degree(y**2 + x**3, 1))
    raises(PolynomialError, lambda: degree(x, 1.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

    # Regression test for Issue 29090
    assert degree(x**2) == 2
raises(TypeError, lambda: degree(x*(1/x + 1)))  # no help for gen=0
raises(TypeError, lambda: degree(x + 1/x))  # no guessing for gen=0

def test_Poly_degree_list():
    assert Poly(0, x).degree_list() == (-oo,)
    assert Poly(0, x, y).degree_list() == (-oo, -oo)
    assert Poly(0, x, y, z).degree_list() == (-oo, -oo, -oo)

    assert Poly(1, x).degree_list() == (0,)
    assert Poly(1, x, y).degree_list() == (0, 0)
    assert Poly(1, x, y, z).degree_list() == (0, 0, 0)

    assert Poly(x**2*y + x**3*z**2 + 1).degree_list() == (3, 1, 2)

    assert degree_list(1, x) == (0,)
    assert degree_list(x, x) == (1,)

    assert degree_list(x*y**2) == (1, 2)

    raises(ComputationFailed, lambda: degree_list(1))


def test_Poly_total_degree():
    assert Poly(x**2*y + x**3*z**2 + 1).total_degree() == 5
    assert Poly(x**2 + z**3).total_degree() == 3
    assert Poly(x*y*z + z**4).total_degree() == 4
    assert Poly(x**3 + x + 1).total_degree() == 3

Review comment (suggestion):
```suggestion
    raises(TypeError, lambda: degree(x*(1/x + 1)))  # no help for gen=0
    raises(TypeError, lambda: degree(x + 1/x))  # no guessing for gen=0
```

Code after:
    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
    assert degree(Z**big, pi) is S.NegativeInfinity
    Z = Add(0, Mul(0, x, evaluate=False), evaluate=False)
    assert degree(Z, x) is S.NegativeInfinity
    assert degree(cos(1)**2 + sin(1)**2 - 1, x) == 0  # should be -oo

    # /!\ user was warned; anything could change as Poly is improved-#
    eq = x + 1/x**2                                                  #
    raises(PolynomialError, lambda: degree(eq**big, x))              #
    assert degree(eq**big, 1/x) == 2*big                             #
                                                                     #
    eq = exp(x) + 1/exp(2*x)                                         #
    assert degree(eq, exp(x)) == 1                                   #
    assert degree(eq, exp(-x)) == 2                                  #
                                                                     #
    one = Pow(x, 0, evaluate=False)                                  #
    raises(PolynomialError, lambda: degree(one, one))                #
    assert degree(x, 1.1) == degree(x, pi) == 0                      #