Skip to content

Commit 247ea88

Browse files
author
Release Manager
committed
gh-36335: using more itertools.product a few more cases where one can use itertools.product instead of our custom iterator ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. URL: #36335 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents f4dc2bc + 7bd6a9f commit 247ea88

File tree

5 files changed

+34
-43
lines changed

5 files changed

+34
-43
lines changed

src/sage/combinat/multiset_partition_into_sets_ordered.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,19 @@
5252
((1,), (1, 2, 3)), ((1, 2), (2, 3)), ((1, 2, 3), (3,))]
5353
"""
5454

55-
#*****************************************************************************
55+
# ****************************************************************************
5656
# Copyright (C) 2018 Aaron Lauve <lauve at math.luc.edu>
5757
#
5858
# Distributed under the terms of the GNU General Public License (GPL)
5959
# as published by the Free Software Foundation; either version 2 of
6060
# the License, or (at your option) any later version.
61-
# http://www.gnu.org/licenses/
61+
# https://www.gnu.org/licenses/
6262
# ****************************************************************************
63-
64-
6563
from functools import reduce
66-
from itertools import chain
64+
from itertools import chain, product
6765

6866
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
6967
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
70-
from sage.categories.cartesian_product import cartesian_product
7168
from sage.categories.classical_crystals import ClassicalCrystals
7269
from sage.categories.tensor import tensor
7370
from sage.structure.unique_representation import UniqueRepresentation
@@ -672,7 +669,7 @@ def split_blocks(self, k=2):
672669
return {tuple([self]*k): 1}
673670

674671
out = {}
675-
for t in cartesian_product([_split_block(block, k) for block in self]):
672+
for t in product(*[_split_block(block, k) for block in self]):
676673
tt = tuple([P([l for l in c if l]) for c in zip(*t)])
677674
out[tt] = out.get(tt, 0) + 1
678675
return out
@@ -711,8 +708,8 @@ def finer(self, strong=False):
711708
if not self:
712709
return set([self])
713710

714-
CP = cartesian_product([_refine_block(block, strong) for block in self])
715-
return set(P(_concatenate(map(list,c))) for c in CP)
711+
CP = product(*[_refine_block(block, strong) for block in self])
712+
return set(P(_concatenate(map(list, c))) for c in CP)
716713

717714
def is_finer(self, co):
718715
"""
@@ -2892,16 +2889,16 @@ def _iterator_size(size, length=None, alphabet=None):
28922889
max_p = max(alphabet)
28932890
for alpha in IntegerListsLex(size, length=length, min_part=1,
28942891
max_part=min(size, sum(alphabet))):
2895-
for p in cartesian_product([IntegerListsLex(a, min_slope=1,
2896-
min_part=min_p,
2897-
max_part=min(a, max_p))
2898-
for a in alpha]):
2892+
for p in product(*[IntegerListsLex(a, min_slope=1,
2893+
min_part=min_p,
2894+
max_part=min(a, max_p))
2895+
for a in alpha]):
28992896
if frozenset(_concatenate(p)).issubset(frozenset(alphabet)):
29002897
yield tuple(frozenset(k) for k in p)
29012898
else:
29022899
for alpha in IntegerListsLex(size, length=length, min_part=1, max_part=size):
2903-
for p in cartesian_product([IntegerListsLex(a, min_slope=1,
2904-
min_part=1) for a in alpha]):
2900+
for p in product(*[IntegerListsLex(a, min_slope=1,
2901+
min_part=1) for a in alpha]):
29052902
yield tuple(frozenset(k) for k in p)
29062903

29072904

@@ -2967,11 +2964,11 @@ def _iterator_order(A, d, lengths=None):
29672964
yield ()
29682965
else:
29692966
for alpha in IntegerListsLex(d, length=k, min_part=1, max_part=n):
2970-
for co in cartesian_product([Subsets_sk(A, a) for a in alpha]):
2967+
for co in product(*[Subsets_sk(A, a) for a in alpha]):
29712968
yield tuple(frozenset(X) for X in co)
29722969

29732970

2974-
def _descents(w):
2971+
def _descents(w) -> list:
29752972
r"""
29762973
Return descent positions in the word ``w``.
29772974
@@ -2983,7 +2980,7 @@ def _descents(w):
29832980
sage: _descents([])
29842981
[]
29852982
"""
2986-
return [j for j in range(len(w)-1) if w[j] > w[j+1]]
2983+
return [j for j in range(len(w) - 1) if w[j] > w[j + 1]]
29872984

