import { bRemoveLeadingZeros } from "./b-remove-leading-zeros.js"; const { max } = Math; /** * Returns the result of subtracting the second polynomial from the first with * coefficients given as bigints; (p1 - p2). * * @param a minuend; the polynomial from which will be subtracted; a polynomial * with coefficients given densely as an array of bigints * from highest to lowest power, e.g. `[5,-3,0]` represents the * polynomial `5x^2 - 3x` * @param b subtrahend; the polynomial that will be subtracted * * @example * ```typescript * bSubtract([2n,3n],[4n,4n]); //=> [-2n, -1n] * ``` * * @doc */ function bSubtract(a: bigint[], b: bigint[]): bigint[] { // Initialize result array const da = a.length - 1; const db = b.length - 1; const Δd = da - db; const Δd2 = Δd > 0 ? -Δd : 0; const Δd1 = Δd < 0 ? +Δd : 0; const d = max(da, db); // Add coefficients const result = new Array(d+1); for (let i=0; i