Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac 18231: fix dict_to_list
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Aug 13, 2015
1 parent 2dd0d09 commit 00dd4ac
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/sage/matrix/matrix_space.py
Expand Up @@ -842,7 +842,7 @@ def basis(self):
]
"""
v = [self.zero_matrix().__copy__() for _ in range(self.dimension())]
one = self.base_ring()(1)
one = self.base_ring().one()
i = 0
for r in range(self.__nrows):
for c in range(self.__ncols):
Expand Down Expand Up @@ -876,7 +876,6 @@ def dims(self):
"""
return (self.__nrows, self.__ncols)

from sage.misc.cachefunc import cached_method
@cached_method
def identity_matrix(self):
"""
Expand All @@ -903,18 +902,18 @@ def identity_matrix(self):
TESTS::
sage: MS1.one().is_mutable()
False
sage: MS1.one()[1,2] = 3
Traceback (most recent call last):
...
ValueError: matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).
"""
if self.__nrows != self.__ncols:
raise TypeError("self must be a space of square matrices")
A = self.zero_matrix().__copy__()
for i in xrange(self.__nrows):
A[i,i] = 1
A.set_immutable()
return A
res = self._matrix_class(self, self.base_ring().one(), False, False)
res.set_immutable()
return res

one = identity_matrix

Expand Down Expand Up @@ -1010,7 +1009,7 @@ def zero_matrix(self):
sage: MM.zero().is_mutable()
False
"""
res = self._matrix_class(self, 0, coerce=False, copy=False)
res = self._matrix_class(self, self.base_ring().zero(), coerce=False, copy=False)
res.set_immutable()
return res

Expand Down Expand Up @@ -1373,6 +1372,7 @@ def matrix(self, entries=None, coerce=True, copy=True):
raise TypeError("cannot construct an element of {} from {} of type {}!"
.format(self, entries, type(entries)))


__call__ = matrix

def matrix_space(self, nrows=None, ncols=None, sparse=False):
Expand Down Expand Up @@ -1543,7 +1543,7 @@ def _magma_init_(self, magma):
s = 'RMatrixSpace(%s,%s,%s)'%(K.name(), self.__nrows, self.__ncols)
return s

def dict_to_list(entries, nrows, ncols):
def dict_to_list(entries, nrows, ncols, zero):
"""
Given a dictionary of coordinate tuples, return the list given by
reading off the nrows\*ncols matrix in row order.
Expand All @@ -1554,14 +1554,13 @@ def dict_to_list(entries, nrows, ncols):
sage: d = {}
sage: d[(0,0)] = 1
sage: d[(1,1)] = 2
sage: dict_to_list(d, 2, 2)
[1, 0, 0, 2]
sage: dict_to_list(d, 2, 3)
[1, 0, 0, 0, 2, 0]
sage: dict_to_list(d, 2, 2, None)
[1, None, None, 2]
sage: dict_to_list(d, 2, 3, 'a')
[1, 'a', 'a', 'a', 2, 'a']
"""
v = [0]*(nrows*ncols)
for ij, y in entries.iteritems():
i,j = ij
v = [zero]*(nrows*ncols)
for (i,j), y in entries.iteritems():
v[i*ncols + j] = y
return v

Expand All @@ -1584,7 +1583,7 @@ def list_to_dict(entries, nrows, ncols, rows=True):
if ncols == 0 or nrows == 0:
return d
for i, x in enumerate(entries):
if x != 0:
if x:
col = i % ncols
row = i // ncols
if rows:
Expand All @@ -1593,7 +1592,6 @@ def list_to_dict(entries, nrows, ncols, rows=True):
d[(col,row)] = x
return d


def test_trivial_matrices_inverse(ring, sparse=True, checkrank=True):
"""
Tests inversion, determinant and is_invertible for trivial matrices.
Expand Down

0 comments on commit 00dd4ac

Please sign in to comment.