From 0618a2885abfdc98b6b5e039f419894f78febaae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 9 May 2023 11:01:57 +0200 Subject: [PATCH] simplify some all(all(...)) in combinat and geometry --- src/sage/combinat/combinat_cython.pyx | 2 +- src/sage/combinat/crystals/tensor_product.py | 2 +- src/sage/combinat/designs/resolvable_bibd.py | 4 +- src/sage/combinat/diagram.py | 10 ++-- src/sage/combinat/gelfand_tsetlin_patterns.py | 56 +++++++++---------- src/sage/combinat/path_tableaux/frieze.py | 3 +- .../combinat/path_tableaux/semistandard.py | 4 +- src/sage/combinat/plane_partition.py | 9 +-- src/sage/combinat/ribbon_shaped_tableau.py | 15 ++++- src/sage/combinat/symmetric_group_algebra.py | 9 ++- src/sage/geometry/fan_morphism.py | 4 +- src/sage/geometry/polyhedron/base_ZZ.py | 6 +- src/sage/geometry/polyhedron/constructor.py | 5 +- 13 files changed, 72 insertions(+), 57 deletions(-) diff --git a/src/sage/combinat/combinat_cython.pyx b/src/sage/combinat/combinat_cython.pyx index 3d8e8042311..1f8c56cc16d 100644 --- a/src/sage/combinat/combinat_cython.pyx +++ b/src/sage/combinat/combinat_cython.pyx @@ -762,7 +762,7 @@ def set_partition_composition(tuple sp1, tuple sp2): diagram.append(tuple(block)) # Everything else should be completely contained in the top block - assert all(all(val > 0 for val in top) for top in remaining_top) + assert all(val > 0 for top in remaining_top for val in top) diagram.extend(remaining_top) return (tuple(diagram), num_loops) diff --git a/src/sage/combinat/crystals/tensor_product.py b/src/sage/combinat/crystals/tensor_product.py index 6748af04b77..fe90ccc0d9d 100644 --- a/src/sage/combinat/crystals/tensor_product.py +++ b/src/sage/combinat/crystals/tensor_product.py @@ -934,7 +934,7 @@ def __classcall_private__(cls, cartan_type, shapes = None, shape = None): n1 = n + 1 else: n1 = n - if not all(all(i == 0 for i in shape[n1:]) for shape in shapes): + if not all(i == 0 for shape in shapes for i in shape[n1:]): raise ValueError("shapes should all have length at most equal to the rank or the rank + 1 in type A") spin_shapes = tuple((tuple(shape) + (0,)*(n1-len(shape)))[:n1] for shape in shapes) try: diff --git a/src/sage/combinat/designs/resolvable_bibd.py b/src/sage/combinat/designs/resolvable_bibd.py index c2236229c98..e9b90579bc2 100644 --- a/src/sage/combinat/designs/resolvable_bibd.py +++ b/src/sage/combinat/designs/resolvable_bibd.py @@ -592,13 +592,13 @@ def PBD_4_7(v,check=True, existence=False): parall = [] plus_one = None for S in AF: - if all(all(x not in SS for x in S) for SS in parall): + if all(x not in SS for SS in parall for x in S): parall.append(S) elif plus_one is None: plus_one = S if len(parall) == 4 and plus_one is not None: break - X = set(sum(parall,plus_one)) + X = set(sum(parall, plus_one)) S_4_5_7 = [X.intersection(S) for S in AF] S_4_5_7 = [S for S in S_4_5_7 if len(S)>1] diff --git a/src/sage/combinat/diagram.py b/src/sage/combinat/diagram.py index 2da21bbe3e0..8ef81bafece 100644 --- a/src/sage/combinat/diagram.py +++ b/src/sage/combinat/diagram.py @@ -475,7 +475,7 @@ def check(self): sage: D = Diagram([(0,0), (0,-3), (2,2), (2,4)]) Traceback (most recent call last): ... - ValueError: Diagrams must be indexed by non-negative integers + ValueError: diagrams must be indexed by non-negative integers The next example fails because one cell is indexed by rational numbers:: @@ -483,12 +483,12 @@ def check(self): sage: D = Diagram([(0,0), (0,3), (2/3,2), (2,4)]) Traceback (most recent call last): ... - ValueError: Diagrams must be indexed by non-negative integers + ValueError: diagrams must be indexed by non-negative integers """ from sage.sets.non_negative_integers import NonNegativeIntegers NN = NonNegativeIntegers() - if not all(all(list(i in NN for i in c)) for c in self._cells): - raise ValueError("Diagrams must be indexed by non-negative integers") + if not all(i in NN for c in self._cells for i in c): + raise ValueError("diagrams must be indexed by non-negative integers") def specht_module(self, base_ring=None): r""" @@ -884,7 +884,7 @@ def check(self): sage: NorthwestDiagram([(0,1/2)]) Traceback (most recent call last): ... - ValueError: Diagrams must be indexed by non-negative integers + ValueError: diagrams must be indexed by non-negative integers """ from itertools import combinations Diagram.check(self) diff --git a/src/sage/combinat/gelfand_tsetlin_patterns.py b/src/sage/combinat/gelfand_tsetlin_patterns.py index 1c2b40393e3..676c1685484 100644 --- a/src/sage/combinat/gelfand_tsetlin_patterns.py +++ b/src/sage/combinat/gelfand_tsetlin_patterns.py @@ -53,7 +53,7 @@ class GelfandTsetlinPattern(ClonableArray, - metaclass=InheritComparisonClasscallMetaclass): + metaclass=InheritComparisonClasscallMetaclass): r""" A Gelfand-Tsetlin (sometimes written as Gelfand-Zetlin or Gelfand-Cetlin) pattern. They were originally defined in [GC50]_. @@ -197,7 +197,7 @@ def _repr_diagram(self) -> str: for i, row in enumerate(self): if i != 0: ret += '\n' - ret += ' '*i + ret += ' ' * i ret += ' '.join('%3s' % val for val in row) return ret @@ -234,13 +234,13 @@ def _latex_(self) -> str: n = len(self) if n == 0: return "\\emptyset" - ret = "\\begin{array}{" + 'c'*(n*2-1) + "}\n" + ret = "\\begin{array}{" + 'c' * (n * 2 - 1) + "}\n" for i, row in enumerate(self): if i > 0: ret += " \\\\\n" - ret += "& "*i + ret += "& " * i ret += " & & ".join(repr(val) for val in row) - ret += " &"*i + ret += " &" * i return ret + "\n\\end{array}" @combinatorial_map(name='to semistandard tableau') @@ -282,9 +282,9 @@ def to_tableau(self): if j >= len(ret): if val == 0: break - ret.append([i+1]*val) + ret.append([i + 1] * val) else: - ret[j].extend([i+1]*(val-len(ret[j]))) + ret[j].extend([i + 1] * (val - len(ret[j]))) S = SemistandardTableaux(max_entry=len(self)) return S(ret) @@ -306,7 +306,7 @@ def boxed_entries(self) -> tuple: ret = [] for i in range(1, len(self)): for j in range(len(self[i])): - if self[i][j] == self[i-1][j]: + if self[i][j] == self[i - 1][j]: ret.append((i, j)) return tuple(ret) @@ -328,7 +328,7 @@ def circled_entries(self) -> tuple: ret = [] for i in range(1, len(self)): for j in range(len(self[i])): - if self[i][j] == self[i-1][j+1]: + if self[i][j] == self[i - 1][j + 1]: ret.append((i, j)) return tuple(ret) @@ -457,7 +457,7 @@ def weight(self) -> tuple: sage: G.weight() (2, 2, 3) """ - wt = [self.row_sums()[-1]] + [self.row_sums()[i-1]-self.row_sums()[i] for i in reversed(range(1, len(self[0])))] + wt = [self.row_sums()[-1]] + [self.row_sums()[i - 1] - self.row_sums()[i] for i in reversed(range(1, len(self[0])))] return tuple(wt) def Tokuyama_coefficient(self, name='t'): @@ -502,7 +502,7 @@ def Tokuyama_coefficient(self, name='t'): t = R.gen(0) if not self.is_strict(): return R.zero() - return (t+1)**(self.number_of_special_entries()) * t**(self.number_of_boxes()) + return (t + 1)**self.number_of_special_entries() * t**self.number_of_boxes() @combinatorial_map(order=2, name='Bender-Knuth involution') def bender_knuth_involution(self, i) -> GelfandTsetlinPattern: @@ -703,15 +703,16 @@ def __contains__(self, gt): if self._n is not None and len(gt) != self._n: return False # Check if it has the correct maximum value - if self._k is not None and any( val > self._k for row in gt for val in row ): + if self._k is not None and any(val > self._k for row in gt + for val in row): return False # Check if it is a GT pattern - if not all( gt[i-1][j] >= gt[i][j] >= gt[i-1][j+1] - for i in range(1, len(gt)) for j in range(len(gt[i])) ): + if not all(gt[i-1][j] >= gt[i][j] >= gt[i-1][j+1] + for i in range(1, len(gt)) for j in range(len(gt[i]))): return False # Check if it is strict if applicable - if self._strict and any( gt[i][j] == gt[i][j-1] for i in range(len(gt)) - for j in range(1, len(gt[i])) ): + if self._strict and any(gt[i][j] == gt[i][j-1] for i in range(len(gt)) + for j in range(1, len(gt[i]))): return False return True @@ -965,7 +966,7 @@ def _top_row_iter(self, n): continue # If it would create an invalid entry, backstep if (pos > 0 and (row[pos] >= row[pos-1] - or (self._strict and row[pos] == row[pos-1]-1)) ) \ + or (self._strict and row[pos] == row[pos-1]-1))) \ or (self._k is not None and row[pos] >= self._k): row[pos] = -1 pos -= 1 @@ -1052,8 +1053,7 @@ def _toggle_markov_chain(self, chain_state, row, col, direction): sage: G._toggle_markov_chain(state, 0, 2, 0) sage: state [[4, 2, 0], [3, 2], [2]] - - """ + """ if direction == 1: upbound = self._k if row != 0: @@ -1130,8 +1130,7 @@ def _cftp(self, start_row): True """ from sage.misc.randstate import current_randstate - from sage.misc.randstate import seed - from sage.misc.randstate import random + from sage.misc.randstate import seed, random count = self._n * self._k seedlist = [(current_randstate().long_seed(), count)] @@ -1148,7 +1147,8 @@ def _cftp(self, start_row): direction = random() % 2 self._toggle_markov_chain(upper, row, col, direction) self._toggle_markov_chain(lower, row, col, direction) - if all(all(x == y for x, y in zip(l1, l2)) for l1, l2 in zip(upper, lower)): + if all(x == y for l1, l2 in zip(upper, lower) + for x, y in zip(l1, l2)): break count = seedlist[0][1] * 2 seedlist.insert(0, (current_randstate().long_seed(), count)) @@ -1283,7 +1283,7 @@ def __iter__(self): [[4, 2, 1], [4, 2], [4]]] """ # If we enforce strictness, check to see if a specified top row is strict - if self._strict and any(self._row[i] == self._row[i+1] for i in range(self._n-1)): + if self._strict and any(self._row[i] == self._row[i + 1] for i in range(self._n - 1)): return if self._n == 0: yield self.element_class(self, []) @@ -1292,8 +1292,8 @@ def __iter__(self): yield self.element_class(self, [list(self._row)]) return # Setup the first row - iters = [None]*self._n - ret = [None]*self._n + iters = [None] * self._n + ret = [None] * self._n ret[0] = list(self._row) min_pos = 1 iters[1] = self._row_iter(ret[0]) @@ -1307,7 +1307,7 @@ def __iter__(self): yield self.element_class(self, ret[:]) pos -= 1 continue - iters[pos] = self._row_iter(ret[pos-1]) + iters[pos] = self._row_iter(ret[pos - 1]) except StopIteration: pos -= 1 @@ -1358,12 +1358,12 @@ def Tokuyama_formula(self, name='t'): 0 """ n = self._n - variables = [name] + ["x%d" % i for i in range(1, n+1)] + variables = [name] + ["x%d" % i for i in range(1, n + 1)] R = PolynomialRing(ZZ, names=variables) t = R.gen(0) x = R.gens()[1:] GT = GelfandTsetlinPatterns(top_row=self._row, strict=True) - return sum((t+1)**(gt.number_of_special_entries()) * t**(gt.number_of_boxes()) * prod(x[i]**gt.weight()[i] for i in range(n)) for gt in GT) + return sum((t + 1)**gt.number_of_special_entries() * t**gt.number_of_boxes() * prod(x[i]**gt.weight()[i] for i in range(n)) for gt in GT) def _cftp_upper(self) -> list: """ diff --git a/src/sage/combinat/path_tableaux/frieze.py b/src/sage/combinat/path_tableaux/frieze.py index b1bbdab1ed7..fd33a72ff26 100644 --- a/src/sage/combinat/path_tableaux/frieze.py +++ b/src/sage/combinat/path_tableaux/frieze.py @@ -295,7 +295,8 @@ def is_integral(self): """ n = len(self) cd = CylindricalDiagram(self).diagram - return all(all(k in ZZ for k in a[i+1:n+i-2]) for i, a in enumerate(cd)) + return all(k in ZZ for i, a in enumerate(cd) + for k in a[i + 1:n + i - 2]) def triangulation(self): r""" diff --git a/src/sage/combinat/path_tableaux/semistandard.py b/src/sage/combinat/path_tableaux/semistandard.py index 5fefebfe51a..f92dfcdd6c8 100644 --- a/src/sage/combinat/path_tableaux/semistandard.py +++ b/src/sage/combinat/path_tableaux/semistandard.py @@ -246,7 +246,7 @@ def is_skew(self): """ return bool(self[0]) - def is_integral(self): + def is_integral(self) -> bool: """ Return ``True`` if all entries are non-negative integers. @@ -259,7 +259,7 @@ def is_integral(self): sage: path_tableaux.SemistandardPathTableau([[],[3],[3,-2]]).is_integral() False """ - return all(all(i in NN for i in a) for a in self) + return all(i in NN for a in self for i in a) def local_rule(self, i): r""" diff --git a/src/sage/combinat/plane_partition.py b/src/sage/combinat/plane_partition.py index 642bcca5ed0..58b84afddd0 100644 --- a/src/sage/combinat/plane_partition.py +++ b/src/sage/combinat/plane_partition.py @@ -213,7 +213,7 @@ def check(self): ValueError: entries not all integers """ - if not all(all(a in ZZ for a in b) for b in self): + if not all(a in ZZ for b in self for a in b): raise ValueError("entries not all integers") for row in self: if not all(c >= 0 for c in row): @@ -1374,7 +1374,7 @@ def __contains__(self, pp): if isinstance(pp, (list, tuple)): if not pp: return True - if not all(all(a in ZZ for a in b) for b in pp): + if not all(a in ZZ for b in pp for a in b): return False for row in pp: if not all(c >= 0 for c in row): @@ -1852,7 +1852,8 @@ def to_poset(self): def comp(x, y): return all(a <= b for a, b in zip(x, y)) - pl = [(x, y, z) for x in range(a) for y in range(x+1) for z in range(c)] + pl = [(x, y, z) for x in range(a) for y in range(x + 1) + for z in range(c)] from sage.combinat.posets.posets import Poset return Poset((pl, comp)) @@ -2048,7 +2049,7 @@ def comp2(x, y): return comp(x, y) or comp(x, (y[2], y[0], y[1])) or comp(x, (y[1], y[2], y[0])) pl = [(x, y, z) for x in range(a) for y in range(b) for z in range(x, c) - if y <= z and (x != z or y == x)] + if y <= z and (x != z or y == x)] from sage.combinat.posets.posets import Poset return Poset((pl, comp2)) diff --git a/src/sage/combinat/ribbon_shaped_tableau.py b/src/sage/combinat/ribbon_shaped_tableau.py index ef860b9703d..4f17dc6008e 100644 --- a/src/sage/combinat/ribbon_shaped_tableau.py +++ b/src/sage/combinat/ribbon_shaped_tableau.py @@ -78,6 +78,18 @@ def __classcall_private__(cls, rows): sage: RibbonShapedTableau([[2,3],[1,4,5]]) [[None, None, 2, 3], [1, 4, 5]] + + TESTS:: + + sage: RibbonShapedTableau([4,5]) + Traceback (most recent call last): + ... + TypeError: rows must be lists of positive integers + + sage: RibbonShapedTableau([[2,3],[-4,5]]) + Traceback (most recent call last): + ... + TypeError: r must be a list of positive integers """ try: r = [tuple(r) for r in rows] @@ -85,7 +97,8 @@ def __classcall_private__(cls, rows): raise TypeError("rows must be lists of positive integers") if not r: return StandardRibbonShapedTableaux()(r) - if all(all(j is None or (isinstance(j, (int, Integer)) and j>0) for j in i) for i in r): + if all(j is None or (isinstance(j, (int, Integer)) and j > 0) + for i in r for j in i): return StandardRibbonShapedTableaux()(r) raise TypeError("r must be a list of positive integers") diff --git a/src/sage/combinat/symmetric_group_algebra.py b/src/sage/combinat/symmetric_group_algebra.py index d7a4439154f..ef328aff704 100644 --- a/src/sage/combinat/symmetric_group_algebra.py +++ b/src/sage/combinat/symmetric_group_algebra.py @@ -1339,14 +1339,13 @@ def rsw_shuffling_element(self, k): sage: def test_rsw_comm(n): ....: QSn = SymmetricGroupAlgebra(QQ, n) ....: rsws = [QSn.rsw_shuffling_element(k) for k in range(2, n)] - ....: return all( all( rsws[i] * rsws[j] == rsws[j] * rsws[i] - ....: for j in range(i) ) - ....: for i in range(len(rsws)) ) + ....: return all(ri * rsws[j] == rsws[j] * ri + ....: for i, ri in enumerate(rsws) for j in range(i)) sage: test_rsw_comm(3) True - sage: test_rsw_comm(4) + sage: test_rsw_comm(4) # long time True - sage: test_rsw_comm(5) # long time + sage: test_rsw_comm(5) # not tested True .. NOTE:: diff --git a/src/sage/geometry/fan_morphism.py b/src/sage/geometry/fan_morphism.py index e94e2cce8c0..7f2e30c10a4 100644 --- a/src/sage/geometry/fan_morphism.py +++ b/src/sage/geometry/fan_morphism.py @@ -1393,8 +1393,8 @@ def is_injective(self): return prod(self.factor()[1:]).is_injective() # Now we know that underlying lattice morphism is bijective. Sigma = self.domain_fan() - return all(all(self.image_cone(sigma).dim() == d for sigma in Sigma(d)) - for d in range(1, Sigma.dim() + 1)) + return all(self.image_cone(sigma).dim() == d + for d in range(1, Sigma.dim() + 1) for sigma in Sigma(d)) @cached_method def is_surjective(self): diff --git a/src/sage/geometry/polyhedron/base_ZZ.py b/src/sage/geometry/polyhedron/base_ZZ.py index 35b5346f924..b9b36bd6a48 100644 --- a/src/sage/geometry/polyhedron/base_ZZ.py +++ b/src/sage/geometry/polyhedron/base_ZZ.py @@ -511,14 +511,14 @@ def polar(self): if not self.has_IP_property(): raise ValueError('The polytope must have the IP property.') - vertices = tuple( ieq.A()/ieq.b() for - ieq in self.inequality_generator() ) + vertices = tuple(ieq.A() / ieq.b() for + ieq in self.inequality_generator()) ieqs = ((1,) + tuple(v[:]) for v in self.vertices()) pref_rep = 'Hrep' if self.n_vertices() <= self.n_inequalities() else 'Vrep' - if all( all(v_i in ZZ for v_i in v) for v in vertices): + if all(v_i in ZZ for v in vertices for v_i in v): parent = self.parent() vertices = (v.change_ring(ZZ) for v in vertices) else: diff --git a/src/sage/geometry/polyhedron/constructor.py b/src/sage/geometry/polyhedron/constructor.py index 0a8cc1854ac..559a8459f4f 100644 --- a/src/sage/geometry/polyhedron/constructor.py +++ b/src/sage/geometry/polyhedron/constructor.py @@ -722,7 +722,8 @@ def Polyhedron(vertices=None, rays=None, lines=None, if base_ring not in Fields(): got_compact_Vrep = got_Vrep and not rays and not lines - got_cone_Vrep = got_Vrep and all(all(x == 0 for x in v) for v in vertices) + got_cone_Vrep = got_Vrep and all(x == 0 + for v in vertices for x in v) if not got_compact_Vrep and not got_cone_Vrep: base_ring = base_ring.fraction_field() convert = True @@ -743,7 +744,7 @@ def Polyhedron(vertices=None, rays=None, lines=None, raise ValueError("the only allowed inexact ring is 'RDF' with backend 'cdd'") # Add the origin if necessary - if got_Vrep and len(vertices) == 0 and len(rays + lines) > 0: + if got_Vrep and len(vertices) == 0 and bool(rays + lines): vertices = [[0] * ambient_dim] # Specific backends can override the base_ring