Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement zero_pattern_matrix #29839

Closed
kliem opened this issue Jun 10, 2020 · 73 comments
Closed

Implement zero_pattern_matrix #29839

kliem opened this issue Jun 10, 2020 · 73 comments

Comments

@kliem
Copy link
Contributor

kliem commented Jun 10, 2020

We implement a method on matrices that gives a new matrix with specified base ring
and entries 1 where and only where the initial matrix had zero entries, and 0 elsewhere.

Motivation: This method is useful to obtain the incidence matrix of a polyhedron from the slack matrix in #29837.

Along the way this ticket also fixes a bug:

M = Matrix(ZZ, [[0,1],[0,1]])
sage: M._mod_int(2).transpose()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-8587f9dc80b2> in <module>()
----> 1 M._mod_int(Integer(2)).transpose()

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/matrix/matrix_mod2_dense.pyx in sage.matrix.matrix_mod2_dense.Matrix_mod2_dense.transpose (build/cythonized/sage/matrix/matrix_mod2_dense.c:10873)()
   1400             1
   1401         """
-> 1402         cdef Matrix_mod2_dense A = self.new_matrix(ncols = self._nrows,  nrows = self._ncols)
   1403         if self._nrows == 0 or self._ncols == 0:
   1404             return A

TypeError: Cannot convert sage.matrix.matrix_modn_dense_float.Matrix_modn_dense_float to sage.matrix.matrix_mod2_dense.Matrix_mod2_dense

To optimize this method, a matrix class can use the optimized get_is_zero_unsafe.
In this ticket this is done for most matrix classes. We also use this new optimized zero check in places where it makes sense.

Component: basic arithmetic

Keywords: incidence matrix, zero pattern

Author: Jonathan Kliem, Travis Scrimshaw

Branch/Commit: 82c6b4a

Reviewer: Travis Scrimshaw, Jonathan Kliem

Issue created by migration from https://trac.sagemath.org/ticket/29839

@kliem kliem added this to the sage-9.2 milestone Jun 10, 2020
@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

comment:1

Better suggestions for a name would be welcome. _zero_matrix is somewhat strange.

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

Branch: public/29839

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

Commit: 43e6dfb

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

New commits:

43e6dfbimplement _zero_matrix

@dimpase
Copy link
Member

dimpase commented Jun 10, 2020

comment:3

zero_pattern_matrix?

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

comment:4

Better anyway. With public or semiprivate? (leading underscore).

Btw, there is a bug here. I copied this from _mod_two which apparently sets up the parent incorrect:

M = Matrix(ZZ, [[0,1],[0,1]])
sage: M._mod_int(2).transpose()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-8587f9dc80b2> in <module>()
----> 1 M._mod_int(Integer(2)).transpose()

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/matrix/matrix_mod2_dense.pyx in sage.matrix.matrix_mod2_dense.Matrix_mod2_dense.transpose (build/cythonized/sage/matrix/matrix_mod2_dense.c:10873)()
   1400             1
   1401         """
-> 1402         cdef Matrix_mod2_dense A = self.new_matrix(ncols = self._nrows,  nrows = self._ncols)
   1403         if self._nrows == 0 or self._ncols == 0:
   1404             return A

TypeError: Cannot convert sage.matrix.matrix_modn_dense_float.Matrix_modn_dense_float to sage.matrix.matrix_mod2_dense.Matrix_mod2_dense
sage:  MS = matrix_space.MatrixSpace(IntegerModRing(2), self._nrows, self._ncols)

@dimpase
Copy link
Member

dimpase commented Jun 10, 2020

comment:5

shouldn't def _zero_matrix(self): (or rather named as in comment:3) be cpdef, to facilitate calling from Cython?

@dimpase
Copy link
Member

dimpase commented Jun 10, 2020

comment:6

Replying to @kliem:

Better anyway. With public or semiprivate? (leading underscore).

Btw, there is a bug here. I copied this from _mod_two which apparently sets up the parent incorrect:

