Esempio n. 1
0
def test_single_variable_trig_sin():
    """analytic hessian is -25 sin (5 * x + 3)"""
    x = ad.Variable()
    f = ad.Sin(5 * x + 3)
    assert (equals(f.hessian({x: 1}), -25 * np.sin(5 * 1 + 3)))
    assert (equals(f.hessian({x: 2}), -25 * np.sin(5 * 2 + 3)))
    assert (equals(f.hessian({x: 3}), -25 * np.sin(5 * 3 + 3)))
Esempio n. 2
0
def test_single_variable_trig_hyperbolic_2():
    x = ad.Variable()
    # x^2  Cosh[Sin[x] + Tanh[Exp[3 * x] + Log[x]]]
    g = ad.Sin(x) + ad.Tanh(ad.Exp(3 * x) + ad.Log(x))
    f = x * x * ad.Cosh(g)
    assert (equals(f.hessian({x: 1}), 11.464317742))
    assert (equals(f.hessian({x: 2}), -13.704377252))
def test_sine():
    x = ad.Variable("x")
    y = ad.Sin(x)
    yd1 = y.d_expr()
    assert np.isclose(yd1.eval({x: pi / 2}), 0.0)
    assert np.isclose(yd1.eval({x: 0.0}), 1.0)
    assert np.isclose(yd1.d({x: 0.0}), 0.0)
def test_logexp_multivar_hessian_2():
    x, y, z = ad.Variable(), ad.Variable(), ad.Variable()
    f = ad.Sinh(ad.Exp(x - 3.0) * y) + ad.Log(y + x**2) * z * ad.Sin(x)
    h = f.hessian({x: 1, y: 3, z: 5})
    assert (equals(h[x][x], -1.5705707143))
    assert (equals(h[y][y], -0.2553174360))
    assert (equals(h[z][z], 0))
    assert (equals(h[x][y], 0.31902899408))
    assert (equals(h[y][z], 0.2103677462))
    assert (equals(h[x][z], 1.1697535323))
def test_trig_multivar_hessian_1():
    x, y, z = ad.Variable(), ad.Variable(), ad.Variable()
    f = ad.Sin(x * y) + z * ad.Cos(z * ad.Tan(1 / z))
    h = f.hessian({x: 1, y: 3, z: 5})
    assert (equals(h[x][x], -1.2700800725))
    assert (equals(h[y][y], -0.14112000805))
    assert (equals(h[z][z], -0.0050593837))
    assert (equals(h[x][y], -1.41335252))
    assert (equals(h[y][z], 0))
    assert (equals(h[x][z], 0))
Esempio n. 6
0
def test_exp_exceptions():
    x = ad.Variable('x')
    y = ad.Exp(x)
    z = ad.Cos(x)
    a = ad.Log(x)
    b = ad.Sin(x)
    f1 = y*2 + y
    f2 = z*2 + z
    f3 = a*2 + a
    f4 = b*2 + b
    assert np.isclose(f1.d_n(1, 1), 8.154845485377136)
    assert np.isclose(f2.d_n(1, 1), -2.5244129544236893)
    assert np.isclose(f3.d_n(1, 1), 3.0)
    assert np.isclose(f4.d_n(1, 1), 1.6209069176044193)
Esempio n. 7
0
def test_variable_inheritance_three():
    x = ad.Variable()
    y = ad.Variable()
    z = ad.Variable()

    f = ad.Cos(x) * y
    g = ad.Sin(f) + z * z * ad.Log(z) + 1
    assert (x in f.dep_vars)
    assert (y in f.dep_vars)
    assert (len(f.dep_vars) == 2)
    assert (y not in x.dep_vars)
    assert (x not in y.dep_vars)
    assert (len(g.dep_vars) == 3)
    assert (x in g.dep_vars)
    assert (y in g.dep_vars)
    assert (z in g.dep_vars)
    assert (z not in f.dep_vars)
Esempio n. 8
0
def test_inverse_trig_exceptions():
    x = ad.Variable('x')
    y = ad.Variable('y')
    f5 = ad.Arccos(x)
    f6 = ad.Arcsin(x)
    f7 = ad.Arctan(x)
    f8 = ad.Log(x)
    f9 = ad.Sin(x)
    assert f5._d_expr(y).eval({x:0}) == 0
    assert np.isclose(f5._d_expr(x).eval({x:0.5}), f5.d({x: 0.5}))
    assert f6._d_expr(y).eval({x:0}) == 0
    assert np.isclose(f6._d_expr(x).eval({x:0}), f6.d({x: 0}))
    assert f7._d_expr(y).eval({x:0}) == 0
    assert np.isclose(f7._d_expr(x).eval({x:0}), f7.d({x: 0}))
    assert f8._d_expr(y).eval({x:1}) == 0
    assert np.isclose(f8._d_expr(x).eval({x:2}), f8.d({x: 2}))
    assert f9._d_expr(y).eval({x:1}) == 0
    assert np.isclose(f9._d_expr(x).eval({x:2}), f9.d({x: 2}))
Esempio n. 9
0
def test_unop():
    x = ad.Variable("x")

    y = -x
    assert np.isclose(y.d_n(n=0, val=2.0), -2.0)
    assert np.isclose(y.d_n(n=1, val=2.0), -1.0)
    assert np.isclose(y.d_n(n=2, val=2.0), 0.0)

    y = ad.Sin(2 * x)
    assert np.isclose(y.d_n(n=0, val=0.0), 0.0)
    assert np.isclose(y.d_n(n=1, val=0.0), 2.0)
    assert np.isclose(y.d_n(n=3, val=0.0), -8.0)

    y = ad.Exp(3 * x)
    assert np.isclose(y.d_n(n=0, val=0.0), 1.0)
    assert np.isclose(y.d_n(n=1, val=0.0), 3.0)
    assert np.isclose(y.d_n(n=3, val=0.0), 27.0)

    y = ad.Log(2 * x)
    assert np.isclose(y.d_n(n=0, val=0.5), 0.0)
    assert np.isclose(y.d_n(n=1, val=0.5), 2.0)
    assert np.isclose(y.d_n(n=3, val=0.5), 2.0 / (0.5**3))
Esempio n. 10
0
def test_complex():
    x = ad.Variable("x")
    y = -12 * ad.Cos(x**2) + 8 * (x**3) * ad.Sin(x**2)
    yd5 = y.d_expr(5)
    assert np.isclose(y.d_n(5, 2.0), yd5.eval({x: 2.0}))
Esempio n. 11
0
def test_sine_expression():
    a = ad.Variable('a')
    b = ad.Sin(a)
    assert np.isclose(b.eval({a: pi/2}), 1)
    assert np.isclose(b.d({a: pi/2}), 0)
def test_addition():
    x = ad.Variable("x")
    y = x + ad.Sin(x)
    yd1 = y.d_expr()
    assert np.isclose(yd1.eval({x: 0.0}), 2.0)