import { assert } from "../assert.js";

// const lamb = 0.865;

/**
 * Modified bessel function of the first kind zeroth order
 * @see https://mathworld.wolfram.com/ModifiedBesselFunctionoftheFirstKind.html
 * @see https://computergraphics.stackexchange.com/questions/6393/kaiser-windowed-sinc-filter-for-mip-mapping
 * @see https://github.com/terifan/ImageResampler/blob/dbc212ce6aaa769bf3c9623cb6ead58ffd51d76c/src/org/terifan/image_resampler/FilterFactory.java
 * @param {number} x
 * @returns {number}
 */
export function modified_bessel_i0(x) {
    assert.notNaN(x, 'x');

    let sum = 1.0;

    const y = x * x * 0.25;

    let t = y;

    for (let i = 2; t > 1e-7; i++) {
        sum += t;
        t *= y / (i * i);
    }

    return sum;
}

