Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Added deprecation to IntegerListLex global_options arg. Fixed doctests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Scrimshaw committed Dec 27, 2013
1 parent 4143c96 commit 1adb368
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 45 deletions.
9 changes: 9 additions & 0 deletions src/sage/combinat/integer_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ def __init__(self,
sage: C.cardinality().parent() is ZZ
True
sage: TestSuite(C).run()
sage: C = IntegerListsLex(2, global_options=Partitions.global_options)
doctest:...: DeprecationWarning: the global_options argument is deprecated
since, in general, pickling is broken; create your own class instead
See http://trac.sagemath.org/15525 for details.
"""
# Convert to float infinity
from sage.rings.infinity import infinity
Expand All @@ -943,6 +947,11 @@ def __init__(self,
if max_part == infinity:
max_part = float('+inf')

if global_options is not None:
from sage.misc.superseded import deprecation
deprecation(15525, 'the global_options argument is deprecated since, in general,'
' pickling is broken; create your own class instead')

if floor is None:
self.floor_list = []
elif isinstance(floor, __builtin__.list):
Expand Down
234 changes: 189 additions & 45 deletions src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3747,7 +3747,8 @@ def remove_horizontal_border_strip(self, k):
sage: Partition([5,3,1]).remove_horizontal_border_strip(6).list()
[]
The result is returned as an instance of :class:`IntegerListsLex`::
The result is returned as an instance of
:class:`Partitions_with_constraints`::
sage: Partition([5,3,1]).remove_horizontal_border_strip(5)
The subpartitions of [5, 3, 1] obtained by removing an horizontal border strip of length 5
Expand All @@ -3763,15 +3764,13 @@ def remove_horizontal_border_strip(self, k):
sage: Partition([]).remove_horizontal_border_strip(6).list()
[]
"""
return IntegerListsLex(n = self.size()-k,
min_length = len(self)-1,
max_length = len(self),
floor = self[1:]+[0],
ceiling = self[:],
max_slope = 0,
element_class = Partition,
global_options = Partitions.global_options,
name = "The subpartitions of %s obtained by removing an horizontal border strip of length %s"%(self,k))
return Partitions_with_constraints(n = self.size()-k,
min_length = len(self)-1,
max_length = len(self),
floor = self[1:]+[0],
ceiling = self[:],
max_slope = 0,
name = "The subpartitions of {} obtained by removing an horizontal border strip of length {}".format(self,k))

def k_conjugate(self, k):
r"""
Expand Down Expand Up @@ -4251,17 +4250,17 @@ class Partitions(UniqueRepresentation, Parent):
(see :trac:`15467`).
- ``regular=ell`` specifies that the partitions are `\ell`-regular,
and can only be combined with the ``max_part`` keyword if `n` is
not specified
and can only be combined with the ``max_length`` or ``max_part``, but
not both, keywords if `n` is not specified
The ``max_*`` versions, along with ``inner`` and ``ending``, work
analogously.
Right now, the ``parts_in``, ``starting``, and ``ending`` keyword
arguments are mutually exclusive, both of each other and of other
Right now, the ``parts_in``, ``starting``, ``ending``, and ``regular``
keyword arguments are mutually exclusive, both of each other and of other
keyword arguments. If you specify, say, ``parts_in``, all other
keyword arguments will be ignored; ``starting`` and ``ending`` work
the same way.
keyword arguments will be ignored; ``starting``, ``ending``, and
``regular`` work the same way.
EXAMPLES:
Expand Down Expand Up @@ -4339,6 +4338,16 @@ class Partitions(UniqueRepresentation, Parent):
sage: Partitions(10, min_part=2, length=3).list()
[[6, 2, 2], [5, 3, 2], [4, 4, 2], [4, 3, 3]]
Some examples using the ``regular`` keyword::
sage: Partitions(regular=4)
4-Regular Partitions
sage: Partitions(regular=4, max_length=3)
4-Regular Partitions with max length 3
sage: Partitions(regular=4, max_part=3)
4-Regular 3-Bounded Partitions
sage: Partitions(3, regular=4)
4-Regular Partitions of the integer 3
Here are some further examples using various constraints::
Expand Down Expand Up @@ -4396,7 +4405,7 @@ class Partitions(UniqueRepresentation, Parent):
sage: TestSuite(Partitions(0)).run()
sage: TestSuite(Partitions(5)).run()
sage: TestSuite(Partitions(5, min_part=2)).run() # Not tested: todo - IntegerListsLex needs to pickle properly
sage: TestSuite(Partitions(5, min_part=2)).run()
sage: repr( Partitions(5, min_part=2) )
'Partitions of the integer 5 satisfying constraints min_part=2'
Expand Down Expand Up @@ -4472,10 +4481,6 @@ def __classcall_private__(cls, n=None, **kwargs):
sage: P2 = Partitions(int(4))
sage: P is P2
True
sage: P = Partitions(4, length=2, parts_in=[3,1,1])
sage: P2 = Partitions(4, length=2, parts_in=(3,1,1))
sage: P is P2
True
"""
if n == infinity:
raise ValueError("n cannot be infinite")
Expand All @@ -4486,8 +4491,14 @@ def __classcall_private__(cls, n=None, **kwargs):
return Partitions_all_bounded(kwargs['max_part'])
if 'regular' in kwargs:
return RegularPartitions_all(kwargs['regular'])
elif len(kwargs) == 2 and 'max_part' in kwargs and 'regular' in kwargs:
return RegularPartitions_all_bounded(kwargs['regular'], kwargs['max_part'])
elif len(kwargs) == 2:
if 'regular' in kwargs:
if kwargs['regular'] < 2:
raise ValueError("the regularity must be at least 2")
if 'max_part' in kwargs:
return RegularPartitions_bounded(kwargs['regular'], kwargs['max_part'])
if 'max_length' in kwargs:
return RegularPartitions_truncated(kwargs['regular'], kwargs['max_length'])
raise ValueError("the size must be specified with any keyword argument")
return Partitions_all()
elif isinstance(n, (int,Integer)):
Expand Down Expand Up @@ -4541,6 +4552,9 @@ def __classcall_private__(cls, n=None, **kwargs):
del kwargs['inner']
return Partitions_with_constraints(n, **kwargs)

raise ValueError("n must be an integer or be equal to one of "
"None, NN, NonNegativeIntegers()")

def __init__(self, is_infinite=False):
"""
Initialize ``self``.
Expand Down Expand Up @@ -5937,11 +5951,9 @@ def __setstate__(self, data):
[[2, 1], [1, 1, 1]]
"""
n = data['n']
self.__class__ = IntegerListsLex
self.__class__ = Partitions_with_constraints
constraints = {'max_slope' : 0,
'min_part' : 1,
'element_class' : Partition,
'global_options' : Partitions.global_options}
'min_part' : 1}
constraints.update(data['constraints'])
self.__init__(n, **constraints)