29882985

29892986
def _break_at_descents(alpha, weak=True):

src/sage/combinat/set_partition_ordered.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#
2525
# https://www.gnu.org/licenses/
2626
# ****************************************************************************
27+
from itertools import product
28+
2729
from sage.arith.misc import factorial, multinomial
28-
from sage.categories.cartesian_product import cartesian_product
2930
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
3031
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
3132
from sage.combinat.combinat import stirling_number2
@@ -452,7 +453,7 @@ def finer(self):
452453
if not self:
453454
return FiniteEnumeratedSet([self])
454455
return FiniteEnumeratedSet([par(sum((list(i) for i in C), []))
455-
for C in cartesian_product([OrderedSetPartitions(X) for X in self])])
456+
for C in product(*[OrderedSetPartitions(X) for X in self])])
456457

457458
def is_finer(self, co2):
458459
"""
@@ -548,7 +549,7 @@ def fatten(self, grouping):
548549
result = [None] * len(grouping)
549550
j = 0
550551
for i in range(len(grouping)):
551-
result[i] = set().union(*self[j:j+grouping[i]])
552+
result[i] = set().union(*self[j:j + grouping[i]])
552553
j += grouping[i]
553554
return parent(self)(result)
554555

@@ -636,7 +637,7 @@ def bottom_up_osp(X, comp):
636637
result = [None] * len(comp)
637638
j = 0
638639
for i in range(len(comp)):
639-
result[i] = set(xs[j:j+comp[i]])
640+
result[i] = set(xs[j:j + comp[i]])
640641
j += comp[i]
641642
return OrderedSetPartitions(X)(result)
642643

@@ -671,7 +672,7 @@ def strongly_finer(self):
671672
return FiniteEnumeratedSet([self])
672673
buo = OrderedSetPartition.bottom_up_osp
673674
return FiniteEnumeratedSet([par(sum((list(P) for P in C), []))
674-
for C in cartesian_product([[buo(X, comp) for comp in Compositions(len(X))] for X in self])])
675+
for C in product(*[[buo(X, comp) for comp in Compositions(len(X))] for X in self])])
675676

676677
def is_strongly_finer(self, co2):
677678
r"""
@@ -779,7 +780,7 @@ def strongly_fatter(self):
779780
# arbitrarily, and then concatenate the results.
780781
fattenings = [list(subcomp.fatter()) for subcomp in subcomps]
781782
return FiniteEnumeratedSet([OrderedSetPartition(sum([list(gg) for gg in fattening], []))
782-
for fattening in cartesian_product(fattenings)])
783+
for fattening in product(*fattenings)])
783784

784785
@combinatorial_map(name='to packed word')
785786
def to_packed_word(self):

src/sage/graphs/path_enumeration.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Functions
3131
# (at your option) any later version.
3232
# https://www.gnu.org/licenses/
3333
# ****************************************************************************
34+
from itertools import product
3435

