From 2bc5dbc7f209ab9f510a6f4825a4c1b8d2197cac Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Thu, 11 Feb 2016 17:47:15 +0100 Subject: [PATCH 1/2] Remove broken visualize_structure() for matrix_modn_sparse --- src/sage/matrix/matrix2.pyx | 6 +++ src/sage/matrix/matrix_modn_sparse.pyx | 73 -------------------------- 2 files changed, 6 insertions(+), 73 deletions(-) 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 From 2110b6a1ec388a7d0e5c9123a0df20fba2312db4 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Thu, 11 Feb 2016 17:55:02 +0100 Subject: [PATCH 2/2] visualize_structure() for matrix dimensions >= 2^31 --- src/sage/matrix/matrix2.pyx | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index c3a8a5fbc93..03157dd81eb 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -8510,10 +8510,11 @@ explicitly setting the argument to `True` or `False` will avoid this message.""" Test :trac:`17341`:: sage: random_matrix(GF(2), 8, 586, sparse=True).visualize_structure() + 512x6px 24-bit RGB image """ - cdef int x, y, _x, _y, v, bi, bisq - cdef int ir,ic - cdef float b, fct + cdef Py_ssize_t x, y, _x, _y, v, bi, bisq + cdef Py_ssize_t ir, ic + cdef double b, fct mr, mc = self.nrows(), self.ncols() if maxsize is None: ir = mc @@ -8528,20 +8529,20 @@ explicitly setting the argument to `True` or `False` will avoid this message.""" ir = mc ic = mr b = 1.0 - bi = round(b) + bi = int(round(b)) bisq = bi*bi fct = 255.0/bisq from sage.repl.image import Image img = Image('RGB', (ir, ic)) pixel = img.pixels() - for x from 0 <= x < ic: - for y from 0 <= y < ir: + for x in range(ic): + for y in range(ir): v = bisq - for _x from 0 <= _x < bi: - for _y from 0 <= _y < bi: - if not self.get_unsafe((x*b + _x), (y*b + _y)).is_zero(): - v-=1 #increase darkness - v = round(v*fct) + for _x in range(bi): + for _y in range(bi): + if not self.get_unsafe((x*b + _x), (y*b + _y)).is_zero(): + v -= 1 #increase darkness + v = (v * fct + 0.5) pixel[y, x] = (v, v, v) return img