def test_updateMultipliers(self): nP = 10 m = np.random.rand(nP) W1 = sdiag(np.random.rand(nP)) W2 = sdiag(np.random.rand(nP)) phi1 = objectivefunction.L2ObjectiveFunction(W=W1) phi2 = objectivefunction.L2ObjectiveFunction(W=W2) phi = phi1 + phi2 self.assertTrue(phi(m) == phi1(m) + phi2(m)) phi.multipliers[0] = Zero() self.assertTrue(phi(m) == phi2(m)) phi.multipliers[0] = 1. phi.multipliers[1] = Zero() self.assertTrue(len(phi.objfcts) == 2) self.assertTrue(len(phi.multipliers) == 2) self.assertTrue(len(phi) == 2) self.assertTrue(phi(m) == phi1(m))
def test_2sum(self): nP = 80 alpha1 = 100 alpha2 = 200 phi1 = ( objectivefunction.L2ObjectiveFunction( W=sdiag(np.random.rand(nP)) ) + alpha1 * objectivefunction.L2ObjectiveFunction() ) phi2 = objectivefunction.L2ObjectiveFunction() + alpha2 * phi1 self.assertTrue(phi2.test(eps=EPS)) self.assertTrue(len(phi1.multipliers) == 2) self.assertTrue(len(phi2.multipliers) == 2) self.assertTrue(len(phi1.objfcts) == 2) self.assertTrue(len(phi2.objfcts) == 2) self.assertTrue(len(phi2) == 2) self.assertTrue(len(phi1) == 2) self.assertTrue(len(phi2) == 2) self.assertTrue(np.all(phi1.multipliers == np.r_[1., alpha1])) self.assertTrue(np.all(phi2.multipliers == np.r_[1., alpha2]))
def test_early_exits(self): nP = 10 m = np.random.rand(nP) v = np.random.rand(nP) W1 = sdiag(np.random.rand(nP)) phi1 = objectivefunction.L2ObjectiveFunction(W=W1) phi2 = Error_if_Hit_ObjFct() with self.assertRaises(Exception): phi2(m) with self.assertRaises(Exception): phi2.deriv(m) with self.assertRaises(Exception): phi2.deriv2(m) objfct = phi1 + 0 * phi2 self.assertTrue(len(objfct) == 2) self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0])) self.assertTrue(objfct(m) == phi1(m)) self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m))) self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v))) objfct.multipliers[1] = Zero() self.assertTrue(len(objfct) == 2) self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0])) self.assertTrue(objfct(m) == phi1(m)) self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m))) self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v)))
def test_zero(self): z = Zero() assert z == 0 assert not (z < 0) assert z <= 0 assert not (z > 0) assert z >= 0 assert +z == z assert -z == z assert z + 1 == 1 assert z + 3 + z == 3 assert z - 3 == -3 assert z - 3 - z == -3 assert 3 * z == 0 assert z * 3 == 0 assert z / 3 == 0 a = 1 a += z assert a == 1 a = 1 a += z assert a == 1 self.assertRaises(ZeroDivisionError, lambda: 3 / z) assert mkvc(z) == 0 assert sdiag(z) * a == 0 assert z.T == 0 assert z.transpose() == 0
def test_sum_fail(self): nP1 = 10 nP2 = 30 phi1 = objectivefunction.L2ObjectiveFunction( W=sdiag(np.random.rand(nP1)) ) phi2 = objectivefunction.L2ObjectiveFunction( W=sdiag(np.random.rand(nP2)) ) with self.assertRaises(Exception): phi1 + phi2 with self.assertRaises(Exception): phi1 + 100 * phi2
def test_mat_shape(self): o = Identity() S = sdiag(np.r_[2, 3])[:1, :] self.assertRaises(ValueError, lambda: S + o) def check(exp, ans): assert np.all((exp).todense() == ans) check(S * o, [[2, 0]]) check(S * -o, [[-2, 0]])
def test_scalarmul(self): scalar = 10. nP = 100 objfct_a = objectivefunction.L2ObjectiveFunction( W=sdiag(np.random.randn(nP)) ) objfct_b = scalar * objfct_a m = np.random.rand(nP) objfct_c = objfct_a + objfct_b self.assertTrue(scalar * objfct_a(m) == objfct_b(m)) self.assertTrue(objfct_b.test()) self.assertTrue(objfct_c(m) == objfct_a(m) + objfct_b(m)) self.assertTrue(len(objfct_c.objfcts) == 2) self.assertTrue(len(objfct_c.multipliers) == 2) self.assertTrue(len(objfct_c) == 2)
def test_mat_one(self): o = Identity() S = sdiag(np.r_[2, 3]) def check(exp, ans): assert np.all((exp).todense() == ans) check(S * o, [[2, 0], [0, 3]]) check(o * S, [[2, 0], [0, 3]]) check(S * -o, [[-2, 0], [0, -3]]) check(-o * S, [[-2, 0], [0, -3]]) check(S / o, [[2, 0], [0, 3]]) check(S / -o, [[-2, 0], [0, -3]]) self.assertRaises(NotImplementedError, lambda: o / S) check(S + o, [[3, 0], [0, 4]]) check(o + S, [[3, 0], [0, 4]]) check(S - o, [[1, 0], [0, 2]]) check(S + -o, [[1, 0], [0, 2]]) check(-o + S, [[1, 0], [0, 2]])
def test_invXXXBlockDiagonal(self): a = [np.random.rand(5, 1) for i in range(4)] B = inv2X2BlockDiagonal(*a) A = sp.vstack((sp.hstack( (sdiag(a[0]), sdiag(a[1]))), sp.hstack( (sdiag(a[2]), sdiag(a[3]))))) Z2 = B * A - sp.identity(10) self.assertTrue(np.linalg.norm(Z2.todense().ravel(), 2) < TOL) a = [np.random.rand(5, 1) for i in range(9)] B = inv3X3BlockDiagonal(*a) A = sp.vstack((sp.hstack((sdiag(a[0]), sdiag(a[1]), sdiag(a[2]))), sp.hstack((sdiag(a[3]), sdiag(a[4]), sdiag(a[5]))), sp.hstack((sdiag(a[6]), sdiag(a[7]), sdiag(a[8]))))) Z3 = B * A - sp.identity(15) self.assertTrue(np.linalg.norm(Z3.todense().ravel(), 2) < TOL)
def test_mat_zero(self): z = Zero() S = sdiag(np.r_[2, 3]) assert S * z == 0