Skip to content

Commit

Permalink
Add examples to OneSumNode in seymour_decomposition.pyx, add methods …
Browse files Browse the repository at this point in the history
…block_matrix_form, summand_matrices (sagemath#8)

* wip onesum

* wip onesum2

* wip onesum test

* onesum node example

* fixed names of .summand_matrices() and .block_matrix_form,
....: and added examples to ._children()

* style fixes

---------

Co-authored-by: J S <javier@javiers-mbp.mynetworksettings.com>
Co-authored-by: J S <javier@campus-017-091.ucdavis.edu>
  • Loading branch information
3 people authored and mkoeppe committed Jun 2, 2024
1 parent 85de394 commit d7651a0
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions src/sage/matrix/seymour_decomposition.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,35 @@ cdef class DecompositionNode(SageObject):

@cached_method
def _children(self):
r"""
Returns tuple of summands, () in the case of graphic or leaf nodes.
EXAMPLES::
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]], [[1, 0], [0,1]]); M
[ 1 0| 0 0| 0 0]
[-1 1| 0 0| 0 0]
[-----+-----+-----]
[ 0 0| 1 1| 0 0]
[ 0 0|-1 0| 0 0]
[-----+-----+-----]
[ 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
sage: certificate._children()
(GraphicNode, GraphicNode, GraphicNode, GraphicNode)
sage: M2 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 2, 2, sparse=True),
...: [[1, 1], [-1, 0]]); M2
[ 1 1]
[-1 0]
sage: result, certificate = M.is_totally_unimodular(certificate=True); certificate
GraphicNode
certificate._children()
()
"""
return tuple(create_DecompositionNode(CMRdecChild(self._dec, index),
self._root or self)
for index in range(CMRdecNumChildren(self._dec)))
Expand Down Expand Up @@ -194,14 +223,65 @@ cdef class SumNode(DecompositionNode):

summands = DecompositionNode._children

def summand_matrices(self):
return tuple(s.matrix() for s in self._children())


cdef class OneSumNode(SumNode):

pass
def block_matrix_form(self):
r"""
EXAMPLES::
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
sage: certificate.summand_matrices()
(
[ 1 0] [ 1 1]
[-1 1], [-1 0]
)
sage: certificate.block_matrix_form()
[ 1 0| 0 0]
[-1 1| 0 0]
[-----+-----]
[ 0 0| 1 1]
[ 0 0|-1 0]
sage: M3 = Matrix_cmr_chr_sparse.one_sum([[1, 0], [-1, 1]], [[1, 1], [-1, 0]], [[1, 0], [0,1]]
....: [[1, 1], [-1, 0]], [[1, 0], [0,1]]); M3
[ 1 0| 0 0| 0 0]
[-1 1| 0 0| 0 0]
[-----+-----+-----]
[ 0 0| 1 1| 0 0]
[ 0 0|-1 0| 0 0]
[-----+-----+-----]
[ 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
sage: certificate.summand_matrices()
(
[ 1 0] [ 1 1]
[-1 1], [-1 0], [1], [1]
)
sage: certificate.block_matrix_form()
[ 1 0| 0 0| 0| 0]
[-1 1| 0 0| 0| 0]
[-----+-----+--+--]
[ 0 0| 1 1| 0| 0]
[ 0 0|-1 0| 0| 0]
[-----+-----+--+--]
[ 0 0| 0 0| 1| 0]
[-----+-----+--+--]
[ 0 0| 0 0| 0| 1]
"""
return Matrix_cmr_chr_sparse.one_sum(*self.summand_matrices())


cdef class TwoSumNode(SumNode):

pass


Expand Down

0 comments on commit d7651a0

Please sign in to comment.