def test_ConstantFunction(self): M, N, K = 3,4,5 ig = ImageGeometry(M, N, K) tmp = ig.allocate('random_int') constant = 10 f = ConstantFunction(constant) # check call res1 = f(tmp) self.assertAlmostEqual(res1, constant) # check gradient with and without out res1 = f.gradient(tmp) out = ig.allocate() self.assertNumpyArrayAlmostEqual(res1.as_array(), out) out1 = ig.allocate() f.gradient(tmp, out=out1) self.assertNumpyArrayAlmostEqual(res1.as_array(), out1) # check convex conjugate res1 = f.convex_conjugate(tmp) res2 = tmp.maximum(0).sum() self.assertNumpyArrayAlmostEqual(res1.as_array(), res2.as_array()) # check proximal tau = 0.4 res1 = f.proximal(tmp, tau) self.assertNumpyArrayAlmostEqual(res1.as_array(), tmp.as_array())
def test_ConstantFunction(self): k = ConstantFunction(constant=1) ig = ImageGeometry(1,2,3) x = ig.allocate(2) grad = k.gradient(x) out = ig.allocate(-1) k.gradient(x, out=out) #out.fill(-3) self.assertNumpyArrayEqual(numpy.zeros(x.shape), grad.as_array()) self.assertNumpyArrayEqual(out.as_array(), grad.as_array())