/**
 * Cauchy's bound on the magnitude of polynomial roots.
 *
 * For a polynomial `p(x) = c[0] + c[1]·x + ... + c[degree]·x^degree` with
 * `c[degree] ≠ 0`, every (real or complex) root `z` satisfies
 *
 *   |z| ≤ 1 + max(|c[0]|, |c[1]|, ..., |c[degree − 1]|) / |c[degree]|
 *
 * The bound is loose for general polynomials but cheap to compute and serves
 * as a good initial radius for iterative root-finders such as Aberth–Ehrlich.
 *
 * @param {number[]|Float32Array|Float64Array} coeffs ascending-power, length ≥ degree + 1
 * @param {number} degree polynomial degree, ≥ 1; the leading coefficient `coeffs[degree]` must be nonzero
 * @returns {number}
 */
export function polynomial_root_bound_cauchy(coeffs, degree) {
    const lead = Math.abs(coeffs[degree]);

    let m = 0;

    for (let i = 0; i < degree; i++) {
        const a = Math.abs(coeffs[i]);

        if (a > m) {
            m = a;
        }
    }

    return 1 + m / lead;
}
