diff --git a/.eslintrc.json b/.eslintrc.json index 7912e12..67986a1 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,6 +6,7 @@ "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/ban-ts-comment": 0, "node/no-unpublished-import": ["error", {"allowModules": ["vitest"]}], + "prefer-const": ["error", {"destructuring": "all"}], "no-restricted-syntax": ["error", "SequenceExpression"] } } diff --git a/src/__benchmarks__/monzo.bench.ts b/src/__benchmarks__/monzo.bench.ts index 3c41bc3..c23ff7f 100644 --- a/src/__benchmarks__/monzo.bench.ts +++ b/src/__benchmarks__/monzo.bench.ts @@ -1,13 +1,21 @@ import {describe, bench} from 'vitest'; // It's important to use the distributed versions for a realistic comparison -import {toMonzoLegacy, primeLimitLegacy} from '../../legacy/legacy'; -import {toMonzo, primeLimit} from '../../dist'; +import { + toMonzoLegacy, + primeLimitLegacy, + toMonzoAndResidualLegacy, +} from '../../legacy/legacy'; +import {toMonzo, primeLimit, toMonzoAndResidual} from '../../dist'; function randInt() { return Math.ceil(Math.random() * 1000000000); } +function randNumComponents() { + return 2 + Math.floor(Math.random() * 10); +} + describe('Number to prime exponent vector conversion', () => { bench('Old implementation', () => { try { @@ -31,3 +39,23 @@ describe('Prime limit calculator', () => { primeLimit(randInt()); }); }); + +describe('Monzo with residual', () => { + bench('Current implementation', () => { + toMonzoAndResidual(randInt(), randNumComponents()); + }); + + bench('Old implementation', () => { + toMonzoAndResidualLegacy(randInt(), randNumComponents()); + }); +}); + +describe('Monzo with residual (bigint)', () => { + bench('Current implementation', () => { + toMonzoAndResidual(BigInt(randInt()), randNumComponents()); + }); + + bench('Old implementation', () => { + toMonzoAndResidualLegacy(BigInt(randInt()), randNumComponents()); + }); +}); diff --git a/src/__tests__/monzo.spec.ts b/src/__tests__/monzo.spec.ts index c54434b..a498d44 100644 --- a/src/__tests__/monzo.spec.ts +++ b/src/__tests__/monzo.spec.ts @@ -145,6 +145,14 @@ describe('Fraction to monzo converter', () => { expect(monzo[0]).toBe(1); expect(monzo[1]).toBe(0); }); + + it('leaves negative residual for integers', () => { + const [monzo, residual] = toMonzoAndResidual(-10, 2); + expect(residual.toFraction()).toBe('-5'); + expect(monzo).toHaveLength(2); + expect(monzo[0]).toBe(1); + expect(monzo[1]).toBe(0); + }); }); describe('Monzo to fraction converter', () => { diff --git a/src/legacy.ts b/src/legacy.ts index 07c4e39..4eba4b0 100644 --- a/src/legacy.ts +++ b/src/legacy.ts @@ -162,3 +162,86 @@ function bigIntPrimeLimit( } } } + +/** + * Extract the exponents of the prime factors of a rational number. + * @param n Rational number to convert to a monzo. + * @param numberOfComponents Number of components in the result. + * @returns The monzo representing `n` and a multiplicative residue that cannot be represented in the given limit. + */ +export function toMonzoAndResidualLegacy( + n: bigint, + numberOfComponents: number +): [Monzo, bigint]; +export function toMonzoAndResidualLegacy( + n: FractionValue, + numberOfComponents: number +): [Monzo, Fraction]; +export function toMonzoAndResidualLegacy( + n: FractionValue | bigint, + numberOfComponents: number +): [Monzo, Fraction] | [Monzo, bigint] { + if (typeof n === 'bigint') { + return bigIntToMonzoAndResidualLegacy(n, numberOfComponents); + } + n = new Fraction(n); + const numerator = n.n; + const denominator = n.d; + + if (!n.n) { + return [Array(numberOfComponents).fill(0), new Fraction(0)]; + } + + let nProbe = 1; + let dProbe = 1; + + const result = Array(numberOfComponents).fill(-1); + for (let i = 0; i < numberOfComponents; ++i) { + let lastProbe; + do { + lastProbe = nProbe; + nProbe *= PRIMES[i]; + result[i]++; + } while (numerator % nProbe === 0); + nProbe = lastProbe; + + // The fraction is in lowest terms so we know that positive components exclude negative components. + if (result[i]) { + continue; + } + + result[i] = 1; + do { + lastProbe = dProbe; + dProbe *= PRIMES[i]; + result[i]--; + } while (denominator % dProbe === 0); + dProbe = lastProbe; + } + + return [result, (n as Fraction).div(new Fraction(nProbe, dProbe))]; +} + +function bigIntToMonzoAndResidualLegacy( + n: bigint, + numberOfComponents: number +): [Monzo, bigint] { + if (!n) { + return [Array(numberOfComponents).fill(0), 0n]; + } + + let probe = 1n; + + const result = Array(numberOfComponents).fill(-1); + for (let i = 0; i < numberOfComponents; ++i) { + let lastProbe; + do { + lastProbe = probe; + probe *= BIG_INT_PRIMES[i]; + result[i]++; + } while (n % probe === 0n); + probe = lastProbe; + } + + return [result, n / probe]; +} diff --git a/src/monzo.ts b/src/monzo.ts index cce980e..718ff55 100644 --- a/src/monzo.ts +++ b/src/monzo.ts @@ -6,6 +6,15 @@ import {BIG_INT_PRIMES, PRIMES} from './primes'; */ export type Monzo = number[]; +/** + * Calculate the absolute value of a BigInt. + * @param n Integer to measure. + * @returns Size of the integer as a BigInt. + */ +export function bigAbs(n: bigint) { + return n < 0n ? -n : n; +} + /** * Check if two monzos are equal. * @param a The first monzo. @@ -225,89 +234,6 @@ function bigIntToMonzo(n: bigint) { } } -/** - * Extract the exponents of the prime factors of a rational number. - * @param n Rational number to convert to a monzo. - * @param numberOfComponents Number of components in the result. - * @returns The monzo representing `n` and a multiplicative residue that cannot be represented in the given limit. - */ -export function toMonzoAndResidual( - n: bigint, - numberOfComponents: number -): [Monzo, bigint]; -export function toMonzoAndResidual( - n: FractionValue, - numberOfComponents: number -): [Monzo, Fraction]; -export function toMonzoAndResidual( - n: FractionValue | bigint, - numberOfComponents: number -): [Monzo, Fraction] | [Monzo, bigint] { - if (typeof n === 'bigint') { - return bigIntToMonzoAndResidual(n, numberOfComponents); - } - n = new Fraction(n); - const numerator = n.n; - const denominator = n.d; - - if (!n.n) { - return [Array(numberOfComponents).fill(0), new Fraction(0)]; - } - - let nProbe = 1; - let dProbe = 1; - - const result = Array(numberOfComponents).fill(-1); - for (let i = 0; i < numberOfComponents; ++i) { - let lastProbe; - do { - lastProbe = nProbe; - nProbe *= PRIMES[i]; - result[i]++; - } while (numerator % nProbe === 0); - nProbe = lastProbe; - - // The fraction is in lowest terms so we know that positive components exclude negative components. - if (result[i]) { - continue; - } - - result[i] = 1; - do { - lastProbe = dProbe; - dProbe *= PRIMES[i]; - result[i]--; - } while (denominator % dProbe === 0); - dProbe = lastProbe; - } - - return [result, (n as Fraction).div(new Fraction(nProbe, dProbe))]; -} - -function bigIntToMonzoAndResidual( - n: bigint, - numberOfComponents: number -): [Monzo, bigint] { - if (!n) { - return [Array(numberOfComponents).fill(0), 0n]; - } - - let probe = 1n; - - const result = Array(numberOfComponents).fill(-1); - for (let i = 0; i < numberOfComponents; ++i) { - let lastProbe; - do { - lastProbe = probe; - probe *= BIG_INT_PRIMES[i]; - result[i]++; - } while (n % probe === 0n); - probe = lastProbe; - } - - return [result, n / probe]; -} - /** * Convert a monzo to the fraction it represents. * @param monzo Iterable of prime exponents. @@ -444,3 +370,203 @@ function bigIntPrimeLimit( } } } + +/** + * Extract the exponents of the prime factors of a rational number. + * @param n Rational number to convert to a monzo. + * @param numberOfComponents Number of components in the result. + * @returns The monzo representing `n` and a multiplicative residue that cannot be represented in the given limit. + */ +export function toMonzoAndResidual( + n: bigint, + numberOfComponents: number +): [Monzo, bigint]; +export function toMonzoAndResidual( + n: FractionValue, + numberOfComponents: number +): [Monzo, Fraction]; +export function toMonzoAndResidual( + n: FractionValue | bigint, + numberOfComponents: number +): [Monzo, Fraction] | [Monzo, bigint] { + if (typeof n === 'bigint') { + return bigIntToMonzoAndResidual(n, numberOfComponents); + } + n = new Fraction(n); + + if (!n.n) { + return [Array(numberOfComponents).fill(0), new Fraction(0)]; + } + + let [n7, numerator] = intToMonzo7(n.n); + let [d7, denominator] = intToMonzo7(n.d); + + const result: Monzo = Array(numberOfComponents).fill(-1); + + result[0] = n7[0] - d7[0]; + result[1] = n7[1] - d7[1]; + result[2] = n7[2] - d7[2]; + result[3] = n7[3] - d7[3]; + + let nProbe = 1; + let dProbe = 1; + + for (let i = 4; i < numberOfComponents; ++i) { + let lastProbe: number; + do { + lastProbe = nProbe; + nProbe *= PRIMES[i]; + result[i]++; + } while (numerator % nProbe === 0); + nProbe = lastProbe; + + // The fraction is in lowest terms so we know that positive components exclude negative components. + if (result[i]) { + continue; + } + + result[i] = 1; + do { + lastProbe = dProbe; + dProbe *= PRIMES[i]; + result[i]--; + } while (denominator % dProbe === 0); + dProbe = lastProbe; + } + + numerator /= nProbe; + denominator /= dProbe; + + // Silly user, 7-limit is fine for everyone. + while (result.length > numberOfComponents) { + const exponent = result.pop()!; + if (exponent > 0) { + numerator *= PRIMES[result.length] ** exponent; + } else if (exponent < 0) { + denominator *= PRIMES[result.length] ** -exponent; + } + } + + const residual = new Fraction(numerator, denominator); + residual.s = (n as Fraction).s; + + return [result, residual]; +} + +function bigIntToMonzoAndResidual( + n: bigint, + numberOfComponents: number +): [Monzo, bigint] { + if (!n) { + return [Array(numberOfComponents).fill(0), 0n]; + } + + let [result, residual] = bigIntToMonzo7(bigAbs(n)); + + let probe = 1n; + + for (let i = 4; i < numberOfComponents; ++i) { + let lastProbe: bigint; + do { + lastProbe = probe; + probe *= BIG_INT_PRIMES[i]; + result[i]++; + } while (residual % probe === 0n); + probe = lastProbe; + } + + residual /= probe; + + while (result.length > numberOfComponents) { + const exponent = result.pop()!; + residual *= BIG_INT_PRIMES[result.length] ** BigInt(exponent); + } + + return [result, n < 0n ? -residual : residual]; +} + +// Factors of 315 factorized to monzos +const M0 = [0, 0, 0, 0]; +const M1 = [0, 1, 0, 0]; +const M2 = [0, 0, 1, 0]; +const M3 = [0, 0, 0, 1]; +const M4 = [0, 2, 0, 0]; +const M5 = [0, 1, 1, 0]; +const M6 = [0, 1, 0, 1]; +const M7 = [0, 0, 1, 1]; +const M8 = [0, 2, 1, 0]; +const M9 = [0, 2, 0, 1]; +const Ma = [0, 1, 1, 1]; +const Mb = [0, 2, 1, 1]; + +// prettier-ignore +const MONZO_LOOKUP: [number, Monzo][] = [ + [315, Mb], [1, M0], [1, M0], [3, M1], [1, M0], [5, M2], [3, M1], [7, M3], [1, M0], [9, M4], [5, M2], [1, M0], [3, M1], [1, M0], [7, M3], + [15, M5], [1, M0], [1, M0], [9, M4], [1, M0], [5, M2], [21, M6], [1, M0], [1, M0], [3, M1], [5, M2], [1, M0], [9, M4], [7, M3], [1, M0], + [15, M5], [1, M0], [1, M0], [3, M1], [1, M0], [35, M7], [9, M4], [1, M0], [1, M0], [3, M1], [5, M2], [1, M0], [21, M6], [1, M0], [1, M0], + [45, M8], [1, M0], [1, M0], [3, M1], [7, M3], [5, M2], [3, M1], [1, M0], [1, M0], [9, M4], [5, M2], [7, M3], [3, M1], [1, M0], [1, M0], + [15, M5], [1, M0], [1, M0], [63, M9], [1, M0], [5, M2], [3, M1], [1, M0], [1, M0], [3, M1], [35, M7], [1, M0], [9, M4], [1, M0], [1, M0], + [15, M5], [1, M0], [7, M3], [3, M1], [1, M0], [5, M2], [9, M4], [1, M0], [1, M0], [21, M6], [5, M2], [1, M0], [3, M1], [1, M0], [1, M0], + [45, M8], [7, M3], [1, M0], [3, M1], [1, M0], [5, M2], [3, M1], [1, M0], [7, M3], [9, M4], [5, M2], [1, M0], [3, M1], [1, M0], [1, M0], + [105, Ma], [1, M0], [1, M0], [9, M4], [1, M0], [5, M2], [3, M1], [7, M3], [1, M0], [3, M1], [5, M2], [1, M0], [9, M4], [1, M0], [7, M3], + [15, M5], [1, M0], [1, M0], [3, M1], [1, M0], [5, M2], [63, M9], [1, M0], [1, M0], [3, M1], [5, M2], [1, M0], [3, M1], [7, M3], [1, M0], + [45, M8], [1, M0], [1, M0], [3, M1], [1, M0], [35, M7], [3, M1], [1, M0], [1, M0], [9, M4], [5, M2], [1, M0], [21, M6], [1, M0], [1, M0], + [15, M5], [1, M0], [1, M0], [9, M4], [7, M3], [5, M2], [3, M1], [1, M0], [1, M0], [3, M1], [5, M2], [7, M3], [9, M4], [1, M0], [1, M0], + [15, M5], [1, M0], [1, M0], [21, M6], [1, M0], [5, M2], [9, M4], [1, M0], [1, M0], [3, M1], [35, M7], [1, M0], [3, M1], [1, M0], [1, M0], + [45, M8], [1, M0], [7, M3], [3, M1], [1, M0], [5, M2], [3, M1], [1, M0], [1, M0], [63, M9], [5, M2], [1, M0], [3, M1], [1, M0], [1, M0], + [15, M5], [7, M3], [1, M0], [9, M4], [1, M0], [5, M2], [3, M1], [1, M0], [7, M3], [3, M1], [5, M2], [1, M0], [9, M4], [1, M0], [1, M0], + [105, Ma], [1, M0], [1, M0], [3, M1], [1, M0], [5, M2], [9, M4], [7, M3], [1, M0], [3, M1], [5, M2], [1, M0], [3, M1], [1, M0], [7, M3], + [45, M8], [1, M0], [1, M0], [3, M1], [1, M0], [5, M2], [21, M6], [1, M0], [1, M0], [9, M4], [5, M2], [1, M0], [3, M1], [7, M3], [1, M0], + [15, M5], [1, M0], [1, M0], [9, M4], [1, M0], [35, M7], [3, M1], [1, M0], [1, M0], [3, M1], [5, M2], [1, M0], [63, M9], [1, M0], [1, M0], + [15, M5], [1, M0], [1, M0], [3, M1], [7, M3], [5, M2], [9, M4], [1, M0], [1, M0], [3, M1], [5, M2], [7, M3], [3, M1], [1, M0], [1, M0], + [45, M8], [1, M0], [1, M0], [21, M6], [1, M0], [5, M2], [3, M1], [1, M0], [1, M0], [9, M4], [35, M7], [1, M0], [3, M1], [1, M0], [1, M0], + [15, M5], [1, M0], [7, M3], [9, M4], [1, M0], [5, M2], [3, M1], [1, M0], [1, M0], [21, M6], [5, M2], [1, M0], [9, M4], [1, M0], [1, M0], + [15, M5], [7, M3], [1, M0], [3, M1], [1, M0], [5, M2], [9, M4], [1, M0], [7, M3], [3, M1], [5, M2], [1, M0], [3, M1], [1, M0], [1, M0] +]; + +const BIG_LOOKUP: [bigint, Monzo][] = MONZO_LOOKUP.map(l => [ + BigInt(l[0]), + l[1], +]); + +// XXX: Not resistant to zero, check your inputs. +function intToMonzo7(n: number): [Monzo, number] { + const result = [0, 0, 0, 0]; + // Factors of two using bit magic + while (!(n & 1)) { + const twos = (n ^ (n - 1)) & n; + n /= twos; + result[0] += Math.log2(twos); + } + while (true) { + const [factor, m] = MONZO_LOOKUP[n % 315]; + if (factor === 1) { + break; + } + n /= factor; + result[1] += m[1]; + result[2] += m[2]; + result[3] += m[3]; + } + return [result, n]; +} + +// XXX: Not resistant to zero, check your inputs. +function bigIntToMonzo7(n: bigint): [Monzo, bigint] { + const result = [0, 0, 0, 0]; + // Factors of two using bit magic + const twos = (n ^ (n - 1n)) & n; + n /= twos; + result[0] += Math.log2(Number(twos)); + while (true) { + const [factor, m] = BIG_LOOKUP[Number(n % 315n)]; + if (factor === 1n) { + break; + } + n /= factor; + result[1] += m[1]; + result[2] += m[2]; + result[3] += m[3]; + } + return [result, n]; +}