35-
from sage.categories.cartesian_product import cartesian_product
3636
from sage.misc.misc_c import prod
3737
from libcpp.queue cimport priority_queue
3838
from libcpp.pair cimport pair
@@ -263,7 +263,7 @@ def all_paths(G, start, end, use_multiedges=False, report_edges=False, labels=Fa
263263
if report_edges and labels:
264264
path_with_labels = []
265265
for p in all_paths:
266-
path_with_labels.extend(cartesian_product([edge_labels[e] for e in zip(p[:-1], p[1:])]))
266+
path_with_labels.extend(product(*[edge_labels[e] for e in zip(p[:-1], p[1:])]))
267267
return path_with_labels
268268
elif use_multiedges and G.has_multiple_edges():
269269
multiple_all_paths = []
@@ -1586,7 +1586,7 @@ def _all_paths_iterator(self, vertex, ending_vertices=None,
15861586
neighbor in ending_vertices):
15871587
newpath = path + [neighbor]
15881588
if report_edges and labels:
1589-
for p in cartesian_product([my_dict[e] for e in zip(newpath[:-1], newpath[1:])]):
1589+
for p in product(*[my_dict[e] for e in zip(newpath[:-1], newpath[1:])]):
15901590
yield list(p)
15911591
elif use_multiedges and self.has_multiple_edges():
15921592
m = prod(edge_multiplicity[e] for e in zip(newpath[:-1], newpath[1:]))
@@ -1610,7 +1610,7 @@ def _all_paths_iterator(self, vertex, ending_vertices=None,
16101610
if path[-1] in ending_vertices:
16111611
# yield good path
16121612
if report_edges and labels:
1613-
for p in cartesian_product([my_dict[e] for e in zip(path[:-1], path[1:])]):
1613+
for p in product(*[my_dict[e] for e in zip(path[:-1], path[1:])]):
16141614
yield list(p)
16151615
elif use_multiedges and self.has_multiple_edges():
16161616
m = prod(edge_multiplicity[e] for e in zip(path[:-1], path[1:]))

src/sage/modules/filtered_vector_space.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
# the License, or (at your option) any later version.
108108
# https://www.gnu.org/licenses/
109109
# ***************************************************************************
110+
from itertools import product
110111

111112
from sage.rings.rational_field import QQ
112113
from sage.rings.integer_ring import ZZ
@@ -1122,11 +1123,10 @@ def _power_operation(self, n, operation):
11221123

11231124
iters = [self.support()] * n
11241125
filtration = {}
1125-
from sage.categories.cartesian_product import cartesian_product
1126-
for degrees in cartesian_product(iters):
1126+
for degrees in product(*iters):
11271127
deg = sum(degrees)
11281128
filt_deg = filtration.get(deg, set())
1129-
for i in cartesian_product([indices.get(d) for d in degrees]):
1129+
for i in product(*[indices.get(d) for d in degrees]):
11301130
pow_i = T.index_map(*i)
11311131
if pow_i is not None:
11321132
filt_deg.add(pow_i)

src/sage/modules/tensor_operations.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
# the License, or (at your option) any later version.
6161
# https://www.gnu.org/licenses/
6262
# ****************************************************************************
63-
63+
from itertools import product
6464
from collections import defaultdict
6565

6666
from sage.modules.free_module import FreeModule_ambient_field
@@ -90,12 +90,11 @@ def symmetrized_coordinate_sums(dim, n):
9090
((0, 0), (0, 1) + (1, 0), (1, 1))
9191
"""
9292
from sage.structure.formal_sum import FormalSum
93-
from sage.categories.cartesian_product import cartesian_product
9493

9594
coordinates = [list(range(dim)) for i in range(n)]
9695
table = defaultdict(list)
9796

98-
for i in cartesian_product(coordinates):
97+
for i in product(*coordinates):
9998
sort_i = tuple(sorted(i))
10099
table[sort_i].append([1, tuple(i)])
101100

@@ -344,11 +343,7 @@ def _init_product_vectors(self, i):
344343
"""
345344
# Pick out the i[j]-th vector
346345
rays = [list(self._V[j].vectors()[k]) for j, k in enumerate(i)]
347-
v = []
348-
# Note: convert to list, as cartesian_product of vectors is unrelated
349-
from sage.categories.cartesian_product import cartesian_product
350-
for r in cartesian_product(rays):
351-
v.append(prod(r)) # build up the tensor product
346+
v = [prod(r) for r in product(*rays)] # build up the tensor product
352347
v = tuple(v)
353348
# Use index of pre-existing tensor product vector if there is one
354349
try:
@@ -415,8 +410,7 @@ def _init_product(self):
415410
[((0, 0), 0), ((0, 1), 1), ((1, 0), 2), ((1, 1), 3), ((2, 0), 3), ((2, 1), 2)]
416411
"""
417412
V_list_indices = [list(range(V.n_vectors())) for V in self._V]
418-
from sage.categories.cartesian_product import cartesian_product
419-
for i in cartesian_product(V_list_indices):
413+
for i in product(*V_list_indices):
420414
self._index_map[tuple(i)] = self._init_product_vectors(i)
421415
self._symmetrize_indices = False
422416

@@ -433,12 +427,11 @@ def _init_symmetric(self):
433427
sage: sorted(Sym2_R._index_map.items())
434428
[((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((1, 1), 3), ((1, 2), 4), ((2, 2), 3)]
435429
"""
436-
from sage.categories.cartesian_product import cartesian_product
437430
V_list_indices = [list(range(V.n_vectors())) for V in self._V]
438431
Sym = symmetrized_coordinate_sums(self._V[0].dimension(),
439432
len(self._V))
440433
N = len(V_list_indices)
441-
for i in cartesian_product(V_list_indices):
434+
for i in product(*V_list_indices):
442435
if any(i[j - 1] > i[j] for j in range(1, N)):
443436
continue
444437
self._index_map[tuple(i)] = self._init_power_operation_vectors(i, Sym)

0 commit comments

Comments
 (0)