Utils

Warning

This page of the documentation is unfinished, and may significantly change in future versions.

Constraints and templates

class popcor.utils.constraint.Constraint(index: int = 0, polyrow_a: PolyMatrix | None = None, polyrow_b: PolyMatrix | None = None, A_poly: PolyMatrix | None = None, A_sparse: csr_matrix | csc_matrix | None = None, b: ndarray | csr_matrix | csc_matrix | None = None, a: ndarray | csr_matrix | csc_matrix | None = None, a_full: ndarray | csr_matrix | csc_matrix | None = None, b_full: ndarray | csr_matrix | csc_matrix | None = None, mat_var_dict: dict | list | None = None, mat_param_dict: dict | list | None = None, known: bool = False, template_idx: int = 0, rhs: int | float = 0)[source]

Stores representations of constraints and avoids redundant computations.

static init_from_A_poly(lifter: Any, A_poly: PolyMatrix, mat_var_dict: dict, known: bool = False, index: int = 0, template_idx: int = 0, compute_polyrow_b: bool = False, rhs: int | float = 0) Constraint[source]

Initializes a Constraint from a PolyMatrix A_poly.

static init_from_b(index: int, b: ndarray, mat_var_dict: dict | list, lifter=None, mat_param_dict: dict | list | None = None, convert_to_polyrow: bool = True, known: bool = True, template_idx: int = 0) Constraint | None[source]

Initializes a Constraint from vector b.

static init_from_polyrow_b(polyrow_b: PolyMatrix, lifter: Any, index: int = 0, known: bool = False, template_idx: int = 0, mat_var_dict: dict | None = None) Constraint[source]

Initializes a Constraint from a PolyMatrix polyrow_b.

scale_to_new_lifter(lifter: Any) Constraint[source]

Scales the constraint to a new lifter’s variable dictionary.

Helpers for matrix and vector operations

Common utilities for symmetric matrix vectorization and sparsity handling.

popcor.utils.common.create_symmetric(vec: ndarray | csr_matrix | csc_matrix, eps_sparse: float, correct: bool = False, sparse: bool = False) ndarray | csr_array[source]

Create a symmetric matrix from the vectorized upper-triangular elements.

popcor.utils.common.diag_indices(n: int) ndarray[source]

Return indices of diagonal elements in the vectorized upper-triangular matrix.

popcor.utils.common.get_aggregate_sparsity(matrix_list_sparse: Sequence[spmatrix]) csr_matrix[source]

Aggregate sparsity pattern from a list of sparse matrices.

popcor.utils.common.get_labels(p: str, zi: str, zj: str, var_dict: Dict[str, int]) List[str][source]

Generate labels for matrix/vector elements based on variable sizes.

popcor.utils.common.get_vec(mat: ndarray | csc_matrix | csr_matrix, correct: bool = True, sparse: bool = False) ndarray | csr_matrix[source]

Convert NxN symmetric matrix to vectorized upper-triangular form preserving inner product.

popcor.utils.common.ravel_multi_index_triu(index_tuple: Tuple[ndarray, ndarray], shape: Tuple[int, int]) List[int][source]

Convert (i, j) indices to flat indices for upper-triangular part of a matrix.

popcor.utils.common.unravel_multi_index_triu(flat_indices: Sequence[int], shape: Tuple[int, int]) Tuple[ndarray, ndarray][source]

Convert flat indices to (i, j) indices for upper-triangular part of a matrix.

popcor.utils.common.upper_triangular(p: ndarray) ndarray[source]

Return the vectorized upper-triangular part of the outer product of p.