def test_equality_gradient(): x0 = ReverseADNode(0.8) x1 = ReverseADNode(0.8) curr_func = x0**2 + x1**2 curr_func.grad_value = 1 x0.grad() x1.grad() assert x0 == x1
def test_pow_frac(): x0 = ReverseADNode(4) curr_func = x0**0.5 curr_func.grad_value = 1.0 assert curr_func.value == 2.0 and x0.grad() == 0.25
def test_rsub_ReverseADNode(): x0 = ReverseADNode(5) curr_func = x0 - (x0 + 6.3) curr_func.grad_value = 1.0 assert abs(curr_func.value - -6.3) < epsilon and x0.grad() == 0
def test_sub(): x0 = ReverseADNode(0) curr_func = x0 - 5 curr_func.grad_value = 1.0 assert curr_func.value == -5.0 and x0.grad() == 1
def test_radd_float(): x0 = ReverseADNode(0.0) curr_func = x0 + 5.0 curr_func.grad_value = 1.0 assert curr_func.value == 5.0 and x0.grad() == 1
def test_cosh_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = cosh(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 1.2118866516740001) < epsilon and abs( x0.grad() - 1.0953507642095226) < epsilon
def test_equality_values(): x0 = ReverseADNode(0.8) x1 = ReverseADNode(0.8) x0.grad_value = 1 x1.grad_value = 1 assert x0 == x1
def test_exp_ReverseADNode(): x0 = ReverseADNode(2) curr_func = exp(x0**2) curr_func.grad_value = 1.0 assert curr_func.value == 54.598150033144236 and x0.grad( ) == 218.39260013257694
def test_init_None(): with pytest.raises(TypeError): x0 = ReverseADNode(None)
def test_log_neg(): x0 = ReverseADNode(-4) with pytest.raises(ValueError): curr_func = log(x0)
def test_log_ReverseADNode(): x0 = ReverseADNode(10) curr_func = log(x0**2) real_log_value = 4.605170185988092 curr_func.grad_value = 1.0 assert curr_func.value == 4.605170185988092 and x0.grad() == 0.2
def test_init_float(): x0 = ReverseADNode(0.0) assert x0.value == 0.0 and x0.grad() == 0.0
def test_pow_ReverseADNode(): x0 = ReverseADNode(3) curr_func = (x0**2)**x0 curr_func.grad_value = 1.0 assert curr_func.value == 729 and x0.grad() == 3059.776716878104
def test_rpow(): x0 = ReverseADNode(2) curr_func = 6**x0 curr_func.grad_value = 1.0 assert curr_func.value == 36 and x0.grad() == 64.50334089220998
def test_radd_int(): x0 = ReverseADNode(0) curr_func = 5 + x0 curr_func.grad_value = 1.0 assert curr_func.value == 5 and x0.grad() == 1
def test_sin_ReverseADNode(): x0 = ReverseADNode(2) curr_func = sin(x0**2) curr_func.grad_value = 1.0 assert curr_func.value == -0.7568024953079282 and x0.grad( ) == -2.6145744834544478
def test_sinh_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = sinh(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 0.6845942276309516) < epsilon and abs( x0.grad() - 1.9390186426784002) < epsilon
def test_cos_ReverseADNode(): x0 = ReverseADNode(2) curr_func = cos(x0**2) curr_func.grad_value = 1.0 assert curr_func.value == -0.6536436208636119 and x0.grad( ) == 3.027209981231713
def test_tanh_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = tanh(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 0.5648995528462251) < epsilon and abs( x0.grad() - 1.089421592310616) < epsilon
def test_tan_ReverseADNode(): x0 = ReverseADNode(2) curr_func = tan(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 1.1578212823495775) < epsilon and abs( x0.grad() - 9.36220048744648) < epsilon
def test_inequality_None(): x0 = ReverseADNode(0.8) x1 = ReverseADNode(0.8) assert not x0 == x1
def test_sqrt_ReverseADNode(): x0 = ReverseADNode(2) curr_func = sqrt(x0 + 6) curr_func.grad_value = 1.0 assert abs(curr_func.value - 2.8284271247461903) < epsilon and abs( x0.grad() - 0.17677669529663687) < epsilon
def test_inequality_gradient(): x0 = ReverseADNode(0.8) x1 = ReverseADNode(0.8) curr_func = x0**2 + x1**3 curr_func.grad_value = 1 assert not x0 == x1
def test_arcsin_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = arcsin(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 0.6944982656265561) < epsilon assert abs(x0.grad() - 2.082316825181415) < epsilon
def test_add_ReverseADNode(): x0 = ReverseADNode(5) curr_func = (x0 + 6.3) + x0 curr_func.grad_value = 1.0 assert curr_func.value == 16.3 and x0.grad() == 2
def test_arccos_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = arccos(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 0.8762980611683405) < epsilon assert abs(x0.grad() - -2.082316825181415) < epsilon
def test_rsub(): x0 = ReverseADNode(0) curr_func = 5 - x0 curr_func.grad_value = 1.0 assert curr_func.value == 5.0 and x0.grad() == -1
def test_arctan_ReverseADNode(): x0 = ReverseADNode(0.8) curr_func = arctan(x0**2) curr_func.grad_value = 1.0 assert abs(curr_func.value - 0.5693131911006619) < epsilon assert abs(x0.grad() - 1.1350737797956867) < epsilon
def test_mul_0(): x0 = ReverseADNode(0) curr_func = x0 * 5 curr_func.grad_value = 1.0 assert curr_func.value == 0.0 and x0.grad() == 5
def test_pow(): x0 = ReverseADNode(4) curr_func = x0**2 curr_func.grad_value = 1.0 assert curr_func.value == 16.0 and x0.grad() == 8.0