def test_scipy_sparse(self): """Test cvxopt sparse interface. """ interface = intf.get_matrix_interface(sp.csc_matrix) # const_to_matrix mat = interface.const_to_matrix([1, 2, 3]) self.assertEqual(interface.shape(mat), (3, 1)) # C = cvxopt.spmatrix([1, 1, 1, 1, 1], [0, 1, 2, 0, 0, ], [0, 0, 0, 1, 2]) # mat = interface.const_to_matrix(C) # self.assertEqual(interface.shape(mat), (3, 3)) # identity mat = interface.identity(4) cmp_mat = interface.const_to_matrix(np.eye(4)) self.assertEqual(interface.shape(mat), interface.shape(cmp_mat)) assert (mat - cmp_mat).nnz == 0 # scalar_matrix mat = interface.scalar_matrix(2, (4, 3)) self.assertEqual(interface.shape(mat), (4, 3)) self.assertEqual(interface.index(mat, (1, 2)), 2) # reshape mat = interface.const_to_matrix([[1, 2, 3], [3, 4, 5]]) mat = interface.reshape(mat, (6, 1)) self.assertEqual(interface.index(mat, (4, 0)), 4) # Test scalars. scalar = interface.scalar_matrix(1, (1, 1)) self.assertEqual(type(scalar), np.ndarray) scalar = interface.scalar_matrix(1, (1, 3)) self.assertEqual(scalar.shape, (1, 3)) # index mat = interface.const_to_matrix([[1, 2, 3, 4], [3, 4, 5, 6]]) self.assertEqual(interface.index(mat, (0, 1)), 3) mat = interface.index(mat, (slice(1, 4, 2), slice(0, 2, None))) assert not (mat - np.array([[2, 4], [4, 6]])).any() # scalar value mat = sp.eye(1) self.assertEqual(intf.scalar_value(mat), 1.0) # Sign self.sign_for_intf(interface) # Complex # define sparse matrix [[0, 1j],[-1j,0]] row = np.array([0, 1]) col = np.array([1, 0]) data = np.array([1j, -1j]) A = sp.csr_matrix((data, (row, col)), shape=(2, 2)) mat = interface.const_to_matrix(A) self.assertEquals(mat[0, 1], 1j) self.assertEquals(mat[1, 0], -1j)
def _process_constr(self, constr, mat_cache, vert_offset): """Extract the coefficients from a constraint. Parameters ---------- constr : LinConstr The linear constraint to process. mat_cache : MatrixCache The cached version of the matrix-vector pair. vert_offset : int The row offset of the constraint. """ V, I, J = mat_cache.coo_tup coeffs = op2mat.get_coefficients(constr.expr) for id_, block in coeffs: vert_start = vert_offset vert_end = vert_start + constr.size[0]*constr.size[1] if id_ is lo.CONSTANT_ID: # Flatten the block. block = self.vec_intf.const_to_matrix(block) block_size = intf.size(block) block = self.vec_intf.reshape( block, (block_size[0]*block_size[1], 1) ) mat_cache.const_vec[vert_start:vert_end, :] += block else: horiz_offset = self.sym_data.var_offsets[id_] if intf.is_scalar(block): block = intf.scalar_value(block) V.append(block) I.append(vert_start) J.append(horiz_offset) else: # Block is a numpy matrix or # scipy CSC sparse matrix. if not intf.is_sparse(block): block = intf.DEFAULT_SPARSE_INTF.const_to_matrix( block ) block = block.tocoo() V.extend(block.data) I.extend(block.row + vert_start) J.extend(block.col + horiz_offset)
def test_scipy_sparse(self): interface = intf.get_matrix_interface(sp.csc_matrix) # const_to_matrix mat = interface.const_to_matrix([1, 2, 3]) self.assertEquals(interface.size(mat), (3, 1)) C = cvxopt.spmatrix([1, 1, 1, 1, 1], [ 0, 1, 2, 0, 0, ], [0, 0, 0, 1, 2]) mat = interface.const_to_matrix(C) self.assertEquals(interface.size(mat), (3, 3)) # identity mat = interface.identity(4) cmp_mat = interface.const_to_matrix(np.eye(4)) self.assertEquals(interface.size(mat), interface.size(cmp_mat)) assert (mat - cmp_mat).nnz == 0 # scalar_matrix mat = interface.scalar_matrix(2, 4, 3) self.assertEquals(interface.size(mat), (4, 3)) self.assertEquals(interface.index(mat, (1, 2)), 2) # reshape mat = interface.const_to_matrix([[1, 2, 3], [3, 4, 5]]) mat = interface.reshape(mat, (6, 1)) self.assertEquals(interface.index(mat, (4, 0)), 4) mat = interface.const_to_matrix(1, convert_scalars=True) self.assertEquals(type(interface.reshape(mat, (1, 1))), type(mat)) # Test scalars. scalar = interface.scalar_matrix(1, 1, 1) self.assertEquals(type(scalar), np.ndarray) scalar = interface.scalar_matrix(1, 1, 3) self.assertEquals(scalar.shape, (1, 3)) # index mat = interface.const_to_matrix([[1, 2, 3, 4], [3, 4, 5, 6]]) self.assertEquals(interface.index(mat, (0, 1)), 3) mat = interface.index(mat, (slice(1, 4, 2), slice(0, 2, None))) assert not (mat - np.matrix("2 4; 4 6")).any() # scalar value mat = sp.eye(1) self.assertEqual(intf.scalar_value(mat), 1.0) # Sign self.sign_for_intf(interface)
def value(self): # Catch the case when the expression is known to be # zero through DCP analysis. if self.is_zero(): result = intf.DEFAULT_INTERFACE.zeros(*self.size) else: arg_values = [] for arg in self.args: # A argument without a value makes all higher level # values None. if arg.value is None: return None else: arg_values.append(arg.value) result = self.numeric(arg_values) # Reduce to a scalar if possible. if intf.size(result) == (1, 1): return intf.scalar_value(result) else: return result
def _process_constr(self, constr, mat_cache, vert_offset): """Extract the coefficients from a constraint. Parameters ---------- constr : LinConstr The linear constraint to process. mat_cache : MatrixCache The cached version of the matrix-vector pair. vert_offset : int The row offset of the constraint. """ V, I, J = mat_cache.coo_tup coeffs = op2mat.get_coefficients(constr.expr) for id_, block in coeffs: vert_start = vert_offset vert_end = vert_start + constr.size[0] * constr.size[1] if id_ is lo.CONSTANT_ID: # Flatten the block. block = self.vec_intf.const_to_matrix(block) block_size = intf.size(block) block = self.vec_intf.reshape( block, (block_size[0] * block_size[1], 1)) mat_cache.const_vec[vert_start:vert_end, :] += block else: horiz_offset = self.sym_data.var_offsets[id_] if intf.is_scalar(block): block = intf.scalar_value(block) V.append(block) I.append(vert_start) J.append(horiz_offset) else: # Block is a numpy matrix or # scipy CSC sparse matrix. if not intf.is_sparse(block): block = intf.DEFAULT_SPARSE_INTERFACE.const_to_matrix( block) block = block.tocoo() V.extend(block.data) I.extend(block.row + vert_start) J.extend(block.col + horiz_offset)
def test_scipy_sparse(self): interface = intf.get_matrix_interface(sp.csc_matrix) # const_to_matrix mat = interface.const_to_matrix([1, 2, 3]) self.assertEqual(interface.size(mat), (3, 1)) # C = cvxopt.spmatrix([1, 1, 1, 1, 1], [0, 1, 2, 0, 0, ], [0, 0, 0, 1, 2]) # mat = interface.const_to_matrix(C) # self.assertEqual(interface.size(mat), (3, 3)) # identity mat = interface.identity(4) cmp_mat = interface.const_to_matrix(np.eye(4)) self.assertEqual(interface.size(mat), interface.size(cmp_mat)) assert (mat - cmp_mat).nnz == 0 # scalar_matrix mat = interface.scalar_matrix(2, 4, 3) self.assertEqual(interface.size(mat), (4, 3)) self.assertEqual(interface.index(mat, (1, 2)), 2) # reshape mat = interface.const_to_matrix([[1, 2, 3], [3, 4, 5]]) mat = interface.reshape(mat, (6, 1)) self.assertEqual(interface.index(mat, (4, 0)), 4) mat = interface.const_to_matrix(1, convert_scalars=True) self.assertEqual(type(interface.reshape(mat, (1, 1))), type(mat)) # Test scalars. scalar = interface.scalar_matrix(1, 1, 1) self.assertEqual(type(scalar), np.ndarray) scalar = interface.scalar_matrix(1, 1, 3) self.assertEqual(scalar.shape, (1, 3)) # index mat = interface.const_to_matrix([[1, 2, 3, 4], [3, 4, 5, 6]]) self.assertEqual(interface.index(mat, (0, 1)), 3) mat = interface.index(mat, (slice(1, 4, 2), slice(0, 2, None))) assert not (mat - np.matrix("2 4; 4 6")).any() # scalar value mat = sp.eye(1) self.assertEqual(intf.scalar_value(mat), 1.0) # Sign self.sign_for_intf(interface)
def _constr_matrix(self, constraints, var_offsets, x_length, matrix_intf, vec_intf): """Returns a matrix and vector representing a list of constraints. In the matrix, each constraint is given a block of rows. Each variable coefficient is inserted as a block with upper left corner at matrix[variable offset, constraint offset]. The constant term in the constraint is added to the vector. Parameters ---------- constraints : list A list of constraints. var_offsets : dict A dict of variable id to horizontal offset. x_length : int The length of the x vector. matrix_intf : interface The matrix interface to use for creating the constraints matrix. vec_intf : interface The matrix interface to use for creating the constant vector. Returns ------- tuple A (matrix, vector) tuple. """ rows = sum([c.size[0] * c.size[1] for c in constraints]) cols = x_length V, I, J = [], [], [] const_vec = vec_intf.zeros(rows, 1) vert_offset = 0 for constr in constraints: coeffs = op2mat.get_coefficients(constr.expr) for id_, block in coeffs: vert_start = vert_offset vert_end = vert_start + constr.size[0]*constr.size[1] if id_ is lo.CONSTANT_ID: # Flatten the block. block = self._DENSE_INTF.const_to_matrix(block) block_size = intf.size(block) block = self._DENSE_INTF.reshape( block, (block_size[0]*block_size[1], 1) ) const_vec[vert_start:vert_end, :] += block else: horiz_offset = var_offsets[id_] if intf.is_scalar(block): block = intf.scalar_value(block) V.append(block) I.append(vert_start) J.append(horiz_offset) else: # Block is a numpy matrix or # scipy CSC sparse matrix. if not intf.is_sparse(block): block = self._SPARSE_INTF.const_to_matrix(block) block = block.tocoo() V.extend(block.data) I.extend(block.row + vert_start) J.extend(block.col + horiz_offset) vert_offset += constr.size[0]*constr.size[1] # Create the constraints matrix. if len(V) > 0: matrix = sp.coo_matrix((V, (I, J)), (rows, cols)) # Convert the constraints matrix to the correct type. matrix = matrix_intf.const_to_matrix(matrix, convert_scalars=True) else: # Empty matrix. matrix = matrix_intf.zeros(rows, cols) return (matrix, -const_vec)
def extract_dual_value(result_vec, offset, constraint): value = result_vec[offset:offset + constraint.size] if constraint.size == 1: value = intf.scalar_value(value) offset += constraint.size return value, offset
def _constr_matrix(self, constraints, var_offsets, x_length, matrix_intf, vec_intf): """Returns a matrix and vector representing a list of constraints. In the matrix, each constraint is given a block of rows. Each variable coefficient is inserted as a block with upper left corner at matrix[variable offset, constraint offset]. The constant term in the constraint is added to the vector. Parameters ---------- constraints : list A list of constraints. var_offsets : dict A dict of variable id to horizontal offset. x_length : int The length of the x vector. matrix_intf : interface The matrix interface to use for creating the constraints matrix. vec_intf : interface The matrix interface to use for creating the constant vector. Returns ------- tuple A (matrix, vector) tuple. """ rows = sum([c.size[0] * c.size[1] for c in constraints]) cols = x_length V, I, J = [], [], [] const_vec = vec_intf.zeros(rows, 1) vert_offset = 0 for constr in constraints: coeffs = op2mat.get_coefficients(constr.expr) for id_, size, block in coeffs: vert_start = vert_offset vert_end = vert_start + constr.size[0] * constr.size[1] if id_ is lo.CONSTANT_ID: # Flatten the block. block = self._DENSE_INTF.const_to_matrix(block) block_size = intf.size(block) block = self._DENSE_INTF.reshape( block, (block_size[0] * block_size[1], 1)) const_vec[vert_start:vert_end, :] += block else: horiz_offset = var_offsets[id_] if intf.is_scalar(block): block = intf.scalar_value(block) V.append(block) I.append(vert_start) J.append(horiz_offset) else: # Block is a numpy matrix or # scipy CSC sparse matrix. if not intf.is_sparse(block): block = self._SPARSE_INTF.const_to_matrix(block) block = block.tocoo() V.extend(block.data) I.extend(block.row + vert_start) J.extend(block.col + horiz_offset) vert_offset += constr.size[0] * constr.size[1] # Create the constraints matrix. if len(V) > 0: matrix = sp.coo_matrix((V, (I, J)), (rows, cols)) # Convert the constraints matrix to the correct type. matrix = matrix_intf.const_to_matrix(matrix, convert_scalars=True) else: # Empty matrix. matrix = matrix_intf.zeros(rows, cols) return (matrix, -const_vec)