Skip to content

Commit 025b1f9

Browse files
committed
-
1 parent 85ca584 commit 025b1f9

File tree

3 files changed

+81
-69
lines changed

3 files changed

+81
-69
lines changed

source_py3/python_toolbox/combi/perm_space.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,6 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
237237
# #
238238
### Finished figuring out whether space is dapplied. ##################
239239

240-
### Doing interim calculation of the length: ##########################
241-
# #
242-
# The length calculated here will be true only for perm spaces that
243-
# don't have any additional complications.
244-
# #
245-
### Finished doing interim calculation of the length. #################
246-
247-
248240
### Figuring out fixed map: ###########################################
249241
# #
250242
if fixed_map is None:
@@ -270,20 +262,6 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
270262

271263
self.is_fixed = bool(self.fixed_map)
272264
if self.is_fixed:
273-
if self.is_recurrent:
274-
self._unsliced_undegreed_length = math_tools.shitfuck(
275-
self.n_elements - len(self.fixed_map),
276-
nifty_collections.FrozenCounterCounter(
277-
collections.Counter(self.free_values).values()
278-
)
279-
)
280-
else:
281-
self._unsliced_undegreed_length = math_tools.factorial(
282-
len(self.free_indices),
283-
start=(len(self.free_indices) -
284-
(self.n_elements - len(self.fixed_map)) + 1)
285-
)
286-
287265
if not (self.is_dapplied or self.is_rapplied or degrees or slice_
288266
or (n_elements is not None) or self.is_combination):
289267
self._just_fixed = self
@@ -293,20 +271,6 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
293271
fixed_map=self._undapplied_unrapplied_fixed_map,
294272
)
295273
else:
296-
297-
if self.is_recurrent:
298-
function_to_use = math_tools.catshit if self.is_combination else \
299-
math_tools.shitfuck
300-
self._unsliced_undegreed_length = \
301-
function_to_use(self.n_elements, self._frozen_counter_counter)
302-
else:
303-
self._unsliced_undegreed_length = \
304-
math_tools.factorial(
305-
self.sequence_length,
306-
start=(self.sequence_length - self.n_elements + 1)
307-
) // (math_tools.factorial(self.n_elements) if
308-
self.is_combination else 1)
309-
# This division is always without a remainder, because math.
310274

311275
if not (self.is_dapplied or self.is_rapplied or degrees or slice_
312276
or (n_elements is not None) or self.is_combination):
@@ -327,7 +291,6 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
327291
if (not degrees) or cute_iter_tools.are_equal(degrees, all_degrees):
328292
self.is_degreed = False
329293
self.degrees = all_degrees
330-
self._unsliced_length = self._unsliced_undegreed_length
331294
else:
332295
self.is_degreed = True
333296
if self.is_combination:
@@ -348,13 +311,6 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
348311
self.degrees = tuple(
349312
degree for degree in degrees if degree in all_degrees
350313
)
351-
self._unsliced_length = sum(
352-
math_tools.abs_stirling(
353-
self.sequence_length - len(self.fixed_map),
354-
self.sequence_length - degree -
355-
self._n_cycles_in_fixed_items_of_just_fixed
356-
) for degree in self.degrees
357-
)
358314

359315
# #
360316
### Finished figuring out degrees. ####################################
@@ -400,6 +356,60 @@ def __init__(self, iterable_or_length, domain=None, *, n_elements=None,
400356
__init__.signature = inspect.signature(__init__)
401357

402358

359+
@caching.CachedProperty
360+
def _unsliced_length(self):
361+
362+
if self.is_degreed:
363+
assert not self.is_recurrent and not self.is_partial and \
364+
not self.is_combination
365+
return sum(
366+
math_tools.abs_stirling(
367+
self.sequence_length - len(self.fixed_map),
368+
self.sequence_length - degree -
369+
self._n_cycles_in_fixed_items_of_just_fixed
370+
) for degree in self.degrees
371+
)
372+
elif self.is_fixed:
373+
assert not self.is_degreed and not self.is_combination
374+
if self.is_recurrent:
375+
return math_tools.calculate_length_of_recurrent_perm_space(
376+
self.n_elements - len(self.fixed_map),
377+
nifty_collections.FrozenCounterCounter(
378+
collections.Counter(self.free_values).values()
379+
)
380+
)
381+
else:
382+
return math_tools.factorial(
383+
len(self.free_indices),
384+
start=(len(self.free_indices) -
385+
(self.n_elements - len(self.fixed_map)) + 1)
386+
)
387+
388+
else:
389+
assert not self.is_degreed and not self.is_fixed
390+
if self.is_recurrent:
391+
if self.is_combination:
392+
return math_tools.calculate_length_of_recurrent_comb_space(
393+
self.n_elements,
394+
self._frozen_counter_counter
395+
)
396+
else:
397+
return math_tools.calculate_length_of_recurrent_perm_space(
398+
self.n_elements,
399+
self._frozen_counter_counter
400+
)
401+
402+
else:
403+
return math_tools.factorial(
404+
self.sequence_length,
405+
start=(self.sequence_length - self.n_elements + 1)
406+
) // (math_tools.factorial(self.n_elements) if
407+
self.is_combination else 1)
408+
# This division is always without a remainder, because math.
409+
410+
411+
412+
403413
is_recurrent = None
404414

405415
@caching.CachedProperty

source_py3/python_toolbox/math_tools/sequences.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ def abs_stirling(n, k):
5858

5959
###############################################################################
6060

61-
_shitfuck_cache = {}
61+
_length_of_recurrent_perm_space_cache = {}
6262

63-
def shitfuck(k, recurrence_counter):
63+
def calculate_length_of_recurrent_perm_space(k, recurrence_counter):
6464
from python_toolbox import nifty_collections
6565
from python_toolbox import cute_iter_tools
66+
cache = _length_of_recurrent_perm_space_cache
6667
if not isinstance(recurrence_counter, nifty_collections.FrozenCounterCounter):
6768
recurrence_counter = \
6869
nifty_collections.FrozenCounterCounter(recurrence_counter)
@@ -74,7 +75,7 @@ def shitfuck(k, recurrence_counter):
7475
# unlike Python's `Counter`.)
7576
return recurrence_counter.n_elements
7677
try:
77-
return _shitfuck_cache[(k, recurrence_counter)]
78+
return cache[(k, recurrence_counter)]
7879
except KeyError:
7980
pass
8081

@@ -86,7 +87,7 @@ def shitfuck(k, recurrence_counter):
8687
{recurrence_counter_: recurrence_counter_.
8788
get_sub_counters_for_one_crate_removed()
8889
for recurrence_counter_ in current_reccurence_counters
89-
if (k_, recurrence_counter_) not in _shitfuck_cache}
90+
if (k_, recurrence_counter_) not in cache}
9091
)
9192
current_reccurence_counters = \
9293
set(itertools.chain(*levels[-1].values()))
@@ -95,30 +96,31 @@ def shitfuck(k, recurrence_counter):
9596
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
9697
if k_ == 1:
9798
for recurrence_counter_, sub_counters_counter in level.items():
98-
_shitfuck_cache[(k_, recurrence_counter_)] = \
99+
cache[(k_, recurrence_counter_)] = \
99100
recurrence_counter_.n_elements
100101
else:
101102
for recurrence_counter_, sub_counters_counter in level.items():
102-
_shitfuck_cache[(k_, recurrence_counter_)] = sum(
103-
(_shitfuck_cache[(k_ - 1, sub_counter)] * factor for
103+
cache[(k_, recurrence_counter_)] = sum(
104+
(cache[(k_ - 1, sub_counter)] * factor for
104105
sub_counter, factor in sub_counters_counter.items())
105106
)
106107

107-
return _shitfuck_cache[(k, recurrence_counter)]
108+
return cache[(k, recurrence_counter)]
108109

109110

110111

111112

112113
###############################################################################
113114

114-
_catshit_cache = {}
115+
_length_of_recurrent_comb_space_cache = {}
115116

116-
def catshit(k, recurrence_counter):
117+
def calculate_length_of_recurrent_comb_space(k, recurrence_counter):
117118
'''
118119
blocktodo gotta properly name these two sons of bitches
119120
'''
120121
from python_toolbox import nifty_collections
121122
from python_toolbox import cute_iter_tools
123+
cache = _length_of_recurrent_comb_space_cache
122124
if not isinstance(recurrence_counter, nifty_collections.FrozenCounterCounter):
123125
recurrence_counter = \
124126
nifty_collections.FrozenCounterCounter(recurrence_counter)
@@ -130,7 +132,7 @@ def catshit(k, recurrence_counter):
130132
# unlike Python's `Counter`.)
131133
return recurrence_counter.n_elements
132134
try:
133-
return _catshit_cache[(k, recurrence_counter)]
135+
return cache[(k, recurrence_counter)]
134136
except KeyError:
135137
pass
136138

@@ -142,7 +144,7 @@ def catshit(k, recurrence_counter):
142144
{recurrence_counter_: recurrence_counter_.
143145
get_sub_counters_for_one_crate_and_previous_piles_removed()
144146
for recurrence_counter_ in current_reccurence_counters
145-
if (k_, recurrence_counter_) not in _catshit_cache}
147+
if (k_, recurrence_counter_) not in cache}
146148
)
147149
current_reccurence_counters = \
148150
set(itertools.chain(*levels[-1].values()))
@@ -151,15 +153,15 @@ def catshit(k, recurrence_counter):
151153
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
152154
if k_ == 1:
153155
for recurrence_counter_, sub_counters in level.items():
154-
_catshit_cache[(k_, recurrence_counter_)] = len(sub_counters)
156+
cache[(k_, recurrence_counter_)] = len(sub_counters)
155157
else:
156158
for recurrence_counter_, sub_counters in level.items():
157-
_catshit_cache[(k_, recurrence_counter_)] = sum(
158-
(_catshit_cache[(k_ - 1, sub_counter)] for
159+
cache[(k_, recurrence_counter_)] = sum(
160+
(cache[(k_ - 1, sub_counter)] for
159161
sub_counter in sub_counters)
160162
)
161163

162-
return _catshit_cache[(k, recurrence_counter)]
164+
return cache[(k, recurrence_counter)]
163165

164166

165167

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
from python_toolbox.math_tools import abs_stirling, shitfuck, catshit
3+
from python_toolbox.math_tools import abs_stirling, calculate_length_of_recurrent_perm_space, calculate_length_of_recurrent_comb_space
44

55

66
def test_abs_stirling():
@@ -17,14 +17,14 @@ def test_abs_stirling():
1717
assert abs_stirling(200, 50) == 525010571470323062300307763288024029929662200077890908912803398279686186838073914722860457474159887042512346530620756231465891831828236378945598188429630326359716300315479010640625526167635598138598969330736141913019490812196987045505021083120744610946447254207252791218757775609887718753072629854788563118348792912143712216969484697600
1818

1919

20-
def test_shitfuck():
21-
assert shitfuck(3, (3, 1, 1)) == 13
22-
assert shitfuck(2, (3, 2, 2, 1)) == 15
23-
assert shitfuck(3, (3, 2, 2, 1)) == 52
20+
def test_recurrent_perm_space_length():
21+
assert calculate_length_of_recurrent_perm_space(3, (3, 1, 1)) == 13
22+
assert calculate_length_of_recurrent_perm_space(2, (3, 2, 2, 1)) == 15
23+
assert calculate_length_of_recurrent_perm_space(3, (3, 2, 2, 1)) == 52
2424

2525

26-
def test_catshit():
27-
assert catshit(3, (3, 1, 1)) == 4
28-
assert catshit(2, (3, 2, 2, 1)) == 9
29-
assert catshit(3, (3, 2, 2, 1)) == 14
26+
def test_recurrent_comb_space_length():
27+
assert calculate_length_of_recurrent_comb_space(3, (3, 1, 1)) == 4
28+
assert calculate_length_of_recurrent_comb_space(2, (3, 2, 2, 1)) == 9
29+
assert calculate_length_of_recurrent_comb_space(3, (3, 2, 2, 1)) == 14
3030

0 commit comments

Comments
 (0)