From 832be74b1489be0afc9e14145681618915ffe023 Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Thu, 27 Jul 2023 10:29:13 +0200 Subject: [PATCH 1/7] sparse iter and _copy_zero --- .../matrix/matrix_modn_dense_template.pxi | 58 +++++++++++++------ src/sage/matrix/matrix_space.py | 14 ++++- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index ca3382859f5..ce59770bcbf 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -123,7 +123,7 @@ from sage.structure.proof.proof import get_flag as get_proof_flag from sage.structure.richcmp cimport rich_to_bool from sage.misc.randstate cimport randstate, current_randstate import sage.matrix.matrix_space as matrix_space -from .args cimport MatrixArgs_init +from .args cimport SparseEntry, MatrixArgs_init from sage.cpython.string cimport char_to_str @@ -518,27 +518,49 @@ cdef class Matrix_modn_dense_template(Matrix_dense): """ ma = MatrixArgs_init(parent, entries) cdef long i, j - it = ma.iter(False) + it = ma.iter(convert=False, sparse=True) R = ma.base p = R.characteristic() - for i in range(ma.nrows): - v = self._matrix[i] - for j in range(ma.ncols): - x = next(it) - if type(x) is int: - tmp = (x) % p - v[j] = tmp + (tmp<0)*p - elif type(x) is IntegerMod_int and (x)._parent is R: - v[j] = (x).ivalue - elif type(x) is Integer: - if coerce: - v[j] = mpz_fdiv_ui((x).value, p) - else: - v[j] = mpz_get_ui((x).value) - elif coerce: - v[j] = R(x) + + for t in it: + se = t + x = se.entry + v = self._matrix[se.i] + if type(x) is int: + tmp = (x) % p + v[se.j] = tmp + (tmp<0)*p + elif type(x) is IntegerMod_int and (x)._parent is R: + v[se.j] = (x).ivalue + elif type(x) is Integer: + if coerce: + v[se.j] = mpz_fdiv_ui((x).value, p) else: v[j] = x + v[se.j] = mpz_get_ui((x).value) + elif coerce: + v[se.j] = R(x) + else: + v[se.j] = x + + + #for i in range(ma.nrows): + # v = self._matrix[i] + # for j in range(ma.ncols): + # x = next(it) + # if type(x) is int: + # tmp = (x) % p + # v[j] = tmp + (tmp<0)*p + # elif type(x) is IntegerMod_int and (x)._parent is R: + # v[j] = (x).ivalue + # elif type(x) is Integer: + # if coerce: + # v[j] = mpz_fdiv_ui((x).value, p) + # else: + # v[j] = mpz_get_ui((x).value) + # elif coerce: + # v[j] = R(x) + # else: + # v[j] = x cdef long _hash_(self) except -1: """ diff --git a/src/sage/matrix/matrix_space.py b/src/sage/matrix/matrix_space.py index f3952cecda7..75582868a88 100644 --- a/src/sage/matrix/matrix_space.py +++ b/src/sage/matrix/matrix_space.py @@ -1804,7 +1804,12 @@ def identity_matrix(self): """ if self.__nrows != self.__ncols: raise TypeError("identity matrix must be square") - A = self.zero_matrix().__copy__() + + if self._copy_zero: + A = self.zero_matrix().__copy__() + else: + A = self.matrix() + for i in range(self.__nrows): A[i, i] = 1 A.set_immutable() @@ -1863,7 +1868,12 @@ def diagonal_matrix(self, entries): raise TypeError("diagonal matrix must be square") if self.__nrows < len(entries): raise ValueError('number of diagonal matrix entries (%s) exceeds the matrix size (%s)' % (len(entries), self.__nrows)) - A = self.zero_matrix().__copy__() + + if self._copy_zero: + A = self.zero_matrix().__copy__() + else: + A = self.matrix() + for i in range(len(entries)): A[i, i] = entries[i] return A From ae6a7dd3303673cc6a7a9d16aa464f6a3cf5b0ad Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Thu, 3 Aug 2023 13:41:39 +0200 Subject: [PATCH 2/7] calloc --- src/sage/matrix/matrix_modn_dense_template.pxi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index ce59770bcbf..05fa881778a 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -90,7 +90,7 @@ We test corner cases for multiplication:: from libc.stdint cimport uint64_t from cpython.bytes cimport * -from cysignals.memory cimport check_malloc, check_allocarray, sig_malloc, sig_free +from cysignals.memory cimport check_malloc, check_allocarray, check_calloc, sig_malloc, sig_free from cysignals.signals cimport sig_check, sig_on, sig_off from sage.libs.gmp.mpz cimport * @@ -447,8 +447,8 @@ cdef class Matrix_modn_dense_template(Matrix_dense): if p >= MAX_MODULUS: raise OverflowError("p (=%s) must be < %s."%(p, MAX_MODULUS)) - self._entries = check_allocarray(self._nrows * self._ncols, sizeof(celement)) - self._matrix = check_allocarray(self._nrows, sizeof(celement*)) + self._entries = check_calloc(self._nrows * self._ncols, sizeof(celement)) + self._matrix = check_calloc(self._nrows, sizeof(celement*)) cdef unsigned int k cdef Py_ssize_t i From f724b2127140a57a2afe481b9a68f46025dfc522 Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Fri, 4 Aug 2023 10:10:33 +0200 Subject: [PATCH 3/7] add arg zeroed_alloc cinit --- src/sage/matrix/matrix_modn_dense_template.pxi | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index 05fa881778a..b228c074a02 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -441,14 +441,18 @@ cpdef __matrix_from_rows_of_matrices(X): cdef class Matrix_modn_dense_template(Matrix_dense): - def __cinit__(self): + def __cinit__(self, *args, bint zeroed_alloc=True, **kwds): cdef long p = self._base_ring.characteristic() self.p = p if p >= MAX_MODULUS: raise OverflowError("p (=%s) must be < %s."%(p, MAX_MODULUS)) - self._entries = check_calloc(self._nrows * self._ncols, sizeof(celement)) - self._matrix = check_calloc(self._nrows, sizeof(celement*)) + if zeroed_alloc: + self._entries = check_calloc(self._nrows * self._ncols, sizeof(celement)) + self._matrix = check_calloc(self._nrows, sizeof(celement*)) + else: + self._entries = check_allocarray(self._nrows * self._ncols, sizeof(celement)) + self._matrix = check_allocarray(self._nrows, sizeof(celement*)) cdef unsigned int k cdef Py_ssize_t i @@ -866,7 +870,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): False """ cdef Matrix_modn_dense_template A - A = self.__class__.__new__(self.__class__, self._parent, 0, 0, 0) + A = self.__class__.__new__(self.__class__, self._parent, 0, 0, 0, zeroed_alloc=False) memcpy(A._entries, self._entries, sizeof(celement)*self._nrows*self._ncols) if self._subdivisions is not None: A.subdivide(*self.subdivisions()) From 91b58eece8d9a91b239832a17d70d7a033e2d9fa Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Mon, 7 Aug 2023 09:01:29 +0200 Subject: [PATCH 4/7] add zeroed_alloc=False --- src/sage/matrix/matrix_modn_dense_template.pxi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index b228c074a02..2d32d7f6f73 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -812,7 +812,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): cdef Matrix_modn_dense_template M cdef celement p = self.p - M = self.__class__.__new__(self.__class__, self._parent,None,None,None) + M = self.__class__.__new__(self.__class__, self._parent,None,None,None, zeroed_alloc=False) sig_on() for i in range(self._nrows*self._ncols): @@ -851,7 +851,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): cdef celement p = self.p cdef celement a = left - M = self.__class__.__new__(self.__class__, self._parent,None,None,None) + M = self.__class__.__new__(self.__class__, self._parent,None,None,None,zeroed_alloc=False) sig_on() for i in range(self._nrows*self._ncols): @@ -909,7 +909,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): cdef celement k, p cdef Matrix_modn_dense_template M - M = self.__class__.__new__(self.__class__, self._parent,None,None,None) + M = self.__class__.__new__(self.__class__, self._parent,None,None,None,zeroed_alloc=False) p = self.p cdef celement* other_ent = (right)._entries @@ -946,7 +946,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): cdef celement k, p cdef Matrix_modn_dense_template M - M = self.__class__.__new__(self.__class__, self._parent, None, None, None) + M = self.__class__.__new__(self.__class__, self._parent, None, None, None, zeroed_alloc=False) p = self.p cdef celement* other_ent = (right)._entries From bd431d36bb30066b007d2cd192e91bf17a3c74c6 Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Mon, 14 Aug 2023 10:33:19 +0200 Subject: [PATCH 5/7] branch update --- .../matrix/matrix_modn_dense_template.pxi | 23 +------------------ src/sage/matrix/matrix_space.py | 14 ++--------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index 2d32d7f6f73..1632df1a63d 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -539,33 +539,12 @@ cdef class Matrix_modn_dense_template(Matrix_dense): if coerce: v[se.j] = mpz_fdiv_ui((x).value, p) else: - v[j] = x v[se.j] = mpz_get_ui((x).value) elif coerce: v[se.j] = R(x) else: v[se.j] = x - - #for i in range(ma.nrows): - # v = self._matrix[i] - # for j in range(ma.ncols): - # x = next(it) - # if type(x) is int: - # tmp = (x) % p - # v[j] = tmp + (tmp<0)*p - # elif type(x) is IntegerMod_int and (x)._parent is R: - # v[j] = (x).ivalue - # elif type(x) is Integer: - # if coerce: - # v[j] = mpz_fdiv_ui((x).value, p) - # else: - # v[j] = mpz_get_ui((x).value) - # elif coerce: - # v[j] = R(x) - # else: - # v[j] = x - cdef long _hash_(self) except -1: """ EXAMPLES:: @@ -870,7 +849,7 @@ cdef class Matrix_modn_dense_template(Matrix_dense): False """ cdef Matrix_modn_dense_template A - A = self.__class__.__new__(self.__class__, self._parent, 0, 0, 0, zeroed_alloc=False) + A = self.__class__.__new__(self.__class__,self._parent,None,None,None,zeroed_alloc=False) memcpy(A._entries, self._entries, sizeof(celement)*self._nrows*self._ncols) if self._subdivisions is not None: A.subdivide(*self.subdivisions()) diff --git a/src/sage/matrix/matrix_space.py b/src/sage/matrix/matrix_space.py index 75582868a88..f3952cecda7 100644 --- a/src/sage/matrix/matrix_space.py +++ b/src/sage/matrix/matrix_space.py @@ -1804,12 +1804,7 @@ def identity_matrix(self): """ if self.__nrows != self.__ncols: raise TypeError("identity matrix must be square") - - if self._copy_zero: - A = self.zero_matrix().__copy__() - else: - A = self.matrix() - + A = self.zero_matrix().__copy__() for i in range(self.__nrows): A[i, i] = 1 A.set_immutable() @@ -1868,12 +1863,7 @@ def diagonal_matrix(self, entries): raise TypeError("diagonal matrix must be square") if self.__nrows < len(entries): raise ValueError('number of diagonal matrix entries (%s) exceeds the matrix size (%s)' % (len(entries), self.__nrows)) - - if self._copy_zero: - A = self.zero_matrix().__copy__() - else: - A = self.matrix() - + A = self.zero_matrix().__copy__() for i in range(len(entries)): A[i, i] = entries[i] return A From 78846ca23a2b6bd094cd8fb62c5a2e4a747dcd0d Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Fri, 18 Aug 2023 17:23:18 +0200 Subject: [PATCH 6/7] add missed zeroed_alloc --- src/sage/matrix/matrix_integer_dense.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sage/matrix/matrix_integer_dense.pyx b/src/sage/matrix/matrix_integer_dense.pyx index 7fe211835cc..0c411a72382 100644 --- a/src/sage/matrix/matrix_integer_dense.pyx +++ b/src/sage/matrix/matrix_integer_dense.pyx @@ -1614,7 +1614,7 @@ cdef class Matrix_integer_dense(Matrix_dense): return self._mod_two() elif p < MAX_MODULUS_FLOAT: res_f = Matrix_modn_dense_float.__new__(Matrix_modn_dense_float, - matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), None, None, None) + matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), None, None, None, zeroed_alloc=False) for i from 0 <= i < self._nrows: res_row_f = res_f._matrix[i] for j from 0 <= j < self._ncols: @@ -1623,7 +1623,7 @@ cdef class Matrix_integer_dense(Matrix_dense): elif p < MAX_MODULUS_DOUBLE: res_d = Matrix_modn_dense_double.__new__(Matrix_modn_dense_double, - matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), None, None, None) + matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), None, None, None, zeroed_alloc=False) for i from 0 <= i < self._nrows: res_row_d = res_d._matrix[i] for j from 0 <= j < self._ncols: @@ -1649,11 +1649,11 @@ cdef class Matrix_integer_dense(Matrix_dense): if p < MAX_MODULUS_FLOAT: res.append( Matrix_modn_dense_float.__new__(Matrix_modn_dense_float, matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), - None, None, None) ) + None, None, None, zeroed_alloc=False) ) elif p < MAX_MODULUS_DOUBLE: res.append( Matrix_modn_dense_double.__new__(Matrix_modn_dense_double, matrix_space.MatrixSpace(IntegerModRing(p), self._nrows, self._ncols, sparse=False), - None, None, None) ) + None, None, None, zeroed_alloc=False) ) else: raise ValueError("p=%d too big."%p) From 6db8d9fac59fc65aa5a3f573b8d73a391e85e725 Mon Sep 17 00:00:00 2001 From: Marie BONBOIRE Date: Mon, 21 Aug 2023 09:11:22 +0200 Subject: [PATCH 7/7] remove _matrix calloc --- src/sage/matrix/matrix_modn_dense_template.pxi | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index 1632df1a63d..93933d5765d 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -449,11 +449,10 @@ cdef class Matrix_modn_dense_template(Matrix_dense): if zeroed_alloc: self._entries = check_calloc(self._nrows * self._ncols, sizeof(celement)) - self._matrix = check_calloc(self._nrows, sizeof(celement*)) else: self._entries = check_allocarray(self._nrows * self._ncols, sizeof(celement)) - self._matrix = check_allocarray(self._nrows, sizeof(celement*)) - + + self._matrix = check_allocarray(self._nrows, sizeof(celement*)) cdef unsigned int k cdef Py_ssize_t i k = 0