From a62a0c70010c9d5a1653caf26fd0260dbdcd3e65 Mon Sep 17 00:00:00 2001 From: Steven An Date: Tue, 10 Mar 2026 22:22:49 -0700 Subject: [PATCH 1/2] allow variables for block_elimination to be 0-indexed --- cython/pycddlib.pxi | 9 +++++---- test/test_block_elimination.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cython/pycddlib.pxi b/cython/pycddlib.pxi index 94ce110..40901cf 100644 --- a/cython/pycddlib.pxi +++ b/cython/pycddlib.pxi @@ -89,14 +89,15 @@ cdef _get_set(set_type set_): elem for elem in range(set_[0]) if set_member(elem + 1, set_) } -cdef _set_set(set_type set_, elems): +cdef _set_set(set_type set_, elems, bool is_var_ind=False): # set elements of set_type by elements from a Python Container cdef unsigned long elem + offset = 2 if is_var_ind else 1 for elem in range(set_[0]): if elem in elems: - set_addelem(set_, elem + 1) + set_addelem(set_, elem + offset) else: - set_delelem(set_, elem + 1) + set_delelem(set_, elem + offset) cdef setfam_from_ptr(dd_SetFamilyPtr dd_setfam): # create Python Sequence[Set] from dd_SetFamilyPtr, and @@ -1191,7 +1192,7 @@ def block_elimination(mat: Matrix, col_set: Container[int]) -> Matrix: cdef dd_ErrorType error = dd_NoError set_initialize(&dd_colset, mat.dd_mat.colsize) try: - _set_set(dd_colset, col_set) + _set_set(dd_colset, col_set, True) dd_mat = dd_BlockElimination(mat.dd_mat, dd_colset, &error) return matrix_from_ptr_with_error(dd_mat, error) finally: diff --git a/test/test_block_elimination.py b/test/test_block_elimination.py index b446425..32d583d 100644 --- a/test/test_block_elimination.py +++ b/test/test_block_elimination.py @@ -7,7 +7,7 @@ def test_block_elimination_1() -> None: # 0 <= 1 + a + b + c + d, 0 <= 1 + 2a - b - c - d array = [[1, 1, 1, 1, 1], [1, 2, -1, -1, -1]] mat1 = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY) - mat2 = cdd.block_elimination(mat1, {2, 3, 4}) + mat2 = cdd.block_elimination(mat1, {1, 2, 3}) # 0 <= 2 + 3a assert_matrix_almost_equal(mat2.array, [[2, 3]]) assert mat2.lin_set == set() @@ -23,7 +23,7 @@ def test_block_elimination_2() -> None: ] mat1 = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY) # eliminate last variable, same as fourier - mat2 = cdd.block_elimination(mat1, {3}) + mat2 = cdd.block_elimination(mat1, {2}) assert_matrix_almost_equal( mat2.array, [ @@ -39,7 +39,7 @@ def test_block_elimination_3() -> None: # 0 = -2 + x + y, 0 <= y array = [[-2, 1, 1], [0, 0, 1]] mat1 = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY, lin_set=[0]) - mat2 = cdd.block_elimination(mat1, {2}) + mat2 = cdd.block_elimination(mat1, {1}) # 0 <= 2 - x assert_matrix_almost_equal(mat2.array, [[2, -1]]) assert mat2.lin_set == set() From 5c34833b3b78cbc79e6aa8f9e37de40526f98c5d Mon Sep 17 00:00:00 2001 From: Steven An Date: Tue, 10 Mar 2026 22:48:20 -0700 Subject: [PATCH 2/2] change block_elimination signature --- cython/pycddlib.pxi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cython/pycddlib.pxi b/cython/pycddlib.pxi index 40901cf..c014234 100644 --- a/cython/pycddlib.pxi +++ b/cython/pycddlib.pxi @@ -1172,8 +1172,8 @@ def fourier_elimination(mat: Matrix) -> Matrix: return matrix_from_ptr_with_error(dd_mat, error) -def block_elimination(mat: Matrix, col_set: Container[int]) -> Matrix: - """Eliminate the variables *col_set* from the system of linear inequalities *mat*. +def block_elimination(mat: Matrix, var_set: Container[int]) -> Matrix: + """Eliminate the variables *var_set* from the system of linear inequalities *mat*. It does this by using the generators of the dual linear system, where the generators are calculated using the double description algorithm. @@ -1192,7 +1192,7 @@ def block_elimination(mat: Matrix, col_set: Container[int]) -> Matrix: cdef dd_ErrorType error = dd_NoError set_initialize(&dd_colset, mat.dd_mat.colsize) try: - _set_set(dd_colset, col_set, True) + _set_set(dd_colset, var_set, True) dd_mat = dd_BlockElimination(mat.dd_mat, dd_colset, &error) return matrix_from_ptr_with_error(dd_mat, error) finally: