Skip to content

Commit

Permalink
Trac #27014: Deprecate sage.misc.misc.uniq
Browse files Browse the repository at this point in the history
`uniq(X)` is just a shorthand for `sorted(set(X))`: I don't think that
we need a separate function for that. More seriously, the sorting is
problematic since arbitrary sets may not be sortable in general,
especially in Python 3 or after applying #22029.

URL: https://trac.sagemath.org/27014
Reported by: jdemeyer
Ticket author(s): Jeroen Demeyer
Reviewer(s): Martin Rubey, Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Jan 23, 2019
2 parents c128368 + b4f67ca commit 4fb44bc
Show file tree
Hide file tree
Showing 34 changed files with 119 additions and 153 deletions.
24 changes: 12 additions & 12 deletions src/sage/categories/coxeter_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sage.misc.cachefunc import cached_method, cached_in_parent_method
from sage.misc.lazy_import import LazyImport
from sage.misc.constant_function import ConstantFunction
from sage.misc.misc import attrcall, uniq
from sage.misc.misc import attrcall
from sage.categories.category_singleton import Category_singleton
from sage.categories.enumerated_sets import EnumeratedSets
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
Expand Down Expand Up @@ -1926,14 +1926,15 @@ def bruhat_upper_covers(self):
sage: set(S) == set(C)
True
"""
Covers = []
Covers = set()
for i in self.parent().index_set():
if i in self.descents(side='right'):
Covers += [ x.apply_simple_reflection(i, side='right') for x in self.apply_simple_reflection(i,side='right').bruhat_upper_covers()
if i not in x.descents(side='right') ]
Covers.update(x.apply_simple_reflection(i, side='right')
for x in self.apply_simple_reflection(i,side='right').bruhat_upper_covers()
if i not in x.descents(side='right'))
else:
Covers += [ self.apply_simple_reflection(i,side='right') ]
return uniq(Covers)
Covers.add(self.apply_simple_reflection(i,side='right'))
return sorted(Covers)

@cached_in_parent_method
def bruhat_lower_covers_reflections(self):
Expand Down Expand Up @@ -1994,17 +1995,16 @@ def bruhat_upper_covers_reflections(self):
sage: w = W.from_reduced_word([3,1,2,1])
sage: w.bruhat_upper_covers_reflections()
[(s1*s2*s3*s2*s1, s3), (s2*s3*s1*s2*s1, s2*s3*s2), (s3*s4*s1*s2*s1, s4), (s4*s3*s1*s2*s1, s1*s2*s3*s4*s3*s2*s1)]
"""

Covers = []
Covers = set()
for i in self.parent().index_set():
wi = self.apply_simple_reflection(i)
if i in self.descents():
Covers += [(u.apply_simple_reflection(i), r.apply_conjugation_by_simple_reflection(i)) for u, r in wi.bruhat_upper_covers_reflections() if i not in u.descents()]
Covers.update((u.apply_simple_reflection(i), r.apply_conjugation_by_simple_reflection(i))
for u, r in wi.bruhat_upper_covers_reflections() if i not in u.descents())
else:
Covers += [(wi, self.parent().simple_reflection(i))]
return uniq(Covers)
Covers.add((wi, self.parent().simple_reflection(i)))
return sorted(Covers)

def cover_reflections(self, side='right'):
r"""
Expand Down
9 changes: 3 additions & 6 deletions src/sage/combinat/combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from sage.arith.all import binomial
from .combinat import CombinatorialClass
from .integer_vector import IntegerVectors
from sage.misc.misc import uniq


def Combinations(mset, k=None):
Expand Down Expand Up @@ -209,8 +208,7 @@ def __contains__(self, x):
except TypeError:
return False

return all(i in self.mset for i in x) and len(uniq(x)) == len(x)

return all(i in self.mset for i in x) and len(set(x)) == len(x)

def __repr__(self):
"""
Expand Down Expand Up @@ -345,10 +343,9 @@ def __iter__(self):
sage: Combinations(['a','a','b'],2).list() # indirect doctest
[['a', 'a'], ['a', 'b']]
"""
items = map(self.mset.index, self.mset)
indices = uniq(sorted(items)) # this consumes "items" in python3
items = [self.mset.index(x) for x in self.mset]
indices = sorted(set(items))
counts = [0] * len(indices)
items = map(self.mset.index, self.mset)
for i in items:
counts[indices.index(i)] += 1
for iv in IntegerVectors(self.k, len(indices), outer=counts):
Expand Down
11 changes: 7 additions & 4 deletions src/sage/combinat/k_tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from sage.combinat.root_system.weyl_group import WeylGroup
from sage.combinat.core import Core
from sage.rings.all import ZZ
from sage.misc.misc import uniq
from sage.functions.generalized import sgn
from sage.misc.flatten import flatten
from sage.combinat.skew_partition import SkewPartition
Expand Down Expand Up @@ -918,7 +917,11 @@ def residues_of_entries(self, v):
sage: t.residues_of_entries(1)
[2, 3]
"""
return uniq([(j - i)%(self.k+1) for i in range(len(self)) for j in range(len(self[i])) if self[i][j] == v])
S = set((j - i) % (self.k+1)
for i in range(len(self))
for j in range(len(self[i]))
if self[i][j] == v)
return sorted(S)

