condensed scale_to_coprime; added test of scale_to_coprime
This commit is contained in:
parent
9999560247
commit
e64d353865
|
@ -166,10 +166,13 @@ def scale_to_coprime(v):
|
||||||
"""Least common multiple."""
|
"""Least common multiple."""
|
||||||
return a * b // np.gcd(a, b)
|
return a * b // np.gcd(a, b)
|
||||||
|
|
||||||
denominators = [int(get_square_denominator(i)) for i in v]
|
m = (np.array(v) * reduce(lcm, map(lambda x: int(get_square_denominator(x)),v)) ** 0.5).astype(np.int)
|
||||||
s = reduce(lcm, denominators) ** 0.5
|
m = m//reduce(np.gcd,m)
|
||||||
m = (np.array(v)*s).astype(np.int)
|
|
||||||
return m//reduce(np.gcd,m)
|
if not np.allclose(v/m,v[0]/m[0]):
|
||||||
|
raise ValueError(f'Invalid result {m} for input {v}. Insufficient precision?')
|
||||||
|
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
####################################################################################################
|
####################################################################################################
|
||||||
|
|
|
@ -1,11 +1,32 @@
|
||||||
|
import pytest
|
||||||
|
import numpy as np
|
||||||
from damask import util
|
from damask import util
|
||||||
|
|
||||||
|
|
||||||
class TestUtil:
|
class TestUtil:
|
||||||
|
|
||||||
def test_execute_direct(self):
|
def test_execute_direct(self):
|
||||||
out,err = util.execute('echo test')
|
out,err = util.execute('echo test')
|
||||||
assert out=='test\n' and err==''
|
assert out=='test\n' and err==''
|
||||||
|
|
||||||
def test_execute_env(self):
|
def test_execute_env(self):
|
||||||
out,err = util.execute('sh -c "echo $test_for_execute"',env={'test_for_execute':'test'})
|
out,err = util.execute('sh -c "echo $test_for_execute"',env={'test_for_execute':'test'})
|
||||||
assert out=='test\n' and err==''
|
assert out=='test\n' and err==''
|
||||||
|
|
||||||
|
def test_croak(self):
|
||||||
|
util.croak('Burp!')
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('input,output',
|
||||||
|
[
|
||||||
|
([0.5,0.5],[1,1]),
|
||||||
|
([1./2.,1./3.],[3,2]),
|
||||||
|
([2./3.,1./2.,1./3.],[4,3,2]),
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_scale2coprime(self,input,output):
|
||||||
|
assert np.allclose(util.scale_to_coprime(np.array(input)),
|
||||||
|
np.array(output).astype(int))
|
||||||
|
|
||||||
|
def test_lackofprecision(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
util.scale_to_coprime(np.array([0.66,0.5,0.33]))
|
||||||
|
|
Loading…
Reference in New Issue