def test_mul_c2_addx():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    z = x * y * 4 + x
    assert z.val == [9]
    np.testing.assert_array_equal(z.der, np.array([9, 8]).reshape(1, -1))
    assert z.name == ['x', 'y']
def test_le_values():
    x = AD(2, 1, 'x')
    y = AD(3, 1, 'y')
    z = AD(3, 1, 'z')
    assert (x <= y) == True
    assert (y <= x) == False
    assert (z <= y) == True
def test_add_list():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    w = AD([x + y, y - x])
    z = [1, 2]
    with pytest.raises(ValueError):
        w + z
def test_pow_2():
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    z = x**y
    assert z.name == ['x', 'y']
    assert z.val == [1]
    np.testing.assert_array_equal(z.der, np.array([8, 0]).reshape(1, -1))
def test_div():
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    z = x / y
    assert z.name == ['x', 'y']
    assert z.val == [0.5]
    np.testing.assert_array_equal(z.der, np.array([2, -7 / 4]).reshape(1, -1))
def test_ne_different_dim():
    x = AD(1, 1, 'x')
    y = AD(3, 1, 'y')
    z = AD(4, 1, 'z')
    w = x + y
    with pytest.raises(AttributeError):
        w != z
def test_ge_values():
    x = AD(2, 1, 'x')
    y = AD(3, 1, 'y')
    z = AD(3, 1, 'z')
    assert (y >= x) == True
    assert (x >= y) == False
    assert (z >= y) == True
def test_ne_values():
    x = AD(2, 1, 'x')
    y = AD(3, 1, 'y')
    z = AD(3, 1, 'z')
    assert (y != x) == True
    assert (x != y) == True
    assert (z != y) == False
def test_add_2_vec_diff_dim():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    w = AD([x + y, y - x])
    z = np.array([1.0, 2.0, 3.0])
    with pytest.raises(ValueError):
        w + z
def test_mul_c1():  # todo: same
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    z = x * y + 4
    assert z.val == [6]
    np.testing.assert_array_equal(z.der, np.array([2, 2]).reshape(1, -1))
    assert z.name == ['x', 'y']
def test_add_vec_str():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    w = AD([x + y, y - x])
    z = np.array(['x', 2.0]).reshape(2, 1)
    with pytest.raises(TypeError):
        w + z
def test_add_c():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    z = x + y + 4
    assert z.val == [7]
    np.testing.assert_array_equal(z.der, np.array([1, 2]).reshape(1, -1))
    assert z.name == ['x', 'y']
def test_add_2_vec():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    w = AD([x + y, y - x])
    z = np.array([1.0, 2.0]).reshape(2, 1)
    q = w + z
    np.testing.assert_array_equal(q.val, np.array([[4.0], [3.0]]))
def test_eq_values():
    x = AD(2, 1, 'x')
    y = AD(3, 1, 'y')
    z = AD(3, 1, 'z')
    assert (y == x) == False
    assert (x == y) == False
    assert (z == y) == True
def test_add_2_vec_str():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    w = AD([x + y, y - x])
    z = np.array(['a', 'b'])
    with pytest.raises(ValueError):
        q = w + z
def test_rtrue_div():
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    z = 2 / (x + y)
    assert z.name == ['x', 'y']
    assert z.val == [2 / 3]
    np.testing.assert_array_equal(z.der,
                                  np.array([-8 / 9, -14 / 9]).reshape(1, -1))
def test_div_3_var():
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    z = AD(3, 2, 'z')
    w = x * y * z
    assert w.name == ['x', 'y', 'z']
    assert w.val == [6]
    np.testing.assert_array_equal(w.der, np.array([24, 21, 4]).reshape(1, -1))
def test_mul_last():
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    w = x * y
    z = x + y
    v = z * w
    assert v.name == ['x', 'y']
    assert v.val == [6]
def test_fn_power_array2():
    x = AD(0, 1, 'x')
    y = AD(0, 1, 'x')
    z = AD([x, y])
    n = AD(0, 1, 'n')

    with pytest.raises(TypeError):
        w = z**n
Ejemplo n.º 20
0
def _function(init_vals):
    for k, v in init_vals.items():
        globals()[k] = AD(v, 1, k)
    f = x1**2 - x2
    g = x1 - 2 * x2
    v = AD([f, g])
    v.sort(['x1', 'x2'])
    return v
def test_arccos():
    x = AD(0.5, 3, 'x')
    z = x.arccos()
    print('x=', x)
    print(z)
    assert z.val == [np.arccos(0.5)]
    # np.testing.assert_array_equal(z.der, np.array([-3/np.sqrt(1 - 0.5**2)]))
    assert z.der == [-3 * (1 - 0.5**2)**(-0.5)]
def test_beug():
    x = AD(1, 1, 'x')
    y = AD(2, 1, 'y')
    f1 = AD([10 * x, 10 * y])
    f2 = AD([3 * x, 3 * y])
    z = f1 + f2
    np.testing.assert_array_equal(z.val, np.array([13, 26]).reshape(2, 1))
    np.testing.assert_array_equal(
        z.sort(order=['x', 'y']).der, np.array([[13, 0], [0, 13]]))
def test_pow_1():
    x = AD(3, 4, 'x')
    y = AD(2, 7, 'y')
    z = x**(y + x)
    assert z.name == ['x', 'y']
    assert z.val == [3**5]
    np.testing.assert_array_equal(
        z.der,
        np.array([(4 * np.log(3) + 20 / 3) * 3**5,
                  7 * np.log(3) * 3**5]).reshape(1, -1))
def test_div_hard():  # we should cover the lines in rtruediv here
    x = AD(1, 4, 'x')
    y = AD(2, 7, 'y')
    z = x * y
    w = x + y
    u = z / w
    assert u.name == ['x', 'y']
    assert u.val == 2 / 3
    np.testing.assert_array_equal(u.der,
                                  np.array([16 / 9, 7 / 9]).reshape(1, -1))
def test_multi_dim_2():
    x = AD(1, 4, 'x')
    y = AD(2, 3, 'y')
    z = AD(1, 7, 'z')
    print('1 x value is', x)
    print(x * y)
    print(y + z)
    print(z + x)
    print('y value is', y)
    print('2 x value is', x)
    print(x + y)
    v = AD([x * y, y + z, z + x, 4, x + y, x + y])
    print(v)
def test_sub():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    z = y - x
    assert z.val == [1]
    np.testing.assert_array_equal(z.der, np.array([2, -1]).reshape(1, -1))
def test_sqrt2():
    x = AD(10, 3, 'x')
    y = x.sqrt()
    assert y.val == np.sqrt(10)
    assert y.der == 0.5 * 3 * 10**(-0.5)
def test_sqrt1():
    x = AD(-1, 1, 'x')
    with pytest.raises(ValueError):
        y = x.sqrt()
def test_rpow_4():
    x = AD(-1, 1, 'x')
    with pytest.raises(ZeroDivisionError):
        y = (0)**x
def test_order():
    x = AD(1, 1, 'x')
    y = AD(2, 2, 'y')
    z = y + x
    z.sort(['x', 'y'])
    print(z)