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_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_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_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_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_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_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
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_multi_dim_4(): x = AD(1, 1, 'x') y = AD(2, 2, 'y') z = AD([y.cos() + x.cos(), y.tan() + x.tanh()]) z.sort(['x', 'y']) np.testing.assert_allclose(z.val, np.array([0.12415547, -1.42344571]).reshape(2, 1), atol=1e-5) np.testing.assert_allclose(z.der, np.array([[-0.84147098, -1.81859485], [0.41997434, 11.54879841]]), atol=1e-5) assert z.name == ['x', 'y']