def test_gmatmul(self): x = cp.Variable(2, pos=True) A = cp.Parameter(shape=(2, 2)) A.value = np.array([[-5, 2], [1, -3]]) b = np.array([3, 2]) expr = cp.gmatmul(A, x) problem = cp.Problem(cp.Minimize(1.0), [expr == b]) self.assertTrue(problem.is_dgp(dpp=True)) problem.solve(gp=True, enforce_dpp=True) sltn = np.exp(np.linalg.solve(A.value, np.log(b))) self.assertItemsAlmostEqual(x.value, sltn) x_par = cp.Parameter(2, pos=True) expr = cp.gmatmul(A, x_par) self.assertFalse(expr.is_dgp(dpp=True)) self.assertTrue(expr.is_dgp(dpp=False))
def test_gmatmul(self): x = cvxpy.Variable(2, pos=True) A = np.array([[-5., 2.], [1., -3.]]) b = np.array([3, 2]) expr = cvxpy.gmatmul(A, x) x.value = b self.assertItemsAlmostEqual(expr.value, [3**-5 * 2**2, 3. / 8]) A_par = cvxpy.Parameter((2, 2), value=A) self.assertItemsAlmostEqual( cvxpy.gmatmul(A_par, x).value, [3**-5 * 2**2, 3. / 8]) x.value = None prob = cvxpy.Problem(cvxpy.Minimize(1.0), [expr == b]) prob.solve(gp=True) sltn = np.exp(np.linalg.solve(A, np.log(b))) self.assertItemsAlmostEqual(x.value, sltn)
def test_gmatmul(self): x = cvxpy.Variable(2, pos=True) A = cvxpy.Variable((2, 2)) with self.assertRaises(Exception) as cm: cvxpy.gmatmul(A, x) self.assertTrue(str(cm.exception) == "gmatmul(A, X) requires that A be constant.") x = cvxpy.Variable(2) A = np.ones((4, 2)) with self.assertRaises(Exception) as cm: cvxpy.gmatmul(A, x) self.assertTrue(str(cm.exception) == "gmatmul(A, X) requires that X be positive.") x = cvxpy.Variable(3, pos=True) A = np.ones((4, 3)) gmatmul = cvxpy.gmatmul(A, x) self.assertTrue(gmatmul.is_dgp()) self.assertTrue(gmatmul.is_log_log_affine()) self.assertTrue(gmatmul.is_log_log_convex()) self.assertTrue(gmatmul.is_log_log_concave()) self.assertTrue(gmatmul.is_nonneg()) self.assertTrue(gmatmul.is_incr(0)) self.assertTrue(cvxpy.gmatmul(-A, x).is_decr(0)) x = cvxpy.Variable((2, 3), pos=True) A = np.array([[2., -1.], [0., 3.]]) gmatmul = cvxpy.gmatmul(A, x) self.assertTrue(gmatmul.is_dgp()) self.assertTrue(gmatmul.is_log_log_affine()) self.assertTrue(gmatmul.is_log_log_convex()) self.assertTrue(gmatmul.is_log_log_concave()) self.assertFalse(gmatmul.is_incr(0)) self.assertFalse(gmatmul.is_decr(0))