'Test gradient for log.'

def test_log(self):

    expr = log(self.a)
    self.a.value = 2
    self.assertAlmostEqual(expr.grad[self.a], (1.0 / 2))
    self.a.value = 3
    self.assertAlmostEqual(expr.grad[self.a], (1.0 / 3))
    self.a.value = (-1)
    self.assertAlmostEqual(expr.grad[self.a], None)
    expr = log(self.x)
    self.x.value = [3, 4]
    val = (np.zeros((2, 2)) + np.diag([(1 / 3), (1 / 4)]))
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    expr = log(self.x)
    self.x.value = [(-1e-09), 4]
    self.assertAlmostEqual(expr.grad[self.x], None)
    expr = log(self.A)
    self.A.value = [[1, 2], [3, 4]]
    val = (np.zeros((4, 4)) + np.diag([1, (1 / 2), (1 / 3), (1 / 4)]))
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), val)
