def test_composite(): f1 = AutoDiff(name='x', val=np.pi/4) f2 = AutoDiff(name='y', val=np.pi / 2) u = AutoDiffVector((f1, f2)) v = AutoDiffVector([f1, np.pi]) z = AutoDiffVector((f2, -f1)) np.testing.assert_array_almost_equal(ad.cos(ad.sin(u)), [0.7602445970756302, 0.5403023058681398]), "Composite failed" J, order = (ad.cos(ad.sin(u))).get_jacobian() np.testing.assert_array_almost_equal(J, [[-0.4593626849327842, 0], [0, 0]]), "Composite failed" np.testing.assert_array_almost_equal(ad.cos(ad.sin(v)), [0.7602445970756302, 1]), "Composite failed" J, order = (ad.cos(ad.sin(v))).get_jacobian() np.testing.assert_array_almost_equal(J, [[-0.4593626849327842], [0]]), "Composite failed" np.testing.assert_array_almost_equal(u*ad.cos(ad.sin(u)), [0.597094710276033, 0.8487048774164866]), "Composite failed" J, order = (u*ad.cos(ad.sin(u))).get_jacobian() np.testing.assert_array_almost_equal(J, [[0.3994619879961, 0], [0, 0.5403023058681397]]), "Composite failed" np.testing.assert_array_almost_equal(z*ad.cos(ad.sin(u)), [1.194189420552066, -0.4243524387082433]), "Composite failed" J, order = (z*ad.cos(ad.sin(u))).get_jacobian() np.testing.assert_array_almost_equal(J, [[-0.7215652181590587, 0.7602445970756302], [-0.5403023058681398, 0]]), "Composite failed" np.testing.assert_array_almost_equal((z*ad.cos(ad.sin(u)))**2, [1.4260883721584792, 0.18007499223763337]), "Composite failed" J, order = ((z*ad.cos(ad.sin(u)))**2).get_jacobian() np.testing.assert_array_almost_equal(J, [[-1.7233710995277831, 1.815752109719], [0.4585572, 0]]), "Composite failed"
def test_duplicate_instantiation(): f1 = AutoDiff(name='x', val=1) f2 = AutoDiff(name='x', val=3) try: AutoDiffVector((f1, f2)) except Exception: print("Caught error as expected")
def test_and(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) np.testing.assert_array_equal(u, u) and np.testing.assert_array_equal( y, y), "And failed"
def test_divide(): x = AutoDiff(name='x', val=6) y = AutoDiff(name='y', val=-12) z = AutoDiff(name='z', val=0) q = AutoDiff(name='b0', val="string") assert (x / 2) == 3, 'Division failed' assert (x / 2).trace['d_x'] == (1 / 2), 'Division failed' assert (18 / x) == 3, 'Division failed' assert (18 / x).trace['d_x'] == -(1 / 2), 'Division failed' assert (y / x) == -2, 'Division failed' assert (y / x).trace['d_x'] == (12 / 36), 'Division failed' assert (y / x).trace['d_y'] == (1 / 6), 'Division failed' assert (x / y) == -0.5, 'Division failed' assert (x / y).trace['d_x'] == (1 / -12), 'Division failed' assert (x / y).trace['d_y'] == (-6 / 144), 'Division failed' try: assert (z / z) == 0 except ZeroDivisionError as e: print("Caught Zero Division Error") try: assert (z / z).trace['d_z'] == 0 except ZeroDivisionError as e: print("Caught Zero Division Error") try: (q / 5) except TypeError: print("Caught error as expected")
def test_cot(): f1 = AutoDiff(name='x', val=4) f2 = AutoDiff(name='y', val=np.pi/8) u = AutoDiffVector((f1, f2)) np.testing.assert_array_almost_equal(ad.cot(u).val, [0.863691, 2.414214]), 'Cotangent failed' J, order = (ad.cot(u)).get_jacobian() np.testing.assert_array_almost_equal(J, [[-1.746, 0], [0, -6.8284]], decimal=4), 'Cotangent failed'
def test_sec(): f1 = AutoDiff(name='x', val=0) f2 = AutoDiff(name='y', val=np.pi) u = AutoDiffVector((f1, f2)) np.testing.assert_array_almost_equal(ad.sec(u).val, [1, -1]), 'Secant failed' J, order = (ad.sec(u)).get_jacobian() np.testing.assert_array_almost_equal(J, [[0, 0], [0, 0]], decimal=4), 'Secant failed'
def test_round(): x = AutoDiff(name='x', val=5.7) y = AutoDiff(name='y', val=-4.6) z = AutoDiff(name='z', val=0) assert round(x) == 6, "Round failed" assert -5 == round(y), "Round failed" assert 0 == round(z), "Round failed"
def test_vec_multiply(): f1 = AutoDiff(name='x', val=-1) f2 = AutoDiff(name='y', val=3) u = AutoDiffVector((f1, f2)) v = AutoDiffVector((-f2, f1)) q = [2, 0] t = [4, 4] np.testing.assert_array_equal((u * 3).val, [-3, 9]), 'Multiplication failed' J, order = (u * 3).get_jacobian() np.testing.assert_array_equal(J, [[3, 0], [0, 3]]), "Multiplication failed" np.testing.assert_array_equal((-4 * u).val, [4, -12]), 'Multiplication failed' np.testing.assert_array_equal((u * q).val, [-2, 0]), 'Multiplication failed' np.testing.assert_array_equal((q * u).val, [-2, 0]), 'Multiplication failed' J, order = (u * t).get_jacobian() np.testing.assert_array_equal(J, [[4, 0], [0, 4]]), "Multiplication failed" J, order = (u * q).get_jacobian() np.testing.assert_array_equal(J, [[2, 0], [0, 0]]), 'Multiplication failed' J, order = (q * u).get_jacobian() np.testing.assert_array_equal(J, [[2, 0], [0, 0]]), 'Multiplication failed' J, order = (u * v).get_jacobian() np.testing.assert_array_equal(J, [[-3, 1], [3, -1]]), 'Multiplication failed' J, order = (v * u).get_jacobian() np.testing.assert_array_equal(J, [[-3, 1], [3, -1]]), 'Multiplication failed'
def test_csc(): f1 = AutoDiff(name='x', val=-2) f2 = AutoDiff(name='y', val=np.pi / 8) u = AutoDiffVector((f1, f2)) np.testing.assert_array_almost_equal(ad.csc(u).val, [-1.09975, 2.613126]), 'Cosecant failed' J, order = (ad.csc(u)).get_jacobian() np.testing.assert_array_almost_equal(J, [[0.5033, 0], [0, -6.3086]], decimal=4), 'Cosecant failed'
def test_eq(): x = AutoDiff(name='x', val=2) y = AutoDiff(name='x', val=2) assert 2 == x, "Equals failed" assert x == 2, "Equals failed" assert x == y, "Equals failed" assert y == x, "Equals failed"
def test_str(): f1 = AutoDiff(name='x', val=3.3333) f2 = AutoDiff(name='y', val=-5.888) u = AutoDiffVector((f1, f2)) assert str( u ) == "[{'val': 3.3333, 'd_x': 1},{'val': -5.888, 'd_y': 1}]", "Str failed"
def test_or(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) assert u == [3, 5] or y == [0, 0], "Or failed" assert u == [3, 5] or y == [5, 8], "Or failed"
def test_exponentiation(): x = AutoDiff(name='x', val=3) y = AutoDiff(name='y', val=0) z = AutoDiff(name='z', val=-2) q = AutoDiff(name='b0', val="string") r = AutoDiff(name='r', val=5) assert (x**2) == 9, "Exponentiation failed" assert (x**2).trace['d_x'] == 6, "Exponentiation failed" assert (2**x) == 8, "Exponentiation failed" assert np.allclose((2**x).trace['d_x'], 5.545177444479562, atol=1e-12) is True, "Exponentiation failed" assert (x**0) == 1, "Exponentiation failed" assert (x**0).trace['d_x'] == 0, "Exponentiation failed" assert (x**-2) == (1 / 9), "Exponentiation failed" assert (x**-2).trace['d_x'] == -2 / (3**3), "Exponentiation failed" assert (z**2) == 4, "Exponentiation failed" assert (z**2).trace['d_z'] == -4, "Exponentiation failed" assert (z**3) == -8, "Exponentiation failed" assert (z**3).trace['d_z'] == 12, "Exponentiation failed" assert (y**2) == 0, "Exponentiation failed" assert (y**2).trace['d_y'] == 0, "Exponentiation failed" assert (x**x) == 27, "Exponentiation failed" assert (r**x) == 125, "Exponentiation failed" try: (q**5) except TypeError: print("Caught error as expected")
def test_bool(): f1 = AutoDiff(name='x', val=0) f2 = AutoDiff(name='y', val=0) u = AutoDiffVector((f1, f2)) try: bool(u) except TypeError: print("Caught error as expected")
def test_shift(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=4) u = AutoDiffVector((f1, f2)) np.testing.assert_array_equal(u >> 2, [16, 32]), "Shift failed" np.testing.assert_array_equal(u << 2, [12, 16]), "Shift failed" np.testing.assert_array_equal(3 >> u, [24, 32]), "Shift failed" np.testing.assert_array_equal(3 << u, [24, 48]), "Shift failed"
def test_contains(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=4) u = AutoDiffVector((f1, f2)) try: assert 2 in u except NotImplementedError: print("Caught error as expected")
def test_ne(): x = AutoDiff(name='x', val=10) y = AutoDiff(name='y', val=100) q = AutoDiff(name='b0', val="string") assert x != 11, "Not equal failed" assert 11 != x, "Not equal failed" assert x != y, "Not equal failed" assert 12 != q, "Not equal failed"
def test_le(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) assert [3, 5] <= u, "Less than failed" assert u <= [100, 100], "Less than failed" assert u <= y, "Less than failed"
def test_lt(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) assert [0, 0] < u, "Less than failed" assert u < [100, 100], "Less than failed" assert u < y, "Less than failed"
def test_gt(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) assert u > [0, 0], "Greater than failed" assert [100, 100] > u, "Greater than failed" assert y > u, "Greater than failed"
def test_ge(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) assert u >= [0, 0], "Greater than or equal to failed" assert u >= [3, 5], "Greater than or equal to failed" assert [100, 100] >= u, "Greater than or equal to failed" assert y >= u, "Greater than or equal to failed"
def test_instantiation_zero(): f1 = AutoDiff(name='x', val=0) f2 = AutoDiff(name='y', val=0) u = AutoDiffVector((f1, f2)) np.testing.assert_array_equal(u.val, [0, 0]), "Positive instantiation failed" J, order = u.get_jacobian() np.testing.assert_array_equal( J, [[1, 0], [0, 1]]), "Positive instantiation failed"
def test_get_gradient(): x = AutoDiff(name='x', val=3) grad1, varnames = x.get_gradient() grad2, _ = (8 * x).get_gradient() grad3, _ = x.gradient assert np.allclose(grad1, np.array([1.])), "Get gradient failed" assert np.allclose(grad2, np.array([8.])), "Get gradient failed" assert np.allclose(grad3, np.array([1.])), "Get gradient property failed"
def test_ne(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) q = AutoDiff(name='b0', val="string") assert u != 11, "Not equal failed" assert 11 != u, "Not equal failed" assert u != y, "Not equal failed" assert y != q, "Not equal failed"
def test_xor(): x = AutoDiff(name='x', val=0) y = AutoDiff(name='y', val=1) assert (x ^ y) == 1, "Xor failed" assert (x ^ 1) == 1, "Xor failed" assert (1 ^ x) == 1, "Xor failed" try: assert x ^ x == 1 except AssertionError: print("Caught Error as expected")
def test_sin(): f1 = AutoDiff(name='x', val=0) f2 = AutoDiff(name='y', val=np.pi/2) u = AutoDiffVector((f1, f2)) v = AutoDiffVector([0, np.pi/2]) np.testing.assert_array_almost_equal(ad.sin(u), [0, 1]), 'Sine failed' J, order = (ad.sin(u)).get_jacobian() np.testing.assert_array_almost_equal(J, [[1, 0], [0, 0]]), 'Sine failed' np.testing.assert_array_almost_equal(ad.sin(v), [0, 1]), 'Sine failed' J, order = (ad.sin(v)).get_jacobian() np.testing.assert_array_almost_equal(J, [[0], [0]]), 'Sine failed'
def test_xor(): f1 = AutoDiff(name='x', val=3) f2 = AutoDiff(name='y', val=5) u = AutoDiffVector((f1, f2)) y = AutoDiffVector((f2, 8)) np.testing.assert_array_equal(u ^ y, [6, 13]), "Xor failed" np.testing.assert_array_equal(y ^ u, [6, 13]), "Xor failed" try: assert (u ^ y) == 1, "Xor failed" except ValueError: print("Caught error as expected")
def test_lt(): x = AutoDiff(name='x', val=10) y = AutoDiff(name='y', val=100) q = AutoDiff(name='b0', val="string") assert 2 < x, "Less than failed" assert x < 20, "Less than failed" assert x < y, "Less than failed" try: (12 < q) except TypeError: print("Caught error as expected")
def test_gt(): x = AutoDiff(name='x', val=10) y = AutoDiff(name='y', val=100) q = AutoDiff(name='b0', val="string") assert x > 2, "Greater than failed" assert 20 > x, "Greater than failed" assert y > x, "Greater than failed" try: (12 > q) except TypeError: print("Caught error as expected")
def test_double_instantiation(): try: AutoDiffVector(name='x', val=3, trace=3) except TypeError: print("Caught error as expected") f1 = AutoDiff(name='x', val=1) f2 = AutoDiff(name='y', val=3) try: AutoDiffVector((f1, f2), f1) except TypeError: print("Caught error as expected")