def test_problematic():
    a = Variable(val=2, name='a')
    with pytest.raises(ZeroDivisionError):
        t1 = Diff().auto_diff(function=div_zero, eval_point=[a])
    with pytest.raises(NotImplementedError):
        t1 = Diff().hessian(functions=[add_function], eval_points=[a])
    with pytest.raises(NotImplementedError):
        print(bm.arcsinh(1))
    with pytest.raises(NotImplementedError):
        print(bm.arccosh(1))
    with pytest.raises(NotImplementedError):
        print(bm.arctanh(1))
Example #2
0
def test_hyperbolic():
    a = Variable(val=1, name='x')
    b = Variable(val=2, name='y')
    t1 = Diff().auto_diff(function=sinh_cosh, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.val, np.sinh(1) + np.cosh(2))
    np.testing.assert_approx_equal(t1.der['x'], np.cosh(1))
    np.testing.assert_approx_equal(t1.der['y'], np.sinh(2))

    t2 = Diff().auto_diff(function=tanh, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.tanh(2))
    np.testing.assert_approx_equal(t2.der['x'], 2 * (1 / (np.cosh(2)))**2)
    np.testing.assert_approx_equal(t2.der['y'], (1 / (np.cosh(2)))**2)
Example #3
0
def test_bm_vector():
    a = Variable(val=2, name='x')
    b = Variable(val=3, name='y')

    t1 = Diff().auto_diff(function=log_exp_function, eval_point=[a, b])
    assert (t1.val == np.log(2) + np.exp(3))
    assert (t1.der['x'] == 0.5)
    assert (t1.der['y'] == np.exp(3))

    t2 = Diff().auto_diff(function=sqrt_function, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.sqrt(2) + np.sqrt(3))
    np.testing.assert_approx_equal(t2.der['x'], 1 / (2 * np.sqrt(2)))
    np.testing.assert_approx_equal(t2.der['y'], 1 / (2 * np.sqrt(3)))
def test_other_functions():
    a = Variable(val=1, name='a')

    t1 = Diff().auto_diff(function=log_exp_function, eval_point=[a])
    assert (t1.val == np.exp(1))
    assert (t1.der['a'] == np.exp(1) + 1)

    t2 = Diff().auto_diff(function=logk_none, eval_point=[a])
    assert (t2.val == 0)
    assert (t2.der['a'] == 1)

    a = Variable(10, name='a')
    t3 = Diff().auto_diff(function=logk_e, eval_point=[a])
    assert (t3.val == np.log(10))
    assert (t3.der['a'] == 0.1)
Example #5
0
def test_hyp():
    a = Variable(val=1, name='a')
    b = Variable(val=0.5, name='b')

    t1 = Diff().auto_diff(function=sinh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.sec_der['a'], -0.62895, significant=5)
    np.testing.assert_approx_equal(t1.sec_der['b'], -2.5158, significant=5)

    t2 = Diff().auto_diff(function=cosh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], -0.401501, significant=6)
    np.testing.assert_approx_equal(t2.sec_der['b'], -1.606, significant=4)

    t3 = Diff().auto_diff(function=tanh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], -0.302366, significant=6)
    np.testing.assert_approx_equal(t3.sec_der['b'], -1.20946, significant=6)
Example #6
0
def test_invtrig():
    a = Variable(val=1, name='a')
    b = Variable(val=0.5, name='b')

    t1 = Diff().auto_diff(function=arcsin_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.sec_der['a'], -0.096225, significant=5)
    np.testing.assert_approx_equal(t1.sec_der['b'], -2.6943, significant=5)

    t2 = Diff().auto_diff(function=arccos_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], 0.096225, significant=5)
    np.testing.assert_approx_equal(t2.sec_der['b'], 2.6943, significant=5)

    t3 = Diff().auto_diff(function=arctan_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], 0.08, significant=2)
    np.testing.assert_approx_equal(t3.sec_der['b'], -1.28, significant=3)
def test_trig():
    a = Variable(val=0, name='a')
    # Sine and cosine
    t1 = Diff().auto_diff(function=sin_cos, eval_point=[a])
    assert (t1.val == 1)
    assert (t1.der['a'] == 1)

    # Tan
    t2 = Diff().auto_diff(function=tan_function, eval_point=[a])
    assert (t2.val == 0)
    assert (t2.der['a'] == 2)

    # Arcsin, arccos, arctan
    t3 = Diff().auto_diff(function=inverse_trig, eval_point=[a])
    assert (np.abs(t3.val + np.pi / 2) < epsilon)
    assert (t3.der['a'] > 0)