def dictionary_of_coordinates_at_residues(self, v):
r"""
Expand Down Expand Up @@ -2517,7 +2520,7 @@ def _is_valid_marked( self ):
T = self.to_standard_list()
size = Core([len(t) for t in T], self.k+1).length()
inner_size = Core([y for y in (len([x for x in row if x is None]) for row in T) if y > 0], self.k+1).length()
if len(uniq([v for v in flatten(list(T)) if v in ZZ and v<0]))!=size-inner_size:
if len(set(v for v in flatten(list(T)) if v in ZZ and v < 0)) != size - inner_size:
return False # TT does not have exactly self.size() marked cells
for i in range(len(T)):
for j in range(len(T[i])):
Expand Down Expand Up @@ -3174,7 +3177,7 @@ def height_of_ribbon(self, v):
sage: StrongTableau([],4).height_of_ribbon(1)
0
"""
return len(uniq([c[0] for c in self.cells_of_marked_ribbon(v)]))
return len(set(c[0] for c in self.cells_of_marked_ribbon(v)))

def number_of_connected_components(self, v):
r"""
Expand Down
13 changes: 4 additions & 9 deletions src/sage/combinat/matrices/latin.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
from sage.groups.perm_gps.permgroup import PermutationGroup
from sage.arith.all import is_prime
from sage.rings.finite_rings.finite_field_constructor import FiniteField
from sage.misc.misc import uniq
from sage.misc.flatten import flatten

from .dlxcpp import DLXCPP
Expand Down Expand Up @@ -475,8 +474,7 @@ def is_empty_column(self, c):
sage: L.is_empty_column(0)
True
"""

return uniq(self.column(c)) == [-1]
return list(set(self.column(c))) == [-1]

def is_empty_row(self, r):
"""
Expand All @@ -492,8 +490,7 @@ def is_empty_row(self, r):
sage: L.is_empty_row(0)
True
"""

return uniq(self.row(r)) == [-1]
return list(set(self.row(r))) == [-1]

def nr_distinct_symbols(self):
"""
Expand All @@ -513,10 +510,8 @@ def nr_distinct_symbols(self):
sage: L.nr_distinct_symbols()
2
"""

symbols = uniq(flatten([list(x) for x in list(self.square)]))
symbols = set(flatten([list(x) for x in list(self.square)]))
symbols = [x for x in symbols if x >= 0]

return len(symbols)

def apply_isotopism(self, row_perm, col_perm, sym_perm):
Expand Down Expand Up @@ -1193,7 +1188,7 @@ def disjoint_mate_dlxcpp_rows_and_map(self, allow_subtrade):
# If this is an empty cell of self then we do nothing.
if self[r, c] < 0: continue

