Skip to content

Commit f4f2cd3

Browse files
committed
preserve sparsity through change_ring
1 parent 84f02af commit f4f2cd3

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/sage/modules/free_module.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5681,7 +5681,9 @@ def echelonized_basis(self):
56815681

56825682
def change_ring(self, R):
56835683
"""
5684-
Return the ambient free module over R of the same rank as self.
5684+
Return the ambient free module over ``R`` of the same rank as ``self``.
5685+
5686+
This also preserves the sparsity.
56855687
56865688
EXAMPLES::
56875689
@@ -5690,20 +5692,27 @@ def change_ring(self, R):
56905692
sage: A = ZZ^3; A.change_ring(GF(5))
56915693
Vector space of dimension 3 over Finite Field of size 5
56925694
5693-
For ambient modules any change of rings is defined.
5694-
5695-
::
5695+
For ambient modules any change of rings is defined::
56965696
56975697
sage: A = GF(5)**3; A.change_ring(QQ)
56985698
Vector space of dimension 3 over Rational Field
5699+
5700+
TESTS:
5701+
5702+
Check for :trac:`29630`::
5703+
5704+
sage: V = VectorSpace(QQ, 2, sparse=True)
5705+
sage: V.change_ring(RR).is_sparse()
5706+
True
56995707
"""
57005708
if self.base_ring() is R:
57015709
return self
57025710
from .free_quadratic_module import is_FreeQuadraticModule
57035711
if is_FreeQuadraticModule(self):
5704-
return FreeModule(R, self.rank(), inner_product_matrix=self.inner_product_matrix())
5705-
else:
5706-
return FreeModule(R, self.rank())
5712+
return FreeModule(R, self.rank(),
5713+
inner_product_matrix=self.inner_product_matrix(),
5714+
sparse=self.is_sparse())
5715+
return FreeModule(R, self.rank(), sparse=self.is_sparse())
57075716

57085717
def linear_combination_of_basis(self, v):
57095718
"""

src/sage/modules/free_module_element.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,10 +1536,20 @@ cdef class FreeModuleElement(Vector): # abstract base class
15361536
15371537
sage: v = vector(QQ['x,y'], [1..5]); v.change_ring(GF(3))
15381538
(1, 2, 0, 1, 2)
1539+
1540+
TESTS:
1541+
1542+
Check for :trac:`29630`::
1543+
1544+
sage: v = vector(QQ, 4, {0:1}, sparse=True)
1545+
sage: v.change_ring(AA).is_sparse()
1546+
True
15391547
"""
15401548
if self.base_ring() is R:
15411549
return self
15421550
M = self._parent.change_ring(R)
1551+
if M.is_sparse():
1552+
return M(self.dict(), sparse=True, coerce=True)
15431553
return M(self.list(), coerce=True)
15441554

15451555
def coordinate_ring(self):

0 commit comments

Comments
 (0)