Skip to content

Commit 832be74

Browse files
author
Marie BONBOIRE
committed
sparse iter and _copy_zero
1 parent 543f8d6 commit 832be74

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

src/sage/matrix/matrix_modn_dense_template.pxi

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ from sage.structure.proof.proof import get_flag as get_proof_flag
123123
from sage.structure.richcmp cimport rich_to_bool
124124
from sage.misc.randstate cimport randstate, current_randstate
125125
import sage.matrix.matrix_space as matrix_space
126-
from .args cimport MatrixArgs_init
126+
from .args cimport SparseEntry, MatrixArgs_init
127127

128128

129129
from sage.cpython.string cimport char_to_str
@@ -518,27 +518,49 @@ cdef class Matrix_modn_dense_template(Matrix_dense):
518518
"""
519519
ma = MatrixArgs_init(parent, entries)
520520
cdef long i, j
521-
it = ma.iter(False)
521+
it = ma.iter(convert=False, sparse=True)
522522
R = ma.base
523523
p = R.characteristic()
524-
for i in range(ma.nrows):
525-
v = self._matrix[i]
526-
for j in range(ma.ncols):
527-
x = next(it)
528-
if type(x) is int:
529-
tmp = (<long>x) % p
530-
v[j] = tmp + (tmp<0)*p
531-
elif type(x) is IntegerMod_int and (<IntegerMod_int>x)._parent is R:
532-
v[j] = <celement>(<IntegerMod_int>x).ivalue
533-
elif type(x) is Integer:
534-
if coerce:
535-
v[j] = mpz_fdiv_ui((<Integer>x).value, p)
536-
else:
537-
v[j] = mpz_get_ui((<Integer>x).value)
538-
elif coerce:
539-
v[j] = R(x)
524+
525+
for t in it:
526+
se = <SparseEntry>t
527+
x = se.entry
528+
v = self._matrix[se.i]
529+
if type(x) is int:
530+
tmp = (<long>x) % p
531+
v[se.j] = tmp + (tmp<0)*p
532+
elif type(x) is IntegerMod_int and (<IntegerMod_int>x)._parent is R:
533+
v[se.j] = <celement>(<IntegerMod_int>x).ivalue
534+
elif type(x) is Integer:
535+
if coerce:
536+
v[se.j] = mpz_fdiv_ui((<Integer>x).value, p)
540537
else:
541538
v[j] = <celement>x
539+
v[se.j] = mpz_get_ui((<Integer>x).value)
540+
elif coerce:
541+
v[se.j] = R(x)
542+
else:
543+
v[se.j] = <celement>x
544+
545+
546+
#for i in range(ma.nrows):
547+
# v = self._matrix[i]
548+
# for j in range(ma.ncols):
549+
# x = next(it)
550+
# if type(x) is int:
551+
# tmp = (<long>x) % p
552+
# v[j] = tmp + (tmp<0)*p
553+
# elif type(x) is IntegerMod_int and (<IntegerMod_int>x)._parent is R:
554+
# v[j] = <celement>(<IntegerMod_int>x).ivalue
555+
# elif type(x) is Integer:
556+
# if coerce:
557+
# v[j] = mpz_fdiv_ui((<Integer>x).value, p)
558+
# else:
559+
# v[j] = mpz_get_ui((<Integer>x).value)
560+
# elif coerce:
561+
# v[j] = R(x)
562+
# else:
563+
# v[j] = <celement>x
542564

543565
cdef long _hash_(self) except -1:
544566
"""

src/sage/matrix/matrix_space.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,7 +1804,12 @@ def identity_matrix(self):
18041804
"""
18051805
if self.__nrows != self.__ncols:
18061806
raise TypeError("identity matrix must be square")
1807-
A = self.zero_matrix().__copy__()
1807+
1808+
if self._copy_zero:
1809+
A = self.zero_matrix().__copy__()
1810+
else:
1811+
A = self.matrix()
1812+
18081813
for i in range(self.__nrows):
18091814
A[i, i] = 1
18101815
A.set_immutable()
@@ -1863,7 +1868,12 @@ def diagonal_matrix(self, entries):
18631868
raise TypeError("diagonal matrix must be square")
18641869
if self.__nrows < len(entries):
18651870
raise ValueError('number of diagonal matrix entries (%s) exceeds the matrix size (%s)' % (len(entries), self.__nrows))
1866-
A = self.zero_matrix().__copy__()
1871+
1872+
if self._copy_zero:
1873+
A = self.zero_matrix().__copy__()
1874+
else:
1875+
A = self.matrix()
1876+
18671877
for i in range(len(entries)):
18681878
A[i, i] = entries[i]
18691879
return A

0 commit comments

Comments
 (0)