Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fractionSum for simplicity and performance #1674

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions music21/meter/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def fractionToSlashMixed(fList: NumDenomTuple) -> tuple[tuple[str, int], ...]:


@lru_cache(512)
def fractionSum(numDenomTuple: NumDenomTuple) -> NumDenom:
def fractionSum(numDenomTuple):
'''
Given a tuple of tuples of numerator and denominator,
find the sum; does NOT reduce to its lowest terms.
Expand All @@ -245,39 +245,27 @@ def fractionSum(numDenomTuple: NumDenomTuple) -> NumDenom:
>>> fractionSum(())
(0, 1)

This method might seem like an easy place to optimize and simplify
by just doing a fractions.Fraction() sum (I tried!), but not reducing to
its lowest terms is a feature of this method. 3/8 + 3/8 = 6/8, not 3/4:
Not reducing to the lowest terms is a feature of this method.
i.e. 3/8 + 3/8 = 6/8, not 3/4:

>>> fractionSum(((3, 8), (3, 8)))
(6, 8)
'''
nList = []
dList = []
dListUnique = set()

dUnique = set()
total = 0
for n, d in numDenomTuple:
nList.append(n)
dList.append(d)
dListUnique.add(d)

if len(dListUnique) == 1:
n = sum(nList)
d = next(iter(dListUnique))
# Does not reduce to lowest terms...
return (n, d)
else: # there might be a better way to do this
d = 1
d = math.lcm(*dListUnique)
# after finding d, multiply each numerator
nShift = []
for i in range(len(nList)):
nSrc = nList[i]
dSrc = dList[i]
scalar = d // dSrc
nShift.append(nSrc * scalar)
return (sum(nShift), d)
dUnique.add(d)
total += n / d

if len(dUnique) == 1:
# This intentionally doesn't reduce to lowest terms...
d = dUnique.pop()
else:
d = math.lcm(*dUnique)

# put total in terms of denominator
n = round(total * d)
return (n, d)


@lru_cache(512)
Expand Down
Loading