Skip to content

Commit

Permalink
sage.matrix.seymour_decomposition: Show dimensions in repr
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoeppe committed Dec 11, 2023
1 parent faafdbf commit 956af74
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
40 changes: 20 additions & 20 deletions src/sage/matrix/matrix_cmr_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -838,27 +838,27 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: result, certificate = MFR2cmr._is_binary_linear_matroid_regular(
....: certificate=True)
sage: result, certificate
(False, (OneSumNode with 2 children, NotImplemented))
(False, (OneSumNode (6×14) with 2 children, NotImplemented))
sage: certificate[0].summands()[0].parent_rows_and_columns()
((0, 1, 2), (0, 4, 5, 6, 2, 3, 1))
sage: certificate[0].summands()[1].parent_rows_and_columns()
((3, 4, 5), (7, 11, 12, 13, 9, 10, 8))
sage: unicode_art(certificate[0]) # random (whether the left or the right branch has been followed)
╭OneSumNode with 2 children
│ │
SeriesParallelReductionNode UnknownNode
╭OneSumNode (6×14) with 2 children╮
SeriesParallelReductionNode (3×7) UnknownNode (3×7)
ThreeConnectedIrregularNode
ThreeConnectedIrregularNode (3×4)
sage: result, certificate = MFR2cmr._is_binary_linear_matroid_regular(
....: certificate=True, complete_tree=True)
sage: result, certificate
(False, (OneSumNode with 2 children, NotImplemented))
(False, (OneSumNode (6×14) with 2 children, NotImplemented))
sage: unicode_art(certificate[0])
╭OneSumNode with 2 children
│ │
SeriesParallelReductionNode SeriesParallelReductionNode
│ │
ThreeConnectedIrregularNode ThreeConnectedIrregularNode
╭OneSumNode (6×14) with 2 children╮
SeriesParallelReductionNode (3×7) SeriesParallelReductionNode (3×7)
ThreeConnectedIrregularNode (3×4) ThreeConnectedIrregularNode (3×4)
TESTS:
Expand All @@ -879,20 +879,20 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: result, certificate = M._is_binary_linear_matroid_regular(
....: certificate=True, complete_tree=True)
sage: result, certificate
(True, GraphicNode)
(True, GraphicNode (11×11))
sage: unicode_art(certificate)
GraphicNode
GraphicNode (11×11)
sage: result, certificate = M._is_binary_linear_matroid_regular(
....: certificate=True, complete_tree=True,
....: use_direct_graphicness_test=False)
sage: result, certificate
(True, TwoSumNode with 2 children)
(True, TwoSumNode (11×11) with 2 children)
sage: unicode_art(certificate)
╭─────TwoSumNode with 2 children
│ │
GraphicNode SeriesParallelReductionNode
GraphicNode
╭──────────TwoSumNode (11×11) with 2 children
GraphicNode (7×8) SeriesParallelReductionNode (5×4)
GraphicNode (4×4)
"""
cdef bool result
cdef CMR_REGULAR_PARAMETERS params
Expand Down Expand Up @@ -954,7 +954,7 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.is_totally_unimodular()
True
sage: M.is_totally_unimodular(certificate=True)
(True, GraphicNode)
(True, GraphicNode (3×2))
sage: MF = matroids.named_matroids.Fano(); MF
Fano: Binary matroid of rank 3 on 7 elements, type (3, 0)
Expand Down
42 changes: 26 additions & 16 deletions src/sage/matrix/seymour_decomposition.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ cdef class DecompositionNode(SageObject):
def __hash__(self):
return <int>self._dec

def nrows(self):
return CMRdecNumRows(self._dec)

def ncols(self):
return CMRdecNumColumns(self._dec)

def dimensions(self):
return self.nrows(), self.ncols()

@cached_method
def matrix(self):
r"""
Expand All @@ -44,14 +53,14 @@ cdef class DecompositionNode(SageObject):
[ 0 1]
sage: result, certificate = M.is_totally_unimodular(certificate=True)
sage: result, certificate
(True, GraphicNode)
(True, GraphicNode (3×2))
sage: certificate.matrix() is None
True
sage: result, certificate = M.is_totally_unimodular(certificate=True,
....: construct_matrices=True)
sage: result, certificate
(True, GraphicNode)
(True, GraphicNode (3×2))
sage: certificate.matrix()
[ 1 0]
[-1 1]
Expand Down Expand Up @@ -95,9 +104,9 @@ cdef class DecompositionNode(SageObject):
sage: result, certificate = M2cmr.is_totally_unimodular(certificate=True,
....: construct_matrices=True)
sage: result, certificate
(True, OneSumNode with 2 children)
(True, OneSumNode (6×4) with 2 children)
sage: C = certificate.summands(); C
(GraphicNode, GraphicNode)
(GraphicNode (3×2), GraphicNode (3×2))
sage: C[0].parent_rows_and_columns()
((0, 1, 2), (0, 1))
sage: C[1].parent_rows_and_columns()
Expand Down Expand Up @@ -133,11 +142,11 @@ cdef class DecompositionNode(SageObject):
sage: result, certificate = M2cmr.is_totally_unimodular(certificate=True,
....: construct_matrices=True)
sage: T = certificate.as_ordered_tree(); T
OneSumNode with 2 children[GraphicNode[], GraphicNode[]]
OneSumNode (6×4) with 2 children[GraphicNode (3×2)[], GraphicNode (3×2)[]]
sage: unicode_art(T)
╭─────OneSumNode with 2 children
│ │
GraphicNode GraphicNode
╭───────────OneSumNode (6×4) with 2 children
GraphicNode (3×2) GraphicNode (3×2)
"""
from sage.combinat.ordered_tree import LabelledOrderedTree
return LabelledOrderedTree([child.as_ordered_tree() for child in self._children()],
Expand Down Expand Up @@ -185,16 +194,16 @@ cdef class DecompositionNode(SageObject):
[ 0 0| 0 0| 1 0]
[ 0 0| 0 0| 0 1]
sage: result, certificate = M.is_totally_unimodular(certificate=True); certificate
OneSumNode with 4 children
OneSumNode (6×6) with 4 children
sage: certificate._children()
(GraphicNode, GraphicNode, GraphicNode, GraphicNode)
(GraphicNode (2×2), GraphicNode (2×2), GraphicNode (1×1), GraphicNode (1×1))
sage: M2 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 2, 2, sparse=True),
....: [[1, 1], [-1, 0]]); M2
[ 1 1]
[-1 0]
sage: result, certificate = M2.is_totally_unimodular(certificate=True); certificate
GraphicNode
GraphicNode (2×2)
sage: certificate._children()
()
"""
Expand All @@ -204,7 +213,8 @@ cdef class DecompositionNode(SageObject):
key=lambda node: node.parent_rows_and_columns()))

