def test_add_different_AD():
    x = AD(val=[3], index=0, magnitude=2)
    y = AD(val=[4], index=1, magnitude=2)
    z = x + y
    assert z.val == pytest.approx([7])
    for d in z.der:
        assert d == pytest.approx([1])
def test_neg_different_AD_many_val():
    x = AD(val=[10, -1, 3.2, 4], index=0, magnitude=2)
    y = AD(val=[-2, 0, 1, 100], index=1, magnitude=2)
    z = -(x + y)
    assert z.val == pytest.approx([-8, 1, -4.2, -104])
    for d in z.der:
        assert d == pytest.approx([-1, -1, -1, -1])
def test_div_different_AD():
    x = AD(val=[2], index=0, magnitude=2)
    y = AD(val=[4], index=1, magnitude=2)
    z = x / y
    assert z.val == pytest.approx([0.5])
    assert z.der[0] == pytest.approx([0.25])
    assert z.der[1] == pytest.approx([-0.125])
def test_add_different_AD_many_val():
    x = AD(val=[10, -1, 3.2, 4], index=0, magnitude=2)
    y = AD(val=[-2, 0, 1, 100], index=1, magnitude=2)
    z = x + y
    assert z.val == pytest.approx([8, -1, 4.2, 104])
    for d in z.der:
        assert d == pytest.approx([1, 1, 1, 1])
def test_mul_different_AD():
    x = AD(val=[3], index=0, magnitude=2)
    y = AD(val=[4], index=1, magnitude=2)
    z = x * y
    assert z.val == pytest.approx([12])
    assert z.der[0] == pytest.approx([4])
    assert z.der[1] == pytest.approx([3])
def test_sub_different_AD():
    x = AD(val=[3], index=0, magnitude=2)
    y = AD(val=[4], index=1, magnitude=2)
    z = x - y
    assert z.val == pytest.approx([-1])
    assert z.der[0] == pytest.approx([1])
    assert z.der[1] == pytest.approx([-1])
def test_sub_different_AD_many_val():
    x = AD(val=[10, -1, 3.2, 4], index=0, magnitude=2)
    y = AD(val=[-2, 0, 1, 100], index=1, magnitude=2)
    z = x - y
    assert z.val == pytest.approx([12, -1, 2.2, -96])
    assert z.der[0] == pytest.approx([1, 1, 1, 1])
    assert z.der[1] == pytest.approx([-1, -1, -1, -1])
def test_pow_different_AD():
    x = AD(val=[2], index=0, magnitude=2)
    y = AD(val=[3], index=1, magnitude=2)
    z = x**y
    assert z.val == pytest.approx([8])
    assert z.der[0] == pytest.approx([12])
    assert z.der[1] == pytest.approx([5.545177444479562])
def test_pos_different_AD():
    x = AD(val=[10], index=0, magnitude=2)
    y = AD(val=[-20], index=1, magnitude=2)
    z = +(x + y)
    assert z.val == pytest.approx([-10])
    for d in z.der:
        assert d == pytest.approx([1])
Esempio n. 10
0
def test_div_different_AD_many_val():
    x = AD(val=[-2, 4, 10, 100], index=0, magnitude=2)
    y = AD(val=[1, 2, 2, 4], index=1, magnitude=2)
    z = x / y
    assert z.val == pytest.approx([-2, 2, 5, 25])
    assert z.der[0] == pytest.approx([1, 0.5, 0.5, 0.25])
    assert z.der[1] == pytest.approx([2, -1, -2.5, -6.25])
Esempio n. 11
0
def test_neg_different_AD():
    x = AD(val=[10], index=0, magnitude=2)
    y = AD(val=[20], index=1, magnitude=2)
    z = -(x + y)
    assert z.val == pytest.approx([-30])
    for d in z.der:
        assert d == pytest.approx([-1])
Esempio n. 12
0
def test_mul_different_AD_many_val():
    x = AD(val=[10, -1, 3.2, 4], index=0, magnitude=2)
    y = AD(val=[-2, 0, 1, 100], index=1, magnitude=2)
    z = x * y
    assert z.val == pytest.approx([-20, 0, 3.2, 400])
    assert z.der[0] == pytest.approx([-2, 0, 1, 100])
    assert z.der[1] == pytest.approx([10, -1, 3.2, 4])
Esempio n. 13
0
def test_pow_different_AD_many_val():
    x = AD(val=[1, 2, 3, 4], index=0, magnitude=2)
    y = AD(val=[4, 3, 2, 1], index=1, magnitude=2)
    z = x**y
    assert z.val == pytest.approx([1, 8, 9, 4])
    assert z.der[0] == pytest.approx([4, 12, 6, 1])
    assert z.der[1] == pytest.approx(
        [0, 5.545177444479562, 9.887510598012987, 5.545177444479562])
def test_arctan4():
    # AD object with .val=[1,2]
    x1 = AD(val=[0.3, 0.4], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[0.4, 0.5], index=1, magnitude=2)
    y = ef.arctan(x1 + x2)
    assert y.val == pytest.approx(np.arctan([0.7, 0.9]))
    assert y.der[0] == pytest.approx([0.6711409395973155, 0.5524861878453039])
    assert y.der[1] == pytest.approx([0.6711409395973155, 0.5524861878453039])
