'Test domain for max_elemwise.'

def test_max_elemwise(self):

    b = Variable()
    expr = max_elemwise(self.a, b)
    self.a.value = 2
    b.value = 4
    self.assertAlmostEqual(expr.grad[self.a], 0)
    self.assertAlmostEqual(expr.grad[b], 1)
    self.a.value = 3
    b.value = 0
    self.assertAlmostEqual(expr.grad[self.a], 1)
    self.assertAlmostEqual(expr.grad[b], 0)
    self.a.value = (-1)
    b.value = 2
    self.assertAlmostEqual(expr.grad[self.a], 0)
    self.assertAlmostEqual(expr.grad[b], 1)
    y = Variable(2)
    expr = max_elemwise(self.x, y)
    self.x.value = [3, 4]
    y.value = [5, (-5)]
    val = (np.zeros((2, 2)) + np.diag([0, 1]))
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    val = (np.zeros((2, 2)) + np.diag([1, 0]))
    self.assertItemsAlmostEqual(expr.grad[y].todense(), val)
    expr = max_elemwise(self.x, y)
    self.x.value = [(-1e-09), 4]
    y.value = [1, 4]
    val = (np.zeros((2, 2)) + np.diag([0, 1]))
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    val = (np.zeros((2, 2)) + np.diag([1, 0]))
    self.assertItemsAlmostEqual(expr.grad[y].todense(), val)
    expr = max_elemwise(self.A, self.B)
    self.A.value = [[1, 2], [3, 4]]
    self.B.value = [[5, 1], [3, 2.3]]
    div = (self.A.value / self.B.value).A.ravel(order='F')
    val = (np.zeros((4, 4)) + np.diag([0, 1, 1, 1]))
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), val)
    val = (np.zeros((4, 4)) + np.diag([1, 0, 0, 0]))
    self.assertItemsAlmostEqual(expr.grad[self.B].todense(), val)
