'Test grad for affine atoms.'

def test_affine(self):

    expr = (- self.a)
    self.a.value = 2
    self.assertAlmostEqual(expr.grad[self.a], (-1))
    expr = (2 * self.a)
    self.a.value = 2
    self.assertAlmostEqual(expr.grad[self.a], 2)
    expr = (self.a / 2)
    self.a.value = 2
    self.assertAlmostEqual(expr.grad[self.a], 0.5)
    expr = (- self.x)
    self.x.value = [3, 4]
    val = (np.zeros((2, 2)) - np.diag([1, 1]))
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    expr = (- self.A)
    self.A.value = [[1, 2], [3, 4]]
    val = (np.zeros((4, 4)) - np.diag([1, 1, 1, 1]))
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), val)
    expr = self.A[(0, 1)]
    self.A.value = [[1, 2], [3, 4]]
    val = np.zeros((4, 1))
    val[2] = 1
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), val)
    z = Variable(3)
    expr = vstack(self.x, z)
    self.x.value = [1, 2]
    z.value = [1, 2, 3]
    val = np.zeros((2, 5))
    val[:, 0:2] = np.eye(2)
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    val = np.zeros((3, 5))
    val[:, 2:] = np.eye(3)
    self.assertItemsAlmostEqual(expr.grad[z].todense(), val)
    expr = cumsum(self.x)
    self.x.value = [1, 2]
    val = np.ones((2, 2))
    val[(1, 0)] = 0
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    expr = cumsum(self.x, axis=1)
    self.x.value = [1, 2]
    val = np.eye(2)
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
