2025-10-25 00:21:43 +00:00
from copapy import variable , Target
import pytest
2025-10-26 21:26:12 +00:00
import copapy as cp
2025-11-05 20:47:18 +00:00
import math as ma
import warnings
2025-10-25 00:21:43 +00:00
def test_corse ( ) :
2025-10-26 21:26:12 +00:00
a_i = 9
a_f = 2.5
c_i = variable ( a_i )
c_f = variable ( a_f )
2025-10-25 00:21:43 +00:00
# c_b = variable(True)
2025-10-26 21:26:12 +00:00
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)
2025-10-25 00:21:43 +00:00
tg = Target ( )
print ( ' * compile and copy ... ' )
tg . compile ( ret_test )
print ( ' * run and copy ... ' )
tg . run ( )
print ( ' * finished ' )
2025-10-26 21:26:12 +00:00
for test , ref in zip ( ret_test , ret_refe ) :
assert isinstance ( test , cp . variable )
2025-10-25 00:21:43 +00:00
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 ( ) :
2025-10-26 21:26:12 +00:00
a_i = 9
a_f = 2.5
c_i = variable ( a_i )
c_f = variable ( a_f )
2025-10-25 00:21:43 +00:00
# c_b = variable(True)
2025-11-01 12:43:22 +00:00
ret_test = ( c_f * * 2 ,
c_i * * - 1 ,
cp . sqrt ( c_i ) ,
cp . sqrt ( c_f ) ,
cp . sin ( c_f ) ,
cp . cos ( c_f ) ,
cp . tan ( c_f ) ) # , c_i & 3)
2025-11-03 21:49:31 +00:00
2025-11-05 20:47:18 +00:00
re2_test = ( a_f * * 2 ,
2025-11-01 12:43:22 +00:00
a_i * * - 1 ,
cp . sqrt ( a_i ) ,
cp . sqrt ( a_f ) ,
cp . sin ( a_f ) ,
cp . cos ( a_f ) ,
cp . tan ( a_f ) ) # , a_i & 3)
2025-10-25 00:21:43 +00:00
2025-11-05 20:47:18 +00:00
ret_refe = ( a_f * * 2 ,
a_i * * - 1 ,
ma . sqrt ( a_i ) ,
ma . sqrt ( a_f ) ,
ma . sin ( a_f ) ,
ma . cos ( a_f ) ,
ma . tan ( a_f ) ) # , a_i & 3)
2025-10-25 00:21:43 +00:00
tg = Target ( )
print ( ' * compile and copy ... ' )
tg . compile ( ret_test )
print ( ' * run and copy ... ' )
tg . run ( )
print ( ' * finished ' )
2025-11-05 20:47:18 +00:00
for test , val2 , ref , name in zip ( ret_test , re2_test , ret_refe , ( ' ^2 ' , ' **-1 ' , ' sqrt_int ' , ' sqrt_float ' , ' sin ' , ' cos ' , ' tan ' ) ) :
2025-10-26 21:26:12 +00:00
assert isinstance ( test , cp . variable )
2025-10-25 00:21:43 +00:00
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}"
2025-11-09 23:08:26 +00:00
assert val == pytest . approx ( ref , 1e-4 ) , 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]
2025-11-01 12:43:22 +00:00
def test_trig_precision ( ) :
2025-11-01 20:51:29 +00:00
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 ,
- 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 ]
2025-11-01 12:43:22 +00:00
ret_test = [ r for v in test_vals for r in ( cp . sin ( variable ( v ) ) , cp . cos ( variable ( v ) ) , cp . tan ( variable ( v ) ) ) ]
2025-11-05 20:47:18 +00:00
ret_refe = [ r for v in test_vals for r in ( ma . sin ( v ) , ma . cos ( v ) , ma . tan ( v ) ) ]
2025-11-01 12:43:22 +00:00
tg = Target ( )
tg . compile ( ret_test )
tg . run ( )
for i , ( test , ref ) in enumerate ( zip ( ret_test , ret_refe ) ) :
func_name = [ ' sin ' , ' cos ' , ' tan ' ] [ i % 3 ]
assert isinstance ( test , cp . variable )
val = tg . read_value ( test )
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]
2025-10-25 00:21:43 +00:00
2025-11-05 20:47:18 +00:00
def test_arcus_trig_precision ( ) :
2025-11-09 23:08:26 +00:00
test_vals = [ 0.0 , 0.01 , 0.1 , 0.5 , 0.7 , 0.9 , 0.95 ,
- 0.01 , - 0.1 , - 0.5 , - 0.7 , - 0.9 , 0.95 ]
2025-11-05 20:47:18 +00:00
ret_test = [ r for v in test_vals for r in ( cp . asin ( variable ( v ) ) ,
cp . acos ( variable ( v ) ) ,
cp . atan ( variable ( v ) ) ,
cp . atan2 ( variable ( v ) , variable ( 0.7 ) ) ,
2025-11-09 23:08:26 +00:00
cp . atan2 ( variable ( v ) , variable ( - 0.2 ) ) ) ]
2025-11-05 20:47:18 +00:00
ret_refe = [ r for v in test_vals for r in ( ma . asin ( v ) ,
ma . acos ( v ) ,
ma . atan ( v ) ,
ma . atan2 ( v , 0.7 ) ,
ma . atan2 ( v , - 0.2 ) ) ]
tg = Target ( )
tg . compile ( ret_test )
tg . run ( )
for i , ( test , ref ) in enumerate ( zip ( ret_test , ret_refe ) ) :
func_name = [ ' asin ' , ' acos ' , ' atan ' , ' atan2[1] ' , ' atan2[2] ' ] [ i % 5 ]
assert isinstance ( test , cp . variable )
val = tg . read_value ( test )
2025-11-09 23:08:26 +00:00
print ( f " + Result of { func_name } : { val } ; reference: { ref } " )
2025-11-05 20:47:18 +00:00
#assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 5]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType]
2025-11-09 23:08:26 +00:00
if not val == pytest . approx ( ref , abs = 1e-5 ) : # pyright: ignore[reportUnknownMemberType]
2025-11-05 20:47:18 +00:00
warnings . warn ( f " Result of { func_name } for input { test_vals [ i / / 5 ] } does not match: { val } and reference: { ref } " , UserWarning )
2025-11-01 20:51:29 +00:00
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 ]
ret_test = [ r for v in test_vals for r in ( cp . sqrt ( variable ( v ) ) , ) ]
ret_refe = [ r for v in test_vals for r in ( cp . sqrt ( v ) , ) ]
tg = Target ( )
tg . compile ( ret_test )
tg . run ( )
for i , ( test , ref ) in enumerate ( zip ( ret_test , ret_refe ) ) :
func_name = ' sqrt '
assert isinstance ( test , cp . variable )
val = tg . read_value ( test )
print ( f " + Result of { func_name } : { val } ; reference: { ref } " )
2025-11-05 20:47:18 +00:00
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]
def test_log_exp_precision ( ) :
test_vals = [ 0.1 , 0.5 , 0.9 , 0.999 , 1.0 , 8.8 , 12.0
- 0.1 , - 0.5 , - 0.9 , - 0.999 , - 1.0 , 8.8 , 12.0 ]
2025-11-09 23:08:26 +00:00
ret_test = [ r for v in test_vals for r in ( cp . log ( variable ( abs ( v ) ) ) ,
cp . exp ( variable ( v ) ) ) ]
ret_refe = [ r for v in test_vals for r in ( ma . log ( abs ( v ) ) ,
ma . exp ( v ) ) ]
2025-11-05 20:47:18 +00:00
tg = Target ( )
tg . compile ( ret_test )
tg . run ( )
for i , ( test , ref ) in enumerate ( zip ( ret_test , ret_refe ) ) :
func_name = [ ' log ' , ' exp ' ] [ i % 2 ]
assert isinstance ( test , cp . variable )
val = tg . read_value ( test )
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]
if not val == pytest . approx ( ref , rel = 4e-2 ) : # pyright: ignore[reportUnknownMemberType]
warnings . warn ( f " Result of { func_name } for input { test_vals [ i / / 2 ] } does not match: { val } and reference: { ref } " , UserWarning )
2025-11-01 20:51:29 +00:00
2025-10-25 00:21:43 +00:00
if __name__ == " __main__ " :
test_corse ( )
test_fine ( )
2025-11-01 20:51:29 +00:00
test_sqrt_precision ( )
test_trig_precision ( )