def _unsliced_length(self): ''' The number of perms in the space, ignoring any slicing. This is used as an interim step in calculating the actual length of the space with the slice taken into account. ''' if self.n_elements > self.sequence_length: return 0 if self.is_degreed: assert not self.is_recurrent and not self.is_partial and \ not self.is_combination return sum( math_tools.abs_stirling( self.sequence_length - len(self.fixed_map), self.sequence_length - degree - self._n_cycles_in_fixed_items_of_just_fixed ) for degree in self.degrees ) elif self.is_fixed: assert not self.is_degreed and not self.is_combination if self.is_recurrent: return calculate_length_of_recurrent_perm_space( self.n_elements - len(self.fixed_map), nifty_collections.FrozenBagBag( nifty_collections.Bag(self.free_values).values() ) ) else: return math_tools.factorial( len(self.free_indices), start=(len(self.free_indices) - (self.n_elements - len(self.fixed_map)) + 1) ) else: assert not self.is_degreed and not self.is_fixed if self.is_recurrent: if self.is_combination: return calculate_length_of_recurrent_comb_space( self.n_elements, self._frozen_bag_bag ) else: return calculate_length_of_recurrent_perm_space( self.n_elements, self._frozen_bag_bag ) else: return math_tools.factorial( self.sequence_length, start=(self.sequence_length - self.n_elements + 1) ) // (math_tools.factorial(self.n_elements) if self.is_combination else 1)
def test(): assert combi.misc.get_short_factorial_string(7) == \ str(math_tools.factorial(7)) assert combi.misc.get_short_factorial_string(7, minus_one=True) == \ str(math_tools.factorial(7) - 1) assert combi.misc.get_short_factorial_string(17) == '17!' assert combi.misc.get_short_factorial_string(17, minus_one=True) == \ '17! - 1' assert combi.misc.get_short_factorial_string(float('inf')) == \ '''float('inf')''' assert combi.misc.get_short_factorial_string( float('inf'), minus_one=True) == '''float('inf')'''
def test(): assert combi.misc.get_short_factorial_string(7) == \ str(math_tools.factorial(7)) assert combi.misc.get_short_factorial_string(7, minus_one=True) == \ str(math_tools.factorial(7) - 1) assert combi.misc.get_short_factorial_string(17) == '17!' assert combi.misc.get_short_factorial_string(17, minus_one=True) == \ '17! - 1' assert combi.misc.get_short_factorial_string(float('inf')) == \ '''float('inf')''' assert combi.misc.get_short_factorial_string(float('inf'), minus_one=True) == '''float('inf')'''
def _unsliced_length(self): ''' The number of perms in the space, ignoring any slicing. This is used as an interim step in calculating the actual length of the space with the slice taken into account. ''' if self.n_elements > self.sequence_length: return 0 if self.is_degreed: assert not self.is_recurrent and not self.is_partial and \ not self.is_combination return sum( math_tools.abs_stirling( self.sequence_length - len(self.fixed_map), self.sequence_length - degree - self._n_cycles_in_fixed_items_of_just_fixed) for degree in self.degrees) elif self.is_fixed: assert not self.is_degreed and not self.is_combination if self.is_recurrent: return calculate_length_of_recurrent_perm_space( self.n_elements - len(self.fixed_map), nifty_collections.FrozenBagBag( nifty_collections.Bag(self.free_values).values())) else: return math_tools.factorial( len(self.free_indices), start=(len(self.free_indices) - (self.n_elements - len(self.fixed_map)) + 1)) else: assert not self.is_degreed and not self.is_fixed if self.is_recurrent: if self.is_combination: return calculate_length_of_recurrent_comb_space( self.n_elements, self._frozen_bag_bag) else: return calculate_length_of_recurrent_perm_space( self.n_elements, self._frozen_bag_bag) else: return math_tools.factorial( self.sequence_length, start=(self.sequence_length - self.n_elements + 1)) // (math_tools.factorial(self.n_elements) if self.is_combination else 1)
def test_unrecurrented(): recurrent_perm_space = combi.PermSpace('abcabc') unrecurrented_perm_space = recurrent_perm_space.unrecurrented assert unrecurrented_perm_space.length == math_tools.factorial(6) perm = unrecurrented_perm_space[100] assert all(i in 'abc' for i in perm) assert set(map(perm.index, 'abc')) < set((0, 1, 2, 3, 4)) assert set(''.join(perm)) == set('abc')
def test_unrecurrented(): recurrent_perm_space = combi.PermSpace('abcabc') unrecurrented_perm_space = recurrent_perm_space.unrecurrented assert unrecurrented_perm_space.length == math_tools.factorial(6) perm = unrecurrented_perm_space[100] assert all(i in 'abc' for i in perm) assert set(map(perm.index, 'abc')) < {0, 1, 2, 3, 4} assert set(''.join(perm)) == set('abc')
def short_length_string(self): '''Short string describing size of space, e.g. "12!"''' if not self.is_recurrent and not self.is_partial and \ not self.is_combination and not self.is_fixed and \ not self.is_sliced: assert self.length == math_tools.factorial(self.sequence_length) return misc.get_short_factorial_string(self.sequence_length) else: return str(self.length)
def test_fixed_perm_space(): pure_perm_space = PermSpace(5) small_fixed_perm_space = PermSpace(5, fixed_map={ 0: 0, 2: 2, 4: 4, }) big_fixed_perm_space = PermSpace(5, fixed_map={ 0: 0, 2: 2, }) assert pure_perm_space != big_fixed_perm_space != small_fixed_perm_space assert small_fixed_perm_space.length == \ len(tuple(small_fixed_perm_space)) == 2 assert big_fixed_perm_space.length == \ len(tuple(big_fixed_perm_space)) == 6 for perm in small_fixed_perm_space: assert perm in big_fixed_perm_space assert perm in pure_perm_space for perm in big_fixed_perm_space: assert perm in pure_perm_space assert len([ perm for perm in big_fixed_perm_space if perm not in small_fixed_perm_space ]) == 4 assert small_fixed_perm_space[:] == small_fixed_perm_space assert small_fixed_perm_space[1:][0] == small_fixed_perm_space[1] assert small_fixed_perm_space.index(small_fixed_perm_space[0]) == 0 assert small_fixed_perm_space.index(small_fixed_perm_space[1]) == 1 assert big_fixed_perm_space.index(big_fixed_perm_space[0]) == 0 assert big_fixed_perm_space.index(big_fixed_perm_space[1]) == 1 assert big_fixed_perm_space.index(big_fixed_perm_space[2]) == 2 assert big_fixed_perm_space.index(big_fixed_perm_space[3]) == 3 assert big_fixed_perm_space.index(big_fixed_perm_space[4]) == 4 assert big_fixed_perm_space.index(big_fixed_perm_space[5]) == 5 for perm in small_fixed_perm_space: assert (perm[0], perm[2], perm[4]) == (0, 2, 4) for perm in big_fixed_perm_space: assert (perm[0], perm[2]) == (0, 2) assert big_fixed_perm_space.index(small_fixed_perm_space[1]) != 1 weird_fixed_perm_space = PermSpace(range(100), fixed_map=zip(range(90), range(90))) assert weird_fixed_perm_space.length == math_tools.factorial(10) assert weird_fixed_perm_space[-1234566][77] == 77 assert len(repr(weird_fixed_perm_space)) <= 100
def test_fixed_perm_space(): pure_perm_space = PermSpace(5) small_fixed_perm_space = PermSpace(5, fixed_map={0: 0, 2: 2, 4: 4,}) big_fixed_perm_space = PermSpace(5, fixed_map={0: 0, 2: 2,}) assert pure_perm_space != big_fixed_perm_space != small_fixed_perm_space assert small_fixed_perm_space.length == \ len(tuple(small_fixed_perm_space)) == 2 assert big_fixed_perm_space.length == \ len(tuple(big_fixed_perm_space)) == 6 for perm in small_fixed_perm_space: assert perm in big_fixed_perm_space assert perm in pure_perm_space for perm in big_fixed_perm_space: assert perm in pure_perm_space assert len([perm for perm in big_fixed_perm_space if perm not in small_fixed_perm_space]) == 4 assert small_fixed_perm_space[:] == small_fixed_perm_space assert small_fixed_perm_space[1:][0] == small_fixed_perm_space[1] assert small_fixed_perm_space.index(small_fixed_perm_space[0]) == 0 assert small_fixed_perm_space.index(small_fixed_perm_space[1]) == 1 assert big_fixed_perm_space.index(big_fixed_perm_space[0]) == 0 assert big_fixed_perm_space.index(big_fixed_perm_space[1]) == 1 assert big_fixed_perm_space.index(big_fixed_perm_space[2]) == 2 assert big_fixed_perm_space.index(big_fixed_perm_space[3]) == 3 assert big_fixed_perm_space.index(big_fixed_perm_space[4]) == 4 assert big_fixed_perm_space.index(big_fixed_perm_space[5]) == 5 for perm in small_fixed_perm_space: assert (perm[0], perm[2], perm[4]) == (0, 2, 4) for perm in big_fixed_perm_space: assert (perm[0], perm[2]) == (0, 2) assert big_fixed_perm_space.index(small_fixed_perm_space[1]) != 1 weird_fixed_perm_space = PermSpace(range(100), fixed_map=zip(range(90), range(90))) assert weird_fixed_perm_space.length == math_tools.factorial(10) assert weird_fixed_perm_space[-1234566][77] == 77 assert len(repr(weird_fixed_perm_space)) <= 100