M = Matrix(ZZ, [[0,1],[0,1]])
sage: M._mod_int(2).transpose()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-8587f9dc80b2> in <module>()
----> 1 M._mod_int(Integer(2)).transpose()

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/matrix/matrix_mod2_dense.pyx in sage.matrix.matrix_mod2_dense.Matrix_mod2_dense.transpose (build/cythonized/sage/matrix/matrix_mod2_dense.c:10873)()
   1400             1
   1401         """
-> 1402         cdef Matrix_mod2_dense A = self.new_matrix(ncols = self._nrows,  nrows = self._ncols)
   1403         if self._nrows == 0 or self._ncols == 0:
   1404             return A

TypeError: Cannot convert sage.matrix.matrix_modn_dense_float.Matrix_modn_dense_float to sage.matrix.matrix_mod2_dense.Matrix_mod2_dense
sage:  MS = matrix_space.MatrixSpace(IntegerModRing(2), self._nrows, self._ncols)

why can't you use mod?

sage: M = Matrix(ZZ, [[0,1],[0,1]])
sage: M.mod(2)
[0 1]
[0 1]

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

comment:7

Because it is slow. I just copied the construction of the parent from _mod_two.
I really don't know why, but change_ring is really slow (e.g. for ZZ -> GF(2)). It should be improved, but maybe not in this ticket.

_mod_two uses the ring with two elements to obtain the parent. However, get_matrix_class gives you modn in this case. It gives you mod2 if you pass the field with two elements.

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

comment:8

And M.mod is really not what I want. I just illustrated this bug.

@kliem

This comment has been minimized.

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

Changed keywords from incidence matrix, zero entries to incidence matrix, zero pattern

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 10, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

05eba1efix bug
7b70830rename

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 10, 2020

Changed commit from 43e6dfb to 7b70830

@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

New commits:

05eba1efix bug
7b70830rename

@kliem kliem changed the title Implement _zero_matrix for dense integer and rational matrices Implement zero_pattern_matrix for dense integer and rational matrices Jun 10, 2020
@kliem
Copy link
Contributor Author

kliem commented Jun 10, 2020

comment:12

I really want a new integer matrix.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 10, 2020

Changed commit from 7b70830 to 62f1ff9

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 10, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

62f1ff9use integer matrix for zero_pattern_matrix

@kliem

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Jun 10, 2020

comment:15

Have you tried doing either x = 1 - x or x = not x? These should be fast as they avoid intermediate function calls and the first should work for any ring (maybe replace 1 with one where one = self.base_ring().one()).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from 169fa6c to 9462376

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

4257c79Adding get_is_zero_unsafe() for generic sparse matrices.
78ca753Adding a custom get_is_zero_unsafe for sparse integer matrices.
9462376Adding optimized get_is_zero_unsafe() to rational sparse matrices.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

94febdeCustom get_is_zero_unsafe() for modn sparse matrices.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from 9462376 to 94febde

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from 94febde to bf0352f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

bf0352fSee if we can check trivial zeros first for symbolic matrices.

@tscrim

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Jun 13, 2020

comment:39

Okay, that takes care of the sparse matrices and symbolic matrices.

Slightly surprisingly, the gap matrices don't benefit from not going through Sage (well, at least for integer matrices).

I am done now.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

2a08abesmall mistakes

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from bf0352f to 2a08abe

@kliem
Copy link
Contributor Author

kliem commented Jun 13, 2020

comment:41

IMO this is good now. The doctests should also pass.

@kliem
Copy link
Contributor Author

kliem commented Jun 13, 2020

Changed reviewer from Travis Scrimshaw to Travis Scrimshaw, Jonathan Kliem

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

333c4bawrong doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from 2a08abe to 333c4ba

@tscrim
Copy link
Collaborator

tscrim commented Jun 13, 2020

comment:43

Thank you. Two more little things:

         INPUT:
 
-        - `ring` -- (optional); base ring of the output; default is `ZZ`
+        - `ring` -- (optional); base ring of the output; default is ``ZZ``

and we should add a test for the empty row/column and empty matrix for Matrix_gf2e_dense.__bool__ (as that was a good catch). I can do this tomorrow.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Changed commit from 333c4ba to 82c6b4a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 13, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

82c6b4adocstring

@kliem
Copy link
Contributor Author

kliem commented Jun 13, 2020

comment:45

Replying to @tscrim:

and we should add a test for the empty row/column and empty matrix for Matrix_gf2e_dense.__bool__ (as that was a good catch).

Well the bot was segfaulting (and me to, when running the test).

@tscrim
Copy link
Collaborator

tscrim commented Jun 13, 2020

comment:46

Okay, then positive review. Thank you.

@kliem
Copy link
Contributor Author

kliem commented Jun 14, 2020

comment:47

I like that you made a polyhedron only ticket, into a more general improvement for other parts as well.

@slel

This comment has been minimized.

@slel
Copy link
Member

slel commented Jun 18, 2020

comment:48

(Slightly amending ticket description, please check that works for you.).

@vbraun
Copy link
Member

vbraun commented Jun 27, 2020

Changed branch from public/29839 to 82c6b4a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants