def test_transpose(self): """Test transpose op and coefficients. """ size = (5, 4) x = create_var(size) expr = transpose(x) self.assertEqual(expr.size, (4, 5)) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat*test_mat).reshape((4, 5), order='F'), test_mat.reshape(size, order='F').T)
def test_transpose(self): """Test transpose op and coefficients. """ size = (5, 4) x = create_var(size) expr = transpose(x) self.assertEqual(expr.size, (4, 5)) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat * test_mat).reshape((4, 5), order='F'), test_mat.reshape(size, order='F').T)
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 _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 _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 test_index(self): """Test the get_coefficients function for index. """ size = (5, 4) # Eye key = (slice(0,2,None), slice(0,2,None)) x = create_var(size) expr = index(x, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(id_, x.data) self.assertEqual(mat.shape, (4, 20)) test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'), test_mat.reshape(size, order='F')[key]) # Eye with scalar mult. key = (slice(0,2,None), slice(0,2,None)) x = create_var(size) A = create_const(5, (1, 1)) expr = mul_expr(A, x, size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'), 5*test_mat.reshape(size, order='F')[key]) # Promoted key = (slice(0,2,None), slice(0,2,None)) x = create_var((1, 1)) value = np.array(range(20)).reshape(size) A = create_const(value, size) prom_x = promote(x, (size[1], 1)) expr = mul_expr(A, diag_vec(prom_x), size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (4, 1)) self.assertItemsAlmostEqual(mat, value[key]) # Normal size = (5, 5) key = (slice(0,2,None), slice(0,1,None)) x = create_var((5, 1)) A = create_const(np.ones(size), size) expr = mul_expr(A, x, (5, 1)) expr = index(expr, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 5)) self.assertItemsAlmostEqual(mat.todense(), A.data[slice(0,2,None)]) # Blocks size = (5, 5) key = (slice(0,2,None), slice(0,2,None)) x = create_var(size) value = np.array(range(25)).reshape(size) A = create_const(value, size) expr = mul_expr(A, x, size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (4, 25)) test_mat = np.mat(range(25)).T self.assertItemsAlmostEqual((mat*test_mat).reshape((2, 2), order='F'), (A.data*test_mat.reshape(size, order='F'))[key]) # Scalar constant size = (1, 1) A = create_const(5, size) key = (slice(0,1,None), slice(0,1,None)) expr = index(A, (1, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(intf.size(mat), (1, 1)) self.assertEqual(mat, 5) # Dense constant size = (5, 4) key = (slice(0,2,None), slice(0,1,None)) value = np.array(range(20)).reshape(size) A = create_const(value, size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, value[key]) # Sparse constant size = (5, 5) key = (slice(0,2,None), slice(0,1,None)) A = create_const(sp.eye(5), size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, sp.eye(5).todense()[key]) # Parameter size = (5, 4) key = (slice(0,2,None), slice(0,1,None)) param = Parameter(*size) value = np.array(range(20)).reshape(size) param.value = value A = create_param(param, size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, param.value[key])
def test_get_coefficients(self): """Test the get_coefficients function. """ size = (5, 4) # Eye x = create_var(size) coeffs = get_coefficients(x) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(id_, x.data) self.assertItemsAlmostEqual(mat.todense(), sp.eye(20).todense()) # Eye with scalar mult. x = create_var(size) A = create_const(5, (1, 1)) coeffs = get_coefficients(mul_expr(A, x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertItemsAlmostEqual(mat.todense(), 5*sp.eye(20).todense()) # Promoted x = create_var((1, 1)) coeffs = get_coefficients(promote(x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (20, 1)) self.assertItemsAlmostEqual(mat, np.ones((20, 1))) # Normal size = (5, 5) x = create_var((5, 1)) A = create_const(np.ones(size), size) coeffs = get_coefficients(mul_expr(A, x, (5, 1))) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (5, 5)) self.assertItemsAlmostEqual(mat.todense(), A.data) # Blocks size = (5, 5) x = create_var(size) A = create_const(np.ones(size), size) coeffs = get_coefficients(mul_expr(A, x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (25, 25)) self.assertItemsAlmostEqual(mat.todense(), sp.block_diag(5*[np.ones(size)]).todense()) # Scalar constant size = (1, 1) A = create_const(5, size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(intf.size(mat), (1, 1)) self.assertEqual(mat, 5) # Dense constant size = (5, 4) A = create_const(np.ones(size), size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0]*size[1], 1)) self.assertItemsAlmostEqual(mat, np.ones(size)) # Sparse constant size = (5, 5) A = create_const(sp.eye(5), size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0]*size[1], 1)) self.assertItemsAlmostEqual(mat, sp.eye(5).todense()) # Parameter size = (5, 4) param = Parameter(*size) param.value = np.ones(size) A = create_param(param, size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0]*size[1], 1)) self.assertItemsAlmostEqual(mat, param.value)
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)
def test_index(self): """Test the get_coefficients function for index. """ size = (5, 4) # Eye key = (slice(0, 2, None), slice(0, 2, None)) x = create_var(size) expr = index(x, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(id_, x.data) self.assertEqual(mat.shape, (4, 20)) test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat * test_mat).reshape((2, 2), order='F'), test_mat.reshape(size, order='F')[key]) # Eye with scalar mult. key = (slice(0, 2, None), slice(0, 2, None)) x = create_var(size) A = create_const(5, (1, 1)) expr = mul_expr(A, x, size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] test_mat = np.mat(range(20)).T self.assertItemsAlmostEqual((mat * test_mat).reshape( (2, 2), order='F'), 5 * test_mat.reshape(size, order='F')[key]) # Promoted key = (slice(0, 2, None), slice(0, 2, None)) x = create_var((1, 1)) value = np.array(range(20)).reshape(size) A = create_const(value, size) prom_x = promote(x, (size[1], 1)) expr = mul_expr(A, diag_vec(prom_x), size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (4, 1)) self.assertItemsAlmostEqual(mat, value[key]) # Normal size = (5, 5) key = (slice(0, 2, None), slice(0, 1, None)) x = create_var((5, 1)) A = create_const(np.ones(size), size) expr = mul_expr(A, x, (5, 1)) expr = index(expr, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 5)) self.assertItemsAlmostEqual(mat.todense(), A.data[slice(0, 2, None)]) # Blocks size = (5, 5) key = (slice(0, 2, None), slice(0, 2, None)) x = create_var(size) value = np.array(range(25)).reshape(size) A = create_const(value, size) expr = mul_expr(A, x, size) expr = index(expr, (2, 2), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (4, 25)) test_mat = np.mat(range(25)).T self.assertItemsAlmostEqual( (mat * test_mat).reshape((2, 2), order='F'), (A.data * test_mat.reshape(size, order='F'))[key]) # Scalar constant size = (1, 1) A = create_const(5, size) key = (slice(0, 1, None), slice(0, 1, None)) expr = index(A, (1, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(intf.size(mat), (1, 1)) self.assertEqual(mat, 5) # Dense constant size = (5, 4) key = (slice(0, 2, None), slice(0, 1, None)) value = np.array(range(20)).reshape(size) A = create_const(value, size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, value[key]) # Sparse constant size = (5, 5) key = (slice(0, 2, None), slice(0, 1, None)) A = create_const(sp.eye(5), size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, sp.eye(5).todense()[key]) # Parameter size = (5, 4) key = (slice(0, 2, None), slice(0, 1, None)) param = Parameter(*size) value = np.array(range(20)).reshape(size) param.value = value A = create_param(param, size) expr = index(A, (2, 1), key) coeffs = get_coefficients(expr) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (2, 1)) self.assertItemsAlmostEqual(mat, param.value[key])
def test_get_coefficients(self): """Test the get_coefficients function. """ size = (5, 4) # Eye x = create_var(size) coeffs = get_coefficients(x) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(id_, x.data) self.assertItemsAlmostEqual(mat.todense(), sp.eye(20).todense()) # Eye with scalar mult. x = create_var(size) A = create_const(5, (1, 1)) coeffs = get_coefficients(mul_expr(A, x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertItemsAlmostEqual(mat.todense(), 5 * sp.eye(20).todense()) # Promoted x = create_var((1, 1)) coeffs = get_coefficients(promote(x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (20, 1)) self.assertItemsAlmostEqual(mat, np.ones((20, 1))) # Normal size = (5, 5) x = create_var((5, 1)) A = create_const(np.ones(size), size) coeffs = get_coefficients(mul_expr(A, x, (5, 1))) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (5, 5)) self.assertItemsAlmostEqual(mat.todense(), A.data) # Blocks size = (5, 5) x = create_var(size) A = create_const(np.ones(size), size) coeffs = get_coefficients(mul_expr(A, x, size)) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (25, 25)) self.assertItemsAlmostEqual( mat.todense(), sp.block_diag(5 * [np.ones(size)]).todense()) # Scalar constant size = (1, 1) A = create_const(5, size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(intf.size(mat), (1, 1)) self.assertEqual(mat, 5) # Dense constant size = (5, 4) A = create_const(np.ones(size), size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0] * size[1], 1)) self.assertItemsAlmostEqual(mat, np.ones(size)) # Sparse constant size = (5, 5) A = create_const(sp.eye(5), size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0] * size[1], 1)) self.assertItemsAlmostEqual(mat, sp.eye(5).todense()) # Parameter size = (5, 4) param = Parameter(*size) param.value = np.ones(size) A = create_param(param, size) coeffs = get_coefficients(A) assert len(coeffs) == 1 id_, mat = coeffs[0] self.assertEqual(mat.shape, (size[0] * size[1], 1)) self.assertItemsAlmostEqual(mat, param.value)