Skip to content

Instantly share code, notes, and snippets.

@KmolYuan
Last active September 24, 2018 14:39
Show Gist options
  • Save KmolYuan/1920e72fef9e2eaac221b0a06f53a608 to your computer and use it in GitHub Desktop.
Save KmolYuan/1920e72fef9e2eaac221b0a06f53a608 to your computer and use it in GitHub Desktop.
number synthesis
from itertools import product
def Max(NL, NJ):
"""Find max number of joint on each link.
+ NL <= NJ and NJ <= (2 * NL - 3)
+ (2 * NL - 3) <= NJ and NJ <= (NL * (NL - 1) / 2)
+ other exceptions (return -1).
"""
if NL <= NJ <= (2 * NL - 3):
return NJ - NL + 2
if NL == NJ == 0:
return -1
if (2 * NL - 3) <= NJ <= (NL * (NL - 1) / 2):
return NL - 1
return -1
def sum_factors(factors):
"""F0*N2 + F1*N3 + F2*N4 + ... + Fn*N(n+2)"""
factor = 0
for i, f in enumerate(factors):
factor += f * (i + 2)
return factor
def number_synthesis(NL, NJ):
result = []
Mmax = Max(NL, NJ)
if Mmax == -1:
raise Exception("incorrect mechanism.")
for symbols in product(range(NL + 1), repeat = (Mmax - 2)):
NLMmax = NL - sum(symbols)
if NLMmax < 0:
continue
answer = symbols + (NLMmax,)
if sum_factors(answer) == (2 * NJ):
result.append(answer)
return tuple(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment