fixed coprime error when encountering NaN

This commit is contained in:
Philip Eisenlohr 2020-09-19 13:38:32 -04:00
parent d6b6815693
commit 7cbd422ae3
2 changed files with 5 additions and 4 deletions

View File

@ -173,7 +173,7 @@ def scale_to_coprime(v):
m = m//reduce(np.gcd,m) m = m//reduce(np.gcd,m)
with np.errstate(divide='ignore'): with np.errstate(divide='ignore'):
if not np.allclose(v/m,v[0]/m[0]): if not np.allclose(np.ma.masked_invalid(v/m),v[np.argmax(abs(v))]/m[np.argmax(abs(v))]):
raise ValueError(f'Invalid result {m} for input {v}. Insufficient precision?') raise ValueError(f'Invalid result {m} for input {v}. Insufficient precision?')
return m return m

View File

@ -18,16 +18,17 @@ class TestUtil:
@pytest.mark.parametrize('input,output', @pytest.mark.parametrize('input,output',
[ [
([2,0],[1,0]), ([0,-2],[0,-1]),
([0.5,0.5],[1,1]), ([-0.5,0.5],[-1,1]),
([1./2.,1./3.],[3,2]), ([1./2.,1./3.],[3,2]),
([2./3.,1./2.,1./3.],[4,3,2]), ([2./3.,1./2.,1./3.],[4,3,2]),
]) ])
def test_scale2coprime(self,input,output): def test_scale2coprime(self,input,output):
print(util.scale_to_coprime(np.array(input)))
assert np.allclose(util.scale_to_coprime(np.array(input)), assert np.allclose(util.scale_to_coprime(np.array(input)),
np.array(output).astype(int)) np.array(output).astype(int))
def test_lackofprecision(self): def test_lackofprecision(self):
with pytest.raises(ValueError): with pytest.raises(ValueError):
util.scale_to_coprime(np.array([1/3333,1,1])) util.scale_to_coprime(np.array([1/333.333,1,1]))