Godtone
Joined 17 December 2020
→My Python 3 code: added a copyleft license |
|||
| Line 471: | Line 471: | ||
i += 1 | i += 1 | ||
return l | return l | ||
lim = prime_lim | |||
prime_limit = prime_lim | |||
# Can be thought of as the conceptual opposite of strip_list_of_right(). | # Can be thought of as the conceptual opposite of strip_list_of_right(). | ||
def right_justify_list(l, n, justify_with=0): | def right_justify_list(l, n, justify_with=0): | ||
| Line 527: | Line 529: | ||
# Note: when combining two factorisations in some way, | # Note: when combining two factorisations in some way, | ||
# both must either be normal or subgroup-based. | # both must either be normal or subgroup-based. | ||
def mul_fact(* | def mul_fact(*fs): | ||
max_len = max([ len( | max_len = max([ len(f) for f in fs ]) # to convert list to tuple next line | ||
fs = (*[ right_justify_list(f.copy(),max_len) for f in fs ],) # conversion | |||
result = [ sum([ | result = [ sum([ fs[n][i] for n in range(len(fs)) ]) for i in range(max_len)] | ||
return result | return result | ||
def mul_iv(*rs): | |||
return iv( prod([ r[0] for r in rs ]), prod([ r[1] for r in rs ]) ) | |||
def recip_fact(f, p=None): # useless parameter p used for niceness | def recip_fact(f, p=None): # useless parameter p used for niceness | ||
return [-i for i in f] | return [-i for i in f] | ||
def div_fact(x, | def recip_iv(r): | ||
return | return (r[1], r[0]) | ||
def div_fact(f1, f2): | |||
return mul_fact(f1, recip_fact(f2)) | |||
def div_iv(r1, r2): | |||
return mul_iv(r1, recip_iv(r2)) | |||
def as_float(x, p=None): | |||
if type(x)==list: # monzo (factored) | |||
uf = unfact(x, p) | |||
return uf[0] / uf[1] | |||
elif type(x)==tuple: # ratio (unfactored) | |||
return x[0] / x[1] | |||
else: # assumed to already be a float (or integer) | |||
return x | |||
def S(k): | |||
return (k*k, k*k-1) | |||
s = S | |||
def convert(x, totype, p=None): | |||
if totype==float: | |||
return as_float(x,p) | |||
if totype==int: | |||
if x==int: | |||
return x | |||
interval = 0 | |||
if x==tuple: | |||
interval = iv(x[0],x[1]) | |||
if x==list: | |||
interval = unfact(x,p) | |||
if len(fact_int(interval[1]))==1: # if rooted (denom in 2-lim) | |||
return interval[0] | |||
else: | |||
raise TypeError("interval is not rooted") | |||
if totype==tuple: | |||
if x==int: | |||
return (x,1) | |||
if x==tuple: | |||
return x | |||
if x==list: | |||
return unfact(x,p) | |||
if totype==list: | |||
if x==int: | |||
return fact_int(x,p) | |||
if x==tuple: | |||
return fact(x,p) | |||
if x==list: | |||
return x | |||
def fact(r, p=None): | def fact(r, p=None): | ||
if p==None: | if p==None: | ||
| Line 569: | Line 617: | ||
rhs = what[center_loc+len(center):] | rhs = what[center_loc+len(center):] | ||
return ' '*(spaces//2 - len(lhs)) + lhs + center + rhs + ' '*(spaces//2 - len(rhs)) | return ' '*(spaces//2 - len(lhs)) + lhs + center + rhs + ' '*(spaces//2 - len(rhs)) | ||
# Converts a prime factorisation into a prettier and potentially | # Converts a prime factorisation into a prettier and potentially | ||
| Line 669: | Line 709: | ||
val.append(pval_steps) | val.append(pval_steps) | ||
return val | return val | ||
val = make_val | |||
def odd_lim(lim, remove=set(), add=set(), complements=True): | def odd_lim(lim, remove=set(), add=set(), complements=True): | ||
odds = set([i for i in range(1,lim+1,2) if i not in remove]) | odds = set([i for i in range(1,lim+1,2) if i not in remove]) | ||