Expand All @@ -5954,22 +5966,22 @@ class Partitions_with_constraints(IntegerListsLex):
sage: P = Partitions(6, inner=[1,1], max_slope=-1)
sage: list(P)
[[5, 1], [4, 2], [3, 2, 1]]
"""
def __init__(self, n, **kwargs):
"""
Initialize ``self``.
TESTS::
TESTS::
sage: P = Partitions(6, min_part=2, max_slope=-1)
sage: TestSuite(P).run()
sage: P = Partitions(6, min_part=2, max_slope=-1)
sage: TestSuite(P).run()
Test that :trac:`-1` is fixed::
Test that :trac:`15525` is fixed::
sage: loads(dumps(P)) == P
True
"""
IntegerListsLex.__init__(self, n, **kwargs)
sage: loads(dumps(P)) == P
True
"""
# def __init__(self, n, **kwargs):
# """
# Initialize ``self``.
# """
# IntegerListsLex.__init__(self, n, **kwargs)

Element = Partition
global_options = PartitionOptions
Expand All @@ -5983,6 +5995,11 @@ class RegularPartitions(Partitions):
Base class for `\ell`-regular partitions.
A partition is `\ell`-regular if `m_i < \ell` for all `i`.
INPUT:
- ``ell`` -- the value `\ell`
- ``is_infinite`` -- if the subset of `\ell`-regular partitions is infinite
"""
def __init__(self, ell, is_infinte=False):
"""
Expand Down Expand Up @@ -6015,6 +6032,8 @@ def __contains__(self, x):
True
sage: Partition([4,2,2,2]) in P
False
sage: Partition([10,1]) in P
True
"""
if not Partitions.__contains__(self, x):
return False
Expand Down Expand Up @@ -6052,6 +6071,14 @@ def _fast_iterator(self, n, max_part):
class RegularPartitions_all(RegularPartitions):
r"""
The class of all `\ell`-regular partitions.
INPUT:
- ``ell`` -- the value `\ell`
.. SEEALSO::
:class:`~sage.combinat.partition.RegularPartitions`
"""
def __init__(self, ell):
"""
Expand Down Expand Up @@ -6091,9 +6118,117 @@ def __iter__(self):
yield self.element_class(self, p)
n += 1

