Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for #36198: fix return types in permutation.py so that methods return type Integer instead of type int. #36199

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/sage/combinat/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def size(self) -> Integer:
sage: Permutation([3,4,1,2,5]).size()
5
"""
return len(self)
return Integer(len(self))

grade = size # for the category SetsWithGrading()

Expand Down Expand Up @@ -1167,7 +1167,7 @@ def signature(self) -> Integer:
sage: Permutation([]).sign()
1
"""
return (-1)**(len(self)-len(self.to_cycles()))
return Integer((-1)**(len(self)-len(self.to_cycles())))

#one can also use sign as an alias for signature
sign = signature
Expand Down Expand Up @@ -1476,21 +1476,23 @@ def to_inversion_vector(self):
l = len(p)
# lightning fast if the length is less than 3
# (is it really useful?)
zero = Integer(0)
one = Integer(1)
if l < 4:
if l == 0:
return []
if l == 1:
return [0]
return [zero]
if l == 2:
return [p[0] - 1, 0]
return [Integer(p[0] - 1), zero]
if l == 3:
if p[0] == 1:
return [0, p[1] - 2, 0]
return [zero, Integer(p[1] - 2), zero]
if p[0] == 2:
if p[1] == 1:
return [1, 0, 0]
return [2, 0, 0]
return [p[1], 1, 0]
return [one, zero, zero]
return [Integer(2), zero, zero]
return [Integer(p[1]), one, zero]
# choose the best one
if l < 411:
return self._to_inversion_vector_small()
Expand Down Expand Up @@ -1548,7 +1550,7 @@ def _to_inversion_vector_small(self):
for pi in reversed(p):
checked[pi] = 0
iv[pi] = sum(checked[pi:])
return iv[1:]
return [Integer(a) for a in iv[1:]]

def _to_inversion_vector_divide_and_conquer(self):
r"""
Expand Down Expand Up @@ -1614,7 +1616,7 @@ def sort_and_countv(L):
return merge_and_countv(sort_and_countv(L[:l]),
sort_and_countv(L[l:]))

return sort_and_countv(self._list)[0]
return [Integer(a) for a in sort_and_countv(self._list)[0]]

def inversions(self) -> list:
r"""
Expand Down Expand Up @@ -1883,8 +1885,8 @@ def number_of_noninversions(self, k) -> Integer:
"""
if k > len(self):
return 0
return sum(1 for pos in itertools.combinations(self, k)
if all(pos[i] < pos[i + 1] for i in range(k - 1)))
return Integer(sum(1 for pos in itertools.combinations(self, k)
if all(pos[i] < pos[i + 1] for i in range(k - 1))))

def length(self) -> Integer:
r"""
Expand Down Expand Up @@ -1934,7 +1936,7 @@ def inverse(self) -> Permutation:
"""
w = list(range(len(self)))
for i, j in enumerate(self):
w[j - 1] = i + 1
w[j - 1] = Integer(i + 1)
return Permutations()(w)

__invert__ = inverse
Expand Down Expand Up @@ -2239,7 +2241,7 @@ def longest_increasing_subsequence_length(self) -> Integer:
else:
# We replace y by x
r[idx] = x
return len(r)
return Integer(len(r))

def longest_increasing_subsequences(self):
r"""
Expand Down Expand Up @@ -3082,8 +3084,7 @@ def number_of_fixed_points(self) -> Integer:
sage: Permutation([1,2,3,4]).number_of_fixed_points()
4
"""

return len(self.fixed_points())
return Integer(len(self.fixed_points()))

def is_derangement(self) -> bool:
r"""
Expand Down Expand Up @@ -3209,7 +3210,7 @@ def number_of_recoils(self) -> Integer:
sage: Permutation([1,4,3,2]).number_of_recoils()
2
"""
return len(self.recoils())
return Integer(len(self.recoils()))

def recoils_composition(self) -> Composition:
r"""
Expand Down Expand Up @@ -3382,7 +3383,7 @@ def number_of_descents(self, final_descent=False) -> Integer:
sage: Permutation([1,4,3,2]).number_of_descents(final_descent=True)
3
"""
return len(self.descents(final_descent))
return Integer(len(self.descents(final_descent)))

def number_of_idescents(self, final_descent=False) -> Integer:
r"""
Expand All @@ -3400,7 +3401,7 @@ def number_of_idescents(self, final_descent=False) -> Integer:
sage: Permutation([1,4,3,2]).number_of_idescents(final_descent=True)
3
"""
return len(self.idescents(final_descent))
return Integer(len(self.idescents(final_descent)))

@combinatorial_map(name='descent composition')
def descents_composition(self) -> Composition:
Expand Down Expand Up @@ -3497,7 +3498,7 @@ def major_index(self, final_descent=False) -> Integer:
6
"""
descents = self.descents(final_descent)
return sum(descents)
return Integer(sum(descents))

def multi_major_index(self, composition):
r"""
Expand Down Expand Up @@ -3566,7 +3567,7 @@ def imajor_index(self, final_descent=False) -> Integer:
sage: Permutation([4,3,2,1]).imajor_index()
6
"""
return sum(self.idescents(final_descent))
return Integer(sum(self.idescents(final_descent)))

def to_major_code(self, final_descent=False):
r"""
Expand Down Expand Up @@ -3655,7 +3656,7 @@ def number_of_peaks(self) -> Integer:
sage: Permutation([4,1,3,2,6,5]).number_of_peaks()
2
"""
return len(self.peaks())
return Integer(len(self.peaks()))

#############
# Saliances #
Expand Down Expand Up @@ -3701,7 +3702,7 @@ def number_of_saliances(self) -> Integer:
sage: Permutation([5,4,3,2,1]).number_of_saliances()
5
"""
return len(self.saliances())
return Integer(len(self.saliances()))

################
# Bruhat Order #
Expand Down
Loading