for e in uniq(list(valsrow) + list(valscol)):
for e in sorted(set(list(valsrow) + list(valscol))):
# These should be constants
c_OFFSET = e + c*n
r_OFFSET = e + r*n + n*n
Expand Down
5 changes: 2 additions & 3 deletions src/sage/combinat/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@
from sage.graphs.digraph import DiGraph
import itertools
from .combinat import CombinatorialElement, catalan_number
from sage.misc.misc import uniq
from sage.misc.cachefunc import cached_method
from .backtrack import GenericBacktracker
from sage.combinat.combinatorial_map import combinatorial_map
Expand Down Expand Up @@ -4095,7 +4094,7 @@ def permutohedron_join(self, other, side="right"):
must_be_right = [f for f in self[u + 1:] if f < i]
v = other.index(i)
must_be_right += [f for f in other[v + 1:] if f < i]
must_be_right = uniq(sorted(must_be_right))
must_be_right = sorted(set(must_be_right))
for j, q in enumerate(xs):
if q in must_be_right:
xs = xs[:j] + [i] + xs[j:]
Expand Down Expand Up @@ -5978,7 +5977,7 @@ def __contains__(self, x):
if len(x) != self.k:
return False
s = list(self._set)
return all(i in s for i in x) and len(uniq(x)) == len(x)
return all(i in s for i in x) and len(set(x)) == len(x)

def _repr_(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions src/sage/combinat/posets/hasse_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,7 @@ def is_antichain_of_poset(self, elms):
False
"""
from itertools import combinations
from sage.misc.misc import uniq

elms_sorted = uniq(elms)
elms_sorted = sorted(set(elms))
return not any(self.is_lequal(a, b) for a, b in
combinations(elms_sorted, 2))

Expand Down
3 changes: 1 addition & 2 deletions src/sage/combinat/posets/lattices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3500,8 +3500,7 @@ def day_doubling(self, S):
# subset S, but we assume that the user made an error
# if S is not also connected.

from sage.misc.misc import uniq
S = uniq(S)
S = sorted(set(S))
S_ = [self._element_to_vertex(e) for e in S]
if not self._hasse_diagram.is_convex_subset(S_):
raise ValueError("subset S is not convex")
Expand Down
5 changes: 2 additions & 3 deletions src/sage/combinat/posets/poset_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,10 @@ def SymmetricGroupBruhatIntervalPoset(start, end):
sage: P2 = posets.SymmetricGroupBruhatIntervalPoset([1,2,3,4], [4,2,3,1])
sage: ranks1 = [P1.rank(v) for v in P1]
sage: ranks2 = [P2.rank(v) for v in P2]
sage: [ranks1.count(i) for i in uniq(ranks1)]
sage: [ranks1.count(i) for i in sorted(set(ranks1))]
[1, 3, 5, 4, 1]
sage: [ranks2.count(i) for i in uniq(ranks2)]
sage: [ranks2.count(i) for i in sorted(set(ranks2))]
[1, 3, 5, 6, 4, 1]
"""
start = Permutation(start)
end = Permutation(end)
Expand Down
22 changes: 6 additions & 16 deletions src/sage/combinat/posets/posets.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
# python3
from __future__ import division, print_function, absolute_import

from six.moves import range
from six.moves import range, builtins
from six import iteritems

import copy
Expand Down Expand Up @@ -1464,14 +1464,10 @@ def sorted(self, l, allow_incomparable=True, remove_duplicates=False):
sage: P.sorted([], allow_incomparable=False, remove_duplicates=False)
[]
"""
from sage.misc.misc import uniq

v = [self._element_to_vertex(x) for x in l]

if remove_duplicates:
o = uniq(v)
else:
o = sorted(v)
v = set(v)
o = sorted(v)

if not allow_incomparable:
H = self._hasse_diagram
Expand Down Expand Up @@ -4072,19 +4068,15 @@ def isomorphic_subposets(self, other):
If this function takes too much time, try using
:meth:`isomorphic_subposets_iterator`.
"""
from sage.misc.misc import uniq

if not hasattr(other, 'hasse_diagram'):
raise TypeError("'other' is not a finite poset")
L = self._hasse_diagram.transitive_closure().subgraph_search_iterator(other._hasse_diagram.transitive_closure(), induced=True)
# Since subgraph_search_iterator returns labelled copies, we
# remove duplicates.
return [self.subposet([self._list[i] for i in x]) for x in uniq([frozenset(y) for y in L])]
return [self.subposet([self._list[i] for i in x]) for x in sorted(set(frozenset(y) for y in L))]

from six.moves import builtins
# Caveat: list is overridden by the method list above!!!

def antichains(self, element_constructor = builtins.list):
def antichains(self, element_constructor=builtins.list):
"""
Return the antichains of the poset.
Expand Down Expand Up @@ -5710,10 +5702,8 @@ def subposet(self, elements):
...
TypeError: 'sage.rings.integer.Integer' object is not iterable
"""
from sage.misc.misc import uniq

H = self._hasse_diagram
elms = uniq([self._element_to_vertex(e) for e in elements])
elms = sorted(set(self._element_to_vertex(e) for e in elements))

if not elms:
return Poset()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/rooted_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def graft_list(self, other):
sage: x = RootedTree([[[], []], []])
sage: y = RootedTree([[], []])
sage: len(uniq(x.graft_list(y)))
sage: len(set(x.graft_list(y)))
4
"""
resu = []
Expand Down
6 changes: 3 additions & 3 deletions src/sage/combinat/sf/sfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
from sage.categories.tensor import tensor
from sage.combinat.free_module import CombinatorialFreeModule
from sage.matrix.constructor import matrix
from sage.misc.all import prod, uniq
from sage.misc.all import prod
from copy import copy
from functools import reduce

Expand Down Expand Up @@ -2365,7 +2365,7 @@ def _inner_plethysm_pk_g(self, k, g, cache):

p = self.realization_of().p()
res = 0
degrees = uniq([ sum(m) for m in g.support() ])
degrees = sorted(set(sum(m) for m in g.support()))
for d in degrees:
for mu in Partitions_n(d):
mu_k = mu.power(k)
Expand Down Expand Up @@ -2432,7 +2432,7 @@ def _inner_plethysm_pnu_g(self, p_x, cache, nu):
if not nu._list:
s = self.realization_of().s()
degrees = [ part.size() for part in p_x.support() ]
degrees = uniq(degrees)
degrees = sorted(set(degrees))
if 0 in degrees:
ext = self([])
else:
Expand Down
3 changes: 1 addition & 2 deletions src/sage/combinat/species/product_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ def automorphism_group(self):
[{2, 3}*{1, 4}, {2, 3}*{1, 4}, {2, 3}*{1, 4}, {2, 3}*{1, 4}]
"""
from sage.groups.all import PermutationGroupElement, PermutationGroup
from sage.misc.misc import uniq
from sage.combinat.species.misc import change_support

left, right = self._list
Expand All @@ -197,7 +196,7 @@ def automorphism_group(self):

gens = l_aut.gens() + r_aut.gens()
gens = [g for g in gens if g != identity]
gens = uniq(gens) if gens else [[]]
gens = sorted(set(gens)) if gens else [[]]
return PermutationGroup(gens)


Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/superpartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
from sage.structure.global_options import GlobalOptions
from sage.rings.all import ZZ
from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass
from sage.misc.all import uniq


@richcmp_method
@add_metaclass(InheritComparisonClasscallMetaclass)
Expand Down Expand Up @@ -701,7 +701,7 @@ def add_horizontal_border_strip_star(self, h):
# TODO: Check that this is not suppose to be
# a tuple of size 1
+ [(i) for i in circ_list if row_changed[i[0]] == 0]]
if len(uniq([k for (j,k) in new_sp[1]])) == len(new_sp[1]):
if len(set([k for (j,k) in new_sp[1]])) == len(new_sp[1]):
out += [SuperPartition.from_circled_diagram(*new_sp)]
return out

Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
import sage.misc.prandom as random
from sage.combinat import permutation
from sage.groups.perm_gps.permgroup import PermutationGroup
from sage.misc.all import uniq, prod
from sage.misc.all import prod
from sage.misc.misc import powerset
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
Expand Down Expand Up @@ -1998,7 +1998,7 @@ def k_weight(self, k):
if new_s == []:
res.append(0)
continue
x = uniq([ (i-j)%(k+1) for i,j in new_s ])
x = set((i-j) % (k+1) for i, j in new_s)
res.append(len(x))

return res
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/yang_baxter_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def __iter__(self):
sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator
sage: ops = [SwapIncreasingOperator(i) for i in range(4)]
sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops)
sage: uniq(Y.__iter__())
sage: sorted(set(Y))
[(1, 0, 2, 1, 0), (1, 2, 0, 1, 0), (1, 2, 1, 0, 0), (2, 1, 0, 1, 0), (2, 1, 1, 0, 0)]
"""
return self._digraph.vertex_iterator()
Expand Down

0 comments on commit 4fb44bc

Please sign in to comment.