def _repr_(self):
return f'{self.__class__.__name__}'
nrows, ncols = self.dimensions()
return f'{self.__class__.__name__} ({nrows}×{ncols})'

def _unicode_art_(self):
return self.as_ordered_tree()._unicode_art_()
Expand Down Expand Up @@ -249,7 +259,7 @@ cdef class OneSumNode(SumNode):
sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
sage: M = Matrix_cmr_chr_sparse.one_sum([[1, 0], [-1, 1]], [[1, 1], [-1, 0]])
sage: result, certificate = M.is_totally_unimodular(certificate=True); certificate
OneSumNode with 2 children
OneSumNode (4×4) with 2 children
sage: certificate.summand_matrices()
(
[ 1 0] [ 1 1]
Expand All @@ -274,7 +284,7 @@ cdef class OneSumNode(SumNode):
[ 0 0| 0 0| 1 0]
[ 0 0| 0 0| 0 1]
sage: result, certificate = M3.is_totally_unimodular(certificate=True); certificate
OneSumNode with 4 children
OneSumNode (6×6) with 4 children
sage: certificate.summand_matrices()
(
[ 1 0] [ 1 1]
Expand Down Expand Up @@ -319,7 +329,7 @@ cdef class BaseGraphicNode(DecompositionNode):
[ 0 1]
sage: result, certificate = M.is_totally_unimodular(certificate=True)
sage: result, certificate
(True, GraphicNode)
(True, GraphicNode (3×2))
sage: G = certificate.graph(); G
Graph on 4 vertices
sage: G.vertices(sort=True)
Expand All @@ -342,7 +352,7 @@ cdef class BaseGraphicNode(DecompositionNode):
[ 0 1]
sage: result, certificate = M.is_totally_unimodular(certificate=True)
sage: result, certificate
(True, GraphicNode)
(True, GraphicNode (3×2))
sage: certificate.forest_edges()
((1, 2), (7, 1))
"""
Expand Down

0 comments on commit 956af74

Please sign in to comment.