'Test gradient for pnorm'

def test_pnorm(self):

    expr = pnorm(self.x, 1)
    self.x.value = [(-1), 0]
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), [(-1), 0])
    self.x.value = [0, 10]
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), [0, 1])
    expr = pnorm(self.x, 2)
    self.x.value = [(-3), 4]
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), np.matrix([[((-3.0) / 5)], [(4.0 / 5)]]))
    expr = pnorm(self.x, 0.5)
    self.x.value = [(-1), 2]
    self.assertAlmostEqual(expr.grad[self.x], None)
    expr = pnorm(self.x, 0.5)
    self.x.value = [0, 0]
    self.assertAlmostEqual(expr.grad[self.x], None)
    expr = pnorm(self.x, 2)
    self.x.value = [0, 0]
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), [0, 0])
    expr = pnorm(self.x, 2, axis=1)
    self.x.value = [1, 2]
    val = np.eye(2)
    self.assertItemsAlmostEqual(expr.grad[self.x].todense(), val)
    expr = pnorm(self.A, 2)
    self.A.value = np.matrix([[2, (-2)], [2, 2]])
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), [0.5, 0.5, (-0.5), 0.5])
    expr = pnorm(self.A, 2, axis=0)
    self.A.value = np.matrix([[3, (-3)], [4, 4]])
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), np.matrix([[0.6, 0], [0.8, 0], [0, (-0.6)], [0, 0.8]]))
    expr = pnorm(self.A, 2, axis=1)
    self.A.value = np.matrix([[3, (-4)], [4, 3]])
    self.assertItemsAlmostEqual(expr.grad[self.A].todense(), np.matrix([[0.6, 0], [0, 0.8], [(-0.8), 0], [0, 0.6]]))
    expr = pnorm(self.A, 0.5)
    self.A.value = np.matrix([[3, (-4)], [4, 3]])
    self.assertAlmostEqual(expr.grad[self.A], None)
