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

Commit

Permalink
Fix kwargs, repr, and remove double .__init__ call
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorkarn committed Aug 12, 2022
1 parent 6b7906a commit 6d1395e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
86 changes: 29 additions & 57 deletions src/sage/combinat/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@
from sage.structure.parent import Parent
from sage.combinat.tableau import Tableau
from sage.combinat.skew_tableau import SkewTableaux
from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass


class Diagram(ClonableArray):
class Diagram(ClonableArray, metaclass=InheritComparisonClasscallMetaclass):
r"""
A class to model arbitrary combinatorial diagrams.
Expand All @@ -244,7 +245,7 @@ class Diagram(ClonableArray):
sage: TestSuite(D).run()
"""
@staticmethod
def __classcall_private__(self, cells, **kwargs):
def __classcall_private__(self, cells, n_rows=None, n_cols=None, check=False):
r"""
Normalize the input so that it lives in the correct parent.
Expand All @@ -255,9 +256,9 @@ def __classcall_private__(self, cells, **kwargs):
sage: D.parent()
Combinatorial diagrams
"""
return Diagrams()(cells, **kwargs)
return Diagrams()(cells, n_rows, n_cols, check)

def __init__(self, cells, **kwargs):
def __init__(self, parent, cells, n_rows=None, n_cols=None, check=False):
r"""
EXAMPLES::
Expand Down Expand Up @@ -301,8 +302,7 @@ def __init__(self, cells, **kwargs):
self._n_nonempty_rows = len(set(i for i, j in self._cells))
self._n_nonempty_cols = len(set(j for i, j in self._cells))


ClonableArray.__init__(self, Diagrams(), cells, check=False)
ClonableArray.__init__(self, parent, cells, check=False)

def _hash_(self):
r"""
Expand Down Expand Up @@ -332,7 +332,7 @@ def __contains__(self, other):
"""
return other in self._cells

def __repr__(self):
def _repr_(self):
r"""
EXAMPLES::
Expand Down Expand Up @@ -537,7 +537,7 @@ def __init__(self):

Parent.__init__(self, category=Sets())

def __repr__(self):
def _repr_(self):
r"""
EXAMPLES::
Expand All @@ -547,7 +547,7 @@ def __repr__(self):
"""
return 'Combinatorial diagrams'

def _element_constructor_(self, cells, **kwargs):
def _element_constructor_(self, cells, n_rows=None, n_cols=None, check=False):
r"""
EXAMPLES::
Expand All @@ -562,7 +562,7 @@ def _element_constructor_(self, cells, **kwargs):
sage: TestSuite(Dgms).run()
"""
return self.element_class(cells, **kwargs)
return self.element_class(self, cells, n_rows, n_cols, check)

def _an_element_(self):
r"""
Expand All @@ -586,7 +586,7 @@ def _an_element_(self):
# Northwest diagrams
####################

class NorthwestDiagram(Diagram):
class NorthwestDiagram(Diagram, metaclass=InheritComparisonClasscallMetaclass):
r"""
A diagram is a set of cells indexed by natural numbers. Such a diagram
has the *northwest property* if the presence of cells `(i1, j1)` and
Expand Down Expand Up @@ -625,60 +625,25 @@ class NorthwestDiagram(Diagram):
O . . . .
"""
@staticmethod
def __classcall_private__(self, cells, **kwargs):
def __classcall_private__(self, cells, n_rows=None, n_cols=None, check=True):
"""
Normalize input to ensure a correct parent.
EXAMPLES::
sage: from sage.combinat.diagram import NorthwestDiagram
sage: from sage.combinat.diagram import NorthwestDiagram, NorthwestDiagrams
sage: N1 = NorthwestDiagram([(0,1), (0,2)])
sage: N2 = NorthwestDiagram([(0,1), (0,3)])
sage: N1.parent() is N2.parent()
True
sage: N3 = NorthwestDiagrams()([(0,1), (0,2)])
sage: N3.parent() is NorthwestDiagrams()
True
sage: N1.parent() is NorthwestDiagrams()
True
"""
# TODO: Assert that cells is sorted in lex order to speed up lookup.
return NorthwestDiagrams()(cells, **kwargs)

def __init__(self, cells, n_rows=None, n_cols=None, check=True):
r"""
Initialize ``self``.
EXAMPLES::
sage: from sage.combinat.diagram import NorthwestDiagram
sage: N = NorthwestDiagram([(0,1), (0,2)])
TESTS::
sage: TestSuite(N).run()
sage: N = NorthwestDiagram([(0,1), (0,2)], n_cols = 1)
Traceback (most recent call last):
...
ValueError: n_cols is too small
sage: N = NorthwestDiagram([(0,0), (1,0)], n_rows = 1)
Traceback (most recent call last):
...
ValueError: n_rows is too small
"""
self._cells = {c: True for c in cells}
if n_rows is not None:
if n_rows < max(c[0] for c in self._cells) + 1:
raise ValueError('n_rows is too small')
self._n_rows = n_rows
else:
self._n_rows = max(c[0] for c in self._cells) + 1
if n_cols is not None:
if n_cols < max(c[1] for c in self._cells) + 1:
raise ValueError('n_cols is too small')
self._n_cols = n_cols
else:
self._n_cols = max(c[1] for c in self._cells) + 1

self._n_nonempty_rows = len(set(i for i, j in self._cells))
self._n_nonempty_cols = len(set(j for i, j in self._cells))

ClonableArray.__init__(self, NorthwestDiagrams(), cells, check=check)
return NorthwestDiagrams()(cells, n_rows, n_cols, check)

def check(self):
r"""
Expand All @@ -689,9 +654,16 @@ def check(self):
EXAMPLES::
sage: from sage.combinat.diagram import Diagram
sage: D = Diagram([(0,0), (0,3), (2,2), (2,4)])
sage: D.check()
sage: from sage.combinat.diagram import NorthwestDiagram
sage: N = NorthwestDiagram([(0,0), (0,3), (3,0)])
sage: N.check()
Here is a non-example::
sage: notN = NorthwestDiagram([(0,1), (1,0)]) #.check() is implicit
Traceback (most recent call last):
...
AssertionError
"""
from itertools import combinations
assert all((min(i1, i2), min(j1, j2)) in self
Expand Down
26 changes: 26 additions & 0 deletions src/sage/combinat/diagram_minimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sage.categories.sets_cat import Sets
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.list_clone import ClonableArray
from sage.structure.parent import Parent
from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass

class Diagram(ClonableArray, metaclass=InheritComparisonClasscallMetaclass):
@staticmethod
def __classcall_private__(cls, cells):
return Diagrams()(cells)

def __init__(self, parent, cells):
self._cells = {c: True for c in cells}
ClonableArray.__init__(self, parent, cells)

def check(self):
pass

class Diagrams(UniqueRepresentation, Parent):
def __init__(self):
Parent.__init__(self, category=Sets())

def _element_constructor_(self, cells):
return self.element_class(self, cells)

Element = Diagram

0 comments on commit 6d1395e

Please sign in to comment.