def test_non_quadratic(self): x = Variable() y = Variable() z = Variable() s = max(vstack([x, y, z]))**2 self.assertFalse(s.is_quadratic()) t = max(vstack([x**2, power(y, 2), z])) self.assertFalse(t.is_quadratic())
def test_non_quadratic(self): x = Variable() y = Variable() z = Variable() with self.assertRaises(Exception) as cm: (x*y*z).is_quadratic() self.assertEqual(str(cm.exception), "Cannot multiply UNKNOWN and AFFINE.") s = max_entries(vstack(x, y, z))**2 self.assertFalse(s.is_quadratic()) t = max_entries(vstack(x**2, power(y, 2), z)) self.assertFalse(t.is_quadratic())
def gm(t, x, y): length = t.size return SOC(t=reshape(x + y, (length, )), X=vstack( [reshape(x - y, (1, length)), reshape(2 * t, (1, length))]), axis=0)
def test_coefficients(self): exp = vstack(self.x).canonical_form[0] coeffs = exp.coefficients() self.assertEqual(coeffs.keys(), self.x.coefficients().keys()) exp = vstack(self.x, self.y).canonical_form[0] coeffs = exp.coefficients() self.assertItemsEqual(coeffs.keys(), self.x.coefficients().keys() + self.y.coefficients().keys()) for k, blocks in coeffs.items(): self.assertEqual(len(blocks), 1) for block in blocks: self.assertEqual(intf.size(block), (4, 2)) exp = vstack(self.A, self.B, self.C).canonical_form[0] coeffs = exp.coefficients() blocks = coeffs[self.A] self.assertEqual(len(blocks), 2) for block in blocks: self.assertEqual(intf.size(block), (10, 6))
def test_coefficients(self): exp = vstack(self.x).canonical_form[0] coeffs = exp.coefficients() self.assertEqual(coeffs.keys(), self.x.coefficients().keys()) exp = vstack(self.x, self.y).canonical_form[0] coeffs = exp.coefficients() self.assertItemsEqual(coeffs.keys(), self.x.coefficients().keys() + \ self.y.coefficients().keys()) for k, blocks in coeffs.items(): self.assertEqual(len(blocks), 1) for block in blocks: self.assertEqual(intf.size(block), (4, 2)) exp = vstack(self.A, self.B, self.C).canonical_form[0] coeffs = exp.coefficients() blocks = coeffs[self.A] self.assertEqual(len(blocks), 2) for block in blocks: self.assertEqual(intf.size(block), (10, 6))
def bmat(block_lists): """Constructs a block matrix. Takes a list of lists. Each internal list is stacked horizontally. The internal lists are stacked vertically. Parameters ---------- block_lists : list of lists The blocks of the block matrix. Return ------ CVXPY expression The CVXPY expression representing the block matrix. """ row_blocks = [hstack(*blocks) for blocks in block_lists] return vstack(*row_blocks)
def tv(value, *args): """Total variation of a vector, matrix, or list of matrices. Uses L1 norm of discrete gradients for vectors and L2 norm of discrete gradients for matrices. Parameters ---------- value : Expression or numeric constant The value to take the total variation of. args : Matrix constants/expressions Additional matrices extending the third dimension of value. Returns ------- Expression An Expression representing the total variation. """ # Accept single list as argument. if isinstance(value, list) and len(args) == 0: args = value[1:] value = value[0] value = Expression.cast_to_const(value) rows, cols = value.size if value.is_scalar(): raise ValueError("tv cannot take a scalar argument.") # L1 norm for vectors. elif value.is_vector(): return norm(value[1:] - value[0:max(rows, cols)-1], 1) # L2 norm for matrices. else: args = list(map(Expression.cast_to_const, args)) values = [value] + list(args) diffs = [] for mat in values: diffs += [ mat[0:rows-1, 1:cols] - mat[0:rows-1, 0:cols-1], mat[1:rows, 0:cols-1] - mat[0:rows-1, 0:cols-1], ] length = diffs[0].size[0]*diffs[1].size[1] stacked = vstack(*[reshape(diff, 1, length) for diff in diffs]) return sum_entries(norm(stacked, p='fro', axis=0))
def tv(value, *args): """Total variation of a vector, matrix, or list of matrices. Uses L1 norm of discrete gradients for vectors and L2 norm of discrete gradients for matrices. Parameters ---------- value : Expression or numeric constant The value to take the total variation of. args : Matrix constants/expressions Additional matrices extending the third dimension of value. Returns ------- Expression An Expression representing the total variation. """ # Accept single list as argument. if isinstance(value, list) and len(args) == 0: args = value[1:] value = value[0] value = Expression.cast_to_const(value) rows, cols = value.size if value.is_scalar(): raise ValueError("tv cannot take a scalar argument.") # L1 norm for vectors. elif value.is_vector(): return norm(value[1:] - value[0:max(rows, cols)-1], 1) # L2 norm for matrices. else: args = map(Expression.cast_to_const, args) values = [value] + list(args) diffs = [] for mat in values: diffs += [ mat[0:rows-1, 1:cols] - mat[0:rows-1, 0:cols-1], mat[1:rows, 0:cols-1] - mat[0:rows-1, 0:cols-1], ] length = diffs[0].size[0]*diffs[1].size[1] stacked = vstack(*[reshape(diff, 1, length) for diff in diffs]) return sum_entries(norm(stacked, p='fro', axis=0))
def tvnorm2d(value, Dx, Dy): """Total variation of a vector, matrix, or list of matrices. Uses L1 norm of discrete gradients for vectors and L2 norm of discrete gradients for matrices. Parameters ---------- value : Expression or numeric constant The value to take the total variation of. Returns ------- Expression An Expression representing the total variation. """ value = Expression.cast_to_const(value) len = value.size[0] diffs = [Dx * value, Dy * value] stack = vstack(*[reshape(diff, 1, len) for diff in diffs]) return sum_entries(cvxnorm(stack, p='fro', axis=0))
def tv(value, *args): """Total variation of a vector, matrix, or list of matrices. Uses L1 norm of discrete gradients for vectors and L2 norm of discrete gradients for matrices. Parameters ---------- value : Expression or numeric constant The value to take the total variation of. args : Matrix constants/expressions Additional matrices extending the third dimension of value. Returns ------- Expression An Expression representing the total variation. """ value = Expression.cast_to_const(value) if value.ndim == 0: raise ValueError("tv cannot take a scalar argument.") # L1 norm for vectors. elif value.ndim == 1: return norm(value[1:] - value[0:value.shape[0]-1], 1) # L2 norm for matrices. else: rows, cols = value.shape args = map(Expression.cast_to_const, args) values = [value] + list(args) diffs = [] for mat in values: diffs += [ mat[0:rows-1, 1:cols] - mat[0:rows-1, 0:cols-1], mat[1:rows, 0:cols-1] - mat[0:rows-1, 0:cols-1], ] length = diffs[0].shape[0]*diffs[1].shape[1] stacked = vstack([reshape(diff, (1, length)) for diff in diffs]) return sum(norm(stacked, p=2, axis=0))
def tvnorm_anisotropic_2d(signal, Dx, Dy): magnitudes = pnorm(signal, 2, axis=1) diffs = [Dx * magnitudes, Dy * magnitudes] stack = vstack(*[reshape(diff, 1, magnitudes.size[0]) for diff in diffs]) return sum_entries(pnorm(stack, 2, axis=0))
def test_variables(self): exp, constr = vstack(self.x, self.y, self.x + self.y).canonical_form self.assertEquals(constr, []) self.assertItemsEqual(exp.variables(), [self.x, self.y]) exp = vstack(self.A, self.B, self.C).canonical_form[0] self.assertItemsEqual(exp.variables(), [self.A, self.B])