Skip to content

BUG: polydiv returns an infinite quotient for a divisor with a leading zero #33422

Open

Description

@lesshaste

Describe the issue:

np.polydiv returns an infinite quotient for an exact polynomial division.

Reproduce the code example:

import numpy as np

# (x + 1)**2 divided by (0*x**2 + x + 1)
q, r = np.polydiv([1, 2, 1], [0, 1, 1])
print(q, r)

Error message:

# Actual   [inf] [ nan -inf -inf]
# Expected [1. 1.] [0.]

Python and NumPy Versions:

2.5.5
3.12.14 (main, Sep  2 2026, 23:27:36) [GCC 15.3.0]

2.6.0.dev0+git20261122.8b8b80d
3.12.14 (main, Sep  2 2026, 23:27:36) [GCC 15.3.0]

Runtime Environment:

No response

How does this issue affect you or how did you find it:

It gives the wrong answer

Activity

  1. charris commented on Nov 22, 2026

    @charris
    Member

    Yes, it will. Don't do that, trim the zero. There is a trim method for Polynomials.

  2. lesshaste commented on Nov 22, 2026

    @lesshaste
    Author

    @charris I feel numpy should at least output a warning.

  3. charris commented on Nov 22, 2026

    @charris
    Member

    At what value of the leading coefficient? In numerical work, the degree of a polynomial is not a well defined concept, that is why it is left to the user.

  4. lesshaste commented on Nov 22, 2026

    @lesshaste
    Author

    @charris I would adopt an exact zero rule for automated trimming. Small nonzero terms are more difficult and probably just need a note in the docs

  5. charris commented on Nov 22, 2026

    @charris
    Member

    It substitutes one error with another. Improving the documentation is an option, perhaps with an example.

  6. charris commented on Nov 22, 2026

    @charris
    Member

    Note that numpy.polynomial.polynomial.polydiv does not have this problem.

    In [8]: polydiv([1,2,1], [1,1,0])
    Out[8]: (array([1., 1.]), array([0.]))
    
        # c1, c2 are trimmed copies
        [c1, c2] = pu.as_series([c1, c2])
        if c2[-1] == 0:
            raise ZeroDivisionError  # FIXME: add message with details to exception
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions