Add files via upload
This commit is contained in:
parent
64b80ad64e
commit
1675deb3d2
1 changed files with 9 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
# The following are snap functions for finding a best approximated integer or rational number for a real number:
|
||||
|
||||
import numpy as np
|
||||
from sympy import Rational
|
||||
|
||||
def bestApproximation(x,imax):
|
||||
# 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."""
|
||||
p = np.array(p)
|
||||
snaps = np.array(list(bestApproximation(x,100) for x in p))
|
||||
chosen = np.argsort(snaps[:, 3])[:top]
|
||||
return dict(list(zip(chosen, snaps[chosen, 0:3])))
|
||||
chosen = np.argsort(snaps[:, 3])[:top]
|
||||
d = dict(list(zip(chosen, snaps[chosen, 1:3])))
|
||||
d = {k: f"{val[0]}/{val[1]}" for k,val in d.items()}
|
||||
|
||||
return d
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue