示例#1
0
def test_jac_matrix():
    f1 = lambda x, y: Var.log(x)**Var.sin(y)
    f2 = lambda x, y: Var.sqrt(x) / y
    ad = AD(np.array([4.12, 5.13]), np.array([1, 1]))
    assert np.array_equal(
        ad.jac_matrix([f1, f2]),
        np.array([[pytest.approx(-0.11403015),
                   pytest.approx(0.10263124)],
                  [pytest.approx(0.048018),
                   pytest.approx(-0.07712832)]]))
示例#2
0
def test_sqrt():
    x = Var(3, np.array([1]))
    y = 2
    assert Var.sqrt(x) == Var(pytest.approx(1.7320508075688772),
                              np.array([pytest.approx(0.28867513)]))
    assert Var.sqrt(y) == np.sqrt(y)