From 1675deb3d2a7e3e9dbb093d942abbb4b4e7c3551 Mon Sep 17 00:00:00 2001 From: Silviu Marian Udrescu Date: Sun, 28 Jun 2020 14:27:12 -0400 Subject: [PATCH] Add files via upload --- Code/S_snap.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Code/S_snap.py b/Code/S_snap.py index 9fd96e4..02cdcbd 100644 --- a/Code/S_snap.py +++ b/Code/S_snap.py @@ -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 + + +