Beispiel #1
0
def test_vec_add():
    f1 = AutoDiffRev(name='x', val=-1)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    l = AutoDiffRevVector((f1, f2, f1))
    v = AutoDiffRevVector((-f2, f1))
    w = AutoDiffRevVector([10, 10])
    r = AutoDiffRevVector([-3, -3])
    q = [2, 1.5]
    np.testing.assert_array_equal((u + q).val, [1, 4.5]), 'Addition failed'
    np.testing.assert_array_equal((q + u).val, [1, 4.5]), 'Addition failed'
    J, order = (u + q).get_jacobian()
    np.testing.assert_array_equal(J, [[1, 0], [0, 1]]), 'Addition failed'
    J, order = (v + q).get_jacobian()
    np.testing.assert_array_equal(J, [[0, -1], [1, 0]]), 'Addition failed'
    J, order = (q + u).get_jacobian()
    np.testing.assert_array_equal(J, [[1, 0], [0, 1]]), 'Addition failed'
    np.testing.assert_array_equal((u + v).val, [-4,  2]), 'Addition failed'
    np.testing.assert_array_equal((v + u).val, [-4, 2]), 'Addition failed'
    J, order = (u + v).get_jacobian()
    np.testing.assert_array_equal(J, [[1, -1], [1, 1]]), 'Addition failed'
    np.testing.assert_array_equal((w + r), [7, 7]), "Addition failed"
    J, order = (w + r).get_jacobian()
    np.testing.assert_array_equal(J, [[0], [0]]), "Addition failed"
    try:
        u + l
    except Exception:
        print("Caught error as expected")
Beispiel #2
0
def test_vec_multiply():
    f1 = AutoDiffRev(name='x', val=-1)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f2, f1))
    c = AutoDiffRevVector((f1, f1, 9, 3))
    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'
    np.testing.assert_array_equal((c * 2), [-2, -2, 18, 6]), "Multiplication failed"
Beispiel #3
0
def test_or():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((f2, 8))
    assert u == [3, 5] or y == [0,0], "Or failed"
    assert u == [3, 5] or y == [5,8], "Or failed"
Beispiel #4
0
def test_lt():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((f2, 8))
    assert [0, 0] < u, "Less than failed"
    assert u < [100, 100], "Less than failed"
    assert u < y, "Less than failed"
Beispiel #5
0
def test_gt():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((f2, 8))
    assert u > [0, 0], "Greater than failed"
    assert [100, 100] > u, "Greater than failed"
    assert y > u, "Greater than failed"
def test_sin():
    f1 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=np.pi / 2)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector([0, np.pi / 2])
    np.testing.assert_array_almost_equal(ad.sin(u).val, [0, 1]), 'Sine failed'
    J, order = (ad.sin(u)).get_jacobian()
    np.testing.assert_array_almost_equal(J, [[1, 0], [0, 0]]), 'Sine failed'
Beispiel #7
0
def test_le():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((f2, 8))
    assert [3, 5] <= u, "Less than failed"
    assert u <= [100, 100], "Less than failed"
    assert u <= y, "Less than failed"