Example #8
0
def test_operators():
    a = Variable(val=3, name='a')
    b = Variable(val=2, name='b')

    t1 = Diff().auto_diff(function=simple_op_complete, eval_point=[a, b])
    assert (t1.sec_der['a'] == 4)
    assert (t1.sec_der['b'] == 2)

    t2 = Diff().auto_diff(function=log_exp, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], -2 / 9)
    assert (t2.sec_der['b'] == 3 * np.exp(2))

    t3 = Diff().auto_diff(function=sqrt_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], 18 + 6 / (4 * np.sqrt(6)))
    np.testing.assert_approx_equal(t3.sec_der['b'], -27 / (4 * (6**1.5)))

    t4 = Diff().auto_diff(function=sin_cos, eval_point=[a, b])
    np.testing.assert_approx_equal(t4.sec_der['a'],
                                   -1404.91310072818349654572310)
    np.testing.assert_approx_equal(t4.sec_der['b'], -3 * np.cos(2))

    t5 = Diff().auto_diff(function=tan_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t5.sec_der['a'], -5.0503989647387223596)
    np.testing.assert_approx_equal(t5.sec_der['b'], -4.85529005011658178)

    t6 = Diff().auto_diff(function=rpow_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t6.sec_der['a'], 2)
    np.testing.assert_approx_equal(t6.sec_der['b'], 9 * np.log(3)**2)

    t7 = Diff().auto_diff(function=logk_test, eval_point=[a])
    np.testing.assert_approx_equal(t7.sec_der['a'], -2 / (9 * np.log(10)))
