From 28fe2a471bbadbb334ab3d58798940dabf41d599 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Sun, 4 Feb 2024 15:55:20 +0200 Subject: [PATCH] Expose prime monzo to subgroup monzo conversion --- src/__tests__/subgroup.spec.ts | 7 +++++++ src/subgroup.ts | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/__tests__/subgroup.spec.ts b/src/__tests__/subgroup.spec.ts index 0912439..410ee64 100644 --- a/src/__tests__/subgroup.spec.ts +++ b/src/__tests__/subgroup.spec.ts @@ -182,4 +182,11 @@ describe('Fractional just intonation subgroup', () => { const subgroup = new Subgroup('5.41.9.13/7'); expect(subgroup.wartLetters().join('')).toBe('cmqr'); }); + + it('can convert standard monzos to subgroup monzos', () => { + const subgroup = new Subgroup('2.9.7/5'); + expect(subgroup.primeMonzoToSubgroupMonzo([1, 2, 2, -2])).toEqual([ + 1, 1, -2, + ]); + }); }); diff --git a/src/subgroup.ts b/src/subgroup.ts index 16783fa..8a2294a 100644 --- a/src/subgroup.ts +++ b/src/subgroup.ts @@ -224,7 +224,7 @@ export class Subgroup { const dimensions = basisMonzos[0].length; const primeMonzo = toMonzoAndResidual(fraction, dimensions)[0]; - const monzo = this.toMonzo_(primeMonzo, basisMonzos); + const monzo = this.primeMonzoToSubgroupMonzo(primeMonzo, basisMonzos); monzo.forEach((component, index) => { fraction = fraction.div(this.basis[index].pow(component)!); @@ -233,7 +233,13 @@ export class Subgroup { return [monzo, fraction]; } - private toMonzo_(primeMonzo: Monzo, basisMonzos?: Monzo[]): Monzo { + /** + * Convert a monzo in the standard prime basis to a monzo in the subgroup's basis. + * @param primeMonzo Array of prime exponents. + * @param basisMonzos Precalculated array of the subgroup's basis as arrays of prime exponents. + * @returns Array of basis exponents. + */ + primeMonzoToSubgroupMonzo(primeMonzo: Monzo, basisMonzos?: Monzo[]): Monzo { if (basisMonzos === undefined) { basisMonzos = this.basisMonzos(); } @@ -497,7 +503,7 @@ export class Subgroup { ) { if (primeMapping) { // XXX: Strips away residual - return this.toMonzo_(interval as Monzo); + return this.primeMonzoToSubgroupMonzo(interval as Monzo); } else { const result = [...interval]; while (result.length < this.basis.length) {