Add files via upload

This commit is contained in:
Silviu Marian Udrescu 2020-06-28 14:27:12 -04:00 committed by GitHub
parent 64b80ad64e
commit 1675deb3d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
# The following are snap functions for finding a best approximated integer or rational number for a real number: # The following are snap functions for finding a best approximated integer or rational number for a real number:
import numpy as np import numpy as np
from sympy import Rational
def bestApproximation(x,imax): def bestApproximation(x,imax):
# The input is a numpy parameter vector p. # The input is a numpy parameter vector p.
@ -74,5 +75,11 @@ def rationalSnap(p, top=1):
"""Snap to nearest rational number using continued fraction.""" """Snap to nearest rational number using continued fraction."""
p = np.array(p) p = np.array(p)
snaps = np.array(list(bestApproximation(x,100) for x in p)) snaps = np.array(list(bestApproximation(x,100) for x in p))
chosen = np.argsort(snaps[:, 3])[:top] chosen = np.argsort(snaps[:, 3])[:top]
return dict(list(zip(chosen, snaps[chosen, 0:3]))) d = dict(list(zip(chosen, snaps[chosen, 1:3])))
d = {k: f"{val[0]}/{val[1]}" for k,val in d.items()}
return d