diff --git a/src/sage/groups/perm_gps/partn_ref/data_structures.pxd b/src/sage/groups/perm_gps/partn_ref/data_structures.pxd index b469d09c2b5..8c5ae14b5bb 100644 --- a/src/sage/groups/perm_gps/partn_ref/data_structures.pxd +++ b/src/sage/groups/perm_gps/partn_ref/data_structures.pxd @@ -13,7 +13,6 @@ from sage.data_structures.bitset_base cimport * from libc.string cimport memcpy from libc.stdlib cimport rand from sage.libs.gmp.mpz cimport * -from sage.groups.perm_gps.partn_ref2.refinement_generic cimport PartitionRefinement_generic cdef enum: @@ -260,8 +259,7 @@ cdef PS_print(PartitionStack *PS) cdef void PS_unit_partition(PartitionStack *PS) -cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=?, - PartitionRefinement_generic partn_ref_alg=?) +cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=?) cdef PartitionStack *PS_from_list(list L) diff --git a/src/sage/groups/perm_gps/partn_ref/data_structures.pyx b/src/sage/groups/perm_gps/partn_ref/data_structures.pyx index 065c5f3365a..b6567fb3dc9 100644 --- a/src/sage/groups/perm_gps/partn_ref/data_structures.pyx +++ b/src/sage/groups/perm_gps/partn_ref/data_structures.pyx @@ -33,7 +33,10 @@ from cysignals.memory cimport sig_malloc, sig_calloc, sig_realloc, sig_free from sage.data_structures.bitset_base cimport * from sage.rings.integer cimport Integer -from sage.libs.flint.ulong_extras cimport n_is_prime +# from sage.libs.flint.ulong_extras cimport n_is_prime +# -- avoid modularization obstruction -- function is only used for a doctest helper +from sage.arith.misc import is_prime as n_is_prime + # OrbitPartition (OP) @@ -281,8 +284,7 @@ cdef PS_print_partition(PartitionStack *PS, int k): s = s[:-1] + ')' print(s) -cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=NULL, - PartitionRefinement_generic partn_ref_alg=None): +cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=NULL): """ Find the first occurrence of the smallest cell of size greater than one, which is admissible (checked by the function ``test_allowance``). @@ -290,10 +292,9 @@ cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=NULL, """ cdef int i = 0, j = 0, location = 0, n = PS.degree bitset_zero(b) - while 1: + while True: if PS.levels[i] <= PS.depth: - if i != j and n > i - j + 1 and (partn_ref_alg is None or - partn_ref_alg._minimization_allowed_on_col(PS.entries[j])): + if i != j and n > i - j + 1: n = i - j + 1 location = j j = i + 1 @@ -303,19 +304,18 @@ cdef int PS_first_smallest(PartitionStack *PS, bitset_t b, int *second_pos=NULL, # location now points to the beginning of the first, smallest, # nontrivial cell i = location - while 1: + while True: bitset_flip(b, PS.entries[i]) if PS.levels[i] <= PS.depth: break i += 1 if second_pos != NULL: - if n==2: - second_pos[0] = PS.entries[location+1] + if n == 2: + second_pos[0] = PS.entries[location + 1] else: second_pos[0] = -1 - return PS.entries[location] diff --git a/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pxd b/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pxd index e45afd3e578..df300e4e0ce 100644 --- a/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pxd +++ b/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pxd @@ -7,6 +7,7 @@ # http://www.gnu.org/licenses/ #******************************************************************************* +from sage.data_structures.bitset_base cimport * from sage.groups.perm_gps.partn_ref.data_structures cimport OrbitPartition, PartitionStack from sage.libs.gap.element cimport GapElement, GapElement_Permutation from sage.structure.parent cimport Parent @@ -82,3 +83,7 @@ cdef class PartitionRefinement_generic: bint* inner_group_changed, bint* changed_partition, str refine_name) cdef int len(self) + + +cdef int PS_first_smallest_PR(PartitionStack *PS, bitset_t b, int *second_pos=?, + PartitionRefinement_generic partn_ref_alg=?) diff --git a/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pyx b/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pyx index 47d6862333c..6e4e2b27eeb 100644 --- a/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pyx +++ b/src/sage/groups/perm_gps/partn_ref2/refinement_generic.pyx @@ -684,8 +684,7 @@ cdef class PartitionRefinement_generic: bitset_init(b, self._n) PS_move_all_mins_to_front(self._part) cdef int second_pos - cdef int smallest = PS_first_smallest(self._part, b, &second_pos, - self) + cdef int smallest = PS_first_smallest_PR(self._part, b, &second_pos, self) if second_pos != -1: self._fixed_not_minimized.append(second_pos) cdef int pos = smallest @@ -941,3 +940,42 @@ cdef class PartitionRefinement_generic: """ if BACKTRACK_WITHLATEX_DEBUG: self._latex_debug_string += "]\n" + + +cdef int PS_first_smallest_PR(PartitionStack *PS, bitset_t b, int *second_pos=NULL, + PartitionRefinement_generic partn_ref_alg=None): + """ + Find the first occurrence of the smallest cell of size greater than one, + which is admissible (checked by the function ``test_allowance``). + Its entries are stored to b and its minimum element is returned. + + This generalizes :func:`sage.groups.perm_gps.partn_ref.data_structures.PS_first_smallest`. + """ + cdef int i = 0, j = 0, location = 0, n = PS.degree + bitset_zero(b) + while True: + if PS.levels[i] <= PS.depth: + if i != j and n > i - j + 1 and (partn_ref_alg is None or + partn_ref_alg._minimization_allowed_on_col(PS.entries[j])): + n = i - j + 1 + location = j + j = i + 1 + if PS.levels[i] == -1: + break + i += 1 + # location now points to the beginning of the first, smallest, + # nontrivial cell + i = location + while True: + bitset_flip(b, PS.entries[i]) + if PS.levels[i] <= PS.depth: + break + i += 1 + + if second_pos != NULL: + if n == 2: + second_pos[0] = PS.entries[location + 1] + else: + second_pos[0] = -1 + + return PS.entries[location]