def getEpimoricZip(r): "(6/5) -> [(3/2,1), (5/4,-1)]" result = [] while r != Rational(1): p, i = zip(utils.primes(), r)[-1] e = Rational(p, p-1) result.insert(0, (e, i)) r /= e ** i return result
def getEpimoricZip(r): #"(6/5) -> [(3/2,1), (5/4,-1)]" "(6/5) -> [(3,1), (5,-1)]" result = [] while r != Rational(1): p, i = zip(primes, r)[-1] result.insert(0, (p, i)) r /= Rational(p, p - 1)**i return result
def test(): #r = Rational(21, 8) #r = Rational(9, 5) #r = Rational(81, 80) r0 = Rational(9) r1 = Rational(5) for path in getPaths(r0,r1): print "=" * 30 print path.r0, "->", path.r1, "way", path.way for s in path.steps: print str(s) f = formatPath(path) if f: print print f
def getSteps(n0, n1): #"(6/5) -> [(3/2,1), (5/4,-1)]" #"(6/5) -> [(3,1), (5,-1)]" "(6/5) -> (3, -5)" result = () r = Rational(n1, n0) while r != Rational(1): p, i = zip(primes, r)[-1] a = abs(i) s = i//a for j in range(a): result += (p * s,) r /= Rational(p, p-1) ** i return result
def formatPath(path): from collections import defaultdict # grid = defaultdict(defaultdict) for s in path.steps: grid[s.note0][s.scala] = s.i0 grid[s.note1][s.scala] = s.i1 #grid[r0][Rational(1)] = r0 #grid[r1][Rational(1)] = r1 #for i in sorted(grid.keys()): print i, dict(grid[i]) # scalas scalas = [] for row in grid.values(): scalas += row.keys() scalas = sorted(set(scalas)) gcd = Rational.gcd(*scalas) # table columns = sorted(set(scalas + [gcd])) table = [] table.append([""] + [("(%s)" % str(j)) for j in columns]) # columns header for i in sorted(grid.keys())[::-1]: def cell(j): r = grid[i].get(j) if r is not None: return str(r) if j == gcd: return "[%s]" % str(i/gcd) return "" h = i in (path.r0, path.r1) and ">" or "" # row header table.append([h] + [cell(j) for j in columns]) #for row in table: print row # result widths = [max(len(row[j]) for row in table) for j in range(len(table[0]))] return "\n".join(" ".join(s.center(widths[j]) for (j,s) in enumerate(row)) for row in table)
def check_numerator(N): #print N if N < R0: return True if N > R1: return False f = Rational(N, D) #print f, fraction(f), "(%s)" % HARMONIC_DISTANCE(f) needed = self.add_result(f) return needed
def __init__(self, *items, **params): defaultdict.__init__(self, float) if items: weights = params.get("weights", [1.0] * len(items)) for (i, a) in enumerate(items): if isinstance(a, Rational): self[a] = weights[i] else: self[Rational(a)] = weights[ i] # create Rational key by argument
def draw(self, initial=None): keys = sorted(self.keys(), key=float) d = Rational.gcd(*keys) for i in range(int(keys[0] / d), int(keys[-1] / d) + 1): k = d * i w0 = initial and initial.get(k, 0) w1 = self.get(k, 0) print "%s %s%s%s %s" % (initial and ("%-8s" % (w0 or "")) or "", "%2d" % i, k.getFraction(5), "%-6s" % k.getSonant(), "%-8s" % (w1 or ""))
def getFundamentals(self): result = Spectrum() keys = sorted(self.keys(), key=float) # might be not sorted variations = list(utils.variations(len(keys)))[1:] for v in variations: # (0,0,1), (0,1,0), (0,1,1),.. (1,1,1) ks = [k for (b, k) in zip(v, keys) if b] d = Rational.gcd(*ks) value = utils.mult(self[k] for k in ks) value **= 1.0 / len(ks) # geometric mean result[d] += value return result
def matrix(m, n, a=0, b=9, c=1, d=1): #use current time to randomize seed time = str(datetime.datetime.now()) seed = int(time[20:26] + time[17:19] + time[14:16] \ + time[11:13] + time[8:10] + time[5:7] + time[0:4]) random.seed(seed) #generate a list of lists (matrix) with rational entries A = [[Rational(random.randint(a, b), random.randint(c,d)) for i in range(n)] for j in range(m)] #print random matrix display(A) return A
def rationalMatrix(m, n): #intial matrix A A = [] #append each entry of matrix A for i in range(m): print(f'enter each entry as "integer/integer" or just "integer" for row {str(i+1)} of matrix:') row = [] for _ in range(n): entry = input() entry = entry.split('/') try: if len(entry) == 1: ratio = Rational(int(entry[0])) elif len(entry) > 1: ratio = Rational(int(entry[0]),int(entry[1])) except: ratio = Rational() row.append(ratio) A.append(row) print() #display matrix print('matrix =') display(A) return A
def getHarmonicDistance(chord, weights=()): "chord as a list of integers, weights - floats" from rationals import Rational import harmonicity #print chord, weights if not weights: weights = (1.0, ) * len(chord) variations = list(utils.variations(len(chord))) distance = 0.0 for var in variations: ns = [n for (n, b) in zip(chord, var) if b] if len(ns) >= 2: gcd = reduce(utils.gcd, ns) ns = [n / gcd for n in ns] # reduce notes d = sum([harmonicity.simple(5)(Rational(n)) for n in ns]) dw = d * utils.mult([w for (w, b) in zip(weights, var) if b]) #print " ", ns, lcm, `r`, d #, dw distance += dw return distance
def check_denominator(D): d = Rational(1, D) h = self.harmonic_distance(d) R0 = int(math.ceil(self.range[0] * D)) R1 = int(math.floor(self.range[1] * D)) #print "------------ denominator", fraction(d), "(%s)" % h, "nominator %d..%d" % (R0,R1) #trace_result() if self.result_distance and self.result_distance <= h: return False def check_numerator(N): #print N if N < R0: return True if N > R1: return False f = Rational(N, D) #print f, fraction(f), "(%s)" % HARMONIC_DISTANCE(f) needed = self.add_result(f) return needed def co_primes(): for (i,p) in enumerate(utils.primes()): if d[i] == 0: yield p # skip primes used in denominator self.tree(co_primes, check_numerator, self.level) return True
def test_truediv(): a = Rational(3, 4) b = Rational(5, 6) assert str(a / b) == "9/10"
def test_truediv_type_error(): a = Rational(3, 4) with pytest.raises(TypeError): a / 4.5
def test_mul_int(): a = Rational(9, 4) assert str(a * 2) == "9/2"
def test_constructor_default_denom(): assert str(Rational(3) == "3/1")
def test_mul(): a = Rational(3, 4) b = Rational(5, 6) assert str(a * b) == "5/8"
def test_mul_type_error(): a = Rational(3, 4) with pytest.raises(TypeError): a * 4.5
def test_sub_int(): a = Rational(9, 4) assert str(a - 2) == "1/4"
def test_sub_type_error(): a = Rational(3, 4) with pytest.raises(TypeError): a - 4.5
def test_truediv_int(): a = Rational(9, 4) assert str(a / 2) == "9/8"
def test_power_square(): a = Rational(17, 23) assert str(a.square()) == str(a.power(2))
def test_cube(): assert str(Rational(3, 4).cube()) == "27/64"
def test_square(): assert str(Rational(3, 4).square()) == "9/16"
def test_add_int(): a = Rational(3, 4) assert str(a + 2) == "11/4"
def test_power(): assert str(Rational(3, 4).power(5)) == "243/1024"
def test_constructor(): assert str(Rational(3, 4)) == "3/4"
def test_power_type_error(): a = Rational(17, 23) with pytest.raises(TypeError): a.power(4.3)
def test_sub(): a = Rational(3, 4) b = Rational(5, 6) assert str(b - a) == "1/12"