Skip to content

Comments

Added examples to OneSumNode in SeymoursDecomposition.pyx#8

Merged
mkoeppe merged 7 commits intomkoeppe:cmrfrom
jsantillan3:cmr_cographic
Jul 27, 2023
Merged

Added examples to OneSumNode in SeymoursDecomposition.pyx#8
mkoeppe merged 7 commits intomkoeppe:cmrfrom
jsantillan3:cmr_cographic

Conversation

@jsantillan3
Copy link

@jsantillan3 jsantillan3 commented Jul 26, 2023

📚 Description

Added method for block matrix representation of a one-sum node. Issue found with one_sum method in matrix_cmr_sparse.pyx, found as follows:

sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
sage: M3 = Matrix_cmr_chr_sparse.one_sum([[1, 0], [-1, 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.block_diagonal_repr()
[ 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: type(certificate)
<class 'sage.matrix.seymour_decomposition.OneSumNode'>
sage: certificate.block_diagonal_repr()

OverflowError Traceback (most recent call last)
Cell In [7], line 1
----> 1 certificate.block_diagonal_repr()

File ~/sage/worktree-cmr/src/sage/matrix/seymour_decomposition.pyx:252, in sage.matrix.seymour_decomposition.OneSumNode.block_diagonal_repr()
250 [ 0 0| 0 0| 0| 1]
251 """
--> 252 return Matrix_cmr_chr_sparse.one_sum(*self.get_children_matrices())
253
254

File ~/sage/worktree-cmr/src/sage/matrix/seymour_decomposition.pyx:199, in genexpr()
197 if self._children() is None:
198 raise ValueError('Node has no children')
--> 199 return tuple(ch.matrix() for ch in self._children())
200
201

File ~/sage/worktree-cmr/src/sage/matrix/seymour_decomposition.pyx:199, in genexpr()
197 if self._children() is None:
198 raise ValueError('Node has no children')
--> 199 return tuple(ch.matrix() for ch in self._children())
200
201

File ~/sage/worktree-cmr/src/sage/misc/cachefunc.pyx:2301, in sage.misc.cachefunc.CachedMethodCallerNoArgs.call()
2299 if self.cache is None:
2300 f = self.f
-> 2301 self.cache = f(self._instance)
2302 return self.cache
2303

File ~/sage/worktree-cmr/src/sage/matrix/seymour_decomposition.pyx:59, in sage.matrix.seymour_decomposition.DecompositionNode.matrix()
57 if mat == NULL:
58 return None
---> 59 ms = MatrixSpace(ZZ, mat.numRows, mat.numColumns, sparse=True)
60 result = Matrix_cmr_chr_sparse.new(Matrix_cmr_chr_sparse, ms)
61 result._mat = mat

File ~/sage/worktree-cmr/src/sage/misc/classcall_metaclass.pyx:320, in sage.misc.classcall_metaclass.ClasscallMetaclass.call()
318 """
319 if cls.classcall is not None:
--> 320 return cls.classcall(cls, *args, **kwds)
321 else:
322 # Fast version of type.call(cls, *args, **kwds)

File ~/sage/worktree-cmr/src/sage/matrix/matrix_space.py:587, in MatrixSpace.classcall(cls, base_ring, nrows, ncols, sparse, implementation, **kwds)
585 raise ArithmeticError("ncols must be nonnegative")
586 if nrows > sys.maxsize or ncols > sys.maxsize:
--> 587 raise OverflowError("number of rows and columns may be at most %s" % sys.maxsize)
589 matrix_cls = get_matrix_class(base_ring, nrows, ncols, sparse, implementation)
590 return super().classcall(cls, base_ring, nrows,
591 ncols, sparse, matrix_cls, **kwds)

OverflowError: number of rows and columns may be at most 9223372036854775807

📝 Checklist

  • [x ] The title is concise, informative, and self-explanatory.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

⌛ Dependencies


summands = DecompositionNode._children

def get_children_matrices(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In the line above, I define summands as an alias for the internal method _children. This method does not start with _, so this is the intended public method. So instead of get_children_matrices it should be get_summands_matrices.

  2. Grammar: Don't pluralize both words; so it should be get_summand_matrices.

  3. In most cases, the word get is redundant in naming methods and is typically avoided in Python. That's why it's just summands, not get_summands. So it should be just summand_matrices.


def get_children_matrices(self):
if self._children() is None:
raise ValueError('Node has no children')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the implementation of _children, you see that it always returns a tuple and never None.
So this case cannot arise.

Could you add documentation and an example to the _children method please?


pass
def block_diagonal_repr(self):
r"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice idea for a useful method. Some comments:

  1. The name won't generalize to TwoSum and ThreeSumNode; they are still block matrices, but not block diagonal matrices.

  2. I would avoid the word repr in such names because in Python this means a string representation.

@mkoeppe
Copy link
Owner

mkoeppe commented Jul 26, 2023

I'll look into the bug

@mkoeppe
Copy link
Owner

mkoeppe commented Jul 26, 2023

I have reproduced the bug on my machine; for me it segfaults instead of just running into an OverflowError.

@mkoeppe
Copy link
Owner

mkoeppe commented Jul 26, 2023

The bug is fixed on my branch in 6bcdbeb.


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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to break the line between the 3 provided matrices

(GraphicNode, GraphicNode, GraphicNode, GraphicNode)

sage: M2 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 2, 2, sparse=True), [[1, 1], [-
...: 1, 0]]); M2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to break the line before the matrix data

Copy link
Owner

@mkoeppe mkoeppe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great. Thanks!

@mkoeppe mkoeppe merged commit 9df7a19 into mkoeppe:cmr Jul 27, 2023
mkoeppe pushed a commit that referenced this pull request Oct 8, 2023
mkoeppe pushed a commit that referenced this pull request Dec 11, 2023
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
mkoeppe pushed a commit that referenced this pull request Feb 5, 2024
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
mkoeppe pushed a commit that referenced this pull request Feb 16, 2024
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
mkoeppe pushed a commit that referenced this pull request May 14, 2024
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
mkoeppe pushed a commit that referenced this pull request Jun 2, 2024
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
mkoeppe pushed a commit that referenced this pull request Oct 13, 2024
…block_matrix_form, summand_matrices (#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 <[email protected]>
Co-authored-by: J S <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants