UNPKG

flo-poly

Version:

A practical, root-focused JavaScript polynomial utility library.

1,725 lines (1,623 loc) 308 kB
/******/ // The require scope /******/ var __webpack_require__ = {}; /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; ;// ./node_modules/big-float-ts/node/double-expansion/e-estimate.js /** * Returns the result of the given floating point expansion rounded to a double * floating point number. * * The result is within 1 ulps of the actual value, e.g. imagine the worst case * situation where we add (in 4dot4) 1111.1000 + 0.000011111111... The result * will be 1111.1000 whereas as the correct result should be 1111.1001 and we * thus lost 1 ulp of accuracy. It does not matter that the expansion contain * several floats since none is overlapping. * * See Shewchuk https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf * * @param e a floating point expansion */ function eEstimate(e) { let Q = e[0]; for (let i = 1; i < e.length; i++) { Q += e[i]; } return Q; } //# sourceMappingURL=e-estimate.js.map ;// ./src/basic/to-cas-str.ts /** @internal */ function isNumber(x) { return typeof x === 'number'; } /** @internal */ function isShewchuk(x) { return Array.isArray(x); } /** @internal */ function isBigint(x) { return typeof x === 'bigint'; } /** * Returns a string representing the given polynomial that is readable by a * human or a CAS (Computer Algebra System). * * * **note:** if the polynomial coefficients are given as Shewchuk expansions * then the coefficients are first down-converted to double precision * * @param p a polynomial (with coefficients given densely as an array of Shewchuk * floating point expansions **or** double precision floating point numbers **or** * bigints) from highest to lowest power, e.g. `[5,-3,0]` represents the * polynomial `5x^2 - 3x` * * @example * ```typescript * toCasStr([5,4,3,2,1]); //=> "x^4*5 + x^3*4 + x^2*3 + x*2 + 1" * toCasStr([[5],[4],[3],[2],[1]]); //=> "x^4*5 + x^3*4 + x^2*3 + x*2 + 1" * toCasStr([5n,4n,3n,2n,1n]); //=> "x^4*5 + x^3*4 + x^2*3 + x*2 + 1" * ``` * * @doc */ function toCasStr(p) { const d = p.length - 1; let str = ''; for (let i = 0; i < d + 1; i++) { const _v = p[i]; const v = isShewchuk(_v) ? eEstimate(_v) : _v; // bigint or number const absV = isBigint(v) ? (v < 0n ? -v : v) : Math.abs(v); let cStr = nonNegativeNumberToString(absV); cStr = (v >= 0 ? ' + ' : ' - ') + cStr; if (i === d) { str += cStr; } else if (i === d - 1) { str += cStr + '*x'; } else { str += cStr + '*x^' + (d - i).toString(); } } return str; } /** * from https://stackoverflow.com/a/46545519/2010061 * * @internal */ function nonNegativeNumberToString(num) { let numStr = num.toString(); if (isBigint(num)) { return numStr; } if (Math.abs(num) < 1) { const e = parseInt(numStr.split('e-')[1]); if (e) { num *= 10 ** (e - 1); numStr = '0.' + (new Array(e)).join('0') + num.toString().substring(2); } } else { let e = parseInt(numStr.split('+')[1]); if (e > 20) { e -= 20; num /= 10 ** e; numStr = num.toString() + (new Array(e + 1)).join('0'); } } return numStr; } ;// ./src/basic/bigint/b-abs-coeff.ts /** * Returns the polynomial with all coeffients the absolute value of the given * polynomial. * * @param p a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` * represents the polynomial `5x^2 - 3x` * * @doc */ function bAbsCoeff(p) { const p_ = []; for (let i = 0; i < p.length; i++) { const v = p[i]; p_.push(v < 0n ? -v : v); } return p_; } ;// ./src/basic/bigint/b-remove-leading-zeros.ts /** * If the highest power coefficient of the given polynomial is 0 then * removeLeadingZeros can be called to remove all such highest terms so that * the returned array is a valid presentation of a polynomial. * @param p a polynomial whose leading zeros should be removed * * @doc */ function bRemoveLeadingZeros(p) { // @ts-nocheck let lzCount = 0; for (let i = 0; i <= p.length - 1; i++) { if (p[i] !== 0n) { break; } lzCount++; } if (lzCount !== 0) { p = p.slice(lzCount); } return p; } ;// ./src/basic/bigint/b-add.ts // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ Otherwise code is too slow❗ const b_add_bRemoveLeadingZeros = bRemoveLeadingZeros; /** * Returns the result of adding two polynomials with bigint coefficients. * * @param p1 a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * @param p2 another polynomial * * @example * ```typescript * bAdd([1n,2n,3n],[3n,4n]); //=> [1n,5n,7n] * ``` * * @doc */ function bAdd(p1, p2) { // Initialize result array const d1 = p1.length - 1; const d2 = p2.length - 1; const Δd = d1 - d2; const Δd1 = Δd < 0 ? +Δd : 0; const Δd2 = Δd > 0 ? -Δd : 0; const d = Math.max(d1, d2); // Add coefficients const result = []; for (let i = 0; i < d + 1; i++) { const c1 = p1[i + Δd1] || 0n; const c2 = p2[i + Δd2] || 0n; result.push(c1 + c2); } // Ensure the result is a valid polynomial representation return b_add_bRemoveLeadingZeros(result); } ;// ./src/basic/bigint/b-degree.ts /** * Returns the degree of the given polynomial - the zero polynomial degree is * returned as -1 (and not -∞ as is conventional). * * @param p a polynomial with coefficients given densely as an array of bigints * from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * * @example * ```typescript * bDegree([9n,8n,7n,6n,5n,4n,3n,2n,1n]); //=> 8 * ``` * * @doc */ function bDegree(p) { return p.length - 1; } ;// ./src/basic/bigint/b-divide-by-const.ts /** * Divides (using **integer division**) a polynomial by a constant. * * @param p a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * @param c a constant * * @doc */ function bDivideByConst(p, c) { const d = p.length; const r = []; for (let i = 0; i < d; i++) { r.push(p[i] / c); } return r; } ;// ./src/basic/bigint/b-equal.ts /** * Returns true if two polynomials are exactly equal by comparing coefficients, * false otherwise. * * @param a a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * @param b another polynomial * * @example * ```typescript * bEqual([1n,2n,3n,4n], [1n,2n,3n,4n]); //=> true * bEqual([1n,2n,3n,4n], [1n,2n,3n,4n,5n]); //=> false * ``` * * @doc */ function bEqual(a, b) { if (a.length !== b.length) { return false; } for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { return false; } } return true; } ;// ./src/basic/bigint/b-invert.ts /** * Inverts the given polynomial by reversing the order of the coefficients, * i.e. p(x) -> x^deg(p) * p(1/x) * * @param p a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * * @example * ```typescript * bInvert([3n,2n,-5n]); // => [-5n,2n,3n] * ``` * * @doc */ function bInvert(p) { return p.slice().reverse(); } ;// ./src/gcd/bigint/b-integer-gcd.ts /** * Computes and returns the greatest common divisor of two integers a and b, * using the [Euclidean Algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm). * * @doc */ function bGcdInt(a, b) { a = a < 0n ? -a : a; b = b < 0n ? -b : b; // The below 2 commented lines represent Euclid's original algorithm. //if (a === b) { return a; } //return a > b ? gcdInt(a - b, b) : gcdInt(a, b - a); if (a === 0n) { return b; } if (b === 0n) { return a; } while (b !== 0n) { const t = b; b = a % b; a = t; } return a; } /** * Naively computes and returns the greatest common divisor of 2 or more * integers by taking each integer in turn and calculating the GCD of that * integer and the previously calculated GCD (where the first GCD is simply * taken as the first number). * * @param vals the integers for which the GCD is to be calculated * * @doc */ function bGcdInts(vals) { const vals_ = vals.slice(); const len = vals_.length; // make array of numbers all positive for (let i = 0; i < len; i++) { vals_[i] = vals_[i] < 0n ? -vals_[i] : vals_[i]; } let a = vals_[0]; for (let i = 1; i < len; i++) { a = bGcdInt(a, vals_[i]); } return a; } /** * * ❗ don't use - too slow - use [[bGcdInts]] instead ❗ * * Computes and returns the greatest common divisor of 2 or more integers by * calculating GCDs rescursively using a tree (Divide and Conquer). * * * It turns out this method is *slower* than the naive method */ /* function bGcdIntsTree(vals: bigint[]): bigint { const vals_ = vals.slice(); // make array of numbers all positive for (const i=0; i<vals_.length; i++) { vals_[i] = vals_[i] < 0n ? -vals_[i] : vals_[i]; } // Divide and conquer while (vals_.length > 1) { const newVals = []; const len = vals_.length; for (const i=0; i<len-1; i += 2) { newVals.push(bGcdInt(vals_[i], vals_[i+1])); } if (len % 2 !== 0) { newVals.push(vals_[len-1]); } vals_ = newVals; } return vals_[0]; } */ ;// ./src/basic/bigint/b-is-rational-multiple-of.ts /** * Returns true if either polynomial is an exact rational multiple of the other. * * @param a a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * @param b another polynomial * * @doc */ function bIsRationalMultipleOf(a, b) { // If either polynomial is zero if (a.length === 0 || b.length === 0) { return true; } if (a.length !== b.length) { return false; } // multiply by -1 if appropriate to make the leading coefficients positive const a_ = a[0] < 0n ? a.map(c => -c) : a; const b_ = b[0] < 0n ? b.map(c => -c) : b; /** leading coefficient of a */ const lcA = a_[0]; /** leading coefficient of b */ const lcB = b_[0]; const gcd = bGcdInt(lcA, lcB); const A = lcA / gcd; // this division is exact const B = lcB / gcd; // this division is exact for (let i = 0; i < a_.length; i++) { const Ab = A * b_[i]; if (Ab % B !== 0n) { return false; } if (Ab / B !== a_[i]) { return false; } } return true; } ;// ./src/basic/bigint/b-multiply.ts /** * Returns the result of multiplying 2 polynomials with bigint coefficients. * * * see [polynomial arithmetic](https://en.wikipedia.org/wiki/Polynomial_arithmetic) * * see [polynomial multiplication](https://en.wikipedia.org/wiki/Discrete_Fourier_transform#Polynomial_multiplication) * * see [polynomial multiplication](http://web.cs.iastate.edu/~cs577/handouts/polymultiply.pdf) * * @param a a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` * represents the polynomial `5x^2 - 3x` * @param b another polynomial. * * @example * ```typescript * bMultiply([1n,2n,3n], [2n,5n,3n,5n]); //=> [2n, 9n, 19n, 26n, 19n, 15n] * ``` * * @doc */ function bMultiply(a, b) { const da = a.length - 1; const db = b.length - 1; // if either or both is the zero polynomial if (da < 0 || db < 0) { return []; } const d = da + db; const r = new Array(d + 1).fill(0n); for (let i = 0; i < da + 1; i++) { for (let j = 0; j < db + 1; j++) { r[d - (i + j)] += (a[da - i] * b[db - j]); } } return r; } ;// ./src/basic/bigint/b-multiply-by-const.ts /** * Returns the result of multiplies a polynomial (with bigint coefficients) by * a constant. * * @param c a constant * @param p a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` * represents the polynomial `5x^2 - 3x` * * @doc */ function bMultiplyByConst(c, p) { if (c === 0n) { return []; } const d = p.length; const r = []; for (let i = 0; i < d; i++) { r.push(c * p[i]); } return r; } ;// ./src/basic/bigint/b-negate.ts /** * Returns the negative of the given polynomial (p -> -p). * * @param p a polynomial with coefficients given densely as an array of * bigints from highest to lowest power, e.g. `[5n,-3n,0n]` represents the * polynomial `5x^2 - 3x` * * @example * ```typescript * bNegate([1n, -2n]); //=> [-1n, 2n] * ``` * * @doc */ function bNegate(p) { const p_ = []; for (let i = 0; i < p.length; i++) { p_.push(-p[i]); } return p_; } ;// ./src/basic/bigint/b-subtract.ts // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ Otherwise code is too slow❗ const b_subtract_bRemoveLeadingZeros = bRemoveLeadingZeros; /** * 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, b) { // 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 = Math.max(da, db); // Add coefficients const result = []; for (let i = 0; i < d + 1; i++) { const c1 = a[i + Δd1] || 0n; const c2 = b[i + Δd2] || 0n; result.push(c1 - c2); } // Ensure the result is a valid polynomial representation return b_subtract_bRemoveLeadingZeros(result); } ;// ./src/basic/double/abs-coeff.ts /** * Returns the polynomial with all coeffients the absolute value of the given * polynomial. * * @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` * * @doc */ function absCoeff(p) { const p_ = []; for (let i = 0; i < p.length; i++) { p_.push(Math.abs(p[i])); } return p_; } ;// ./src/basic/double/remove-leading-zeros.ts /** * If the highest power coefficient of the given polynomial is 0 then * removeLeadingZeros can be called to remove all such highest terms so that * the returned array is a valid presentation of a polynomial. * * @param p a polynomial whose leading zeros should be removed * * @example * ```typescript * removeLeadingZeros([1e-18, 1e-10, 1e-1]); //=> [1e-18, 1e-10, 1e-1] * removeLeadingZeros([0, 1e-10, 1e-1]); //=> [1e-10, 1e-1] * ``` * * @doc */ function removeLeadingZeros(p) { let lzCount = 0; for (let i = 0; i <= p.length - 1; i++) { if (p[i] !== 0) { break; } lzCount++; } if (lzCount !== 0) { p = p.slice(lzCount); } return p; } ;// ./src/basic/double/add.ts // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ Otherwise code is too slow❗ const add_removeLeadingZeros = removeLeadingZeros; /** * Returns the result of adding two polynomials in double precision. * * @param p1 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` * @param p2 another polynomial * * @example * ```typescript * add([1,2,3],[3,4]); //=> [1,5,7] * ``` * * @doc */ function add(p1, p2) { // Initialize result array const d1 = p1.length - 1; const d2 = p2.length - 1; const Δd = d1 - d2; const Δd1 = Δd < 0 ? +Δd : 0; const Δd2 = Δd > 0 ? -Δd : 0; const d = Math.max(d1, d2); // Add coefficients const result = []; for (let i = 0; i < d + 1; i++) { const c1 = p1[i + Δd1] || 0; const c2 = p2[i + Δd2] || 0; result.push(c1 + c2); } // Ensure the result is a valid polynomial representation return add_removeLeadingZeros(result); } ;// ./src/basic/double/degree.ts /** * Returns the degree of the given polynomial - the zero polynomial degree is * returned as -1 (and not -∞ as is conventional). * * @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 * degree([9,8,7,6,5,4,3,2,1]); //=> 8 * ``` * * @doc */ function degree(p) { return p.length - 1; } ;// ./src/basic/double/divide-by-const.ts /** * Divides a polynomial by a constant 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` * @param c a constant * * @doc */ function divideByConst(p, c) { const d = p.length; const r = []; for (let i = 0; i < d; i++) { r.push(p[i] / c); } return r; } ;// ./src/basic/double/equal.ts /** * Returns true if two polynomials are exactly equal by comparing coefficients, * false otherwise. * * @param p1 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` * @param p2 another polynomial * @example * ```typescript * equal([1,2,3,4], [1,2,3,4]); //=> true * equal([1,2,3,4], [1,2,3,4,5]); //=> false * ``` * * @doc */ function equal(p1, p2) { if (p1.length !== p2.length) { return false; } for (let i = 0; i < p1.length; i++) { if (p1[i] !== p2[i]) { return false; } } return true; } ;// ./src/basic/double/invert.ts /** * Inverts the given polynomial by reversing the order of the coefficients, * i.e. p(x) -> x^deg(p) * p(1/x) * * @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 * invert([3,2,-5]); // => [-5,2,3] * ``` * * @doc */ function invert(p) { return p.slice().reverse(); } ;// ./node_modules/big-float-ts/node/double-representation/double-to-octets.js // Modified from https://github.com/bartaz/ieee754-visualization/ // under the MIT license // Copyright 2013 Bartek Szopka (original author) /** * Returns the ieee-574 8 bytes composing the given double, starting from the * sign bit and ending in the lsb of the significand. * e.g. 123.456 -> [64, 94, 221, 47, 26, 159, 190, 119] */ function doubleToOctets(number) { const buffer = new ArrayBuffer(8); new DataView(buffer).setFloat64(0, number, false); return Array.from(new Uint8Array(buffer)); } //# sourceMappingURL=double-to-octets.js.map ;// ./node_modules/big-float-ts/node/double-representation/parse-double.js // Modified from https://github.com/bartaz/ieee754-visualization/ // under the MIT license // Copyright 2013 Bartek Szopka (original author) /** * Returns the relevant parts of the given IEEE-754 double. The returned * exponent has been normalized (i.e. 1023 ha been subtracted) and the * significand has the hidden bit added if appropriate. * See https://github.com/bartaz/ieee754-visualization */ function parseDouble(x) { const parts = doubleToOctets(x); const p0 = parts[0]; const p1 = parts[1]; const sign = p0 >> 7; const exponent_ = ((p0 & 127) << 4) + ((p1 & 0b11110000) >> 4); //---- Check for negative / positive zero / denormalized numbers. const hiddenMsb = exponent_ === 0 ? 0 : 16; // Note: exponent === 0 => 0 or denormalized number (a.k.a. subnormal number). const exponent = exponent_ === 0 ? exponent_ - 1022 // Subnormals use a biased exponent of 1 (not 0!) : exponent_ - 1023; //---- Break up the significand into bytes const significand = parts.slice(1); significand[0] = (p1 & 15) + hiddenMsb; return { sign, exponent, significand }; } /** * Returns the relevant parts of the given IEEE-754 double. * See https://github.com/bartaz/ieee754-visualization. * This is a slower version of parseDouble that gives binary string * representations of the components. */ function parseDoubleDetailed(x) { const str = doubleToBinaryString(x); // sign{1} exponent{11} fraction{52} === 64 bits (+1 hidden!) const [, sign, exponent, significand] = str.match(/^(.)(.{11})(.{52})$/); const exponent_ = parseInt(exponent, 2); const hidden = exponent_ === 0 ? "0" : "1"; return { full: sign + exponent + hidden + significand, sign, exponent, hidden, significand }; } //# sourceMappingURL=parse-double.js.map ;// ./node_modules/big-float-ts/node/double-representation/exponent.js /** * Returns the normalized exponent of the given number. * @param a A double */ function exponent(a) { return parseDouble(a).exponent; } //# sourceMappingURL=exponent.js.map ;// ./node_modules/big-float-ts/node/double-representation/significand.js /** * Return the significand of the given double with the hidden bit added (in case * a is not subnormal or 0, etc.) * @param a A double */ function significand(a) { return parseDouble(a).significand; } //# sourceMappingURL=significand.js.map ;// ./node_modules/big-float-ts/node/double-representation/get-max-set-bit.js /** * Returns the lowest set bit of the given value in [1, (2**31)-1], * i.e. from 1 up to 2147483647 else if no bit is set (input === 0) returns * NaN, otherwise if the number is out of range returns a non-finite * number. * See https://stackoverflow.com/a/35190288/2010061 */ function getLowestSetBit_(a) { return Math.log2(a & -a); } /** * Returns the lowest set bit of the given number's significand (where the lsb * is bit 0 and the msb is bit 52). If no bit is set (input === 0 or +-inf or * NaN) returns NaN. * See https://stackoverflow.com/a/35190288/2010061 */ function getLowestSetBit(a) { if (a === 0 || !Number.isFinite(a)) { // There is no lowest set bit return NaN; } // Note: the significand includes the hidden bit! const s = significand(a); const len = s.length; for (let i = len - 1; i >= 0; i--) { if (s[i] === 0) { continue; } const l = getLowestSetBit_(s[i]); if (Number.isFinite(l)) { return (8 * (len - i - 1)) + l; } } return NaN; } /** * Returns the highest set bit of the given value in [1, 255], i.e. from 1 up * to 255. If the input number === 0 returns NaN. * See https://stackoverflow.com/a/35190288/2010061 */ function getHighestSetBit_(a) { return a >= 128 ? 7 : a >= 64 ? 6 : a >= 32 ? 5 : a >= 16 ? 4 : a >= 8 ? 3 : a >= 4 ? 2 : a >= 2 ? 1 : a >= 1 ? 0 : NaN; } /** * Returns the highest set bit of the given double. If no bit is set (input * === 0 or +/-inf or NaN) returns NaN. * See https://stackoverflow.com/a/35190288/2010061 */ function getHighestSetBit(a) { if (a === 0 || !Number.isFinite(a)) { // There is no lowest set bit return NaN; } // At this point there must be a highest set bit (always === 52 if the // number is not a subnormal. const s = significand(a); const len = s.length; for (let i = 0; i < len; i++) { const l = getHighestSetBit_(s[i]); if (Number.isFinite(l)) { return (8 * (len - i - 1)) + l; } } return NaN; } //# sourceMappingURL=get-max-set-bit.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-compress.js /** * Returns the result of compressing the given floating point expansion. * * * primarily for internal library use * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * Theorem 23 (Shewchuck): Let e = sum_(i=1)^m(e_i) be a nonoverlapping * expansion of m p-bit components, where m >= 3. Suppose that the components of * e are sorted in order of increasing magnitude, except that any of the e_i may * be zero. Then the following algorithm will produce a nonoverlapping expansion * (nonadjacent if round-to even tiebreaking is used) such that * h = sum_(i=1)^n(h_i) = e, where the components h_i are in order of increasing * magnitude. If h != 0, none of the h_i will be zero. Furthermore, the largest * component h_n approximates h with an error smaller than ulp(h_n). */ function e_compress_eCompress(e) { //return e; const e_ = e.slice(); const m = e_.length; if (m === 1) { return e_; } let Q = e_[m - 1]; let bottom = m; for (let i = m - 2; i >= 0; --i) { const a = Q; const b = e_[i]; Q = a + b; const bv = Q - a; const q = b - bv; if (q) { e_[--bottom] = Q; Q = q; } } let top = 0; for (let i = bottom; i < m; ++i) { const a = e_[i]; const b = Q; Q = a + b; const bv = Q - a; const q = b - bv; if (q) { e_[top++] = q; } } e_[top++] = Q; e_.length = top; return e_; } //# sourceMappingURL=e-compress.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-sign.js /** * Returns the sign of the given expansion such that a negative value means a * negative sign and a positive value means a positive sign, 0 meaning 0 of * course. * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * From Shewchuk: "A nonoverlapping expansion is desirable because it is easy to * determine its sign (take the sign of the largest component) ... " * * @param e A floating point expansion with zeroes eliminated. */ function eSign(e) { return e[e.length - 1]; } //# sourceMappingURL=e-sign.js.map ;// ./node_modules/big-float-ts/node/double-representation/bit-length.js /** * Returns the bit-length of the significand of the given number in such a way * that trailing zeros are not counted. * @param a A double precision floating point number */ function bitLength(a) { if (a === 0) { return 0; } return getHighestSetBit(a) - getLowestSetBit(a) + 1; } /** * Returns the bit-length of the significand of the given floating point * expansion in such a way that trailing zeros are not counted. * * precondition: subnormals not currently supported * @param a A double precision floating point expansion */ function expBitLength(a) { const a_ = e_compress_eCompress(a); if (eSign(a_) === 0) { return 0; } const msbyte = a_[a_.length - 1]; const lsbyte = a_[0]; return exponent(msbyte) - exponent(lsbyte) + (53 - getLowestSetBit(lsbyte)); } //# sourceMappingURL=bit-length.js.map ;// ./src/scale-to-int/scale-floats-to-ints.ts // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ Otherwise code is too slow❗ const scale_floats_to_ints_exponent = exponent; const scale_floats_to_ints_bitLength = bitLength; /** * Returns the result of scaling the given floats by the *same* power of two * such that all floats become integers (bar overflow). * * * the result is exact (no round-off can occur, but overflow can) * * can be used to scale polynomials or Shewchuk expansions * * @param as an array of double precision floating point numbers * * @doc */ function scaleFloatsToInts(as) { let e = -1024; for (let i = 0; i < as.length; i++) { const a = as[i]; if (a === 0) { continue; } const scaleFactor = -scale_floats_to_ints_exponent(a) + scale_floats_to_ints_bitLength(a) - 1; if (scaleFactor > e) { e = scaleFactor; } } return as.map(a => a * 2 ** e); } ;// ./src/gcd/double/integer-gcd.ts /** * Computes the greatest common divisor of two integers a and b, using the * Euclidean Algorithm. * * **precondition** a, b must be integers * * @doc */ function gcdInt(a, b) { a = Math.abs(a); b = Math.abs(b); // The below 2 commented lines represent Euclid's original algorithm. //if (a === b) { return a; } //return a > b ? gcdInt(a - b, b) : gcdInt(a, b - a); if (a === 0) { return b; } if (b === 0) { return a; } while (b !== 0) { const t = b; b = a % b; a = t; } return a; } /** * Computes the greatest common divisor of two integers a and b, using the * binary GCD algorithm - probably slower than just using gcdInt that uses * the Euclidean Algorithm. */ function gcdIntBinary(a, b) { a = Math.abs(a); b = Math.abs(b); if (a === 0) { return b; } if (b === 0) { return a; } // Reduce a and/or b to odd numbers and keep track of the greatest power of // 2 dividing both a and b. let k = 1; while (a % 2 === 0 && b % 2 === 0) { a = a / 2; // right shift b = b / 2; // right shift k = k * 2; // left shift } // Reduce a to an odd number... while (a % 2 === 0) { a = a / 2; // right shift } // Henceforth, a is always odd... while (b) { // Remove all factors of 2 in b as they are not common while (b % 2 === 0) { b = b / 2; // right shift } // a and b are both odd. Swap values such that it is the larger of the // two values, and then set b to the difference (which is even) if (a > b) { [a, b] = [b, a]; } b = b - a; // b=0 iff b=a } // Restore common factors of 2... return k * a; } /** * Naively computes and returns the greatest common divisor of 2 or more * integers by taking each integer in turn and calculating the GCD of that * integer and the previously calculated GCD (where the first GCD is simply * taken as the first number). * * @param vals the integers for which the GCD is to be calculated * * @doc */ function gcdInts(vals) { const vals_ = vals.slice(); const len = vals_.length; // make array of numbers all positive for (let i = 0; i < len; i++) { vals_[i] = Math.abs(vals_[i]); } let a = vals_[0]; for (let i = 1; i < len; i++) { a = gcdInt(a, vals_[i]); } return a; } /** * :::tip Heads up! * don't use - too slow - use [[gcdInts]] instead * ::: * * Computes and returns the greatest common divisor of 2 or more integers by * calculating GCDs rescursively using a tree (Divide and Conquer). * * * It turns out this method is *slower* than the naive method * * @param vals the integers for which the GCD is to be calculated * * @internal */ function gcdIntsTree(vals) { let vals_ = vals.slice(); // make array of numbers all positive for (let i = 0; i < vals_.length; i++) { vals_[i] = Math.abs(vals_[i]); } // Divide and conquer while (vals_.length > 1) { const newVals = []; const len = vals_.length; for (let i = 0; i < len - 1; i += 2) { newVals.push(gcdInt(vals_[i], vals_[i + 1])); } if (len % 2 !== 0) { newVals.push(vals_[len - 1]); } vals_ = newVals; } return vals_[0]; } //export { gcdInt, gcdInts } ;// ./node_modules/big-float-ts/node/basic/two-product.js const f = 134217729; // 2**27 + 1; /** * Returns the exact result of multiplying two doubles. * * * the resulting array is the reverse of the standard twoSum in the literature. * * Theorem 18 (Shewchuk): Let a and b be p-bit floating-point numbers, where * p >= 6. Then the following algorithm will produce a nonoverlapping expansion * x + y such that ab = x + y, where x is an approximation to ab and y * represents the roundoff error in the calculation of x. Furthermore, if * round-to-even tiebreaking is used, x and y are non-adjacent. * * See https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf * @param a A double * @param b Another double */ function two_product_twoProduct(a, b) { const x = a * b; //const [ah, al] = split(a); const c = f * a; const ah = c - (c - a); const al = a - ah; //const [bh, bl] = split(b); const d = f * b; const bh = d - (d - b); const bl = b - bh; const y = (al * bl) - ((x - (ah * bh)) - (al * bh) - (ah * bl)); //const err1 = x - (ah * bh); //const err2 = err1 - (al * bh); //const err3 = err2 - (ah * bl); //const y = (al * bl) - err3; return [y, x]; } //# sourceMappingURL=two-product.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-negative-of.js /** * Returns the negative of the given floating point expansion. * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * @param e a floating point expansion */ function eNegativeOf(e) { const m = e.length; const h = new Array(m); for (let i = 0; i < m; i++) { h[i] = -e[i]; } return h; } //# sourceMappingURL=e-negative-of.js.map ;// ./node_modules/big-float-ts/node/double-expansion/fast-expansion-sum.js // import { eCompress } from "./e-compress.js"; // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ // const compress = eCompress; /** * Returns the result of adding two expansions. * * Theorem 13: Let e = sum_(i=1)^m(e_i) and f = sum_(i=1)^n(f_i) be strongly * nonoverlapping expansions of m and n p-bit components, respectively, where * p >= 4. Suppose that the components of both e and f are sorted in order of * increasing magnitude, except that any of the e_i or f_i may be zero. On a * machine whose arithmetic uses the round-to-even rule, the following algorithm * will produce a strongly nonoverlapping expansion h such that * sum_(i=1)^(m+n)(e_i + f_i) = e + f, where the components of h are also in * order of increasing magnitude, except that any of the h_i may be zero. * * See https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf */ function fastExpansionSum(e, f) { //const g = merge(e,f); // inlined (above line) const lenE = e.length; const lenF = f.length; let i = 0; let j = 0; const g = []; while (i < lenE && j < lenF) { if (e[i] === 0) { i++; continue; } if (f[j] === 0) { j++; continue; } if (Math.abs(e[i]) <= Math.abs(f[j])) { g.push(e[i]); i++; } else { g.push(f[j]); j++; } } while (i < lenE) { g.push(e[i]); i++; } while (j < lenF) { g.push(f[j]); j++; } if (g.length === 0) { return [0]; } // end inlined const len = g.length; if (len === 1) { return g; } //const h: number[] = new Array(len); const h = []; //const q: number; //[h[0], q] = fastTwoSum(g[1], g[0]); // inlined (above line) const a = g[1]; const b = g[0]; let q = a + b; //h[0] = b - (q - a); const hh = b - (q - a); if (hh !== 0) { h.push(hh); } //let j = 0; j = 0; for (let i = 2; i < len; i++) { //[h[i-1], q] = twoSum(q, g[i]); // inlined (above line) const b = g[i]; const R = q + b; const _ = R - q; //h[i-1] = (q - (R - _)) + (b - _); const hh = (q - (R - _)) + (b - _); if (hh !== 0) { h.push(hh); } q = R; } //h[len-1] = q; //h.push(q); if (q !== 0 || h.length === 0) { h.push(q); } //return compress(h); return h; } /** * Returns the result of merging an expansion e and f into a single expansion, * in order of nondecreasing magnitude (possibly with interspersed zeros). * (This function is zero-eliminating) * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * @param e a floating point expansion * @param f another floating point expansion */ function merge(e, f) { const lenE = e.length; const lenF = f.length; let i = 0; let j = 0; const merged = []; while (i < lenE && j < lenF) { if (e[i] === 0) { i++; continue; } if (f[j] === 0) { j++; continue; } if (Math.abs(e[i]) <= Math.abs(f[j])) { merged.push(e[i]); i++; } else { merged.push(f[j]); j++; } } while (i < lenE) { merged.push(e[i]); i++; } while (j < lenF) { merged.push(f[j]); j++; } if (merged.length === 0) { return [0]; } return merged; } //# sourceMappingURL=fast-expansion-sum.js.map ;// ./node_modules/big-float-ts/node/double-expansion/grow-expansion.js // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ const compress = (/* unused pure expression or super */ null && (eCompress)); /** * Returns the result of adding a double to an expansion. * * Let e be a nonoverlapping expansion of m p-bit components, and let b be a * p-bit value where p >= 3. Suppose that the components e_1, ..., e_m are * sorted in order of *increasing* magnitude, except that any of the ei may be * zero. * Then the following algorithm will produce a nonoverlapping expansion such * that h = sum_i(h_i) = e + b, where the components h_1, ..., h_(m+1) are also * in order of increasing magnitude, except that any of the h_i may be zero. * Furthermore, if e is nonadjacent and round-to-even tiebreaking is used, then * h is nonadjacent. * See https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf * @param e A floating point expansion * @param b Another floating point expansion */ function growExpansion(e, b) { const m = e.length; let q = b; //const h: number[] = new Array(m+1); const h = []; //let j = 0; for (let i = 0; i < m; i++) { // Note the use of twoSum and not fastTwoSum. //[h[i], q] = ts(q, e[i]); const ee = e[i]; const x = q + ee; const bv = x - q; const hh = (q - (x - bv)) + (ee - bv); if (hh !== 0) { h.push(hh); } q = x; } //h[j] = q; if (q !== 0 || h.length === 0) { h.push(q); } //return compress(h); return h; } //# sourceMappingURL=grow-expansion.js.map ;// ./node_modules/big-float-ts/node/basic/two-sum.js /** * Returns the exact result of adding two doubles. * * * the resulting array is the reverse of the standard twoSum in the literature. * * Theorem 7 (Knuth): Let a and b be p-bit floating-point numbers. Then the * following algorithm will produce a nonoverlapping expansion x + y such that * a + b = x + y, where x is an approximation to a + b and y is the roundoff * error in the calculation of x. * * See https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf */ function two_sum_twoSum(a, b) { const x = a + b; const bv = x - a; return [(a - (x - bv)) + (b - bv), x]; } // inlined //const R = a + b; const _ = R - a; const r = (a - (R - _)) + (b - _); return [r,R] //# sourceMappingURL=two-sum.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-sum.js // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ const ts = two_sum_twoSum; const addDouble = growExpansion; const e_sum_add = fastExpansionSum; /** * Returns the result of summing an array of floating point expansions. * * * The result is exact in the form of a non-overlapping floating point * expansion. * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * @param terms An array of numbers to be summed; A term is represented by a * floating point expansion. */ // The terms parameter were chosen to always be expansions in order to keep the // function monomorhic, but whether it's really worth it I am not sure. function eSum(terms) { let total = [0]; for (let i = 0; i < terms.length; i++) { const term = terms[i]; // add if (term.length === 1) { if (total.length === 1) { total = ts(total[0], term[0]); } else { total = addDouble(total, term[0]); } } else { if (total.length === 1) { total = addDouble(term, total[0]); } else { total = e_sum_add(total, term); } } } return total; } //# sourceMappingURL=e-sum.js.map ;// ./node_modules/big-float-ts/node/double-expansion/scale-expansion.js const scale_expansion_f = 134217729; // 2**27 + 1; // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ const tp = (/* unused pure expression or super */ null && (twoProduct)); const scale_expansion_ts = (/* unused pure expression or super */ null && (twoSum)); const fts = (/* unused pure expression or super */ null && (fastTwoSum)); const scale_expansion_compress = (/* unused pure expression or super */ null && (eCompress)); /** * Returns the result of multiplying an expansion by a double. * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * Theorem 19 (Shwechuk): Let e = sum_(i=1)^m(e_i) be a nonoverlapping expansion * of m p-bit components, and const b be a p-bit value where p >= 4. Suppose that * the components of e are sorted in order of increasing magnitude, except that * any of the e_i may be zero. Then the following algorithm will produce a * nonoverlapping expansion h such that h = sum_(i=1)^(2m)(h_i) = be, where the * components of h are also in order of increasing magnitude, except that any of * the h_i may be zero. Furthermore, if e is nonadjacent and round-to-even * tiebreaking is used, then h is non-adjacent. * * @param e a double floating point expansion * @param b a double */ function scaleExpansion(e, b) { const m = e.length; //const h: number[] = new Array(2*m); let q_; //[h[0], q] = tp(e[0], b); // inlined (above line) const a = e[0]; let q = a * b; const c = scale_expansion_f * a; const ah = c - (c - a); const al = a - ah; const d = scale_expansion_f * b; const bh = d - (d - b); const bl = b - bh; const h = []; //h[0] = (al*bl) - ((q - (ah*bh)) - (al*bh) - (ah*bl)); const hh = (al * bl) - ((q - (ah * bh)) - (al * bh) - (ah * bl)); if (hh !== 0) { h.push(hh); } for (let i = 1; i < m; i++) { //const [t, T] = tp(e[i], b); // inlined (above line) const a = e[i]; const T = a * b; const c = scale_expansion_f * a; const ah = c - (c - a); const al = a - ah; const d = scale_expansion_f * b; const bh = d - (d - b); const bl = b - bh; const t = (al * bl) - ((T - (ah * bh)) - (al * bh) - (ah * bl)); //[h[2*i-1], q_] = ts(q, t); // inlined (above line) const x = q + t; const bv = x - q; //h[2*i-1] = (q - (x - bv)) + (t - bv); //h.push((q - (x - bv)) + (t - bv)); const hh = (q - (x - bv)) + (t - bv); if (hh !== 0) { h.push(hh); } q_ = x; //[h[2*i], q] = fts(T, q_); // inlined (above line) const xx = T + q_; //h[2*i] = q_ - (xx - T); //h.push(q_ - (xx - T)); const hhh = q_ - (xx - T); if (hhh !== 0) { h.push(hhh); } q = xx; } //h[2*m - 1] = q; //h.push(q); if (q !== 0 || h.length === 0) { h.push(q); } //return eCompress(h); return h; } /** * Returns the result of multiplying an expansion by a double. * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * Theorem 19 (Shwechuk): Let e = sum_(i=1)^m(e_i) be a nonoverlapping expansion * of m p-bit components, and const b be a p-bit value where p >= 4. Suppose that * the components of e are sorted in order of increasing magnitude, except that * any of the e_i may be zero. Then the following algorithm will produce a * nonoverlapping expansion h such that h = sum_(i=1)^(2m)(h_i) = be, where the * components of h are also in order of increasing magnitude, except that any of * the h_i may be zero. Furthermore, if e is nonadjacent and round-to-even * tiebreaking is used, then h is non-adjacent. * * @param e a double floating point expansion * @param b a double */ function scaleExpansion2(b, e) { const m = e.length; //const h: number[] = new Array(2*m); let q_; //[h[0], q] = tp(e[0], b); // inlined (above line) const a = e[0]; let q = a * b; const c = scale_expansion_f * a; const ah = c - (c - a); const al = a - ah; const d = scale_expansion_f * b; const bh = d - (d - b); const bl = b - bh; const h = []; //h[0] = (al*bl) - ((q - (ah*bh)) - (al*bh) - (ah*bl)); const hh = (al * bl) - ((q - (ah * bh)) - (al * bh) - (ah * bl)); if (hh !== 0) { h.push(hh); } for (let i = 1; i < m; i++) { //const [t, T] = tp(e[i], b); // inlined (above line) const a = e[i]; const T = a * b; const c = scale_expansion_f * a; const ah = c - (c - a); const al = a - ah; const d = scale_expansion_f * b; const bh = d - (d - b); const bl = b - bh; const t = (al * bl) - ((T - (ah * bh)) - (al * bh) - (ah * bl)); //[h[2*i-1], q_] = ts(q, t); // inlined (above line) const x = q + t; const bv = x - q; //h[2*i-1] = (q - (x - bv)) + (t - bv); //h.push((q - (x - bv)) + (t - bv)); const hh = (q - (x - bv)) + (t - bv); if (hh !== 0) { h.push(hh); } q_ = x; //[h[2*i], q] = fts(T, q_); // inlined (above line) const xx = T + q_; //h[2*i] = q_ - (xx - T); //h.push(q_ - (xx - T)); const hhh = q_ - (xx - T); if (hhh !== 0) { h.push(hhh); } q = xx; } //h[2*m - 1] = q; //h.push(q); if (q !== 0 || h.length === 0) { h.push(q); } //return eCompress(h); return h; } //# sourceMappingURL=scale-expansion.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-diff.js // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ const negativeOf = eNegativeOf; const e_diff_add = fastExpansionSum; /** * Returns the difference between two floating point expansions, i.e. e - f. * * * see [Shewchuk](https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) * * @param e a floating point expansion * @param f another floating point expansion */ function eDiff(e, f) { const g = negativeOf(f); return e_diff_add(e, g); } //# sourceMappingURL=e-diff.js.map ;// ./node_modules/big-float-ts/node/double-expansion/e-long-divide.js // We *have* to do the below❗ The assignee is a getter❗ The assigned is a pure function❗ const e_long_divide_eNegativeOf = eNegativeOf; const e_long_divide_fastExpansionSum = fastExpansionSum; const e_long_divide_eCompress = e_compress_eCompress; const e_long_divide_growExpansion = growExpansion; const e_long_divide_eSum = eSum; const e_long_divide_scaleExpansion = scaleExpansion; const e_long_divide_eDiff = eDiff; const sign = Math.sign; function eLongDivide(N, D) { N = e_long_divide_eCompress(N); D = e_long_divide_eCompress(D); // get the most significant double // out by at most 1 ulp, exact if d < MAX_SAFE_INT const d = D[D.length - 1]; // trivial cases if (D.length === 1) { if (d === 0) {