Beispiel #1
0
def test_vector_constant_addition():
    a = ad.Constant(2.0 * np.ones(10))
    b = ad.Constant(3.0 * np.ones(10))
    a.eval({}) == 2.0 * np.ones(10)
    b.eval({}) == 3.0 * np.ones(10)
    c = a + b
    assert equals(c.eval({}), 5.0 * np.ones(10))
def test_constant_division():
    a = ad.Constant(5)
    a2 = ad.Constant(-5)
    b = ad.Variable('b')
    c = a / b
    d = b / a2
    assert c.eval({b: 5}) == 1
    assert d.eval({b: 5}) == -1
def test_constant_multiplication():
    a = ad.Constant(5)
    a2 = ad.Constant(-5)
    b = ad.Variable('b')
    c = a * b
    d = b * a2
    assert c.eval({b: 5}) == 25
    assert d.eval({b: 5}) == -25
def test_constant_subtraction():
    a = ad.Constant(5)
    a2 = ad.Constant(-5)
    b = ad.Variable('b')
    c = a - b
    d = b - a2
    assert c.eval({b: 5}) == 0
    assert d.eval({b: 5}) == 10
def test_constant_addition():
    a = ad.Constant(5)
    b = ad.Variable('b')
    c = a + b
    assert c.eval({b: 5}) == 10
Beispiel #6
0
def test_vector_constant_subtraction():
    a = ad.Constant(2.0 * np.ones(10))
    b = ad.Constant(3.0 * np.ones(10))
    c = a - b
    assert equals(c.eval({}), -np.ones(10))
Beispiel #7
0
def test_constant_hessian():
    """Checks the trivial case"""
    constant_5 = ad.Constant(5.1231)
    assert (constant_5.hessian({}) == 0)
Beispiel #8
0
def test_constant_simple_op_hessian():
    c1 = ad.Constant(50)
    c2 = ad.Constant(123)
    c3 = ad.Constant(500)
    f = c1 * c2 + c1 / c3 + (1 - c2) * (-c3)
    assert (f.hessian({}) == 0)
Beispiel #9
0
def test_constant_exceptions():
    x = ad.Constant('x')
    y = ad.Variable()
    assert x._d_expr(y).eval({}) == 0
 def test_casted_constants_right(self):
     c1 = ad.Constant(1.0)
     self.assertEqual((5 - c1).eval({}), 4.0)
     self.assertEqual((1 - c1).eval({}), 0)
     self.assertEqual(((-5) - c1).eval({}), -6.0)
 def test_casted_constants_left(self):
     c1 = ad.Constant(1.0)
     self.assertEqual((c1 - 5).eval({}), -4.0)
     self.assertEqual((c1 - 1).eval({}), 0)
     self.assertEqual((c1 - (-5)).eval({}), 6.0)
 def test_constants(self):
     c1 = ad.Constant(1.0)
     c2 = ad.Constant(5.0)
     y = c1 - c2
     self.assertEqual(-4.0, y.eval({}))
Beispiel #13
0
def test_constant_eval():
    c = ad.Constant(5)
    assert c.eval({}) == 5
    assert c.d({}) == 0