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

BUG: Significant difference between roots() and polyroots() function #27881

Open
GijsB opened this issue Nov 30, 2024 · 3 comments
Open

BUG: Significant difference between roots() and polyroots() function #27881

GijsB opened this issue Nov 30, 2024 · 3 comments
Labels

Comments

@GijsB
Copy link

GijsB commented Nov 30, 2024

Describe the issue:

Description

When I define a polynomial with a single very large root (and other small ones), I get a significant difference between the outcomes of the roots and polyroots functions. Mainly the small roots seem to differ. Maybe even more importantly, the outcomes can also become complex in the polyroots function.

Reproduce the code example:

from numpy import roots, sort, array
from numpy.polynomial.polynomial import polyroots

p = array([0.5, -0.2, -5e+15, 0.04])
print(f'roots:     {sort(roots(p[::-1]))}') # [-1.00000000e-08  9.99999998e-09  1.25000000e+17] 
print(f'polyroots: {sort(polyroots(p))}')   # [-7.50602596e-09  7.50602647e-09  1.25000000e+17]

print()

p = array([0.5, -0.2, -5e+15, 0.05])
print(f'roots:     {sort(roots(p[::-1]))}')  # [-1.00000000e-08  9.99999998e-09  1.00000000e+17]
print(f'polyroots: {sort(polyroots(p))}') # [1.68735664e-16-2.94078821e-09j 1.68735664e-16+2.94078821e-09j ...]

Error message:

No response

Python and NumPy Versions:

  • Numpy version: 2.1.3
  • Python version: 3.13.0
  • Platform: MacOS 15.1.1

Runtime Environment:

No response

Context for the issue:

I'm not 100% sure this can be considered a bug, apologies if this is not the case. The reason for reporting is that as a naive user, I expect 'new' API's to behave identical (or at least similar) to the original functions. I'm using these functions to find the intersections of 2 polynomials, which represent the performance characteristics of a propeller.

As far as I'm concerned, this issue does not need to be prioritised in any way. I just wanted to make sure that this behaviour is known if this wasn't already the case. Thanks either way for your input/help!

@GijsB GijsB added the 00 - Bug label Nov 30, 2024
@charris
Copy link
Member

charris commented Dec 1, 2024

Hmm, with current main and on Intel linux I get

In [21]: polyroots(p[::-1])
Out[21]: array([-9.99999998e+07,  8.00000000e-18,  1.00000000e+08])

In [22]: np.roots(p)
Out[22]: array([ 1.00000000e+08, -9.99999998e+07,  8.00000000e-18])

What results are you seeing?

@GijsB
Copy link
Author

GijsB commented Dec 1, 2024

Hi charris,
Thanks for your quick reply! Just to make sure there's no misunderstanding: In the issue I've encountered, there are 2 small roots symmetric around 0 and 1 large root. The example in your reply is kind of the opposite, it has 2 large roots around 0 and 1 small root. I'll post the results (from my machine) of both examples below.

2 small roots, 1 large

roots:     [-1.00000000e-08  9.99999998e-09  1.25000000e+17]
polyroots: [-7.50602596e-09  7.50602647e-09  1.25000000e+17]

roots:     [-1.00000000e-08  9.99999998e-09  1.00000000e+17]
polyroots: [1.68735664e-16-2.94078821e-09j 1.68735664e-16+2.94078821e-09j 1.00000000e+17+0.00000000e+00j]

1 small root, 2 large

>>> from numpy import roots, sort, array
>>> from numpy.polynomial.polynomial import polyroots
>>> p = [1, 0, -1e16, 8e-2] 
>>> polyroots(p[::-1])
array([-1.e+08,  8.e-18,  1.e+08])
>>> roots(p)
array([-1.e+08,  1.e+08,  8.e-18])

@GijsB
Copy link
Author

GijsB commented Dec 1, 2024

Hi all,
Curiosity got the better of me on a Sunday evening. I tried to measure the root finding error for a third order polynomial, two roots symmetrically around zero (at -1 an 1) and another relatively large root. polyroots is in blue and roots is in orange.
image

Interestingly, it looks like it's very dependent on the symmetry of the 2 small roots:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants
  NODES
COMMUNITY 2
Project 5
USERS 1