def test_non_alphabetical_22():
    x = Variable(val=3, name='z')
    y = Variable(val=5, name='a')
    t1 = Diff().jacobian([f1,f2], [x,y])
    assert(t1.shape == (2,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
def test_jacobian_22():
    x = Variable(val=3, name='x')
    y = Variable(val=5, name='y')
    t1 = Diff().jacobian([f1,f2], [x,y])
    assert(t1.shape == (2,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
def test_jacobian_32():
    x = Variable(val=3, name='c')
    y = Variable(val=5, name='a')
    z = Variable(val=5, name='t')
    t1 = Diff().jacobian([f1,f2,f3], [x,y])
    assert(t1.shape == (3,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
    assert(t1[2][0] == 5)
    assert(t1[2][1] == 4)
def test_jacobian_23():
    x = Variable(val=3, name='x')
    y = Variable(val=5, name='y')
    z = Variable(val=2, name='z')
    t1 = Diff().jacobian([f1,f2], [x,y,z])
    assert(t1.shape == (2,3))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[0][2] == 1)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
    assert(t1[0][2] == 1)
Example #13
0
def test_trig_vector():
    a = Variable(val=1, name='x')
    b = Variable(val=2, name='y')

    t1 = Diff().auto_diff(function=sin_cos, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.val, np.sin(1) + np.cos(2))
    np.testing.assert_approx_equal(t1.der['x'], np.cos(1))
    np.testing.assert_approx_equal(t1.der['y'], -np.sin(2))

    t2 = Diff().auto_diff(function=tan_function, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.tan(4))
    np.testing.assert_approx_equal(t2.der['x'], 4 * (1 / (np.cos(4)))**2)
    np.testing.assert_approx_equal(t2.der['y'], 2 * (1 / (np.cos(4)))**2)

    a = Variable(val=0, name='x')
    b = Variable(val=0, name='y')
    c = Variable(val=0, name='z')

    t3 = Diff().auto_diff(function=inverse_trig, eval_point=[a, b, c])
    np.testing.assert_approx_equal(t3.val, -np.pi / 2)
    np.testing.assert_approx_equal(t3.der['x'], 1)
    np.testing.assert_approx_equal(t3.der['y'], 1)
    np.testing.assert_approx_equal(t3.der['z'], 1)
def test_more_operators():
    a = Variable(val=4, name='a')
    # Power
    t1 = Diff().auto_diff(function=power_scalar, eval_point=[a])
    assert (t1.val == 26)
    assert (t1.der['a'] == 8)

    # Square root
    t2 = Diff().auto_diff(function=sqrt_scalar, eval_point=[a])
    assert (t2.val == 13)
    assert (t2.der['a'] == 1 / 3)

    # Power function
    t3 = Diff().auto_diff(function=power_function, eval_point=[a])
    assert (t3.val == 16)
    assert (t3.der['a'] == bm.log(2) * 16)
    assert (t3.der['a'] == np.log(2) * 16)

    # Power function general
    t4 = Diff().auto_diff(function=power_function_general, eval_point=[a])
    assert (t4.val == 256)
    assert (t4.der['a'] == 256 * (bm.log(4) + 1))
    assert (t4.der['a'] == 256 * (np.log(4) + 1))
def test_simple_operators():
    a = Variable(val=3, name='a')
    b = Variable(val=3, name='b')
    c = Variable(val=5, name='c')
    # Addition and subtraction
    t11 = Diff().auto_diff(function=add_function, eval_point=[a])
    t12 = Diff().auto_diff(function=sub_function, eval_point=[a])
    assert (t11.val == 5 and t11.der['a'] == 2)
    assert (t12.val == 0 and t12.der['a'] == -1)

    # Multiplication
    t2 = Diff().auto_diff(function=mul_function, eval_point=[a])
    assert (t2.val == 29)
    assert (t2.der['a'] == 6)

    # Division
    t3 = Diff().auto_diff(function=div_scalar, eval_point=[a])
    assert (t3.val == 1)
    assert (t3.der['a'] == 1 / 3)

    t4 = Diff().auto_diff(function=div_function, eval_point=[a])
    assert (t4.val == 2)
    assert (t4.der['a'] == -1 / 3)

    t5 = Diff().auto_diff(function=simple_op_complete, eval_point=[a])
    assert (t5.val == 13)
    assert (t5.der['a'] == 9)

    # Unary
    t6 = Diff().auto_diff(function=unary, eval_point=[a])
    assert (t6.val == 6)
    assert (t6.der['a'] == 2)

    # Eq and ne
    assert (a == b)
    assert (a != c)
    assert (a != 3)
    assert (a == a)
    assert ((a == 3) == False)
    assert ((a != b) == False)
Example #16
0
def test_simple_operators():
    x = Variable(val=2, name='x')
    y = Variable(val=1, name='y')
    t1 = Diff().auto_diff(function=add_function, eval_point=[x, y])
    assert (t1.val == 9 and t1.der['x'] == 1 and t1.der['y'] == 1)

    t2 = Diff().auto_diff(function=sub_function, eval_point=[x, y])
    assert (t2.val == 0 and t2.der['x'] == 1 and t2.der['y'] == -1)

    t3 = Diff().auto_diff(function=mul_function1, eval_point=[x, y])
    assert (t3.val == 12 and t3.der['x'] == 6 and t3.der['y'] == 12)

    t4 = Diff().auto_diff(function=mul_function2, eval_point=[x, y])
    assert (t4.val == 7 and t4.der['x'] == 3 and t4.der['y'] == -2)

    t5 = Diff().auto_diff(function=pow_function1, eval_point=[x, y])
    assert (t5.val == 5 and t5.der['x'] == 4 and t5.der['y'] == 2)

    t6 = Diff().auto_diff(function=rpow_function, eval_point=[x, y])
    assert (t6.val == 7 and t6.der['x'] == 4 * np.log(2)
            and t6.der['y'] == 3 * np.log(3))

    t7 = Diff().auto_diff(function=div_scalar, eval_point=[x, y])
    assert (t7.val == 1.25 and t7.der['x'] == 0.5 and t7.der['y'] == 0.25)

    t8 = Diff().auto_diff(function=div_function, eval_point=[x, y])
    assert (t8.val == 6 and t8.der['x'] == 0.5 and t8.der['y'] == -6)

    t9 = Diff().auto_diff(function=rsub_function, eval_point=[x, y])
    assert (t9.val == -2 and t9.der['x'] == -1 and t9.der['y'] == -1)

    t10 = Diff().auto_diff(function=neg_pow_function, eval_point=[x, y])
    assert (t10.val == 1.5 and t10.der['x'] == -0.25 and t10.der['y'] == -1)

    t11 = Diff().auto_diff(function=pow_function2, eval_point=[x, y])
    assert (t11.val == 2 and t11.der['x'] == 1
            and t11.der['y'] == 2 * np.log(2))

    t12 = Diff().auto_diff(function=simple_op_complete, eval_point=[x, y])
    assert (t12.val == 5 and t12.der['x'] == 7 and t12.der['y'] == -2)

    t13 = Diff().auto_diff(function=unary, eval_point=[x, y])
    assert (t13.val == 5 and t13.der['x'] == 3 and t13.der['y'] == -1)