Real roots of c0 + c1*x + c2*x**2 in [0, 1].
(c0, c1, c2)
| 12 | |
| 13 | |
| 14 | def _quadratic_roots_in_01(c0, c1, c2): |
| 15 | """Real roots of c0 + c1*x + c2*x**2 in [0, 1].""" |
| 16 | if abs(c2) < 1e-12: # Linear |
| 17 | if abs(c1) < 1e-12: |
| 18 | return np.array([]) |
| 19 | root = -c0 / c1 |
| 20 | return np.array([root]) if 0 <= root <= 1 else np.array([]) |
| 21 | |
| 22 | disc = c1 * c1 - 4 * c2 * c0 |
| 23 | if disc < 0: |
| 24 | return np.array([]) |
| 25 | |
| 26 | sqrt_disc = np.sqrt(disc) |
| 27 | # Numerically stable quadratic formula |
| 28 | if c1 >= 0: |
| 29 | q = -0.5 * (c1 + sqrt_disc) |
| 30 | else: |
| 31 | q = -0.5 * (c1 - sqrt_disc) |
| 32 | |
| 33 | roots = [] |
| 34 | if abs(q) > 1e-12: |
| 35 | roots.append(c0 / q) |
| 36 | if abs(c2) > 1e-12: |
| 37 | roots.append(q / c2) |
| 38 | |
| 39 | roots = np.asarray(roots) |
| 40 | return roots[(roots >= 0) & (roots <= 1)] |
| 41 | |
| 42 | |
| 43 | def _real_roots_in_01(coeffs): |
no test coverage detected
searching dependent graphs…