UNPKG

flo-poly

Version:

A practical, root-focused JavaScript polynomial utility library.

29 lines (24 loc) 640 B
/** * Returns the result of differentiating the given polynomial in double * precision. * * @param p a polynomial with coefficients given densely as an array of double * floating point numbers from highest to lowest power, e.g. `[5,-3,0]` * represents the polynomial `5x^2 - 3x` * * @example * ```typescript * differentiate([5, 4, 3, 2, 1]); //=> [20, 12, 6, 2] * ``` * * @doc */ function differentiate(p: number[]): number[] { const result: number[] = []; const d = p.length - 1; for (let i=0; i<d; i++) { result.push((d-i) * p[i]); } return result; } export { differentiate }