UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

42 lines (36 loc) 1.25 kB
/** * Multiplies two polynomials given in coefficient-array form. * * out(x) = a(x) * b(x) * * Coefficient layout: index i is the coefficient of x^i (ascending). The * result has length `a_len + b_len - 1`. The first `out_len` entries of * `out` are written; entries beyond that are not touched. * * `out` must not alias `a` or `b` — the routine writes into `out` while it * still reads from the inputs. * * @param {number[]|Float64Array|Float32Array} out destination, length >= a_len + b_len - 1 * @param {number[]|Float64Array|Float32Array} a * @param {number} a_len number of coefficients in a * @param {number[]|Float64Array|Float32Array} b * @param {number} b_len number of coefficients in b * @returns {number} number of coefficients written into `out` (a_len + b_len - 1) */ export function polynomial_multiply(out, a, a_len, b, b_len) { const out_len = a_len + b_len - 1; for (let i = 0; i < out_len; i++) { out[i] = 0; } for (let i = 0; i < a_len; i++) { const ai = a[i]; if (ai === 0) { // short-circuit, 0*X == 0 continue; } for (let j = 0; j < b_len; j++) { out[i + j] += ai * b[j]; } } return out_len; }