cemyuksel / cyCodeBase

An open source programming resource intended for graphics programmers.
MIT License
276 stars 59 forks source link

cy::PolynomialRoots can't find all roots #24

Closed ycpku closed 1 year ago

ycpku commented 1 year ago

Hi,

I found that the polynomial solver can't find all roots. I run the following code in VS2022:

double coef[7] = { 0. ,0. ,1 ,-6 ,13 ,-12 ,4 }; //x^2*(2x-1)^2*(x-1)^2  
double roots[6] = {};
int numRoots = cy::PolynomialRoots<6, double>(roots, coef, errorThreshold);

The polynomial has 6 roots, including three repeated roots at 0, 0.5, and 1. However, when I run the solver with different errorThreshold values, it fails to find all roots.

Specifically, when errorThreshold is set to 1e-5 or 1e-6, the solver cannot find any roots. When errorThreshold is set to 1e-4, the solver only finds two roots, which are 0.499947 and 0.500053. The roots at 0 and 1 are not found.

I wonder why the solver can't find the root at 0 and 1. Can the algorithm guarantee that all roots will be found? Is this failure to find all roots due to the repeated roots of the polynomial, or is there some other issue at play? How can I fix this?

Thank you!

cemyuksel commented 1 year ago

This polynomial and ones like this will not work. The reason is that, though it touches zero, the polynomial is never negative. This implementation finds the points when the value of the polynomial crosses from negative to positive (or the opposite). If the polynomial is always positive, I would not expect it to find any roots.