def test_arcsin4():
    # AD object with .val=[1,2]
    x1 = AD(val=[0.1, 0.2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[0.2, 0.3], index=1, magnitude=2)
    y = ef.arcsin(x1 + x2)
    assert y.val == pytest.approx(np.arcsin([0.3, 0.5]))
    assert y.der[0] == pytest.approx([1.0482848367219182, 1.1547005383792517])
    assert y.der[1] == pytest.approx([1.0482848367219182, 1.1547005383792517])
def test_tanh3():
    # AD object with .val=[1]
    x1 = AD(val=[1], index=0, magnitude=2)
    # AD object with .val=[2]
    x2 = AD(val=[2], index=1, magnitude=2)
    y = ef.tanh(x1 + x2)
    assert y.val == pytest.approx(np.tanh([3]))
    assert y.der[0] == pytest.approx(1 / np.cosh(3))
    assert y.der[1] == pytest.approx(1 / np.cosh(3))
def test_log4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.log(x1 + x2)
    assert y.val == pytest.approx(np.log([3, 5]))
    assert y.der[0] == pytest.approx([0.3333333333333333, 0.2])
    assert y.der[1] == pytest.approx([0.3333333333333333, 0.2])
def test_logistic4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.logistic(x1 + x2)
    assert y.val == pytest.approx([0.95257412, 0.99330714])
    assert y.der[0] == pytest.approx([0.045176659730, 0.006648056670])
    assert y.der[1] == pytest.approx([0.045176659730, 0.006648056670])
def test_tanh4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.tanh(x1 + x2)
    assert y.val == pytest.approx(np.tanh([3, 5]))
    assert y.der[0] == pytest.approx(np.sinh(1 / np.cosh([3, 5])), rel=1e-2)
    assert y.der[1] == pytest.approx(np.sinh(1 / np.cosh([3, 5])), rel=1e-2)
def test_cos4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.cos(x1 + x2)
    assert y.val == pytest.approx(np.cos([3, 5]))
    assert y.der[0] == pytest.approx(-np.sin([3, 5]) * 1)
    assert y.der[1] == pytest.approx(-np.sin([3, 5]) * 1)
def test_sqrt4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.sqrt(x1 + x2)
    assert y.val == pytest.approx(np.sqrt([3, 5]))
    assert y.der[0] == pytest.approx(0.5 / np.sqrt([3, 5]) * 1)
    assert y.der[1] == pytest.approx(0.5 / np.sqrt([3, 5]) * 1)
def test_sin3():
    # AD object with .val=[1]
    x1 = AD(val=[1], index=0, magnitude=2)
    # AD object with .val=[2]
    x2 = AD(val=[2], index=1, magnitude=2)
    y = ef.sin(x1 + x2)
    assert y.val == pytest.approx(np.sin([3]))
    assert y.der[0] == pytest.approx(np.cos(3) * 1)
    assert y.der[1] == pytest.approx(np.cos(3) * 1)
def test_tan4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.tan(x1 + x2)
    assert y.val == pytest.approx(np.tan([3, 5]))
    assert y.der[0] == pytest.approx((1 / np.cos([3, 5]))**2 * 1)
    assert y.der[1] == pytest.approx((1 / np.cos([3, 5]))**2 * 1)
def test_arccos4():
    # AD object with .val=[1,2]
    x1 = AD(val=[0.3, 0.4], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[0.4, 0.5], index=1, magnitude=2)
    y = ef.arccos(x1 + x2)
    assert y.val == pytest.approx(np.arccos([0.7, 0.9]))
    assert y.der[0] == pytest.approx([-1.4002800840280099, -2.294157338705618])
    assert y.der[1] == pytest.approx([-1.4002800840280099, -2.294157338705618])
def test_log2_4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.log2(x1 + x2)
    assert y.val == pytest.approx(np.log2([3, 5]))
    assert y.der[0] == pytest.approx(
        [0.48089834696298783, 0.28853900817779266])
    assert y.der[1] == pytest.approx(
        [0.48089834696298783, 0.28853900817779266])
def test_log10_4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.log10(x1 + x2)
    assert y.val == pytest.approx(np.log10([3, 5]))
    assert y.der[0] == pytest.approx(
        [0.14476482730108392, 0.08685889638065035])
    assert y.der[1] == pytest.approx(
        [0.14476482730108392, 0.08685889638065035])
def test_logb4():
    # AD object with .val=[1,2]
    x1 = AD(val=[1, 2], index=0, magnitude=2)
    # AD object with .val=[2,3]
    x2 = AD(val=[2, 3], index=1, magnitude=2)
    y = ef.logb(x1 + x2, 3)
    assert y.val[0] == pytest.approx(math.log(3, 3))
    assert y.val[1] == pytest.approx(math.log(5, 3))
    assert y.der[0] == pytest.approx(
        [0.30341307554227914, 0.18204784532536747])
    assert y.der[1] == pytest.approx(
        [0.30341307554227914, 0.18204784532536747])
Esempio n. 28
0
def test_getters():
    x = AD(val=[10])
    value = x.val
    derivative = x.der
    assert value == pytest.approx([10], rel=1e-4)
    for d in derivative:
        assert d == pytest.approx([1])
Esempio n. 29
0
def test_pow_AD_many_val():
    x = AD(val=[1, 2, 5, 10])
    y = x**x
    assert y.val == pytest.approx([1, 4, 3125, 10000000000])
    for d in y.der:
        assert d == pytest.approx(
            [1, 6.772588722239782, 8154.493476356564, 33025850929.94046])
Esempio n. 30
0
def test_rdiv_const_many_val():
    x = AD(val=[-1, 1, 7, 14])
    y = 7 / x
    assert y.val == pytest.approx([-7, 7, 1, 0.5])
    for d in y.der:
        assert d == pytest.approx(
            [-7, -7, -0.14285714285714285, -0.03571428571428571])