Beispiel #8
0
def test_ge():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((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 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=0)
    u = AutoDiffRevVector((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"
Beispiel #10
0
def test_ne():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((f2, 8))
    q = AutoDiffRev(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"
Beispiel #11
0
def test_xor():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    y = AutoDiffRevVector((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_double_instantiation():
    try:
        AutoDiffRevVector(name='x', val=3, trace=3)
    except TypeError:
        print("Caught error as expected")
    f1 = AutoDiffRev(name='x', val=1)
    f2 = AutoDiffRev(name='y', val=3)
    try:
        AutoDiffRevVector((f1, f2), f1)
    except TypeError:
        print("Caught error as expected")
Beispiel #13
0
def test_abs():
    f1 = AutoDiffRev(name='x', val=-3)
    f2 = AutoDiffRev(name='y', val=5)
    r = AutoDiffRev(name='b0', val="string")
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((r, r))
    np.testing.assert_array_equal(abs(u), [3, 5]), "Abs val failed"
    try:
        (abs(v))
    except ValueError:
        print("Caught error as expected")
    try:
        (abs(u)).get_jacobian()
    except AttributeError:
        print("Caught error as expected")
def test_log():
    f1 = AutoDiffRev(name='x', val=1)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f1, -f2))
    np.testing.assert_array_equal(ad.log(u),
                                  [np.log(1), np.log(3)]), "Log failed"
    J, order = (ad.log(u)).get_jacobian()
    np.testing.assert_array_almost_equal(J,
                                         [[1, 0], [0, 0.333333]]), "Log failed"
    try:
        np.testing.assert_array_almost_equal(ad.log(v),
                                             [1, 0.04978706836786395])
    except ValueError:
        print("Caught error as expected ")
def test_euler():
    f1 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f1, -f2))
    np.testing.assert_array_equal(
        ad.exp(u), [1, 20.085536923187664]), "Euler's number failed"
    J, order = (ad.exp(u)).get_jacobian()
    np.testing.assert_array_almost_equal(
        J, [[1, 0], [0, 20.085537]]), "Euler's number failed"
    np.testing.assert_array_almost_equal(
        ad.exp(v).val, [1, 0.04978706836786395]), "Euler's number failed"
    J, order = (ad.exp(v)).get_jacobian()
    np.testing.assert_array_almost_equal(
        J, [[-1, 0], [0, -0.049787]]), "Euler's number failed"
Beispiel #16
0
def test_vec_subtract():
    f1 = AutoDiffRev(name='x', val=-1)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f2, f1))
    q = [2, 1.5]
    np.testing.assert_array_equal((u - q).val, [-3, 1.5]), 'Subtraction failed'
    np.testing.assert_array_equal((q - u).val, [3, -1.5]), 'Subtraction failed'
    J, order = (u - q).get_jacobian()
    np.testing.assert_array_equal(J, [[1, 0],[0,1]]), 'Subtraction failed'
    J, order = (q - u).get_jacobian()
    np.testing.assert_array_equal(J, [[-1, 0], [0, -1]]), 'Subtraction failed'
    np.testing.assert_array_equal((u - v).val, [2,  4]), 'Subtraction failed'
    np.testing.assert_array_equal((v - u).val, [-2, -4]), 'Subtraction failed'
    J, order = (u - v).get_jacobian()
    np.testing.assert_array_equal(J, [[1, 1], [-1, 1]]), 'Subtraction failed'
def test_duplicate_instantiation():
    f1 = AutoDiffRev(name='x', val=1)
    f2 = AutoDiffRev(name='x', val=3)
    try:
        AutoDiffRevVector((f1, f2))
    except Exception:
        print("Caught error as expected")
Beispiel #18
0
def test_bool():
    f1 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=0)
    u = AutoDiffRevVector((f1, f2))
    try:
        bool(u)
    except TypeError:
        print("Caught error as expected")
Beispiel #19
0
def test_item():
    v = AutoDiffRevVector([2, 2])
    f1 = AutoDiffRev(name='x', val=3.3)
    f2 = AutoDiffRev(name='y', val=-5.8)
    u = AutoDiffRevVector((f1, f2))
    try:
        v.__delitem__(2)
    except NotImplementedError:
        print("Caught error as expected")
    try:
        v.__setitem__(2, 4)
    except NotImplementedError:
        print("Caught error as expected")
    try:
        u.__getitem__(0) == 3, "Get item failed"
    except AttributeError:
        print("Caught error as expected")
Beispiel #20
0
def test_shift():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=4)
    u = AutoDiffRevVector((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"
Beispiel #21
0
def test_contains():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=4)
    u = AutoDiffRevVector((f1, f2))
    try:
        assert 2 in u
    except NotImplementedError:
        print("Caught error as expected")
def test_composition():
    f1 = AutoDiffRev(name='x', val=5)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f2, -f2))
    z = (u + v) * u * (1 / v)
    np.testing.assert_array_almost_equal(z,
                                         [(-10 / 3), 0]), "Composition failed"
    J, order = (z.get_jacobian())
    np.testing.assert_array_almost_equal(
        J, [[-2.333333, 2.777778], [0, 0]]), "Composition failed"
    np.testing.assert_array_almost_equal(
        ad.exp(u)**2, [22026.465794806707, 403.428793492735])
    J, order = (ad.exp(u)**2).get_jacobian()
    np.testing.assert_array_almost_equal(
        J,
        [[44052.93158961341, 0], [0, 806.85758698547]]), "Composition failed"
def test_cos():
    f1 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=np.pi / 2)
    u = AutoDiffRevVector((f1, f2))
    np.testing.assert_array_almost_equal(ad.cos(u).val,
                                         [1, 0]), 'Cosine failed'
    J, order = (ad.cos(u)).get_jacobian()
    np.testing.assert_array_equal(J, [[0, 0], [0, -1]]), 'Cosine failed'
Beispiel #24
0
def test_vec_divide():
    f1 = AutoDiffRev(name='x', val=-1)
    f2 = AutoDiffRev(name='y', val=3)
    u = AutoDiffRevVector((f1, f2))
    v = AutoDiffRevVector((-f2, f1))
    q = [2, 1]
    t = [4, 4]
    np.testing.assert_array_equal((u / 3).val, [-(1/3), 1]), 'Multiplication failed'
    J, order = (u / 3).get_jacobian()
    np.testing.assert_array_equal(J, [[(1/3), 0], [0, (1/3)]]), "Multiplication failed"
    np.testing.assert_array_equal((-4 / u).val, [4, -(4/3)]), 'Multiplication failed'
    np.testing.assert_array_equal((u / q).val, [-0.5, 3]), 'Multiplication failed'
    np.testing.assert_array_equal((q / u).val, [-2, (1/3)]), 'Multiplication failed'
    J, order = (u / t).get_jacobian()
    np.testing.assert_array_equal(J, [[(1/4), 0], [0, (1/4)]]), "Multiplication failed"
    J, order = (u / v).get_jacobian()
    np.testing.assert_array_equal(J, [[-(1/3), -(1/9)], [-3, -1]]), 'Multiplication failed'
Beispiel #25
0
def test_or_and():
    v = AutoDiffRevVector([2, 2])
    np.testing.assert_array_equal(v.__or__(3), [3, 3]), "ror failed"
    np.testing.assert_array_equal(v.__and__(3), [2, 2]), "ror failed"
    np.testing.assert_array_equal(v.__ror__(3), [3, 3]), "ror failed"
    np.testing.assert_array_equal(v.__rxor__(3), [1, 1]), "ror failed"
    np.testing.assert_array_equal(v.__rand__(3), [2, 2]), "ror failed"
def test_csc():
    f1 = AutoDiffRev(name='x', val=-2)
    f2 = AutoDiffRev(name='y', val=np.pi / 8)
    u = AutoDiffRevVector((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_sec():
    f1 = AutoDiffRev(name='x', val=0)
    f2 = AutoDiffRev(name='y', val=np.pi)
    u = AutoDiffRevVector((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_cot():
    f1 = AutoDiffRev(name='x', val=4)
    f2 = AutoDiffRev(name='y', val=np.pi / 8)
    u = AutoDiffRevVector((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_tan():
    f1 = AutoDiffRev(name='x', val=-2)
    f2 = AutoDiffRev(name='y', val=np.pi / 8)
    u = AutoDiffRevVector((f1, f2))
    np.testing.assert_array_almost_equal(ad.tan(u).val,
                                         [2.18504, 0.414214]), 'Tan failed'
    J, order = (ad.tan(u)).get_jacobian()
    np.testing.assert_array_almost_equal(J, [[5.774399, 0], [0, 1.171573]],
                                         decimal=4), 'Tan failed'
Beispiel #30
0
def test_exponentiation():
    f1 = AutoDiffRev(name='x', val=3)
    f2 = AutoDiffRev(name='y', val=5)
    u = AutoDiffRevVector((f1, f2))
    np.testing.assert_array_almost_equal(u ** 2, [9, 25]), "Exponentiation failed"
    J, order = (u ** 2).get_jacobian()
    np.testing.assert_array_almost_equal(J, [[6, 0], [0, 10]]), "Exponentiation failed"
    J, order = (2 ** u).get_jacobian()
    np.testing.assert_array_almost_equal(J, [[5.545177, 0], [0, 22.18071]]), "Exponentiation failed"