Skip to content

Commit

Permalink
Expose prime monzo to subgroup monzo conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Feb 4, 2024
1 parent 98808eb commit 28fe2a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/subgroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
});
});
12 changes: 9 additions & 3 deletions src/subgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 28fe2a4

Please sign in to comment.