class RegularPartitions_all_bounded(RegularPartitions):
class RegularPartitions_truncated(RegularPartitions):
r"""
The class of `\ell`-regular partitions bounded by `k`.
The class of `\ell`-regular partitions with max length `k`.
INPUT:
- ``ell`` -- the value `\ell`
- ``max_len`` -- the maximum length
.. SEEALSO::
:class:`~sage.combinat.partition.RegularPartitions`
"""
def __init__(self, ell, max_len):
"""
Initialize ``self``.
EXAMPLES::
sage: P = Partitions(regular=4, max_length=3)
sage: TestSuite(P).run()
"""
self.max_len = max_len
RegularPartitions.__init__(self, ell)

def __contains__(self, x):
"""
TESTS::
sage: P = Partitions(regular=4, max_length=3)
sage: [3, 3, 3] in P
True
sage: [] in P
True
sage: [4, 2, 1, 1] in P
False
"""
return len(x) <= self.max_len and RegularPartitions.__contains__(self, x)

def _repr_(self):
"""
TESTS::
sage: from sage.combinat.partition import RegularPartitions_truncated
sage: RegularPartitions_truncated(4, 3)
4-Regular Partitions with max length 3
"""
return "{}-Regular Partitions with max length {}".format(self.ell, self.max_len)

def __iter__(self):
"""
Iterate over ``self``.
EXAMPLES::
sage: P = Partitions(regular=3, max_length=2)
sage: it = P.__iter__()
sage: [it.next() for x in range(10)]
[[], [1], [2], [1, 1], [3], [2, 1], [4], [3, 1], [2, 2], [5]]
"""
n = 0
while True:
for p in self._fast_iterator(n, n):
yield self.element_class(self, p)
n += 1

def _fast_iterator(self, n, max_part, depth=0):
"""
A fast (recursive) iterator which returns a list.
EXAMPLES::
sage: P = Partitions(regular=2, max_length=2)
sage: list(P._fast_iterator(5, 5))
[[5], [4, 1], [3, 2]]
sage: list(P._fast_iterator(5, 3))
[[3, 2]]
sage: list(P._fast_iterator(5, 6))
[[5], [4, 1], [3, 2]]
"""
if n == 0 or depth >= self.max_len:
yield []
return

# Special case
if depth + 1 == self.max_len:
if max_part >= n:
yield [n]
return

if n < max_part:
max_part = n
bdry = self.ell - 1

for i in reversed(range(1, max_part+1)):
for p in self._fast_iterator(n-i, i, depth+1):
if p.count(i) < bdry:
yield [i] + p

class RegularPartitions_bounded(RegularPartitions):
r"""
The class of `\ell`-regular `k`-bounded partitions.
INPUT:
- ``ell`` -- the value `\ell`
- ``k`` -- the value `k`
.. SEEALSO::
:class:`~sage.combinat.partition.RegularPartitions`
"""
def __init__(self, ell, k):
"""
Expand Down Expand Up @@ -6125,11 +6260,11 @@ def _repr_(self):
"""
TESTS::
sage: from sage.combinat.partition import RegularPartitions_all_bounded
sage: RegularPartitions_all_bounded(4, 3)
3-Bounded 4-Regular Partitions
sage: from sage.combinat.partition import RegularPartitions_bounded
sage: RegularPartitions_bounded(4, 3)
4-Regular 3-Bounded Partitions
"""
return "{}-Bounded {}-Regular Partitions".format(self.k, self.ell)
return "{}-Regular {}-Bounded Partitions".format(self.ell, self.k)

def __iter__(self):
"""
Expand All @@ -6149,6 +6284,15 @@ def __iter__(self):
class RegularPartitions_n(RegularPartitions, Partitions_n):
r"""
The class of `\ell`-regular partitions of `n`.
INPUT:
- ``n`` -- the integer `n` to partition
- ``ell`` -- the value `\ell`
.. SEEALSO::
:class:`~sage.combinat.partition.RegularPartitions`
"""
def __init__(self, n, ell):
"""
Expand Down

0 comments on commit 1adb368

Please sign in to comment.