tests updated to meet expected math precision

This commit is contained in:
Nicolas 2025-11-12 21:56:38 +01:00
parent 4b378ecab5
commit e68c94a496
2 changed files with 25 additions and 55 deletions

View File

@ -18,8 +18,6 @@ def run_command(command: list[str]) -> str:
def test_compile_sqrt(): def test_compile_sqrt():
test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0] test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0]
test_vals = [100000.0]
ret = [r for v in test_vals for r in (cp.sqrt(variable(v)),)] ret = [r for v in test_vals for r in (cp.sqrt(variable(v)),)]
@ -54,8 +52,6 @@ def test_compile_sqrt():
def test_compile_log(): def test_compile_log():
test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0] test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0]
test_vals = [100000.0]
ret = [r for v in test_vals for r in (cp.log(variable(v)),)] ret = [r for v in test_vals for r in (cp.log(variable(v)),)]
@ -90,8 +86,6 @@ def test_compile_log():
def test_compile_sin(): def test_compile_sin():
test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0] test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0]
test_vals = [100000.0]
ret = [r for v in test_vals for r in (cp.sin(variable(v)),)] ret = [r for v in test_vals for r in (cp.sin(variable(v)),)]
@ -109,7 +103,7 @@ def test_compile_sin():
il.write_com(_binwrite.Command.END_COM) il.write_com(_binwrite.Command.END_COM)
print('* Data to runner:') print('* Data to runner:')
il.print() #il.print()
il.to_file('bin/test.copapy') il.to_file('bin/test.copapy')
@ -123,6 +117,6 @@ def test_compile_sin():
if __name__ == "__main__": if __name__ == "__main__":
#test_compile_sqrt() test_compile_sqrt()
#test_compile_log() test_compile_log()
test_compile_sin() test_compile_sin()

View File

