diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index 0c04197c300..c3a8a5fbc93 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -8504,6 +8504,12 @@ explicitly setting the argument to `True` or `False` will avoid this message.""" sage: img.save(filename) sage: open(filename).read().startswith('\x89PNG') True + + TESTS: + + Test :trac:`17341`:: + + sage: random_matrix(GF(2), 8, 586, sparse=True).visualize_structure() """ cdef int x, y, _x, _y, v, bi, bisq cdef int ir,ic diff --git a/src/sage/matrix/matrix_modn_sparse.pyx b/src/sage/matrix/matrix_modn_sparse.pyx index c56684cc324..052fe1b9c98 100644 --- a/src/sage/matrix/matrix_modn_sparse.pyx +++ b/src/sage/matrix/matrix_modn_sparse.pyx @@ -560,79 +560,6 @@ cdef class Matrix_modn_sparse(matrix_sparse.Matrix_sparse): return list(nzp) return nzp - def visualize_structure(self, filename=None, maxsize=512): - r""" - Write a PNG image to 'filename' which visualizes self by putting - black pixels in those positions which have nonzero entries. - - White pixels are put at positions with zero entries. If 'maxsize' - is given, then the maximal dimension in either x or y direction is - set to 'maxsize' depending on which is bigger. If the image is - scaled, the darkness of the pixel reflects how many of the - represented entries are nonzero. So if e.g. one image pixel - actually represents a 2x2 submatrix, the dot is darker the more of - the four values are nonzero. - - INPUT: - - - ``filename`` -- String. Name of the filename to save the - resulting image. - - - ``maxsize`` - integer (default: ``512``). Maximal dimension - in either x or y direction of the resulting image. If - ``None`` or a maxsize larger than - ``max(self.nrows(),self.ncols())`` is given the image will - have the same pixelsize as the matrix dimensions. - - EXAMPLES:: - - sage: M = Matrix(GF(7), [[0,0,0,1,0,0,0,0],[0,1,0,0,0,0,1,0]], sparse=True); M - [0 0 0 1 0 0 0 0] - [0 1 0 0 0 0 1 0] - sage: img = M.visualize_structure(); img - 8x2px 24-bit RGB image - - You can use :meth:`~sage.repl.image.Image.save` to save the - resulting image:: - - sage: filename = tmp_filename(ext='.png') - sage: img.save(filename) - sage: open(filename).read().startswith('\x89PNG') - True - """ - cdef Py_ssize_t i, j, k - cdef float blk,invblk - cdef int delta - cdef int x,y,r,g,b - mr, mc = self.nrows(), self.ncols() - if maxsize is None: - ir = mc - ic = mr - blk = 1.0 - invblk = 1.0 - elif max(mr,mc) > maxsize: - maxsize = float(maxsize) - ir = int(mc * maxsize/max(mr,mc)) - ic = int(mr * maxsize/max(mr,mc)) - blk = max(mr,mc)/maxsize - invblk = maxsize/max(mr,mc) - else: - ir = mc - ic = mr - blk = 1.0 - invblk = 1.0 - delta = (255.0 / blk*blk) - from sage.repl.image import Image - img = Image('RGB', (ir, ic), (255, 255, 255)) - pixel = img.pixels() - for i from 0 <= i < self._nrows: - for j from 0 <= j < self.rows[i].num_nonzero: - x = (invblk * self.rows[i].positions[j]) - y = (invblk * i) - r, g, b = pixel[x, y] - pixel[x, y] = (r-delta, g-delta, b-delta) - return img - def density(self): """ Return the density of self, i.e., the ratio of the number of