Exemple #1
0
def test_composition():
    x = AutoDiffRev(name='x', val=4)
    assert np.allclose(ad.sin(ad.exp(x) ** 2).get_value(), 0.4017629715192812,
                       atol=1e-12) is True, "Composition failed"
    g, _ = ad.sin(ad.exp(x) ** 2).get_gradient()
    assert np.allclose(g, -5459.586962682745,
                       atol=1e-12) is True, "Composition failed"
    assert np.allclose(ad.exp(ad.sin(x) ** 2).get_value(), 1.7731365081918968985940,
                       atol=1e-12) is True, "Composition failed"
    g, _ = ad.exp(ad.sin(x) ** 2).get_gradient()
    assert np.allclose(g, 1.75426722676864073577,
                       atol=1e-12) is True, "Composition failed"
Exemple #2
0
def test_composition():
    x = AutoDiff(name='x', val=4)
    assert np.allclose(ad.sin(ad.exp(x)**2).trace['val'],
                       0.4017629715192812,
                       atol=1e-12) is True, "Composition failed"
    assert np.allclose(ad.sin(ad.exp(x)**2).trace['d_x'],
                       -5459.586962682745,
                       atol=1e-12) is True, "Composition failed"
    assert np.allclose(ad.exp(ad.sin(x)**2).trace['val'],
                       1.7731365081918968985940,
                       atol=1e-12) is True, "Composition failed"
    assert np.allclose(ad.exp(ad.sin(x)**2).trace['d_x'],
                       1.75426722676864073577,
                       atol=1e-12) is True, "Composition failed"
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"
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"
Exemple #5
0
def test_Euler():
    x = AutoDiffRev(name='x', val=3)
    y = AutoDiffRev(name='y', val=0)
    z = AutoDiffRev(name='z', val=-0.34020)
    assert np.allclose(ad.exp(x).get_value(), np.exp(3)), "Euler's number failed"
    g, _ = ad.exp(x).get_gradient()
    assert np.allclose(g, [np.exp(3)]), "Euler's number failed"
    assert np.allclose(ad.exp(y).get_value(), np.exp(0)), "Euler's number failed"
    g, _ = ad.exp(y).get_gradient()
    assert np.allclose(g, [np.exp(0)]), "Euler's number failed"
    assert np.allclose(ad.exp(z).get_value(), np.exp(-0.34020)), "Euler's number failed"
    g, _ = ad.exp(z).get_gradient()
    assert np.allclose(g, np.exp(-0.34020)), "Euler's number failed"
Exemple #6
0
def test_euler():
    x = AutoDiff(name='x', val=3)
    y = AutoDiff(name='y', val=0)
    z = AutoDiff(name='z', val=-0.34020)

    assert np.allclose(ad.exp(x).trace['val'],
                       np.exp(3)), "Euler's number failed"
    assert np.allclose(ad.exp(x).trace['d_x'],
                       np.exp(3)), "Euler's number failed"
    assert np.allclose(ad.exp(y).trace['val'],
                       np.exp(0)), "Euler's number failed"
    assert np.allclose(ad.exp(y).trace['d_y'],
                       np.exp(0)), "Euler's number failed"
    assert np.allclose(ad.exp(z).trace['val'],
                       np.exp(-0.34020)), "Euler's number failed"
    assert np.allclose(ad.exp(z).trace['d_z'],
                       np.exp(-0.34020)), "Euler's number failed"