@ -4,33 +4,6 @@ import copapy as cp
import math as ma import math as ma
import warnings import warnings
def test_corse():
a_i = 9
a_f = 2.5
c_i = variable(a_i)
c_f = variable(a_f)
# c_b = variable(True)
ret_test = (c_f ** c_f, c_i ** c_i) # , c_i & 3)
ret_refe = (a_f ** a_f, a_i ** a_i) # , a_i & 3)
tg = Target()
print('* compile and copy ...')
tg.compile(ret_test)
print('* run and copy ...')
tg.run()
print('* finished')
for test, ref in zip(ret_test, ret_refe):
assert isinstance(test, cp.variable)
val = tg.read_value(test)
print('+', val, ref, type(val), test.dtype)
#for t in (int, float, bool):
# assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}"
assert val == pytest.approx(ref, 2), f"Result does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
def test_fine(): def test_fine():
a_i = 9 a_i = 9
a_f = 2.5 a_f = 2.5
@ -40,6 +13,8 @@ def test_fine():
ret_test = (c_f ** 2, ret_test = (c_f ** 2,
c_i ** -1, c_i ** -1,
c_i ** 2.111,
c_f ** 2.111,
cp.sqrt(c_i), cp.sqrt(c_i),
cp.sqrt(c_f), cp.sqrt(c_f),
cp.sin(c_f), cp.sin(c_f),
@ -48,6 +23,8 @@ def test_fine():
re2_test = (a_f ** 2, re2_test = (a_f ** 2,
a_i ** -1, a_i ** -1,
a_i ** 2.111,
a_f ** 2.111,
cp.sqrt(a_i), cp.sqrt(a_i),
cp.sqrt(a_f), cp.sqrt(a_f),
cp.sin(a_f), cp.sin(a_f),
@ -56,6 +33,8 @@ def test_fine():
ret_refe = (a_f ** 2, ret_refe = (a_f ** 2,
a_i ** -1, a_i ** -1,
a_i ** 2.111,
a_f ** 2.111,
ma.sqrt(a_i), ma.sqrt(a_i),
ma.sqrt(a_f), ma.sqrt(a_f),
ma.sin(a_f), ma.sin(a_f),
@ -75,8 +54,8 @@ def test_fine():
print('+', val, ref, type(val), test.dtype) print('+', val, ref, type(val), test.dtype)
#for t in (int, float, bool): #for t in (int, float, bool):
# assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}" # assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}"
assert val == pytest.approx(ref, 1e-4), f"Result for {name} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] assert val == pytest.approx(ref, abs=1e-3), f"Result for {name} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
assert val2 == pytest.approx(ref, 1e-4), f"Local result for {name} does not match: {val2} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] assert val2 == pytest.approx(ref, abs=1e-3), f"Local result for {name} does not match: {val2} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
def test_trig_precision(): def test_trig_precision():
@ -96,9 +75,9 @@ def test_trig_precision():
assert isinstance(test, cp.variable) assert isinstance(test, cp.variable)
val = tg.read_value(test) val = tg.read_value(test)
print(f"+ Result of {func_name}: {val}; reference: {ref}") print(f"+ Result of {func_name}: {val}; reference: {ref}")
#assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
if not val == pytest.approx(ref, 1e-5): # pyright: ignore[reportUnknownMemberType] #if not val == pytest.approx(ref, abs=1e-5): # pyright: ignore[reportUnknownMemberType]
warnings.warn(f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}", UserWarning) # warnings.warn(f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}", UserWarning)
def test_arcus_trig_precision(): def test_arcus_trig_precision():
@ -109,13 +88,11 @@ def test_arcus_trig_precision():
ret_test = [r for v in test_vals for r in (cp.asin(variable(v)), ret_test = [r for v in test_vals for r in (cp.asin(variable(v)),
cp.acos(variable(v)), cp.acos(variable(v)),
cp.atan(variable(v)), cp.atan(variable(v)),
cp.atan2(variable(v), variable(0.7)), cp.atan2(variable(v), variable(3)),)]
cp.atan2(variable(v), variable(-0.2)))]
ret_refe = [r for v in test_vals for r in (ma.asin(v), ret_refe = [r for v in test_vals for r in (ma.asin(v),
ma.acos(v), ma.acos(v),
ma.atan(v), ma.atan(v),
ma.atan2(v, 0.7), ma.atan2(v, 3),)]
ma.atan2(v, -0.2))]
tg = Target() tg = Target()
tg.compile(ret_test) tg.compile(ret_test)
@ -132,7 +109,6 @@ def test_arcus_trig_precision():
def test_sqrt_precision(): def test_sqrt_precision():
test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0] test_vals = [0.0, 0.0001, 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.28318530718, 100.0, 1000.0, 100000.0]
ret_test = [r for v in test_vals for r in (cp.sqrt(variable(v)),)] ret_test = [r for v in test_vals for r in (cp.sqrt(variable(v)),)]
@ -147,15 +123,15 @@ def test_sqrt_precision():
assert isinstance(test, cp.variable) assert isinstance(test, cp.variable)
val = tg.read_value(test) val = tg.read_value(test)
print(f"+ Result of {func_name}: {val}; reference: {ref}") print(f"+ Result of {func_name}: {val}; reference: {ref}")
#assert val == pytest.approx(ref, 1e-5), f"Result of {func_name} for input {test_vals[i]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] assert val == pytest.approx(ref, rel=1e-5), f"Result of {func_name} for input {test_vals[i]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
if not val == pytest.approx(ref, 1e-5): # pyright: ignore[reportUnknownMemberType] #if not val == pytest.approx(ref, abs=1e-5): # pyright: ignore[reportUnknownMemberType]
warnings.warn(f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}", UserWarning) # warnings.warn(f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}", UserWarning)
def test_log_exp_precision(): def test_log_exp_precision():
test_vals = [0.1, 0.5, 0.9, 0.999, 1.0, 8.8, 12.0 test_vals = [0.1, 0.5, 0.9, 0.999, 1.0, 2.5,
-0.1, -0.5, -0.9, -0.999, -1.0, 8.8, 12.0] -0.1, -0.5, -0.9, -0.999, -1.0, 2.5]
ret_test = [r for v in test_vals for r in (cp.log(variable(abs(v))), ret_test = [r for v in test_vals for r in (cp.log(variable(abs(v))),
cp.exp(variable(v)))] cp.exp(variable(v)))]
@ -171,13 +147,13 @@ def test_log_exp_precision():
assert isinstance(test, cp.variable) assert isinstance(test, cp.variable)
val = tg.read_value(test) val = tg.read_value(test)
print(f"+ Result of {func_name}: {val}; reference: {ref}") print(f"+ Result of {func_name}: {val}; reference: {ref}")
#assert val == pytest.approx(ref, rel=4e-2), f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
if not val == pytest.approx(ref, rel=4e-2): # pyright: ignore[reportUnknownMemberType] #if not val == pytest.approx(ref, abs=1e-5): # pyright: ignore[reportUnknownMemberType]
warnings.warn(f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}", UserWarning) # warnings.warn(f"Result of {func_name} for input {test_vals[i // 2]} does not match: {val} and reference: {ref}", UserWarning)
if __name__ == "__main__": if __name__ == "__main__":
test_corse()
test_fine() test_fine()
test_sqrt_precision() test_sqrt_precision()
test_trig_precision() test_trig_precision()
test_log_exp_precision()